ArthurT
ArthurT

Reputation: 367

findLibrary returned null with OpenCV4Android in Android Studio

I want to use OpenCV for the Android app I am currently working on, written in Java.

To setup OpenCV, I followed exactly the steps explained on this page as well as on this one (tried both solutions without success). When I then let my project rebuild it works, but when I let my code run, it fails when trying to do the following:

fdetector = FeatureDetector.create(FeatureDetector.SURF);

The error I get is java.lang.UnsatisfiedLinkError: Native method not found: org.opencv.features2d.FeatureDetector.create_0:(I)J, kind of the same as in the aforementioned question page. I tried all solutions proposed on this page but none of them worked for me.

I do have installed JNI, so I guess this is not the problem. Or m My setup is the following:

-app
    |-libs (with my other app libraries, including javacv.jar)
    |-src
         |-main
               |-java (with my code)
               |-res
               |-jniLibs (with the .so files from the OpenCV's sdk\native\libs\x86)
         |-build.gradle
-libs
     |-opencv
             |-build
             |-jniLibs (with the same .so files as I was not sure where to put them)
             |-res
             |-src
             |-build.gradle

My app's build.gradle contains the following dependencies:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'libs', include: ['*.java'])
compile fileTree(dir: 'libs', include: ['*.so'])
compile 'com.android.support:appcompat-v7:21.0.2'
compile project(':libs:opencv')
}

And the opencv's build.gradle contains the following sourceSets:

sourceSets {
main {
  manifest.srcFile 'AndroidManifest.xml'
  java.srcDirs = ['src']
  resources.srcDirs = ['src']
  res.srcDirs = ['res']
  aidl.srcDirs = ['src']
  jniLibs.srcDirs = ['jniLibs']
}
}

My settings.gradle contains an include ':libs:opencv'

I really cannot see where I did something wrong, can anyone help me with this?

Upvotes: -1

Views: 1724

Answers (5)

user3068659
user3068659

Reputation: 443

after my long research I fixed it with the code given below:

sourceSets.main {
    jni.srcDirs = []
    jniLibs.srcDir 'src/main/libs'
}

Upvotes: 0

ArthurT
ArthurT

Reputation: 367

Thanks everyone for the help, now I got it sorted out!

Actually what I did is to follow the instruction from G3M's video at https://www.youtube.com/watch?v=0fEtrekNcOo to add ndk capability to my project, i.e. adding the following line to my app's local.properties:

ndk.dir=D\:\\AndroidSDK\\ndk

Then, following Kiran's advice, I went through the steps from this setup instruction

In the end, I was able to make the static call suggested by bonnyz to get the "opencv_java" library:

static { 
    System.loadLibrary("opencv_java"); //the name of the .so file, without the 'lib' prefix
}

Upvotes: 0

kiranpradeep
kiranpradeep

Reputation: 11201

The below steps for using Android OpenCV sdk in Android Studio.

  1. Download latest OpenCV sdk for Android from [OpenCV.org][1] and decompress the zip file.
  2. Import OpenCV to Android Studio, From File -> Import Module, choose sdk/java folder in the unzipped opencv archive.
  3. Update build.gradle under imported OpenCV module to update 4 fields to match your project build.gradle a) compileSdkVersion b) buildToolsVersion c) minSdkVersion and 4) targetSdkVersion.
  4. Add module dependency by Application -> Module Settings, and select the Dependencies tab. Click + icon at bottom, choose Module Dependency and select the imported OpenCV module.
  5. Copy libs folder under sdk/native to Android Studio under app/src/main.
  6. In Android Studio, rename the copied libs directory to jniLibs and we are done.

Step (6) is since Android studio expects native libs in app/src/main/jniLibs instead of older libs folder. For those new to Android OpenCV, don't miss below steps

  • include static{ System.loadLibrary("opencv_java"); }
  • For step(5), if you ignore any platform libs like x86, make sure your device/emulator is not on that platform.

Upvotes: 1

G3M
G3M

Reputation: 1031

You need to include NDK support on Android studio by making some changes to your local.properties file and your Build.gradle. Here is a video showing in detail how to build NDK application on Android Studio https://www.youtube.com/watch?v=0fEtrekNcOo

Upvotes: 1

bonnyz
bonnyz

Reputation: 13548

Are you loading the OpenCV library in a JAVA static statement? If not, put this somewhere in your activity:

static { 
   System.loadLibrary("opencv_java"); //the name of the .so file, without the 'lib' prefix
}

Upvotes: 1

Related Questions