When we put the jni folder under projects and compile it using command line tool ndk-build, we will get libs & obj folders like this:

|–jni |–libs |—-armeabi-v7a |–obj

But under Android Studio develop environment, android studio can’t find the so files under libs folder.

We can do it by using following folder structure under Android Studio:

|—app |——jni |——libs |——obj |——src |———androidTest |———main

We can use this folder structure to compile JNI. When we compile the JNI file, ndk-build will generate libs & obj folders.

Finally, we add following to android part in build.gradle to let Android Studio finds the so files generated by jni folder:

sourceSets.main { jniLibs.srcDirs = [’libs’] }

Finally, the build.gradle should look like this:

apply plugin: ‘com.android.application’

android { compileSdkVersion 22 buildToolsVersion “21.1.2”

defaultConfig {
    applicationId "org.waterlin.mediatester"
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

sourceSets.main {
    jniLibs.srcDirs = \['libs'\]
}

}

dependencies { compile fileTree(dir: ’libs’, include: [’*.jar’]) compile ‘com.android.support:appcompat-v7:22.1.1’ }

By this way, Android Studio can let you use local jni code to generate so library and integrated into your Android project.