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)
}
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 to use methods in .so file in Android Studio?
public class Conversions {
private static Conversions mCom=null;
public static Conversions getInstance(){
if(mCom==null){
mCom=new Conversions();
}
return mCom;
}
public native int StdToIso(int itype,byte[] input,byte[] output);
public native int IsoToStd(int itype,byte[] input,byte[] output);
public native int GetDataType(byte[] input);
public native int StdChangeCoord(byte[] input,int size,byte[] output,int dk);
static {
System.loadLibrary("conversions");
System.loadLibrary("libfgtitinit");
}
}
try to replace this out i hope it will work
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.
Cannot add .so lib in Android Studio
I build my .so library again, from the jni, and it worked after that
NDK module not included in final APK
I didn't get several things: if you want to build a shared library with gradle, then you have to use apply plugin: 'com.android.model.library'
and not application
If you want to build an application and use prebuilt library, then you have to write something like this:
sources {
main {
jni {
source {
srcDir "../../core"
srcDir "src/main/jni"
}
dependencies{
library "XM" linkage "shared"
}
And, of course, to set ldFlags previously.
Related Topics
How to Break Styled Text into Pages in Android
Checking My App Version Programmatically in Android Market
Using Intent.Action_Pick for Specific Path
Firebase Messaging Nosuchmethoderror.Zzur Exception
How to Set Toolbar Text and Back Arrow Color
Loading All the Images from Gallery into the Application in Android
Android: Sample Code to Connect to Vpn in Android 4.0 Using Vpnservice API
Defining a Percentage Width for a Linearlayout
How to Display Fetched JSON Data into Listview Using Baseadapter
Creating Different Layout for Android Phone and Tablet
How to Detect App Removed from the Recent List
Out of Memory Exception Due to Large Bitmap Size
Could Not Launch Emulator in Android Studio
Achartengine - Can't Figure How to Use Dates as X Axis - the File I Save Is Empty