This seems to be a popular problem with Android: I have a precompiled .so file that I would like to access via the JNI, but I cannot get Android Studio (in my case version 1.5) to build the project in a way that makes this file accessible. Like the other people asking this question, I hit an UnsatisfiedLinkError when calling System.loadLibrary():
static {
System.loadLibrary("NativeLib");
}
There are several questions like this one on StackOverflow, and the solutions appear to focus on two techniques, neither of which work for me:
Creating a main/jniLibs directory: Several people say that if you create a folder called
/app/src/main/jniLibs
and within that a folder for your target (I tried all ofarmeabi
,armeabi-v7
, andarm64-v8a
), Android Studio will find the library. See also This question. If I do this, the native call in my java code remains highlighted in red with tooltip text, 'Cannot resolve corresponding JNI function Java_my_function_etc'. The tooltip reference is identical to my method call, which I generated with the javah utility.Modifying build.gradle Another proposed solution is to point to the location of the .so file by making a modification to build.gradle. In my case, I tried setting jniLibs.srcDirs to
/src/main/jniLibs
, and 'jniLibs' while the .so file in question was located in/app/src/main/jniLibs/armeabi
,/app/src/main/jniLibs/armeabi-v7a
, and/app/src/main/jniLibs/arm64-v8a
. My gradle file:apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.my.application" minSdkVersion 21 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets { main { jniLibs.srcDirs = ['jniLibs'] } } } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile files('libs/commons-lang3-3.1.jar') }
A third technique which appears to have emerged from a Google Groups posting in 2013 involves packaging the .so files into a jar and including the jar file in the project. I unzipped my APK file and found that the .so files are included in ./lib/armeabi
, ./lib/armeabli-v7a
, and ./lib/arm64-v8a
. I didn't try this technique because it seems obsolete, and unnecessary since my .so files are making it into the APK.
I'm out of things to try. Is there any other technique for including a native .so in an Android Studio project? Is there anything I could have overlooked in the above two techniques? Has Android Studio changed in some critical way since the linked questions were answered?