Ndk: How Include *.So Files in Androidstudio

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



Leave a reply



Submit