- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 Android Studio 中使用 native 代码构建了一个库。它有一些时间相关的部分(在 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 标志都没有在我的 native 代码中定义,所以测量代码永远不会被编译。
我做错了什么?我怎样才能达到我的需要?
附言:获得在 native 中定义的 MEASURE 宏的唯一方法是将 cFlags 放在 defaultConfig 空间中的 buildTypes 之外。以下代码有效,但每次我想使用测量值进行编译时,我都需要对 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 release 下定义 MEASURE 宏(仅在 release 下或同时在 release 和 debug 下),该宏出现在我的 native 代码中定义,de 值在 release 中定义。似乎它只是没有在调试构建类型下定义。对可能发生的事情有任何想法吗?
我使用的是具有最新稳定更新的 Android Studio 2.3.3。此外,我还创建了一个新的测试项目来检查我的旧项目是否存在错误,但两者的情况相同。
更新 2:
我开始认为 Android Studio 可能没有构建正确的 buildType。另一件有趣的事情正在发生,我尝试在调试 buildType 中为 java 定义一个 bool 构建配置字段,即使自动生成的 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
可能会发生什么?
最佳答案
我找到了一个解决方案:
问题不在于宏的定义方式,它工作正常,问题在于 gradle。
有this issue编译库时:基本上,每次您使用 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"
}
将compile project(':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'
}
这解决了我的问题,现在正在为调试创建宏。
关于c - 如何在 gradle 中的构建类型中为 native 代码定义宏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44552332/
如何在 Excel 中编写可以在我将打开的任何 Excel 文档上工作(使用快捷方式运行)的宏? 这可能吗? 最佳答案 您需要将宏添加到 Personal.xlsb 以使它们可用于所有 excel 文
我正在研究 problem #74在4clojure.com,我的解决方案如下: (defn FPS [s] (->> (map read-string (re-seq #"[0-9]+"
我还没有完全理解Clojure 箭头宏thread-first -> 和thread-last 宏->> 之间的区别。在阅读 https://clojure.org/guides/threading_
我想将一些调试输出语句插入到大型 C 代码库中。这些调试输出语句将由编译器选项开关控制。 调试输出语句如下所示: #ifdef DEBUG_FLAG Print(someSymbol) #endif
我正在通过宏将代码注入(inject)到 C++ 类中。有没有办法根据访问修饰符的上下文来做到这一点?有点像 #if (we_are_in_public_context) INJECT_PUBLIC_
这应该与 memoize 类似,但有很大不同。虽然 memoize 应该与纯函数一起使用,但它通常对加速 IO 相关函数很有用。 我正在寻找的函数/宏应该表现得像高阶函数。它产生的功能应该: 第一次调
对于下面的代码: let services: [MyServices] = [ MyService(), #if DEBUG DebugService(), #endi
假设我有以下文本文件 name: John Doe description: My name is John Doe and I'm really good at vim! name: John Do
在创建 Excel 宏方面需要帮助。我有一个 Excel 工作表。Excel 工作表不一致。我打算使它统一和结构化。 例如。 A B C
我正在 excel 中设置一个宏,以便在更新单元格时自动发送电子邮件。是否可以在电子邮件正文中包含单元格的内容?例如,如果单元格 G7 已更新,请在电子邮件中包含单元格 B7 的内容?单元格行将是相同
我创建了一个简单的 Excel 工作表。 这是我的宏代码: Sub MyMacro() Sheets("Sheet1").Select A$ = Cells(1, 1) Msg
在 Excel 的 VB 宏中,如何删除所有出现的以某个字符串开头的单词? 例如: 字符串内容为:xxxx $AUD543.43 yyyy 我想搜索以 $AUD 开头的字符串中的任何内容并删除下一个空
我是 Excel 宏的新手.. 谁能告诉我这个宏是做什么的? Sub People_Add_Document() prow = ActiveCell.row num = Cells(p
我对 Excel 中的 VBA 和宏非常陌生。我有一个非常大的 Excel 电子表格,其中 A 列保存日期。我正在尝试删除值小于某个日期的行,这就是我到现在为止的想法。 Sub DELETEDATE(
我在 Excel 2003 中有一个 VBA 对象,当通过流数据获得某些值时,它会触发三个简单的宏。它运行良好。我想打开一个重复的工作表,但具有不同的流数据,并在各自的工作表上触发宏。它现在可以使用,
下面的宏有什么问题?我只想评估一个选项卡中的一个单元格是否大于另一个选项卡中的另一个单元格。然后消息框: Sub Comhouse() If Worksheets("(2.2) TRA works
需要一个简单的 excel 宏的帮助。我在第 1 列 X1 到 X20 中有数据。我想自动将此信息粘贴到 A 列,然后当我更新 X 列中的数字时,我想将此信息粘贴到 B 列,然后再粘贴到 C 列...
我找到了以下代码,效果很好;但是,我必须手动更改月份,以便它转到第二个工作簿的右侧工作表。由于工作表以月为单位,我怎样才能使其自动更改为当月? Sub AlarmSheet() Dim wkb As
很难说出这里问的是什么。这个问题是模棱两可的、模糊的、不完整的、过于宽泛的或修辞的,无法以目前的形式得到合理的回答。如需帮助澄清这个问题以便重新打开它,visit the help center .
我的公司只使用 MS Office 2003 产品,所以我必须坚持下去。由于我的工作性质,我需要使用很多“复制和粘贴”功能。源数据主要来自网站,我将数据粘贴到 Excel 中的单元格中。问题是剪贴板保
我是一名优秀的程序员,十分优秀!