Importing library having native code in Android studio

  • Replies:1
Madhura shree
  • Forum posts: 3

Jul 2, 2015, 11:59:51 AM via Website

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 library into the IDE which could be having native code as well. Importing native library in studio is 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
After checking out source go to library directory in terminal and execute 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 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 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.
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'))
}

Reply
Michael G
  • Forum posts: 1

Sep 14, 2016, 11:12:03 AM via Website

Hello,

I tried to do your decribed steps. But if I call ndk-Build I got the following error:

Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
G:\Android\sdk\ndk-bundle\build\..\build\core\build-local.mk:151: * Android NDK: Aborting . Stop.

I want to to use teh upscaleDB inside my Android Application. I use Android Studio for developement.

Reply