1

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?

2
  • What do you mean by "Android and Java programs have different JARs"?
    – Michael
    Aug 11, 2015 at 9:06
  • Do you use different OpenCV classes in Java and Android applications?
    – Michael
    Aug 11, 2015 at 9:22

2 Answers 2

1
+50

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") 
0

Michael's answer might be better than this, but I haven't tested it. I had the same problem and here is an alternative solution. I put the library source code into particular folder and then included that folder into Android and Java projects as source sets.

Filetree

- Main project
  |
  - Android project
  - Java project
  - Plain folder containing image processing source code

Android build.gradle

android {    
  sourceSets {
    main.java.srcDirs += ['../imgprocessor/src/main/java']
  }
}

Java build.gradle

sourceSets {
  main.java.srcDirs += ['../imgprocessor/src/main/java']
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.