Android Studio import native library
This is my first time to do import some .so file to AS. I spent a lot of time and searched by websites.
You have many options to import .so file. I just did two ways for importing it.
Using JniLibs file
First step
You need to add as below in build.gradle (Moudle: YOURMOUDLE).
dependencies { compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar') }
This one is outter all of class.
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) }
Then your first step has finished!!
Second step
New a folder 'jnilibs' at MODULE/src/main.
The path image as like below
Module ├ src ├ main ├ jniLibs ├ armeabi | └ libcpu_jni.so ├ armeabi-v7a | └ libcpu_jni.so ├ mips | └ libcpu_jni.so └ x86 └ libcpu_jni.so
Then you can enjoy using it!!
Put them in libs
First step
The same as way one. You need to add something in build.gradle (Moudle: YOURMOUDLE).
android { sourceSets { main { jniLibs.srcDirs = ['libs'] } } }
You have already finished half of process!!!
Second step
You just put *.so file you prepared in the MOUDLE/libs/.
The path image as like below
Module ├ libs ├ armeabi | └ libcpu_jni.so ├ armeabi-v7a | └ libcpu_jni.so ├ mips | └ libcpu_jni.so └ x86 └ libcpu_jni.so
Just easy to import your *.so file.
How to use .so file's method
You just type a little code then you can declare the function.
// Register the function name as following in your libcpu_jni.so file. private static final native boolean readProcFile(String file, int[] format, String[] outStrings, long[] outLongs, float[] outFloats); // Load that libcpu_jni.so file from native library. static { try { System.loadLibrary("cpu_jni"); } catch (UnsatisfiedLinkError ule) { Log.e("LoadJniLib", "Error: Could not load native library: " + ule.getMessage()); } }
Call the native function.
readProcFile("/proc/stat", SYSTEM_CPU_FORMAT, null, sysCpu, null)
My personal like the first way. Beacuse of you can see the folder when you are using 'Android' not 'project'.






