【问题标题】:Using clang from NDK gradle build system使用来自 NDK gradle 构建系统的 clang
【发布时间】:2014-06-25 22:27:10
【问题描述】:

使用旧的基于 Makefile 的 Android 构建系统,可以通过添加使用 clang 来编译源代码

NDK_TOOLCHAIN_VERSION=clang

有没有什么方法可以使用新的 gradle 构建系统来实现相同的目标?

【问题讨论】:

  • NDK_TOOLCHAIN_VERSION=clang 放在Application.mk 文件中更安全,而不是依赖正确的命令行。只要它在那里,你的 gradle 就会把它捡起来。

标签: android-ndk gradle


【解决方案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
    }
}

【讨论】:

    【解决方案2】:

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

    从 Android Gradle 插件 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 {...}
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-21
      • 2011-06-11
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多