Android ndk 使用NDK gradle构建系统中的clang

Android ndk 使用NDK gradle构建系统中的clang,android-ndk,gradle,Android Ndk,Gradle,使用旧的基于Makefile的Android构建系统,可以通过添加 NDK_TOOLCHAIN_VERSION=clang 使用新的gradle构建系统是否有办法实现同样的目标?目前还不能直接实现,但您仍然可以使用gradle中的常规生成文件: import org.apache.tools.ant.taskdefs.condition.Os apply plugin: 'android' android { ... sourceSets.main { jn

使用旧的基于Makefile的Android构建系统,可以通过添加

NDK_TOOLCHAIN_VERSION=clang

使用新的gradle构建系统是否有办法实现同样的目标?

目前还不能直接实现,但您仍然可以使用gradle中的常规生成文件:

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'android'

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

    // call regular ndk-build(.cmd) script from main src directory
    task ndkBuild(type: Exec) {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
        } else {
            commandLine 'ndk-build', '-C', file('src/main').absolutePath
        }
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
}

较新的NDK修订版默认为Clang。但是,您可以使用
-DANDROID\u工具链
开关显式请求工具链

从Android Gradle插件2.2.0开始,一切都变得更好了。您也可以采用
cmake
。你应该看看新的


NDK\u TOOLCHAIN\u VERSION=clang
放在Application.mk文件中更安全,而不依赖于正确的命令行。它一到那儿,你的毕业生就会把它捡起来。
android {
  ...
  defaultConfig {
    ...
    // This block is different from the one you use to link Gradle
    // to your CMake or ndk-build script.
    externalNativeBuild {

      // For ndk-build, instead use ndkBuild {}
      cmake {

        // Passes optional arguments to CMake.
        arguments "-DANDROID_TOOLCHAIN=clang"

        // Sets optional flags for the C compiler.
        cFlags "-D_EXAMPLE_C_FLAG1", "-D_EXAMPLE_C_FLAG2"

        // Sets a flag to enable format macro constants for the C++ compiler.
        cppFlags "-D__STDC_FORMAT_MACROS"
      }
    }
  }

  buildTypes {...}

  productFlavors {
    ...
    demo {
      ...
      externalNativeBuild {
        cmake {
          ...
          // Specifies which native libraries to build and package for this
          // product flavor. If you don't configure this property, Gradle
          // builds and packages all shared object libraries that you define
          // in your CMake or ndk-build project.
          targets "native-lib-demo"
        }
      }
    }

    paid {
      ...
      externalNativeBuild {
        cmake {
          ...
          targets "native-lib-paid"
        }
      }
    }
  }

  // Use this block to link Gradle to your CMake or ndk-build script.
  externalNativeBuild {
    cmake {...}
    // or ndkBuild {...}
  }
}