Cumulations Logo

Google stopped supporting eclipse and has adopted studio as official IDE for developing Android apps. Most of the apps these days will require importing one or more projects as the library into the IDE which could be having the native code as well. Importing native library in the studio is a bit more tedious compared to eclipse. Below are the steps to follow as a developer to import them without having any errors:

Step 1: Build .so files of the native library
My project involved applying filters for the images, so I checked out one of the available open source (https://github.com/ragnraok/android-image-filter). After checking out source go to library directory in terminal and execute a ndk-build command.

cd  
ndk-build 

The above two commands will build variants of .so files in /obj/local directory. For me, it built below variants

arm64-v8a
armeabi
armeabi-v7a
mips
mips64
x86
x86_64

Step 2: Create a native-libs folder inside app/src/main directory of your project and copy the above-generated folders (arm64-v8a, armeabi, armeabi-v7a, mips, mips64, x86, x86_64) inside native-libs.

Step 3: Import native library project in the same way you import any other library projects in the studio.

Step 4: In the last step modify your app's build.gradle file as shown below. I have removed few lines from here which is specific to the app. Source I referred to modify my build.gradle file can be found here: https://gist.github.com/khernyo/4226923

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.1"

    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDir 'src'
            res.srcDir 'res'
            assets.srcDir 'assets'

            jniLibs.srcDir 'src/main/libs'
            jni.srcDirs = []           //disable automatic ndk-build call
            jniLibs.srcDirs = ['libs']
        }
    }

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

    buildTypes {
        release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

// this was added because of conflict in manifest and strings resource file
    packagingOptions {
       exclude 'main/AndroidManifest.xml'
       exclude 'main/res/values/strings.xml'
    }

}

dependencies {
      compile project(':androidImageFilter') // native library that is imported and added as dependency to the project.
      compile fileTree(dir: '../main/libs', include: '*.jar')
      compile files('libs/android-support-v4.jar')
}

// most important part to include .so files into the project
task copyNativeLibs(type: Copy) {
      from(new File(getProjectDir(), 'src/main/native-libs')) { include '**/*.so' }
      into new File(buildDir, 'native-libs')
}

tasks.withType(org.gradle.api.tasks.compile.JavaCompile) { 
        compileTask -> compileTask.dependsOn copyNativeLibs 
}

clean.dependsOn 'cleanCopyNativeLibs'

tasks.withType(com.android.build.gradle.tasks.PackageApplication) {
         pkgTask ->
         pkgTask.jniFolders = new HashSet()
         pkgTask.jniFolders.add(new File(buildDir, 'native-libs'))
}