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:


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:


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.

© 2015, The Big Bang on Reading Busy. All rights reserved.
On republishing this post you must provide link to the original post.

(0) Comments    (454) Views   
Post a Comment