Gradle Build Tools 2.2.0+ - NDK 最接近被称为“魔术”
为了避免实验性的,坦率地说厌倦了 NDK 和它的所有hackery,我很高兴 Gradle Build Tools 的 2.2.x 出来了,现在它可以正常工作了。关键是externalNativeBuild 并将ndkBuild 路径参数指向Android.mk 或将ndkBuild 更改为cmake 并将路径参数指向CMakeLists.txt 构建脚本。
android {
compileSdkVersion 19
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 19
targetSdkVersion 19
ndk {
abiFilters 'armeabi', 'armeabi-v7a', 'x86'
}
externalNativeBuild {
cmake {
cppFlags '-std=c++11'
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_PLATFORM=android-19',
'-DANDROID_STL=gnustl_static',
'-DANDROID_ARM_NEON=TRUE',
'-DANDROID_CPP_FEATURES=exceptions rtti'
}
}
}
externalNativeBuild {
cmake {
path 'src/main/jni/CMakeLists.txt'
}
//ndkBuild {
// path 'src/main/jni/Android.mk'
//}
}
}
更多详情请查看Google's page on adding native code。
正确设置后,您可以./gradlew installDebug 并离开。您还需要注意,由于 gcc 现在在 Android NDK 中已弃用,因此 NDK 正在转向 clang。
Android Studio 清理和构建集成 - 已弃用
其他答案确实指出了防止自动创建 Android.mk 文件的正确方法,但它们未能进一步与 Android Studio 更好地集成。我已经添加了从源代码实际清理和构建的功能,而无需转到命令行。您的local.properties 文件需要有ndk.dir=/path/to/ndk
apply plugin: 'com.android.application'
android {
compileSdkVersion 14
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.application"
minSdkVersion 14
targetSdkVersion 14
ndk {
moduleName "YourModuleName"
}
}
sourceSets.main {
jni.srcDirs = [] // This prevents the auto generation of Android.mk
jniLibs.srcDir 'src/main/libs' // This is not necessary unless you have precompiled libraries in your project.
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
def ndkDir = android.ndkDirectory
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
'-j', Runtime.runtime.availableProcessors(),
'all',
'NDK_DEBUG=1'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
def ndkDir = android.ndkDirectory
commandLine "$ndkDir/ndk-build",
'-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source
'clean'
}
clean.dependsOn 'cleanNative'
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn buildNative
}
}
dependencies {
compile 'com.android.support:support-v4:20.0.0'
}
src/main/jni 目录采用项目的标准布局。它应该是从这个build.gradle 文件位置到jni 目录的相对位置。
Gradle - 对于那些有问题的人
也可以查看Stack Overflow answer。
您的 gradle 版本和常规设置是否正确非常重要。如果您有一个较旧的项目,我强烈建议您使用最新的 Android Studio 创建一个新项目,看看 Google 认为标准项目是什么。另外,请使用gradlew。这可以保护开发人员免受 gradle 版本不匹配的影响。最后,gradle插件必须配置正确。
你问最新版本的 gradle 插件是什么? Check the tools page 并相应地编辑版本。
最终产品 - /build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
// Running 'gradle wrapper' will generate gradlew - Getting gradle wrapper working and using it will save you a lot of pain.
task wrapper(type: Wrapper) {
gradleVersion = '2.2'
}
// Look Google doesn't use Maven Central, they use jcenter now.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
确保gradle wrapper 生成gradlew 文件和gradle/wrapper 子目录。这是一个大问题。
ndk目录
这已经出现过很多次了,但是android.ndkDirectory 是在 1.1 之后获取文件夹的正确方法。 Migrating Gradle Projects to version 1.0.0。如果您使用的是实验版本或旧版本的插件,您的里程可能会有所不同。