Use OpenCV in a Kotlin Multiplatform Mobile(KMM) project

600 views Asked by At

Setup:

We are currently using OpenCV on iOS and Android for image processing. On Android we import OpenCV as a .aar and on iOS we import it via Cocoapods, and they call the same underlying functions.

Problem:

We would like to have a KMM project where we can import OpenCV and share the business logic. We are using expect/actual to depend on OpenCV in platform specific code. We can make it work for Android by importing the same .aar in androidMain. Our problem is with iOS. Currently KMM doesn't support interoperability with C++ (which OpenCV is written in) - see https://kotlinlang.org/docs/roadmap.html#removed-items

What we have tried:

We tried to link a .framework via Gradle and create .def files to point to the .hpp files in OpenCV but we get an error xxx.hpp header must be compiled as C++ when trying to sync Gradle.

build.gradle.kts

iosX64() {
    compilations.getByName("main") {
        val OpenCV by cinterops.creating {
            // Path to .def file
            defFile("src/iosMain/cinterop/OpenCV.def")

            // Directories for header search (an analogue of the -I<path> compiler option)
            includeDirs("../iosApp/Pods/OpenCV/opencv2.framework/Headers/Core")
        }
    }

    binaries.all {
        // Linker options required to link to the library.
        linkerOpts("-L/../iosApp/Pods/OpenCV/opencv2.framework", "-lopencv2")
    }
}

OpenCV.def

headers = cvstd.hpp
package = OpenCV

Question:

Is there any way to depend on OpenCV in KMM?

0

There are 0 answers