简体   繁体   English

Android - 依赖项和代码分离(调试/发布版本)

[英]Android - dependencies and code separation (debug/release builds)

I have some code in my android application which I intend to keep only in debug-builds. 我在我的android应用程序中有一些代码,我打算只保留在debug-builds中。

I am aware of this approach: 我知道这种方法:

if (BuildConfig.DEBUG) {
    //code here
}

However, the problem is that my code relies on external dependencies which I would also like to keep in debug builds only : 但是,问题是我的代码依赖于外部依赖项,我还想在调试版本中保留它们:

debugCompile "dependency1"

//this wont compile in release mode
if (BuildConfig.DEBUG) {
    //code which references dependency1 
}

Given that there is no conditional compilation in java, are there any solutions besides commenting out blocks of code manually every time (which is obviously a huge hassle)? 鉴于java中没有条件编译,除了每次手动注释代码块之外还有任何解决方案吗(这显然是一个巨大的麻烦)?

EDIT: Egor pointed me in the right direction and after doing some more reasearch on "source sets" I found an answer which PRECISELY describes my situation and provides an excellent solution: https://stackoverflow.com/a/31483962/5790273 编辑:Egor 指出我正确的方向,在做了一些关于“源集”的研究后,我找到了一个答案,它精确地描述了我的情况并提供了一个很好的解决方案: https ://stackoverflow.com/a/31483962/5790273

Put the code you only want to compile in debug mode under "src/debug/java", as described here . 将您想要在“src / debug / java”下调试模式下编译的代码放在此处 ,如此处所述。 Gradle allows you to use multiple source sets per build type and flavor, and will only compile those that are relevant for the build configuration. Gradle允许您为每个构建类型和风格使用多个源集,并且仅编译与构建配置相关的源集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android应用程序的发布 - 调试版本 - Release-Debug Builds for Android Application gradle和Android Studio中调试和发布的不同依赖关系 - Different dependencies for debug and release in gradle and Android Studio DEBUG和RELEASE用Java(Eclipse)构建? - DEBUG and RELEASE builds in Java (Eclipse)? 为什么只在发布版本中包含依赖关系? - Why Would Dependencies Be Included Only In Release Builds? 为什么有.Net的Debug和Release版本,但Java没有? - Why are there Debug & Release builds for .Net, but not for Java? 如何在android ant发布和调试版本中附加不同的字符串资源文件? - How can I attach different string resource files during android ant release and debug builds? 是什么导致Android的DisplayMetrics.densityDpi在Release版本和Debug版本之间有所不同? - What could cause Android's DisplayMetrics.densityDpi to differ between Release and Debug builds? 在java - android中调试模式和发布模式之间切换代码 - switching code between debug mode and release mode in java - android 应用发布APK上的依赖项存在冲突,但没有调试? - Dependencies conflict on app release APK, but not debug? 如何为特定的应用程序构建(调试/发布)使用特定的字段? - How to use specific fields for specific application builds (debug/release)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM