Android NDK java.lang.UnsatisfiedLinkError: findLi

2020-01-27 02:29发布

Having the above error in your Android JNI app? Read on...

Up front, I'll say that I've already solved this, in my own way, but I feel something in the Android build system (perhaps regarding Eclipse) is broke, and I hope to save someone else hours of pain. Perhaps others have come across this issue and can comment on what worked for them.

For a while, I've had an Android project with some JNI code that I developed using the NDK. Then, today, I changed something in the java code and then poof, I could no longer load my JNI library. It failed with an exception like:

E/AndroidRuntime( 999): java.lang.UnsatisfiedLinkError: Couldn't load mylibrary: findLibrary returned null

I googled and tried everything (rebuilding, close and relaunch Eclipse, etc, etc)

What finally fixed my problem? I physically uninstalled my app from the device before trying another run. That's it. After that, it worked. What worked for you?

16条回答
Melony?
2楼-- · 2020-01-27 03:05

I added

extern "C"

before functions declare
ex:

extern "C"
jstring
Java_lara_myapplication_MainActivity_stringFromJNI(
            JNIEnv *env,
    jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

then the error was gone

查看更多
Fickle 薄情
3楼-- · 2020-01-27 03:06

First, check you have the jni files inside you libs folder in eclipse or jniLibs folder in android studio. When you checkin in any svn, cross check to check in the .so files too.

My Scenario was when Building with Android studio.

When you are building in android studio, your library .so files or (jni) files should be inside the \src\main\jniLibs\armeabi***.so folder.

where as these files will be inside the \libs\armeabi***.so in eclipse.

When building using both eclipse and android studio, you have to modify your build.gradle file as

main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']

            // Fixed Issue : "android studio java.lang.unsatisfiedlinkerror: couldn't load find library returned null"
            // Cause : For android studio : The JNI files should be placed in the /src/main/jniLibs folder.
            // Fix : Mapping the jniLibs 's source Directories with Lib's folder
            jniLibs.srcDirs = ['libs']
}

Here I am building the project using both the android studio and the eclipse.

查看更多
兄弟一词,经得起流年.
4楼-- · 2020-01-27 03:07

Had identical problem. Just cleaned project and it worked. Mystery..

查看更多
神经病院院长
5楼-- · 2020-01-27 03:08

In my case, while making a System Application the issue was related to permissions. After putting the ".so" files into /system/lib/ or /system/vendor/lib/ directory, I modified the default allocated permissions 600 to 755. It worked well.

查看更多
登录 后发表回答