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 关于添加本机代码的页面。
正确设置后,您就可以./gradlew installDebug
出发了。您还需要注意 NDK 正在转向 clang,因为 gcc 现在在 Android NDK 中已弃用。
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 答案。
您的 gradle 版本和常规设置是否正确非常重要。如果您有一个较旧的项目,我强烈建议您使用最新的 Android Studio 创建一个新项目,并查看 Google 对标准项目的看法。另外,使用gradlew
. 这可以保护开发人员免受 gradle 版本不匹配的影响。最后,必须正确配置 gradle 插件。
你问最新版本的 gradle 插件是什么?检查工具页面并相应地编辑版本。
最终产品 - /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 之后获取文件夹的正确方法。将 Gradle 项目迁移到 1.0.0 版本。如果您使用的是实验版本或旧版本的插件,您的里程可能会有所不同。