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
Android Button Background Color
Setting Android Theme Background Color
Changing the Screen Brightness System Setting Android
Cannot Deserialize Instance of Object Out of Start_Array Token in Spring Webservice
How to Rotate Textview 90 Degrees and Display
Webview Rendering Issue in Android Kitkat
Cannot Create an Instance of Class Viewmodel
How to Include a Library Module Dependency in an Android Studio Project
Scoping States in Jetpack Compose
Communication Between Broadcastreceiver and Activity - Android
Android: Onsaveinstancestate Not Being Called from Activity
Android Accessibility Service Detect Notification
Delete File from Internal Storage
How to Add .So Files to an Android Library Project Using Gradle 0.7+
In Espresso, How to Avoid Ambiguousviewmatcherexception When Multiple Views Match
Set Visibility in Menu Programmatically Android