Reputation: 367
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
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
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
Reputation: 11201
The below steps for using Android OpenCV sdk in Android Studio.
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
static{ System.loadLibrary("opencv_java"); }
Upvotes: 1
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
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