ale*_*exm 5 c macros gradle android-ndk android-studio
我已经在Android Studio中使用本地代码构建了一个库。它有一些与时间相关的部分(在C中),这些部分必须处于一定的延迟水平下,我需要对其进行度量。因此,我已经实现了一些测量例程。但是,我不希望测量例程在版本编译中起作用。
因此,我定义了宏,这些宏将允许我根据构建类型进行编译或不进行代码编写:
在我的本机代码中:
#ifdef MEASURE
measureRoutine1();
if (MEASURE == 1) {
measureRoutine2();
}
#endif
Run Code Online (Sandbox Code Playgroud)
在我的gradle文件中:
buildTypes {
release {
signingConfig signingConfigs.storeSignature
debuggable false
jniDebuggable false
versionNameSuffix "2.3"
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
jniDebuggable true
minifyEnabled false
versionNameSuffix '2.3.1'
}
measure1 {
initWith debug
externalNativeBuild {
cmake {
cppFlags += "-DMEASURE=0"
cFlags += "-DMEASURE=0"
}
}
}
measure2 {
initWith debug
externalNativeBuild {
cmake {
cppFlags += "-DMEASURE=1"
cFlags += "-DMEASURE=1"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是我编译的buildType无关紧要,在我的本机代码中未定义MEASURE标志,因此永远不会编译测量代码。
我究竟做错了什么?我怎样才能达到我所需要的?
PS:我要在本机中定义MEASURE宏的唯一方法是将cFlags放在buildConfigs之外的defaultConfig空间中。下面的代码可以工作,但是每次我要编译测量值时,我都需要对MEASURE宏进行硬编码。
defaultConfig {
minSdkVersion 21
targetSdkVersion 25
versionCode 6
versionName "2.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -fexceptions -O2 -ffast-math -DMEASURE=0"
cFlags "-O2 -ffast-math -DMEASURE=0"
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新1:
如果我在buildType发行版下定义了MEASURE宏(仅在发行版下,或者在发行版和调试下都定义),那么该宏将显示在本机代码中,并且在发行版中定义了de value。似乎它只是未在调试构建类型下定义。对可能发生的事情有任何想法吗?
我正在使用具有最新稳定更新的Android Studio 2.3.3。另外,我创建了一个新的测试项目,以检查我的旧项目是否有错误,但是两者都相同。
更新2:
我开始认为,也许Android Studio无法构建正确的buildType。另一个有趣的事情发生了,我尝试在debug buildType内为java定义一个布尔值生成配置字段,即使自动生成的BuildConfig.java将该字段设置为true,在执行调试时我仍然得到一个错误,该变量不存在(除非我将变量放在发布buildType下)。因此,无论我在构建变体中选择什么,它看起来总是在构建发行版本类型。
这是我完整的测试项目构建过程:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
versionNameSuffix '2.3.1'
externalNativeBuild {
cmake {
cppFlags += "-DMEASURE=0"
cFlags += "-DMEASURE=0"
}
}
buildConfigField "Boolean", "DEBUG_MODE", "Boolean.parseBoolean(\"true\")"
}
}
externalNativeBuild {
cmake {
path 'src/main/cpp/CMakeLists.txt'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud)
这是自动生成的BuildConfig.java文件:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "com.company.TestApp";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.02.3.1";
// Fields from build type: debug
public static final Boolean DEBUG_MODE = Boolean.parseBoolean("true");
}
Run Code Online (Sandbox Code Playgroud)
这是我在代码中使用BuildConfig.DEBUG_MODE时在执行时遇到的错误:
Error:(14, 27) error: cannot find symbol variable DEBUG_MODE
Run Code Online (Sandbox Code Playgroud)
可能会发生什么?
我找到了解决方案:
问题不在于宏是如何定义的,工作正常,问题是 gradle。
编译库时存在这个问题:基本上,每次使用 make project 时,“app”项目旁边的所有项目都会在其发布版本中编译,无论为它们选择什么构建变体。
因此,在这种情况下,问题在于,即使我在“nativelibrary”模块的构建变体框中选择“调试”,发布构建类型也会被编译+我为“app”构建变体选择的任何内容。
解决方案:
将以下代码添加到“nativelibrary”gradle 文件中:
defaultConfig {
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
//DEFINE THE DEFAULT PUBLISH CONFIG WHICH IS RELEASE
defaultPublishConfig 'release'
//ALLOW IT TO PUBLISH A NON DEFAULT WHICH WILL BE THE DEBUG OR
//A CUSTOM BUILD TYPE
publishNonDefault true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Run Code Online (Sandbox Code Playgroud)
将编译项目(':nativelib')以下代码替换为应用程序构建gradle:
dependencies {
//ERASE THE FOLLOWING LINE
//compile project(':nativelib')
//ADD BUILD TYPE SPECIFIC COMPILE TYPES AND REQUEST A SPECIFIC
//CONFIGURATION FOR THE 'nativelibrary' DEPENDING ON THE
//REQUESTED BUILD TYPE.
releaseCompile project(path: ':nativelib', configuration: "release")
debugCompile project(path: ':nativelib', configuration: "debug")
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud)
这解决了我的问题,现在正在创建宏以进行调试。
| 归档时间: |
|
| 查看次数: |
2545 次 |
| 最近记录: |