I'm currently developing a project in Android Studio and I need to use some OpenCV libraries.
I know that Android Studio currently doesn't support NDK development but when doing my research i found that there are ways to manage things around.
I followed this tutorial to add opencv libraries to my project:
How to use opencv in android studio using gradle build tool?
Everything worked fine but when I tried to test an OpenCV example like "FaceDetection" by doing this:
Go to OpenCV Android SDK, pick a sample project that you’d like to try out.
First, delete the ‘res’ folder inside your own project app/src/main, then place the res folder from the samples inside your app/src/main folder.
First, delete the ‘java’ folder from app/src/main, then copy the ‘src’ folder from the samples in there (note, the src has to be renamed to java).
If you building example with native C++ files, you need to have NDK installed. Download it from Google’s developers portal, and add this line to your local.properties in the top-level of your project, below the sdk.dir line:
ndk.dir=/path/to/your/android-ndkBuild and run the example!
I got these errors:
java.lang.UnsatisfiedLinkError: Couldn't load detection_based_tracker from loader dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.ricardonascimento.opencvexamples-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.ricardonascimento.opencvexamples-2, /vendor/lib, /system/lib]]]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:358)
at java.lang.System.loadLibrary(System.java:526)
the problem is when loading library
// Load native library after(!) OpenCV initialization
System.loadLibrary("detection_based_tracker");
What do you suggest to solve this problem? I have searched all over the web but did not find anything.
Can you show your app's build.gradle? You should check the 'sourceSets' settings, by default the source for the native module is 'jniLibs'. Thus, 'jniLibs' directory should contain the necessary module, ex. armeabi-v7a, mips, x86, etc.
Here's a sample build: (Note: I customized 'jniLibs' into 'libs', just a personal preference...)
~/AndroidStudioProjects/OpenCV3-FaceDetection/app/jni$ ndk-build
[armeabi-v7a] Compile++ thumb: detection_based_tracker <= DetectionBasedTracker_jni.cpp
[armeabi-v7a] Prebuilt : libopencv_java3.so <= /home/cobalt/Android/OpenCV-android-sdk/sdk/native/jni/../libs/armeabi-v7a/
[armeabi-v7a] SharedLibrary : libdetection_based_tracker.so
/home/cobalt/Android/adt-bundle-linux-x86-20131030/android-ndk-r10d/toolchains/arm-linux-androideabi-4.8/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld: warning: hidden symbol '__aeabi_atexit' in /home/cobalt/Android/adt-bundle-linux-x86-20131030/android-ndk-r10d/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi-v7a/thumb/libgnustl_static.a(atexit_arm.o) is referenced by DSO /home/cobalt/AndroidStudioProjects/OpenCV3-FaceDetection/app/obj/local/armeabi-v7a/libopencv_java3.so
[armeabi-v7a] Install : libdetection_based_tracker.so => libs/armeabi-v7a/libdetection_based_tracker.so
[armeabi-v7a] Install : libopencv_java3.so => libs/armeabi-v7a/libopencv_java3.so
You can also customize 'jniLibs' into 'libs' from the app directory as follows:
Ex.
sourceSets {
main {
jni.srcDirs = []
jniLibs.srcDirs=['libs']
}
}
This can be found in the app module, the full app build.gradle is as follows (Ex.):
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "org.opencv.samples.opencv3_facedetection"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jni.srcDirs = []
jniLibs.srcDirs=['libs']
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.1'
compile project(':libraries:opencv')
}
Here's a sample project importing OpenCV 3.0 Face Detection Sample in Android Studio:
https://github.com/DeLaSalleUniversity-Manila/opencvfacedetection-melvincabatuan
The procedure can be found in the README.md.
Also, if you want to try Android Studio's Gradle Experimental Plugin for Native cpp development with OpenCV, then you might be interested on the following sample projects:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With