14

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?

4
  • 1
    Is there any workaround to use the opencv in kmp? for both platform Android iOS. Commented Dec 12, 2024 at 12:42
  • 1
    Apple has its own libraries for computer vision. You'd better lean into your ecosystem. Commented Apr 1 at 11:08
  • 1
    @m.reiter you'll have to watch your bounty carefully. you already attracted one AI-generated answer. if you don't watch the bounty, all that rep will go to someone who copied the question into an LLM and copied the response back here. Commented Apr 3 at 9:07
  • 1
    I found the following article that seems to answer your question : medium.com/@dev.almasud/… . So it seems there is already an answer online. Commented 12 hours ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.