2

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:

  1. 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 of armeabi, armeabi-v7, and arm64-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.

  2. 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?

Community
  • 1
  • 1
amm
  • 343
  • 3
  • 11
  • 1
    Have you tried the default, starting idea - to put your `*.so` files in `main/libs` folder, where all the libraries go? :) Not `main/jniLibs` nor `main/libs/jniLibs` but just `main/libs` ? – Shark Feb 01 '16 at 15:01
  • Tried four variants of this suggestion: .so library in `/main/libs`, in `/main/libs/` (all three flavors), in `/libs` where my .jar includes are, and in `/libs/` (again, all three flavors). Android Studio is still complaining about not being able to find the JNI library (i.e. the native call is highlighted in red and the tooltip text has the complaint). – amm Feb 01 '16 at 15:37
  • Did you right-click it, and `Add as Library` ? – Shark Feb 01 '16 at 16:22
  • No such option in the context menu. – amm Feb 01 '16 at 16:48
  • You need to make sure it's in the buildpath somehow. – Shark Feb 02 '16 at 09:19

0 Answers0