How to specify NDK_TOOLCHAIN_VERSION in gradle file for android ndk build

I am moving my android project that uses ndk-build to use the gradle build system as described in the new android build tools examples. In this link http://tools.android.com/tech-docs/new-build-system . I am looking at gradle-samples-0.11 at the bottom of the page for inspiration.

So I was able to set up all the parts I needed by including the following code in my default build.gradle config.

ndk {
            moduleName "MyModuleName"
            ldLibs "log"
            cFlags "-std=c++11 -fexceptions"
            stl "gnustl_static"
}

      

I have this line in my Application.mk file in my original project: NDK_TOOLCHAIN_VERSION: = 4.9 This is the last part that I cannot customize.

I am using NDK Revision 10. I need this NDK_TOOLCHAIN_VERSION: = 4.9 because my assembly is reporting an error: Error (25, 11): expected nested qualifier name before "Integer" and the C ++ code on that line looks like this.

using Integer = int16_t;

      

Does anyone have an idea on how I can solve this please?

+3


source to share


3 answers


I tried to solve this too. But in the end, writing custom tasks to force Android Studio to use Application.mk and Android.mk like in eclipse.

My build.gradle looks like this

apply plugin: 'com.android.application'

android {

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

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 20
        versionCode 1
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

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

task buildNative(type: Exec) {
    def ndkBuild = null;
    def ndkBuildingDir = new File("src/main/jni");
    def hasNdk = false;
    if (System.getenv("NDK_BUILD_CMD") != null) {
        hasNdk = true;
        ndkBuild = new File(System.getenv("NDK_BUILD_CMD"))
    }

    commandLine ndkBuild, "--directory", ndkBuildingDir

    doFirst {
        if (!hasNdk) {
            logger.error('##################')
            logger.error("NDK build failed!!")
            logger.error('Reason: NDK_BUILD_CMD not set.')
            logger.error('##################')
        }
        assert hasNdk: "NDK_BUILD_CMD not set."
    }
}
tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn buildNative }

task cleanNative(type: Exec) {
    def ndkBuild = null;
    def ndkBuildingDir = new File("src/main/jni");
    def hasNdk = false;

    if (System.getenv("NDK_BUILD_CMD") != null) {
        hasNdk = true;
        ndkBuild = new File(System.getenv("NDK_BUILD_CMD"))
    }

    commandLine ndkBuild, "--directory", ndkBuildingDir, "clean"

    doFirst {
        if (!hasNdk) {
            logger.error('##################')
            logger.error("NDK build failed!!")
            logger.error('Reason: NDK_BUILD_CMD not set.')
            logger.error('##################')
        }
        assert hasNdk: "NDK_BUILD_CMD not set."
    }
}
clean.dependsOn 'cleanNative'

      

To do this, you need to set the NDK_BUILD_CMD environment variable to install the exact ndk-build executable.



On Windows, you can simply set the NDK_BUILD_CMD environment variable to your ndk-build.exe

On Mac, path variables set in your .bash_profile are not available in GUI applications (so Android Studio will not be able to read them). So edit your .bash_profile to be something like

GRADLE_HOME={path_to_gradle}
ANDROID_SDK_ROOT={path_to_sdk_dir}
ANDROID_HOME=$ANDROID_SDK_ROOT/platform-tools
ANDROID_NDK_HOME={path_to_ndk}
NDK_BUILD_CMD=$ANDROID_NDK_HOME/ndk-build 

export PATH=$GRADLE_HOME/bin:$ANDROID_HOME:$ANDROID_SDK_ROOT/tools:$ANDROID_NDK_HOME:/opt/local/bin:/opt/local/sbin:$PATH

launchctl setenv GRADLE_HOME $GRADLE_HOME
launchctl setenv ANDROID_HOME $ANDROID_HOME
launchctl setenv ANDROID_NDK_HOME $ANDROID_NDK_HOME
launchctl setenv NDK_BUILD_CMD $NDK_BUILD_CMD

      

The launchctl lines will make your environment variables visible inside your Android Studio. PS: The .bash_profile file runs every time you open a terminal. So for it to work properly with Android Studio, you need to launch the terminal once and then launch Android Studio. Otherwise, the build will fail if NDK_BUILD_CMD is not installed. I haven't found a way to set the values ​​on Mac startup. If anyone can find a way, please feel free to suggest.

+5


source


I had this problem, but my solution was indeed in your original question. The latest version of the NDK uses GCC 4.9 by default ( https://developer.android.com/tools/sdk/ndk/index.html ). This is only the default on a 64 bit machine. Therefore, you no longer need to set the NDK_TOOLCHAIN_VERSION property in your build.gradle file. Just install:

cFlags "-std=c++11 -fexceptions"
stl "gnustl_static"

      



in ndk {} Should be enough. Works for me.

+3


source


In the latest Android Studio 1.5 and Gradle 2.10 with Experimental Plugins 0.7.0-alpha4, you can specify as in this example, it uses the Clang toolchain with version 3.6.

ndk {
                moduleName "MyModuleName"
                ldLibs "log"
                cFlags "-std=c++11 -fexceptions"
                stl "gnustl_static"
                toolchain "clang"
                toolchainVersion = "3.6" 
}

      

You can refer to your NDK directory to find out which programming chain and which versions are available.
For NDK r10e, the options are GCC (4.8, 4.9) and Clang (3.5, 3.6). If you do not specify the toolchain, it will use GCC 4.9 by default.

Refer to LINK to learn more about Gradle app configurations.

+1


source







All Articles