Android的NDK和摇篮:每构建类型不同Android.mk [英] Android NDK and Gradle: Different Android.mk per build type
问题描述
我的本地库包含日志,我想在编译时删除。
My native library contains logs which I would like to remove at compile time.
Logs are shown by defining the pre-processor macro ENABLE_DEBUG
in LOCAL_CFLAGS
like so:
include $(CLEAR_VARS)
LOCAL_MODULE := native-stuff
LOCAL_SRC_FILES := Native.cpp
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -DENABLE_DEBUG
include $(BUILD_SHARED_LIBRARY)
我通过Android的工作室建设有摇篮的应用程序,我想有没有 LOCAL_CFLAGS另一个Android.mk文件:= -DENABLE_DEBUG
的发布版本,有效地禁用日志记录。
I'm building the app with Gradle via Android Studio and I would like to have another Android.mk file without LOCAL_CFLAGS := -DENABLE_DEBUG
for release builds, effectively disabling logging.
我试着在的src
创建文件夹发布/ JNI
来做到这一点,并把Android的副本。 MK没有CFLAGS。它建立并成功部署,但我还是看到了日志。这里是我的的build.gradle
:
I tried to do it by creating the folders release/jni
under src
and put a copy of Android.mk without the CFLAGS. It builds and deploys successfully, but I still see the logs. Here is my build.gradle
:
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 17
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
sourceSets {
main {
//Tell Gradle where to put the compiled shared library
jniLibs.srcDir 'src/main/libs'
//disable automatic ndk-build call
jni.srcDirs = [];
}
release {
jni.srcDirs = [];
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// Tell Gradle the run the ndkBuild task when compiling
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
// This task utilizes the Android.mk file defined in src/main/jni so that you
// have more control over the build parameters (like library inclusion)
// The system must define property 'androidNdkHome' in ~/.gradle/gradle.properties file
// to point to NDK path
task ndkBuild(type: Exec) {
commandLine "$androidNdkHome/ndk-build", '-C', file('src/main/jni').absolutePath
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.2'
}
我的项目结构如下所示:
My project structure looks like this:
src/
main/
jni/
Android.mk
release/
jni/
Android.mk
是否有可能做我想做什么?
Is it possible to do what I want?
推荐答案
我觉得我有一个解决方案在这里。它并不像pretty因为我本来希望它是(特别是因为它涉及创建一个临时文件),但我测试了它和它的工作如预期。
I think I've got a solution here. It's not as pretty as I would have liked it to be (esp. as it involves creating a temporary file), but I've tested it and it's working as intended.
我写这个使用其他的 Application.mk 的调试版本,名为 Application-debug.mk 的。你可以把 APP_CFLAGS:= -DENABLE_DEBUG
里面做你想做的。
I've written this to use another Application.mk for debug builds, called Application-debug.mk. You can put APP_CFLAGS := -DENABLE_DEBUG
inside to do what you want.
它如何工作的注释是在code。如果它是一个应用程序生成的文件,可以使用 applicationVariants.all
而不是 libraryVariants.all
:
Comments on how it works are in the code. If it's for an application build file, use applicationVariants.all
instead of libraryVariants.all
:
android.libraryVariants.all { variant -> // executes this block for each variant, current and futures
def task = tasks.create(name: "ndk${variant.buildType.name.capitalize()}Build") { //create ndkReleaseBuild and ndkDebugBuild tasks
doLast {
exec { //execute all this block when the task is executed. (A Build task gets only the commandLine to be executed, so here we're using doLast.exec instead)
def extraParameter = ""
def rebuildFlag = ""
if (variant.buildType.name == "debug")
extraParameter = "NDK_APPLICATION_MK=" + file('src/main/jni/Application-debug.mk').absolutePath //reference to another Application.mk to use when doing a debug build.
def lastBuildFile = file('src/main/jni/.lastbuild.tmp') //save the last build type to a temp file, to avoid triggering rebuilds all the time.
if (!lastBuildFile.exists())
lastBuildFile.createNewFile()
def lastBuildType = lastBuildFile.text
if (lastBuildType != variant.buildType.name) {
println "== ndk build variant has changed, triggering full rebuild. =="
rebuildFlag = "-B"
}
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'ndk-build.cmd', rebuildFlag, '-C', file('src/main').absolutePath, extraParameter
} else {
commandLine 'ndk-build', rebuildFlag, '-C', file('src/main').absolutePath, extraParameter
}
lastBuildFile.write(variant.buildType.name)
}
}
}
variant.javaCompile.dependsOn task
}
我推这个code作为依据,以及: HTTPS://gist.github。 COM / ph0b / 92f1f664c453869636f8
这篇关于Android的NDK和摇篮:每构建类型不同Android.mk的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!