razaina.fr

Use NDK within Android Studio

Two files have to be updated to make Android-studio automatically ndk-build:

  • build.gradle
  • local.properties

The default content of my build.gradle file is as follows:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "fr.razaina.myapp"
        minSdkVersion 8
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

You just have to specify the name of the module you want to compile. In addition, in my example I did not forget to add the “log” as I am using it most of the time in my projects.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "fr.razaina.myapp"
        minSdkVersion 8
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        ######NDK=====
        ndk{
            moduleName "myLibName"
            ldLibs "log"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

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

Finally you have to specify in your local.properties file the path to NDK’s directory such as follows:

## This file is automatically generated by Android Studio.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file should *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/home/razaina/android-sdks
ndk.dir=/home/razaina/android-ndk-r10d
<< Older