![](/img/trans.png)
[英]How to convert this C code that uses “#define” for macros into Java?
[英]How to define macros for native code inside a build type in gradle?
我在Android Studio中構建了一個包含本機代碼的庫。 它有一些時間相關的部分(在C中)必須在一定的延遲水平下,我需要測量它們。 所以我已經實現了一些測量程序。 但是,我不希望測量例程在發布編譯中工作。
所以我已經定義了允許我根據構建類型編譯或不編碼的宏:
在我的本機代碼中:
#ifdef MEASURE
measureRoutine1();
if (MEASURE == 1) {
measureRoutine2();
}
#endif
在我的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"
}
}
}
}
現在,我的問題是我編譯的buildType無關緊要,我的本機代碼中沒有定義MEASURE標志,因此測量代碼永遠不會被編譯。
我究竟做錯了什么? 我怎樣才能達到我的需要?
PS:我在本機中定義MEASURE宏的唯一方法是將buildFs之外的cFlags放在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"
}
}
}
更新1:
如果我在buildType版本下定義MEASURE宏(僅在發布版本或調試版本下),宏在我的本機代碼中定義,並在發行版中定義de值。 它似乎只是沒有在調試版本類型下定義。 關於可能發生什么的任何想法?
我正在使用Android Studio 2.3.3以及最新的最新穩定更新。 此外,我已經創建了一個新的測試項目,以檢查我的舊項目是否被竊聽,但兩者都是相同的故事。
更新2:
我開始認為Android Studio可能沒有構建正確的buildType。 另一個有趣的事情正在發生,我已經嘗試在debug buildType中為java定義一個布爾構建配置字段,即使自動生成的BuildConfig.java將字段設置為true,在執行時調試我得到一個錯誤,即變量不存在(除非我把變量放在發布版本buildType下)。 所以看起來無論我在構建變體中選擇什么,它總是構建發布構建類型。
這是我的完整測試項目構建gradle:
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'
}
這是自動生成的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");
}
這是我在代碼中使用BuildConfig.DEBUG_MODE時在執行時得到的錯誤:
Error:(14, 27) error: cannot find symbol variable DEBUG_MODE
可能會發生什么?
我找到了一個解決方案:
問題不在於如何定義宏,工作正常,問題是混亂的。
編譯庫時存在這個問題 :基本上,每次使用make項目時,“app”項目旁邊的所有項目都會在其發布版本中進行編譯,無論為它們選擇哪種構建變體。
所以在這種情況下,問題是即使我在“nativelibrary”模塊的構建變體框中選擇了debug,也會編譯發布構建類型+我為“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"
}
將以下代碼替換為編譯項目(':nativelib')到app build 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'
}
這解決了我的問題,現在正在為調試創建宏。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.