UnsatisfiedLinkError while trying to load opencv library - android

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'

Related

GStreamer Android SDK error for tutorial-5

We have taken clone of a project and made all settings as per instruction still not able to build app due to following error:
Error:(7, 10) fatal error: 'gst/gst.h' file not found
above error is in tutorial.c file:
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/video/videooverlay.h>
#include <pthread.h>
GST_DEBUG_CATEGORY_STATIC (debug_category);
#define GST_CAT_DEFAULT debug_category
Please find below gradle and Android.mk file which I have added to my project:
Gradle
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.example.ndktest"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_PLATFORM=android-19',
'-DANDROID_STL=gnustl_static',
'-DANDROID_ARM_NEON=TRUE',
'-DANDROID_CPP_FEATURES=exceptions rtti'
}
}
ndk {
moduleName "tutorial-5"
}
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir new File(buildDir, 'lib')
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
GSTREAMER_ROOT_ANDROID := D:\ndk-gst1.9.1
SHELL := PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin /bin/bash
include $(CLEAR_VARS)
LOCAL_MODULE := tutorial-5
LOCAL_SRC_FILES := tutorial-5.c
LOCAL_SHARED_LIBRARIES := gstreamer_android
LOCAL_LDLIBS := -llog -landroid
include $(BUILD_SHARED_LIBRARY)
ifndef GSTREAMER_ROOT
ifndef GSTREAMER_ROOT_ANDROID
$(error GSTREAMER_ROOT_ANDROID is not defined!)
endif
GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)
endif
GSTREAMER_NDK_BUILD_PATH := $(GSTREAMER_ROOT)/share/gst-android/ndk-build
include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk
GSTREAMER_PLUGINS := $(GSTREAMER_PLUGINS_CORE) $(GSTREAMER_PLUGINS_PLAYBACK) $(GSTREAMER_PLUGINS_CODECS) $(GSTREAMER_PLUGINS_NET) $(GSTREAMER_PLUGINS_SYS) $(GSTREAMER_PLUGINS_CODECS_RESTRICTED)
G_IO_MODULES := gnutls
GSTREAMER_EXTRA_DEPS := gstreamer-video-1.0
include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer-1.0.mk
Please suggest what changes need to be done in above code to make it run.
I already answered a similar question... The problem with the official tutorials from the Gstreamer site is that they are old and made for Eclipse. Maybe your error is because you didn't link your Android Studio project to the C++ files... to do it follow the second step from this answer: Gstreamer examples in Android Studio
Since the linking with C++ files ins't the only error that will occurs I also suggest that you follow all other steps. There's a link for gitlab project with "turorial 5" working in the link above.

UnsatisfiedLinkError in OpenCV example for 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.

OpenCV undefined reference to `cv::fastFree(void*)'

I try to build project with OpenCV, i downloaded OpenCV SDK 3.1 from official website, however during building i get error
/Users/Mario/Downloads/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp
Error:(571) undefined reference to 'cv::fastFree(void*)'
Error:(663) undefined reference to 'cv::Mat::create(int, int const*, int)'
Error:(682) undefined reference to 'cv::Mat::deallocate()'
here is my gradle.build, i use gradle-experimental:0.4.0
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.2"
defaultConfig.with {
applicationId = "pl.mariusz.opencv"
minSdkVersion.apiLevel = 17
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
task ndkBuild(type: Exec) {
commandLine '/Users/Mario/Library/Android/android-ndk-r10e/ndk-build', '-C', file('src/main').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
android.ndk {
moduleName = "source_file"
cppFlags.add("-std=c++11")
cppFlags.add("-fexceptions")
cppFlags.add("-I${file("/Users/Mario/Downloads/OpenCV-android-sdk/sdk/native/jni/include")}".toString())
cppFlags.add("-I${file("/Users/Mario/Downloads/OpenCV-android-sdk/sdk/native/jni/include/opencv")}".toString())
stl = "gnustl_static"//"gnustl_static"//"gnustl_shared"//"stlport_static"
}
android.sources {
main {
jni{
source{
srcDirs = []
}
}
jniLibs {
source {
srcDirs = ['src/main/Libs']
}
}
}
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file('proguard-rules.txt'))
}
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCVROOT:= /Users/Mario/Downloads/OpenCV-android-sdk
OPENCV_LIB_TYPE=STATIC
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := source_file.cpp
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := source_file
include $(BUILD_SHARED_LIBRARY)
and Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-17
i tried mix it with shared,static STL with still same error, how can i fix it?
[EDIT]
[SOLUTION]
its more like compromise not real solution, but i changed gradle-experimental:0.4.0 back to classpath 'com.android.tools.build:gradle:1.5.0' and added file gradle.properties to project root directory with
android.useDeprecatedNdk=true
You have to add opencv libraries to your linker. To do that, you have to do 2 things: 1. add the library files; 2. add the library directory.
Try
LOCAL_LDLIBS += -llog -ldl -LlibPath -lopencv_calib3d -lopencv_core
and so an, add all the opencv libraries (or at least the ones you'll need) in this way. -l will automatically assume the "lib" in the filename and the suffix.
The -L will tell the linker where to find libraries, so instead of libPath please add the absolute path of your opencv library folder. See how to mention path of libraries in Android.mk file or Application.mk file?
Try it and tell me the new error message.

Generate .so file in Android Studio

I'm aware that there are answers related to this, but they're old and outdated. I got Android Studio 1.3 and already downloaded Android NDK. When I run an app, it crashes and gives findLibrary returned null in Logcat. I figured out this was due to no .so library file (Correct me if I'm wrong). My question is - How do I generate a .so file in Android Studio?
What I have -
Android Studio 1.3 Stable version
Gradle 1.5
Android Experimental Plugin 0.2
Notice - If the library .so file has to be built from Cygwin or CMD, please tell me how to do it.
There are a few steps needed to get the NDK hooked up into Android Studio. Currently, support is marked as experimental and AS is starting to bundle the ability to download the NDK within the IDE. By default, AS uses a generic Android.mk and Application.mk when source and/or libs are placed in the jni or jniLibs folder. The instructions below override those defaults in order to provide more customization ability.
In short, you will need to:
Create the default jni and jniLibs directories for your source and libs.
Tell Android Studio where to find your NDK build chain
Tell gradle how to compile and where to place your libs
Create an Android.mk file to specify building and linking order
Create some source
Create directories
Inside /app/src/main create a jni and jniLibs directory.
Update local.properties
Inside your local.properties file, add a line similar to:
ndk.dir=/home/nathan/development/bin/android-ndk-r10e
Update build.gradle
This refers to the module level, not the application level. This ensures that we have defined the build path in the step above and removes the ability for Android Studio to automatically invoke ndk-build. Use the following example as a guide.
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.0"
defaultConfig.with {
applicationId = "com.example.hellojni"
minSdkVersion.apiLevel = 4
targetSdkVersion.apiLevel = 23
}
}
compileOptions.with {
sourceCompatibility=JavaVersion.VERSION_1_7
targetCompatibility=JavaVersion.VERSION_1_7
}
/*
* native build settings
*/
android.ndk {
moduleName = "hello-jni"
/*
* Other ndk flags configurable here are
* cppFlags += "-fno-rtti"
* cppFlags += "-fno-exceptions"
* ldLibs = ["android", "log"]
* stl = "system"
*/
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.txt')
}
}
android.productFlavors {
// for detailed abiFilter descriptions, refer to "Supported ABIs" #
// https://developer.android.com/ndk/guides/abis.html#sa
create("arm") {
ndk.abiFilters += "armeabi"
}
create("arm7") {
ndk.abiFilters += "armeabi-v7a"
}
create("arm8") {
ndk.abiFilters += "arm64-v8a"
}
create("x86") {
ndk.abiFilters += "x86"
}
create("x86-64") {
ndk.abiFilters += "x86_64"
}
create("mips") {
ndk.abiFilters += "mips"
}
create("mips-64") {
ndk.abiFilters += "mips64"
}
// To include all cpu architectures, leaves abiFilters empty
create("all")
}
}
Android.mk
You will need an Android.mk file inside the /app/src/main/jni directory
LOCAL_PATH := $(call my-dir)
# Builds a dylib out of test.cpp
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
test.cpp
Add some awesome C/C++ source code for your lib. These files will start in /app/src/main/jni and will be compiled and linked as specified in your Android.mk
Example file
#include <jni.h>
#include <android/log.h>
static const char *SOME_TAG = "MyAwesomeTag";
extern "C"
{
void
Java_com_something_something_1android_ClassName_some_fn(JNIEnv *env, jobject obj)
{
__android_log_print(ANDROID_LOG_VERBOSE, SOME_TAG, "Hello from NDK :)");
}
} // End extern
Compile and run.
Over a year later with Android Studio 2.2 and above, you can now get all of this done for you for free just by selecting "Include C++ Support" when creating a new project.
For more information check out: https://developer.android.com/studio/projects/add-native-code.html.

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