How to Include *.So Library in Android Studio

How do I add .so file to lib folder in APK?

This method should still work:

 add_library(lib_extra SHARED IMPORTED)
set_target_properties(lib_extra PROPERTIES IMPORTED_LOCATION
${your-extra-lib-location}/${ANDROID_ABI}/libextra.so)

target_link_libraries(${project-lib-name} lib_extra)

Make sure you have a binary(extra.so) for each ABI you plan to include in your app. After build APK, you can check they are there with Studio's build -> Analyze APK... (or just unzip the APK file).

How to add prebuilt *.so library in android studio 2.2

currently need to pack it by the app. it could be something like:

    sourceSets {
main {
// let gradle pack the shared library into apk
jniLibs.srcDirs = ['point/to/your/shared-lib']
}
}

one example is: https://github.com/googlesamples/android-ndk/blob/master/hello-libs/app/build.gradle
If your shared lib [yours is inside project path] is close to your project, put relative path of your shared-lib to your CMakeLists.txt would work.

Some background discussion at the bottom of this bug might help:
https://code.google.com/p/android/issues/detail?id=214664&can=8&q=vulkan&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened

How to include *.so library in Android Studio?

Current Solution

Create the folder project/app/src/main/jniLibs, and then put your *.so files within their abi folders in that location. E.g.,

project/
├──libs/
| └── *.jar <-- if your library has jar files, they go here
├──src/
└── main/
├── AndroidManifest.xml
├── java/
└── jniLibs/
├── arm64-v8a/ <-- ARM 64bit
│ └── yourlib.so
├── armeabi-v7a/ <-- ARM 32bit
│ └── yourlib.so
└── x86/ <-- Intel 32bit
└── yourlib.so

Deprecated solution

Add both code snippets in your module gradle.build file as a dependency:

compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')

How to create this custom jar:

task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
destinationDir file("$buildDir/native-libs")
baseName 'native-libs'
from fileTree(dir: 'libs', include: '**/*.so')
into 'lib/'
}

tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn(nativeLibsToJar)
}

Same answer can also be found in related question: Include .so library in apk in android studio

Android Studio - include and consume .so library

Thank you guys for helping but it was a stupid problem. When I imported my .so files under jniLibs, they were named like libSONAME.so. In these lines of code:

static {
System.loadLibrary("libSONAME");
}

we should not use System.loadLibrary("libSONAME");, but just System.loadLibrary("SONAME");.

Then, just build the project and everything was OK.

Thank you all for helping. I hope this will save time to someone else.



Related Topics



Leave a reply



Submit