问题
I need to set a flag inside Android.mk
file which tells to the C code whether it's debug mode or not.
LOCAL_MODULE := auth
LOCAL_SRC_FILES := auth.c
LOCAL_CFLAGS := -DDEBUG_MODE=0
LOCAL_EXPORT_CFLAGS := $(LOCAL_CFLAGS)
LOCAL_LDLIBS := -llog
LOCAL_C_INCLUDES := auth.h
include $(BUILD_SHARED_LIBRARY)
The problem is that I often forget to change the value when building a release and viceversa so I'm looking forward to a way that does it automatically.
I tried changing Android.mk
to:
LOCAL_CFLAGS := -DDEBUG_MODE
And changed the build.gradle
to the following:
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 25
}
buildTypes {
release {
debuggable false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
ndk {
cFlags = " -DDEBUG_MODE=0 "
}
}
debug {
debuggable true
minifyEnabled false
shrinkResources false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
ndk {
cFlags = " -DDEBUG_MODE=1 "
}
}
}
sourceSets.main {
jni.srcDirs = ['src/main/none']
jniLibs.srcDirs = ["src/main/libs"]
}
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
return void
}
This was not enough, the flag is not changing depending on build type. I found few information about this and the above code is the result of what I read on the internet. Do you have any suggestion about how to get it to work properly?
回答1:
Use externalNativeBuild { ndkBuild {
within the build type. Note that the flags will be added to APP_CFLAGS
(i.e. all modules), not LOCAL_CFLAGS
.
build.gradle:
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.example.ndkbuildtest"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
ndkBuild {
abiFilters "armeabi-v7a"
arguments "APP_STL:=gnustl_static"
cppFlags "-std=c++11","-frtti","-fexceptions"
}
}
}
buildTypes {
release {
externalNativeBuild {
ndkBuild {
cFlags "-DDEBUG_MODE=0"
}
}
}
debug {
externalNativeBuild {
ndkBuild {
cFlags "-DDEBUG_MODE=1"
}
}
}
}
externalNativeBuild {
ndkBuild {
path "Android.mk"
}
}
}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := native-lib
LOCAL_SRC_FILES := src/main/cpp/native-lib.cpp
include $(BUILD_SHARED_LIBRARY)
native-lib.cpp:
#include <jni.h>
#include <string>
#if DEBUG_MODE
#error "Debug mode enabled!
#endif
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_ndkbuildtest_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
回答2:
You can do it in you Application.mk file with just these few lines.
## DEBUG MODE
APP_ABI := armeabi-v7a #Add your target ABI's or change, it's up to you.
APP_CFLAGS += -UNDEBUG -O0 -g -ggdb
APP_OPTIM := debug
APP_CPPFLAGS += -DDMZ_DEBUG=1
来源:https://stackoverflow.com/questions/44084950/set-android-mk-flag-through-gradle-depending-on-build-type