Can't add .so file to Android Studio 3.5.3 using jniLibs-ThrowExceptions

Exception or error:

I’m trying to add a .so file to a new Android project. I’m using Android Studio 3.5.3. I’ve done what was recommended here and here ; so I created folders (jniLibs/arm64-v8a, etc) in src/main and added this to app/build.gradle:

sourceSets {
    main {
        jniLibs.srcDirs = ['src/main/jniLibs']
    }
}

I placed the libhl.so file in /app/src/main/jniLibs/arm64-v8a (yes, 64-bit should be ok). The project will compile just fine, but on execution it will show the following error in logcat:

02-17 10:25:31.719 3691-3691/com.example.bmdtest03 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bmdtest03, PID: 3691
    java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.bmdtest03-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.bmdtest03-2/lib/x86_64, /vendor/lib64, /system/lib64]]] couldn't find "libhl.so"
        at java.lang.Runtime.loadLibrary(Runtime.java:366)
        at java.lang.System.loadLibrary(System.java:989)
        at com.example.bmdtest03.MainActivity.<clinit>(MainActivity.java:14)
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at java.lang.Class.newInstance(Class.java:1572)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

Conclusion: the file is there, the source is updated in build.gradle, what am I missing?

How to solve:

You can manually generate the .so file with NDK . Then you can add the lib.so file in your jni folder.

Add the NDK path to your local.properties.

Refer this page : https://developer.android.com/studio/projects/gradle-external-native-builds

If its not works. You can create javah then you can access it.
Link to make javah : https://developer.android.com/training/articles/perf-jni

Leave a Reply

Your email address will not be published. Required fields are marked *