【发布时间】:2015-03-05 13:13:09
【问题描述】:
我的本机库包含我想在编译时删除的日志。
通过在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
-
这很有趣,但我仍然需要弄清楚构建类型名称,以便将
Android.mk放在单独的文件夹debug/和release/下。知道如何获取当前的构建类型吗? -
查看任务的堆栈跟踪,您各自构建类型的前身 - 类似于 'prepareDebug....' 。并设置将获得正确值的属性值到“Application.mk”中以供最终使用 Android.mk。
-
已经回答了,但是对于那些正在寻求另一种方法的人,您可以查看我的答案。 stackoverflow.com/questions/46284045/…
标签: android android-studio android-ndk java-native-interface android-gradle-plugin