Include .So Library in APK in Android Studio

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

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).

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.

NDK: how include *.so files in AndroidStudio

Three options:

One

Copy yours *.SO libraries on your libs folder and put that on build.gradle:

dependencies
{
compile fileTree(include: ['*.jar'], dir: 'libs')
}

Two

Make a new folder on src/main/jniLibs and write that on your build.gradle:

android {
//Another code
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jnilibs']
}
//Another code
}//sourceSets tag close
}//Android tag close

There

Make a new folder on src/main/jniLibs and write that on your build.gradle:

//Another code....

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


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

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

Include .so library in apk in android studio

I had the same problem. Check out the comment in https://gist.github.com/khernyo/4226923#comment-812526

It says:

for gradle android plugin v0.3 use "com.android.build.gradle.tasks.PackageApplication"

That should fix your problem.

Adding a .so file in Android Studio

You can add pre built *.so files in Android Studio using gradle 0.7.2+. First create the jniLibs at this location /app/src/main/ location and copy the all the folder with *.so files (armeabi, armeabi-v7a, mips, x86) in the jniLibs.

Sample Image

Add .so (prebuilt) library from another directory to APK

Apparently, you have NDK r17 installed and Android plugin v.3.1.0 or higher (we don't see this in the published fragment of build.gradle).

But you set abiFilters to armeabi, which has been dropped. You should set it to armeabi-v7a, and make sure that libtheprebuiltlib.so is also built for this ABI, or you can download an older version of NDK and in build.gradle dependencies set

classpath 'com.android.tools.build:gradle:3.0.1'

You can force the latest plugin handle armeabi if you set it explicitly:

android {
defaultConfig {
ndk {
abiFilters 'armeabi'
}
}
}

(in your script, it is under android.defaultConfig.externalNativeBuild.ndk, so has no effect).

One mistake in your build.gradle, it should read

android {
sourceSets {
main {
jniLibs.srcDir 'C:/svn/sys_libs'
}
}
}

when you have the file C:\svn\sys_libs\armeabi\libtheprebuiltlib.so. But this does not explain why cmake does not work as expected.

Use .So file in android studio

I found the problem and its answer.

The problem was about Gradle version and I had to add this line:

defaultConfig {
ndk {
abiFilters 'armeabi'
}
}


Related Topics



Leave a reply



Submit