java.lang.UnsatisfiedLinkError while loading so library in Android Studio - android

I am trying to run this project .Every time i run the project i get the below error:
java.lang.UnsatisfiedLinkError: dlopen failed: /data/app/path.androidspeakerrec-1/lib/arm/libndkspeaker.so: has text relocations
The project does not load the *.so files. I have changed location of so file from libs to jniLibs, but still get the same issue.
Build .gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.sharannya.androidspeakerrec"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
ndk {
abiFilters "armeabi-v7a","armeabi"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
}
Call from java code
static {
try {
System.loadLibrary("ndkspeaker");
} catch (UnsatisfiedLinkError e) {
Log.e("Error in loading lib","Native code library failed to load" + e);
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndkspeaker
LOCAL_LDLIBS := -llog
LOCAL_SRC_FILES := lib_mfcc/abs.c \
lib_mfcc/abs1.c \
lib_mfcc/fft.c \
lib_mfcc/fi_fft.c \
lib_mfcc/fi_mfcc.c \
lib_mfcc/fi_mfcc_initialize.c \
lib_mfcc/fi_mfcc_rtwutil.c \
lib_mfcc/fi_mfcc_terminate.c \
lib_mfcc/log.c \
lib_mfcc/mfcc_bare.c \
lib_mfcc/mtimes1.c \
lib_mfcc/power1.c \
lib_mfcc/rt_nonfinite.c \
lib_mfcc/rtGetInf.c \
lib_mfcc/rtGetNaN.c \
lib_mfcc/sqrt.c \
lib_mfcc/sum.c \
SpeakerRecognizer.c
APP_ABI := armeabi
include $(BUILD_SHARED_LIBRARY)
The .so file is located in project/app/src/main/jniLibs/armeabi
and project/app/src/main/jniLibs/armeabi-v7a
How do i fix this?

You may have to set the 'jniLibs.srcDirs' in the gradle file. You can take a look here - actually, the code in the question should help you: How to setup NDK libs path in Gradle?

After some time of digging into the code, i got the solution to the problem.
The project i was trying to run was an older project with Android mk ndk build .So i removed all the cpp folder, cmake file from the project.
Then right clicked on libraries and selected the option "Link c++ with gradle". On click of the option , it will show a window to choose type of build system.Select "ndk build" and link the Android.mk path to the project.

Related

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.

Android NDK: Are you sure your NDK_MODULE_PATH variable is properly defined?

recently (3 days ago) started learning Android Studio. I bought an Eclipse game project to play with, but I am getting errors. And when I fix that error, I get a new error. The current error is as following:
Build command failed. Error while executing process
C:\Users\user\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd with
arguments {NDK_PROJECT_PATH=null
APP_BUILD_SCRIPT=C:\APPS\app\src\main\jni\Android.mk
NDK_APPLICATION_MK=C:\APPS\app\src\main\jni\Application.mk
APP_ABI=armeabi NDK_ALL_ABIS=armeabi NDK_DEBUG=0
APP_PLATFORM=android-15
NDK_OUT=C:/APPS/app/build/intermediates/ndkBuild/release/obj
NDK_LIBS_OUT=C:\APPS\app\build\intermediates\ndkBuild\release\lib
APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n} Android
NDK: C:\APPS\app\src\main\jni\Android.mk: Cannot find module
with tag 'box2D' in import path Android NDK: Are you sure your
NDK_MODULE_PATH variable is properly defined ? Android NDK: The
following directories were searched: Android NDK:
process_begin: CreateProcess(NULL, "", ...) failed.
I have googled and I saw a few other people had asked this question on this forum, but I didn't understand how to fix it (again, I'm a complete beginner)
Here's my build.gradle (Module: app):
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.getemplate.catadventure"
minSdkVersion 15
targetSdkVersion 26
ndk {
moduleName "player_shared"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
}
dependencies {
compile 'com.google.android.gms:play-services:+'
compile files('libs/dagger-1.2.2.jar')
compile files('libs/javax.inject-1.jar')
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/support-v4-19.0.1.jar')
}
And here is my Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := player_shared
LOCAL_MODULE_FILENAME := libplayer
LOCAL_SRC_FILES := main.cpp
LOCAL_WHOLE_STATIC_LIBRARIES := core_static cocos2dx_static box2d_static
GOOGLE_PLAY_STORE := true
include $(BUILD_SHARED_LIBRARY)
$(call import-module, box2D)
$(call import-module, core)
$(call import-module, cocos2dx)
$(call import-module,android-ndk-profiler/jni)
Thanks in advance.

Project porting from Eclipse to Studio

I have an android application which is been using OpenGL libraries and PVRTC Tool.
This application was running perfectly on Eclipse till API 19. Now I want to move this till API 23 so think to port the application to Android Studio as well for future updates sync. I am facing problems in this porting please look up the screenshots to see the folder hierarchy I have in Eclipse running code
[
[
Following was my Android.mk file:
LOCAL_PATH := $(call my-dir)
PVRSDKDIR := $(LOCAL_PATH)
include $(PVRSDKDIR)/Tools/OGLES2/Build/Android/Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE := nativeegl
LOCAL_CFLAGS := -DBUILD_OGLES2 -Wall -g
LOCAL_SRC_FILES := CCAppMain.cpp
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2# -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := \
ogles2tools \
android_native_app_glue
LOCAL_C_INCLUDES := \
$(PVRSDKDIR)/Builds/OGLES2/Include \
$(PVRSDKDIR)/Tools \
$(PVRSDKDIR)/Tools/OGLES2
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
And this is respective Application.mk file in Eclipse
# The ARMv7 is significanly faster due to the use of the hardware FPU
#APP_ABI := armeabi armeabi-v7a x86
#APP_OPTIM := release
APP_STL := stlport_static
I am using Android Studio Version 1.4 , Gradle Built 2.5
have set classpath as "classpath 'com.android.tools.build:gradle-experimental:0.2.0' "
My app module built.gradle looks like
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
defaultConfig.with {
applicationId = "xxx"
minSdkVersion.apiLevel = 14
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
}
compileOptions.with {
sourceCompatibility=JavaVersion.VERSION_1_7
targetCompatibility=JavaVersion.VERSION_1_7
}
android.ndk {
moduleName = "nativeegl"
//cppFlags += "-I${file("src/main/jni/native_app_glue")}".toString()
cppFlags += "-I${file("src/main/jni")}".toString()
cppFlags += "-I${file("src/main/jni/Tools")}".toString()
cppFlags += "-I${file("src/main/jni/Tools/OGLES2")}".toString()
cppFlags += "-I${file("src/main/jni/Tools/OGLES2/GLES2")}".toString()
ldLibs += ["android", "EGL", "GLESv2", "OpenSLES", "log"]
stl = "stlport_static"
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.txt')
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:support-v4:23.0.1'
}
I am really not able to configure the application at moment, On syncing it always giving errors such as
Error:Execution failed for task ':app:compileArmeabi-v7aDebugNativeeglSharedLibraryNativeeglMainCpp'.
> Multiple build operations failed.
C++ compiler failed while compiling PVRTPrint3D.cpp.
C++ compiler failed while compiling PVRTBackground.cpp.
C++ compiler failed while compiling PVRTShader.cpp.
C++ compiler failed while compiling PVRTPrint3DAPI.cpp.
C++ compiler failed while compiling PVRTPFXParserAPI.cpp.
See the complete log at: file:///E:/Android_Studio_Project/XXX/app/build/tmp/compileArmeabi-v7aDebugNativeeglSharedLibraryNativeeglMainCpp/output.txt
Can anyone look into it and make correction to my build.gradle file. I am guessing that PVRTC tool is been ignored in it.
Any help will be appreciated.
You are not obliged to forsake your carefully crafted Android.mk for the unpolished and limited DSL of the experimental gradle plugin. See more at define LOCAL_SRC_FILES in ndk{} DSL.
The specific problem in your case seems to be not that the PVRTC files are ignored, but that they are not compiled according to the rules and flags as configured in Tools/OGLES2/Build/Android/Android.mk.
TL;NR: append the following lines to you build.gradle:
def ndkBuild = android.ndkDirectory
import org.apache.tools.ant.taskdefs.condition.Os
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
ndkBuild += '.cmd'
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
commandLine '$ndkBuild', '-C', 'src/main'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
commandLine '$ndkBuild', 'clean', '-C', 'src/main'
}
clean.dependsOn 'cleanNative'
tasks.all {
task ->
if (task.name.startsWith('compile') && task.name.contains('MainC')) {
task.enabled = false
}
if (task.name.startsWith('link')) {
task.enabled = false
}
if (task.name.endsWith("SharedLibrary") ) {
task.dependsOn buildNative
}
}
android {
sourceSets {
main {
jniLibs.srcDirs = ['src/main/libs']
}
}
}

NDK-build vs Android Studio NDK Build

To run some Native code you can execute ndk-build command but I am confused if the Eclipse or Android Studio IDEs execute this command automatically on the Compile-time or how ndk-build command is related to the mentioned IDEs when I compile my code by eclipse or droid studio?
For eclipse, there are already too many online sources related to it, so I will talk about Android Studio here.
If you don't have your onw native files (C++,C) in your project, just put the .a or .so native libraries in the src/main/jniLibs folders and you don't have to modify anything in the gradle file. The system will do everything for you automatically.
If you have your own native files and have put them in the src/main/jni folder, then you have to create your own makefiles and put them in the jni folder as well. You also have to modify the gradle file in your app module.
Here is what I did for a face detection sample of opencv, and I got the original code from this wonderful post(Here), which was actually modified from another one's method for simpler execution. The philosophy is rather simple: compile the native source codes and put the generated .so/.a library into the jniLibs folder:
My project structure:
Project Structure
My makefiles (make sure you have the original sdk of the library for reference in the makefile):
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include /home/ng/Desktop/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)
Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a armeabi
APP_PLATFORM := android-19
My gradle.build file in the app module:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "org.opencv.samples.facedetect"
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
sourceSets.main.jni.srcDirs = []
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
// ndkDir = project.plugins.findPlugin('com.android.application').getNdkFolder()
commandLine "$ndkDir/ndk-build",
'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'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile project(':openCVLibrary300')
}
You don't actually have to change other gradle files for compiling the native part of the project.
You could also compile the native sources codes by command line and put the .so files back to the jniLibs folder. This also works.
I hope this could help in your problem.
If you want you could compile it using Terminal commands or else you could configure both the IDEs to do automatically
Today Android-Studio (Canary) Preview 1.3 RC1 got released with jni/ndk support out of the box. No need to call commandline tools anymore.
https://sites.google.com/a/android.com/tools/download/studio/canary/latest
Release Notes:
https://sites.google.com/a/android.com/tools/recent/androidstudiowithandroidndkpreviewsupportavailable

How to link the "lame" mp3 encoder shared object to an Android studio project

I am trying to write an Android app that uses the Lame mp3 encoder. My development
environment is Android Studio 1.1.
Following the hints under
Lame MP3 Encoder compile for Android
I managed to install the Android NDK and compile Lame. Under
/app/src/main/libs/armeabi
I obtained the shared object "libmp3lame.so", with
libmp3lame.so: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked (uses shared libs), stripped.
However, trying to load this file in a simple Android Studio project, utilizing
static {
System.loadLibrary("mp3lame");
}
always results in the following error:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/.../base.apk"],nativeLibraryDirectories=[/data/app/.../lib/arm64, /vendor/lib64, /system/lib64]]] couldn't find "libmp3lame.so"
I also tried to load the file with
System.load(*full file name*);
but this resulted in the same error.
From what I've read so far I assume that this is a gradle issue but I have no clue how to solve that. Any help will be much appreciated!
My Android.mk looks like this
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmp3lame
LOCAL_SRC_FILES := \
./libmp3lame/bitstream.c \
./libmp3lame/encoder.c \
./libmp3lame/fft.c \
./libmp3lame/gain_analysis.c \
./libmp3lame/id3tag.c \
./libmp3lame/lame.c \
./libmp3lame/mpglib_interface.c \
./libmp3lame/newmdct.c \
./libmp3lame/presets.c \
./libmp3lame/psymodel.c \
./libmp3lame/quantize.c \
./libmp3lame/quantize_pvt.c \
./libmp3lame/reservoir.c \
./libmp3lame/set_get.c \
./libmp3lame/tables.c \
./libmp3lame/takehiro.c \
./libmp3lame/util.c \
./libmp3lame/vbrquantize.c \
./libmp3lame/VbrTag.c \
./libmp3lame/version.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
And this is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "eu.martinloeser.android.lamefromscratch"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
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:21.0.3'
}
As I said before, any help will be much appreciated!
wild ass guess...
Your build is not hooking the ndk-build as it should. So.. read up on AS / ndk integration
making changes as he specifies in order to KEEP your Android.mk and not to get shunted off to auto-gen'd make.
Include in your build gradle his stuff to indicate you are not using jni defaults.
Include the explicit step to run the ndk-build against your JNI folder.
See if u dont get a 'libmp3lame.so' in the apk...
Thanks for your feedback. I finally got it to work. The key lies inside the 'build.gradle'-file.
In the 'defaultConfig'-section you have to tell Android studio that you want to link lame. Therefore, add
ndk{
moduleName "Lame"
ldLibs "log"
}
This did the trick for me :-).
Cheers,
Martin

Categories

Resources