Importing library having native code in Android studio

Pradeep V R

07 Jan 2015

Importing library having native code in Android studio

Google stopped supporting eclipse and has adopted studio as the official IDE for developing Android apps. Let’s look into how to work on native code in Android studio. 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 a 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 any errors:

Steps in working on native code in Android studio

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 sources ( After checking out the source go to the library directory in the terminal and execute a ndk-build command.


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


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:

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( {
  pkgTask ->
  pkgTask.jniFolders = new HashSet()
  pkgTask.jniFolders.add(new File(buildDir, 'native-libs'))



Read more:

10 most interesting developments in IoT technology

How AI Can Be Beneficial to Healthcare Mobile apps?