android tutorial - NDK plugin support for Gradle and AndroidStudio | Developer android - android app development - android studio - android app developement



Enable experimental NDK plugin support for Gradle and AndroidStudio

Enable and configure the experimental Gradle plugin to improve AndroidStudio's NDK support. Check that you fulfill the following requirements:

  • Gradle 2.10 (for this example)
  • Android NDK r10 or later
  • Android SDK with build tools v19.0.0 or later

Configure MyApp/build.gradle file

  • Edit the dependencies.classpath line in build.gradle from e.g.
classpath 'com.android.tools.build:gradle:2.1.2'
click below button to copy code from our android learning website - android tutorial - team

to

classpath 'com.android.tools.build:gradle-experimental:0.7.2'
click below button to copy code from our android learning website - android tutorial - team
  • (v0.7.2 was the latest version at the time of writing. Check the latest version yourself and adapt your line accordingly)

The build.gradle file should look similar to this:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.7.2'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
click below button to copy code from our android learning website - android tutorial - team

Configure MyApp/app/build.gradle file

  • Edit the build.gradle file to look similar to the following example. Your version numbers may look different.
apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion 19
        buildToolsVersion "24.0.1"

        defaultConfig {
            applicationId "com.example.mydomain.myapp"
            minSdkVersion.apiLevel 19
            targetSdkVersion.apiLevel 19
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles.add(file('proguard-android.txt'))
            }
        }
        ndk {
            moduleName "myLib"
            
            /* The following lines are examples of a some optional flags that 
               you may set to configure your build environment
            */ 
            cppFlags.add("-I${file("path/to/my/includes/dir")}".toString())
            cppFlags.add("-std=c++11")
            ldLibs.addAll(['log', 'm'])
            stl = "c++_static"
            abiFilters.add("armeabi-v7a")
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}
click below button to copy code from our android learning website - android tutorial - team
  • Sync and check that there are no errors in the Gradle files before proceeding.

Test if plugin is enabled

  • First make sure you have downloaded the Android NDK module. Then create an new app in AndroidStudio and add the following to the ActivityMain file:
public class MainActivity implements Activity {
    onCreate() {
        // Pregenerated code. Not important here
    }
    static {
        System.loadLibrary("myLib");
    }
    public static native String getString();
}
click below button to copy code from our android learning website - android tutorial - team
  • The getString() part should be highlighted red saying that the corresponding JNI function could not be found. Hover your mouse over the function call until a red lightbulb appears. Click the bulb and select create function JNI_.... This should generate a myLib.c file in the myApp/app/src/main/jni directory with the correct JNI function call. It should look similar to this:
#include <jni.h>

JNIEXPORT jstring JNICALL 
Java_com_example_mydomain_myapp_MainActivity_getString(JNIEnv *env, jobject instance)     
{
    // TODO

    return (*env)->NewStringUTF(env, returnValue);
}
click below button to copy code from our android learning website - android tutorial - team
  • If it doesn't look like this, then the plugin has not correctly been configured or the NDK has not been downloaded

Related Searches to NDK plugin support for Gradle and AndroidStudio