StephenG StephenG - 17 days ago 7
Java Question

Sharing an OpenCV Class between Java and Android Program in Android Studio

I have an OpenCV Java class

processImage
that I would like to share between an Android program and a Java program. If possible, it'd be great to keep all of the code in the same projects, and have modules for the Android app, Java App, and the shared code. The Java and Android APIs are the same, just the JARs that the systems use are different.

Since the Android and Java OpenCV programs have different JVMs and OpenCV JARs that they need to use, I know that I can't just share the compiled library between the two. But I also need a way to share the Java interface.

Are there any Gradle or Android Studio tricks that could help me achieve this?

Answer

If the subset of OpenCV API you're using is similar for Android and Java versions of OpenCV, which I assume is true, you can add Java OpenCV as a runtime dependency of a shared library. And in Android and Java projects you should add the library project and a corresponding OpenCV library is compile dependencies. So build.gradle files for your project will contain the following lines in their dependency blocks:

Library project

runtime files("libs/opencv-java-3.0.0.jar") 

Android project

compile project(":library") 
compile files("libs/opencv-android-3.0.0.jar") 

Java project

compile project(":library") 
compile files("libs/opencv-java-3.0.0.jar")