4

Trying to get started with OpenCV Java SDK on Android Studio creating an app targetting 14 - 19.

I'm following steps defined by @Paito here: https://stackoverflow.com/a/20600674/1291489

1) I package libs in a .jar

enter image description here

2) Add dependencies: ... dependencies {

    //OpenCV
    compile files('libs/opencv-2.4.8.jar')
    compile project(':app:libs:opencv')
}

3) Copy the OpenCV java folder

enter image description here

4) Create and fix build.gradle for OpenCV (same as referenced source)

5) Try to use the library

import org.opencv.android.OpenCVLoader;
...
if (!OpenCVLoader.initDebug()) {}

Results: App builds and runs but I get the following exception:

03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Trying to get library list
03-19 19:54:46.144  E/OpenCV/StaticHelper﹕ OpenCV error: Cannot load info library for OpenCV
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Library list: ""
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ First attempt to load libs
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Trying to init OpenCV libs
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Trying to load library opencv_java
03-19 19:54:46.144  D/OpenCV/StaticHelper﹕ Cannot load library "opencv_java"
03-19 19:54:46.154  W/System.err java.lang.UnsatisfiedLinkError: Couldn't load opencv_java: findLibrary returned null

Not able to see what's wrong here. I tried every possible way of packaging the .jar file and repeated the steps at least 10 times with tweaks.

I also tried structure:

-opencv.jar
  -libs
    -armeabi-v7a
      -(.so files)

I am testing on a Samsung GS3Mini and Genymotion 4.1.1, and getting same results on both. Both are running OpenCV Manager and samples.

Community
  • 1
  • 1
albertpeiro
  • 356
  • 4
  • 14

2 Answers2

1

You should add the *.so files in /main/jniLibs folder if your gradle plugin is 0.7.2+ and add the opencv java as library, follow the steps which I covered here.

How to use opencv in android studio using gradle build tool?

Community
  • 1
  • 1
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • This fixed it! For the sake of correctness the folder is (Project)/(APPModule)/src/main/jniLibs (as you mentioned in your previous answer) be nice if you could fix answer! Thank you @TGMCians! – albertpeiro Mar 19 '14 at 20:20
  • The generated .apk is now 28MB where it was ~2 previously. Any ideas on which of these jniLibs to attach for a production release? Any of these not needed? Will target 4.0+ devices only. – albertpeiro Mar 19 '14 at 20:23
  • You have to add them all for a production release also. – Ajay S Mar 20 '14 at 03:46
  • I followed the steps mentioned in the answer, but finally have the same issue - " Couldn't load opencv_java". Although in the root of my apk there is 'lib' folder with arm64-v8a, armeabi and others folders with libopencv_java3.so file. Could you please suggest what could be wrong? – XZen Jun 09 '15 at 20:31
0

So close, try this structure:

-opencv.jar -lib -armeabi-v7a -(.so files)

Android Studio:

enter image description here

Update: Looks like @TGMCians has a better solution for you with the new Gradle.

Paito
  • 1,553
  • 1
  • 15
  • 24
  • Thank you again. This approach didn't work for me in my first try :( EDIT: As said by @TGMCians above looks like it's a matter of versions with Gradle (will update answers and all when I confirm this). – albertpeiro Mar 19 '14 at 20:11
  • It's not an issue with Gradle versions as I currently use 0.9.+; However I do like TGMCians solution better and will probably switch too it soon. – Paito Mar 20 '14 at 02:32