我需要在Android.mk
文件中设置一个标志,告诉C代码是否是调试模式。
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)
问题是我经常忘记在构建版本时更改值,反之亦然,所以我期待一种自动执行此操作的方法。
我尝试将Android.mk
更改为:
LOCAL_CFLAGS := -DDEBUG_MODE
并将build.gradle
更改为以下内容:
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
}
这还不够,标志不会因构建类型而改变。我发现很少有关于此的信息,上面的代码是我在互联网上阅读的结果。 您对如何使其正常工作有任何建议吗?
答案 0 :(得分:1)
在构建类型中使用externalNativeBuild { ndkBuild {
。请注意,这些标记将添加到APP_CFLAGS
(即所有模块),而不是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)
本机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());
}
答案 1 :(得分:0)
只需几行就可以在Application.mk文件中执行此操作。
## 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