UnsatisfiedLinkError in OpenCV example for Android - android

I've read a lot of very similar posts concerning this problem, but I can't find the issue.
I'm trying to get tutorial-2-mixedprocessing from OpenCV-android-sdk/samples running (version 3.2.0). Following the official tutorial as good as possible, I imported it into Android Studio (2.3) and adjusted the path to Tegra in the Makefile to fit my system (Ubuntu 16.04). The target platform is the project tango tablet (Android 4.4.2).
When I check the APK in the build folder, the library libmixed_sample.so doesn't show up. Naturally, loading the library fails with a UnsatisfiedLinkError.
The simpler examples without native code work fine.
Any ideas why it doesn't copy the library?
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 15
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "org.opencv.samples.tutorial2"
minSdkVersion 9
targetSdkVersion 9
ndk {
moduleName "mixed_sample"
}
}
sourceSets {
main {
jni.srcDirs = ["libs"]
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
splits {
abi {
enable true
reset()
include 'armeabi-v7a'
universalApk false
}
}
}
dependencies {
compile project(':openCVLibrary2482')
}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCVROOT:=/home/fs/NVPACK/OpenCV-2.4.8.2-Tegra-sdk
include ${OPENCVROOT}/sdk/native/jni/OpenCV-tegra3.mk
LOCAL_MODULE := mixed_sample
LOCAL_SRC_FILES := jni_part.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
Error:
06-06 10:30:41.077 24476-24476/org.opencv.samples.tutorial2 E/AndroidRuntime: FATAL EXCEPTION: main Process: org.opencv.samples.tutorial2, PID: 24476
java.lang.UnsatisfiedLinkError: Couldn't load mixed_sample from loader dalvik.system.PathClassLoader[
DexPathList[
[zip file "/data/app/org.opencv.samples.tutorial2-2.apk"],
nativeLibraryDirectories=[
/data/app-lib/org.opencv.samples.tutorial2-2,
/system/lib,
/vendor/lib,
/system/lib
]
]]: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:358)
at java.lang.System.loadLibrary(System.java:526)
at org.opencv.samples.tutorial2.Tutorial2Activity$1.onManagerConnected(Tutorial2Activity.java:49)
at org.opencv.android.AsyncServiceHelper$3.onServiceConnected(AsyncServiceHelper.java:318)
at [...]
You can find the modified example on github.

Related

What is the equivalent of target_link_libraries in Android.mk

I am trying to compile an android project using Android.mk and need to include the following libraries: native-audio-jni android log OpenSLES.
A similar project (https://github.com/googlesamples/android-ndk/tree/master/native-audio), which includes the same libraries but uses CMakeLists.txt instead of Android.mk has this line in the CMakeLists.txt:
target_link_libraries(native-audio-jni android log OpenSLES)
This sample project builds and runs fine.
Based on findings from my online research on the topic, I have tried including the following lines in my Android.mk file, which is in the jni folder:
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -lOpenSLES -lnative-audio-jni
LOCAL_LDLIBS += -landroid
ldLibs = ["android", "log", "native-audio-jni", "OpenSLES"]
LOCAL_SHARED_LIBRARIES += libandroid
LOCAL_LDLIBS := -llog
However, I still get errors like:
undefined reference to `AAssetManager_fromJava'
undefined reference to `AAssetManager_open'
undefined reference to `SL_IID_SEEK'
undefined reference to `SL_IID_MUTESOLO' ...
I also have the following includes in my .c file where the errors are generated:
// for native audio
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
// for native asset manager
#include <sys/types.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include <android/log.h>
So my question is: How to add these libraries to my Android.mk or in other words: What is the equivalent of target_link_libraries(native-audio-jni android log OpenSLES) in Android.mk? For a number of reasons I need to use Android.mk instead of CMakeLists.txt in my project.
Here also is my build.gradle if this is of any help:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.google.ar.sceneform.samples.drawing"
// 24 is the minimum since ARCore only works with 24 and higher.
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
ndk {
//ldLibs "android", "log", "native-audio-jni", "OpenSLES" // Not helping
/*
* Sceneform is available for the following ABIs: arm64-v8a, armv7a,
* x86_64 and x86. This sample app enables arm64-v8a to run on
* devices and x86 to run on the emulator. Your application should
* list the ABIs most appropriate to minimize APK size (arm64-v8a recommended).
*/
abiFilters 'arm64-v8a' ,'x86' // , 'armeabi-v7a'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
ndkBuild {
path '../jni/Android.mk'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
//implementation fileTree(dir: 'libs', include: ['*.jar']) // NOT helping
implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.7.0'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation files('Libs/YouTubeAndroidPlayerApi.jar')
implementation 'com.github.barteksc:android-pdf-viewer:2.0.3'
implementation 'com.xw.repo:bubbleseekbar:3.19-lite'
}
repositories {
mavenCentral()
}
apply plugin: 'com.google.ar.sceneform.plugin'
sceneform.asset('sampledata/models/andy.obj',
'default',
'sampledata/models/andy.sfa',
'src/main/res/raw/andy')
And my Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(CLEAR_VARS)
LOCAL_MODULE := aubio
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libaubio.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := pitch
LOCAL_SRC_FILES := pitch.c
LOCAL_SHARED_LIBRARIES := aubio
include $(BUILD_SHARED_LIBRARY)
LOCAL_LDLIBS := -llog -lOpenSLES -lnative-audio-jni -landroid
An this is a screenshot of the built error:
enter image description here
Your Android.mk got some unnecessary definitions that ruin happen to hide he correct one:
LOCAL_LDLIBS := -llog -lOpenSLES -lnative-audio-jni -landroid
instead of all the lines that you posted. NDK knows where to find these libraries, therefore -L$(SYSROOT)/… is not necessary, but only can hurt. The other lines probably come from frustration.

OpenCV android NDK build error

I'm using NDK to use C++ native code in OpenCV through Android studio.
Every thing is alright except when I'm running this project to an android device I have the following error
Warning:Native C/C++ source code is found, but it seems that NDK option is not configured. Note that if you have an Android.mk, it is not used for compilation. The recommended workaround is to remove the default jni source code directory by adding:
android {
sourceSets {
main {
jni.srcDirs = []
}
}
}
and also
to build.gradle, manually compile the code with ndk-build, and then place the resulting shared object in src/main/jniLibs.
C:\Users\MALIK\Desktop\ImageJ\app\src\main\jni\com_example_malik_imagej_DetectionBasedTracker.cpp
Error:(2, 10) 'opencv2/opencv.hpp' file not found 1 error generated.
and when I do the workaround and make jni.srcDir empty , I got error that "cannot resolve corresponding JNI function "
so the native code won't run and the app crashes , what should I do in this case?
this is my gradle :
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.malik.haar"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:/Android/sdk/ndk-bundle/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { jni.srcDirs = [] } }
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile files('libs/junit-4.12.jar')
compile project(':openCVLibrary320')
}
and this is my Android.mk file :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCVROOT:=C:\OpenCV-android-sdk
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include $(OPENCVROOT)/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := com_example_malik_imagej_DetectionBasedTracker.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)
and this is my Header file of native code :
thanks.

UnsatisfiedLinkError while trying to load opencv library

I am getting this error:
java.lang.UnsatisfiedLinkError: dlopen failed: file offset for the library "libopencv_java3.so" >= file size: 0 >= 0
when I am trying to link openCV native code with my java code. I tried a number of things but nothing resolved this error. Please suggest me any solutions.
Android.mk
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=STATIC
ifdef OPENCV_ANDROID_SDK
ifneq ("","$(wildcard $(OPENCV_ANDROID_SDK)/OpenCV.mk)")
include ${OPENCV_ANDROID_SDK}/OpenCV.mk
else
include ${OPENCV_ANDROID_SDK}/sdk/native/jni/OpenCV.mk
endif
else
include C:/opencv-3.2.0-android-sdk/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
endif
LOCAL_MODULE := Sample
LOCAL_LDLIBS += -llog
LOCAL_CFLAGS := $(OPTIMISATIONS)
LOCAL_SRC_FILES := \
Sample/Sample.cpp
build.gradle
defaultConfig
{
applicationId "com.example.app"
ndk
{
moduleName "Sample"
abiFilters "armeabi-v7a", "x86", "armeabi", "mips"
}
}
externalNativeBuild {
ndkBuild {
path '../../../../../opencv-3.2.0-android-sdk/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk'
path 'src/main/jni/Android.mk'
}
}
The error is occurring at System.loadLibrary("Sample");
I think that LOCAL_SRC_FILE name you given as Sample/Sample.cpp is not correct,check SRC_FILE name with full name like com_example_app_Sample.cpp for your cpp file.
And check ndkBuild path "../../../../../" if this path structure mismatch also the Opencv.mk will not load.
This error means the system cannot find the compile library (Sample.so). You need to make sure the library is at the right place, and that the system knows where to find it - I would suggest making sure jniLibs.srcDirs is set in the build.gradle file. For example, if the library is in 'libs' folder, you put this in the gradle file:
sourceSets {
main {
...
jniLibs.srcDirs 'libs'

Android NDK Exception failed: dlopen failed: cannot locate symbol "_ZN7Tangram11setPositionEdd" referenced by "libtangram.so"

I am trying to run a demo application of a Library in which android ndk is integrated. I have imported this code in Android studio and also downloaded ndk and linked it with project. Code compiles and build succesfully. It gives crash with exception "exception ljava lang unsatisfiedlinkerror thrown while initializing" "failed: dlopen failed: cannot locate symbol "_ZN7Tangram11setPositionEdd" referenced by "libtangram.so"..."
Application.mk:
APP_STL := c++_shared
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi armeabi-v7a x86 mips
APP_PLATFORM := android-19
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := tangram
LOCAL_SRC_FILES := jniExports.cpp jniGenerated.cpp platform_android.cpp
LOCAL_LDLIBS := -llog
LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
include $(BUILD_SHARED_LIBRARY)
Module Gradle File:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
}
}
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group = GROUP
version = VERSION_NAME
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
}
sourceSets.main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
jni.srcDirs = []
assets.srcDirs = ['core/resources']
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:/Users/Administrator/AppData/Local/Android/android-ndk-r10e/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=jniLibs',
'APP_BUILD_SCRIPT=jni/Android.mk',
'NDK_APPLICATION_MK=jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
// Add gdb server to apk
afterEvaluate {
Sync packageTask = project.getTasks().findByName("packageReleaseJniLibs")
if (packageTask) { packageTask.include(['**/gdbserver', '**/gdb.setup']) }
packageTask = project.getTasks().findByName("packageDebugJniLibs")
if (packageTask) { packageTask.include(['**/gdbserver', '**/gdb.setup']) }
}
dependencies {
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'xmlpull:xmlpull:1.1.3.1'
}
apply from: file('gradle-mvn-push.gradle')
In Java class load libraries:
static {
System.loadLibrary("c++_shared");
System.loadLibrary("tangram");
}
I google this issue and in these posts Link1 Link2 Link3 Link4 it is mentioned that it may be the issue of compiling at api 21 or newer and running it on older devices. But i have mentioend APP_PLATFORM := android-19 in my Application.mk and getting this exception.
Any Help?
It looks like you tried to compile the tangram project from your own custom ndk-build as opposed to their recommended build process (https://github.com/tangrams/tangram-es#android). Use their build process with make to create the libtangram.so file, and then copy that .so file into your native libraries directory for your app.
The reason it can't find those symbols is because you aren't including the appropriate source files that define those functions when building with the ndk. By ingnoring undefined symbols, it will compile but won't be able to resolve them at runtime.
The symbol you are missing is created from Tangram::setPosition(double, double) which is defined at https://github.com/tangrams/tangram-es/blob/master/core/src/tangram.cpp#L318 ; however, your Android.mk file doesn't include that source file.

How to use recompiled OpenCV build for Android

So I want to modify one of the files in the OpenCV build for Android and I followed the instructions on their site to download the build and I was able to make it successfully. I modified one of the files (calibinit.cpp) and made it again and copied the files in the
opencv/platforms/build_android_arm/lib/
folder into the jniLibs folder of my AndroidStudio Project.
However, the changes I made are definitely not working. I already had a previous OpenCV library linked to my project, so I'm pretty sure it's still using the old code. How do I use the new build with my AndroidStudio Project?
Can you provide more details about how u use jni in Android Studio Project?
Did u try to clean and rebuild?
Do you have your own custom Android.mk? cause Android Studio will generate its own Android.mk, which can lead to unexpected result as u thought.
delete jniLibs folder and every so file, you don't need them.
define NDK path in local.properties file:
sdk.dir=D:\Android\SDK
ndk.dir=D:\Android\NDK
create a folder(i named it jni) and put Android.mk and Application.mk in it (and an empty dummy.c file for preventing future buggy errors)
as Roy said, Android Studio generates it's own Android.mk, we have our own Android.mk file and Android Studio must use it.jni.srcDirs = []prevents that generation!
Extract openCV sources somewhere, i put them here: D:\Android\Libs\OpenCV
here is my files, modify and use them
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "your.package"
minSdkVersion 11
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
sourceSets {
main {
jni.srcDirs = [] /*disables automatic ndk-build call */
}
}
task ndkBuild(type: Exec) {
commandLine file('D:\\Android\\NDK\\ndk-build.cmd').absoluteFile,
'NDK_PROJECT_PATH='+file('src\\main\\jni').absolutePath,
'APP_BUILD_SCRIPT='+file('src\\main\\jni\\Android.mk').absolutePath,
'NDK_APPLICATION_MK='+file('src\\main\\jni\\Application.mk').absolutePath,
'NDK_LIBS_OUT='+file('src\\main\\jniLibs').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile project(':openCVLibrary300rc1')
}
Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a x86
APP_PLATFORM := android-8
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#opencv
OPENCVROOT:= D:/Android/Libs/OpenCV
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
//I have a local source file, you can remove it
LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)

Categories

Resources