使用来自NDK gradle构建系统的clang

时间:2014-06-25 22:27:10

标签: android-ndk gradle

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

来编译源代码
NDK_TOOLCHAIN_VERSION=clang

使用新的gradle构建系统是否有某种方法可以实现相同的目标?

2 个答案:

答案 0 :(得分:1)

目前还不能直接使用,但您仍然可以使用Gradle中的常规Makefile:

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
    }
}

答案 1 :(得分:0)

较新的NDK版本默认为Clang。但是,您可以使用-DANDROID_TOOLCHAIN开关明确请求工具链。

从Android Gradle Plugin 2.2.0开始,事情变得更好。您也可以采用cmake。你应该查看新的documentation

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 {...}
  }
}