0

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?

Botje
  • 26,269
  • 3
  • 31
  • 41
nastaseion
  • 173
  • 1
  • 2
  • 9
  • Read the error. It tells you where it is looking: `nativeLibraryDirectories=[/data/app/com.example.bmdtest03-2/lib/x86_64, /vendor/lib64, /system/lib64]` That tells me you are running the emulator (which uses the x86_64 architecture) and trying to feed it a precompiled library using the arm64-v8a architecture. That will never work, even if it _does_ find the file. – Botje Feb 17 '20 at 10:23
  • That leaves you two options: recompile your .so file for x86_64 and test in the emulator, or test on a real Android device (make sure it is 64 bit!). – Botje Feb 17 '20 at 10:24

1 Answers1

0

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

Pradeep Simba
  • 282
  • 5
  • 15
  • I'm not sure I get what you're saying. I already have the .so file (I compiled it myself on another machine, using Ubuntu). I placed the file in the proper jni folder (jniLibs/arm64-v8a) and it says it's not there – nastaseion Feb 17 '20 at 09:04
  • Add NDK path to local.properties. – Pradeep Simba Feb 17 '20 at 09:04
  • Thanks. I found a better way to point to the proper NDK location, since it's not recommended to edit that file. Add the NDK location from Android Studio: File > Project Structure > SDK Location > Android NDK location. Still, the error is the same – nastaseion Feb 17 '20 at 09:15
  • Try this . sourceSets { main { jniLibs.srcDirs 'imported-lib/src/', 'more-imported-libs/src/' } } – Pradeep Simba Feb 17 '20 at 09:31
  • Thanks, but tried it and it doesn't work. And I don't see how it could work, since the proper path is 'src/main/jniLibs' – nastaseion Feb 17 '20 at 09:39
  • Just go through I referred this from : https://developer.android.com/studio/projects/gradle-external-native-builds – Pradeep Simba Feb 17 '20 at 09:51
  • That means libhl.so not find in arms64-v8a folde. Try to remove your .so file extra character .1.0.0 If above solution not work follow the below. The .so file not only work in arms64-v8a folder. Add your .so file in armeabi-v7a directory. The folder name depend on Phone Hardware (May be processor). And .so file May be different for specific folder like arms64-v8a or armeabi-v7a and so on. Generally maximum device cover armeabi-v7a – Pradeep Simba Feb 18 '20 at 07:49