如何为Android NDK的gradle构建文件中指定NDK_TOOLCHAIN​​_VERSION [英] how to specify NDK_TOOLCHAIN_VERSION in gradle file for android ndk build

查看:2857
本文介绍了如何为Android NDK的gradle构建文件中指定NDK_TOOLCHAIN​​_VERSION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感动我的使用NDK的构建中的新的构建工具为Android例子说明使用gradle这个构建系统的Andr​​oid项目。在这个链接 http://tool​​s.android.com/tech-docs/new-建立系统。我看了看gradle这个样本 - 0.11页面底部的灵感。

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

所以我设法配置所有我需要通过在我的build.gradle默认配置部分下面code中的作品。

So I managed to configure all of the pieces I need by including the following code in my build.gradle default config section.

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

我在最初的项目我的Application.mk文件中这一行:
  NDK_TOOLCHAIN​​_VERSION:= 4.9
这是最后一块我无法配置。

I have this line in my Application.mk file in the original project: NDK_TOOLCHAIN_VERSION := 4.9 It is the last piece I can't configure.

我使用NDK版本10.我需要这个NDK_TOOLCHAIN​​_VERSION:= 4.9,因为我的体型是报告错误:(25 11)错误:预期的嵌套名称说明之前整数,并在该C ++ code行看起来是这样的。

I am using NDK Revision 10. I need this NDK_TOOLCHAIN_VERSION:=4.9 because my build is reporting Error:(25, 11) error: expected nested-name-specifier before 'Integer' and the c++ code at that line looks like this.

using Integer = int16_t;

有没有人对我怎么能解决这个想法吗?

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

推荐答案

我一直在尝试过解决这个问题。但在通过编写自定义任务,使Android工作室在Eclipse中使用Application.mk和Android.mk就像结束了。

I've been trying to solve this too. But in the ended up by writing custom tasks to make Android Studio use Application.mk and Android.mk just like in eclipse.

我的build.gradle看起来像这样

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'

对于这个工作,你需要设置一个环境变量NDK_BUILD_CMD要设置的确切NDK建造可执行文件。

For this to work, you need to set an environment variable NDK_BUILD_CMD to be set the the exact ndk-build executable.

在Windows上,你可以设置环境变量NDK_BUILD_CMD点到你的NDK-build.exe

On Windows, you can just set an environment variable NDK_BUILD_CMD point to your ndk-build.exe

在Mac上,你在你的.bash_profile设置路径变量不在GUI应用程序访问(因此Android的Studio将无法读取它们)。
所以,编辑你的.bash_profile是像

On Mac,the path variables you set in your .bash_profile is not accessible on GUI applications(hence 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

该launchctl线将使您的环境变量的Andr​​oid Studio中可见。
PS:本.bash_profile中运行每次打开终端的时间。因此,对于这个正确与Android工作室工作,你需要启动一次终端,然后运行Android的工作室。否则,构建就会失败说NDK_BUILD_CMD未设置。我还没有发现任何方式的Mac启动时设置的值。如果有人能找到一个办法,请随时提出。

The launchctl lines will make your environment variables visible inside your Android Studio. PS: The .bash_profile is run every time you open the terminal. So for this to work properly with Android Studio, you need to launch the terminal once and then run Android Studio. Otherwise the build will fail saying NDK_BUILD_CMD not set. I haven't found any way to set the values when Mac starts. If someone else can find a way, please feel free to suggest.

这篇关于如何为Android NDK的gradle构建文件中指定NDK_TOOLCHAIN​​_VERSION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆