Creating a Product Sdk: How to Add a Native Lib (.So) and a Jar with the Sdk I am Creating

Create an SDK (single jar file) with so files

Actually it is what Android does since ADT 17 has come. As matter of fact, when your create a library project and you import it in another project, Android creates a jar file. This is true for native library too. Simple when you build your project library, the armeabi folder will be included in the jar too.

How can I add a '.so' library in my android project

after making your native c code and header file browse to the root directory of your project and run ndk-build command.That will generate the .so file and then place it inside your project/libs folder

How to create a Java Library (API) with native code via JNI

Well, your Library will contain both the .jar file with the java wrapper code as well as the native files (.so if you're on linux based, or .dll if you're on windows).

Here's where the fun begins :

Because native is compiled in processor assembly language you will have to compile the .so for all your supported target (eg for all android with native support since like forever):
armv5, armv7, armv7s , arm64

Now, you will have to provide an archive with all the above.

This is the case where you want a stand alone library, without providing the code to the developer.

If you can provide the code,then you don't need to worry about different architectures.

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



Related Topics



Leave a reply



Submit