Android NDK 和 Gradle:每个构建类型不同 Android.mk
Android NDK and Gradle: Different Android.mk per build type
我的本机库包含我想在编译时删除的日志。
通过在 LOCAL_CFLAGS
中定义预处理器宏 ENABLE_DEBUG
来显示日志,如下所示:
include $(CLEAR_VARS)
LOCAL_MODULE := native-stuff
LOCAL_SRC_FILES := Native.cpp
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -DENABLE_DEBUG
include $(BUILD_SHARED_LIBRARY)
我正在通过 Android Studio 使用 Gradle 构建应用程序,我想要另一个没有 LOCAL_CFLAGS := -DENABLE_DEBUG
的 Android.mk 文件用于发布版本,有效地禁用日志记录.
我试图通过在 src
下创建文件夹 release/jni
并在没有 CFLAGS 的情况下放置 Android.mk 的副本来做到这一点。它成功构建和部署,但我仍然看到日志。这是我的 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'
}
我的项目结构如下所示:
src/
main/
jni/
Android.mk
release/
jni/
Android.mk
是否可以如我所愿?
我想我在这里找到了解决方案。它不像我希望的那样漂亮(特别是因为它涉及创建一个临时文件),但我已经对其进行了测试并且它按预期工作。
我写这个是为了使用另一个 Application.mk 进行调试构建,称为 Application-debug.mk .你可以把APP_CFLAGS := -DENABLE_DEBUG
放在里面做你想做的。
有关其工作原理的评论在代码中。如果它用于应用程序构建文件,请使用 applicationVariants.all
而不是 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
}
我也将这段代码作为要点推送:https://gist.github.com/ph0b/92f1f664c453869636f8
我的本机库包含我想在编译时删除的日志。
通过在 LOCAL_CFLAGS
中定义预处理器宏 ENABLE_DEBUG
来显示日志,如下所示:
include $(CLEAR_VARS)
LOCAL_MODULE := native-stuff
LOCAL_SRC_FILES := Native.cpp
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -DENABLE_DEBUG
include $(BUILD_SHARED_LIBRARY)
我正在通过 Android Studio 使用 Gradle 构建应用程序,我想要另一个没有 LOCAL_CFLAGS := -DENABLE_DEBUG
的 Android.mk 文件用于发布版本,有效地禁用日志记录.
我试图通过在 src
下创建文件夹 release/jni
并在没有 CFLAGS 的情况下放置 Android.mk 的副本来做到这一点。它成功构建和部署,但我仍然看到日志。这是我的 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'
}
我的项目结构如下所示:
src/
main/
jni/
Android.mk
release/
jni/
Android.mk
是否可以如我所愿?
我想我在这里找到了解决方案。它不像我希望的那样漂亮(特别是因为它涉及创建一个临时文件),但我已经对其进行了测试并且它按预期工作。
我写这个是为了使用另一个 Application.mk 进行调试构建,称为 Application-debug.mk .你可以把APP_CFLAGS := -DENABLE_DEBUG
放在里面做你想做的。
有关其工作原理的评论在代码中。如果它用于应用程序构建文件,请使用 applicationVariants.all
而不是 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
}
我也将这段代码作为要点推送:https://gist.github.com/ph0b/92f1f664c453869636f8