现在Android Studio使用Gradle构建系统:有可能有不同的源文件或编译器开关来进行调试和发布构建吗?
事实上,在我的项目中有一些不同-例如,当我的应用程序在调试模式下构建时,我希望有不同的端点urls或bugreport urls。
据我所知,在普通的Java中,编译器开关不可能像这样
#if DEBUG
// do something when in debug build
#else
// do something when not in debug build
#endif在C#中。
Gradle或Android Studio本身是否为我提供了编译依赖于构建类型的不同代码的可能性?如何做到这一点?
两个例子:
@ReportsCrashes(formKey = "", formUri = "http://mydebugcrashreportserver/")
//@ReportsCrashes(formKey = "", formUri = "http://myreleasecrashreportserver/")
public class MyApplication extends Application { ... }根据构建类型的不同,ACRA应报告给不同的服务器。
public class Configuration {
public static final String APPLICATION_BASE_URL = "https://mydebugendpoint/api/rest";
// public static final String APPLICATION_BASE_URL = "https://myreleaseendpoint/api/rest";
public static final String GA_TRACKING_ID = "UA-XXXXXXXX-Y"; // debug Analytics
// public static final String GA_TRACKING_ID = "UA-ZZZZZZZZ-Y"; // release Analytics
}根据构建类型的不同,我的API端点和我的分析帐户也会有所不同。
这是非常恼人的,当我不得不在每次构建之前提醒手动进行这些更改时,会有潜在的bug。
那么如何在Android Studio和/或Gradle中实现自动化呢?
发布于 2014-06-13 17:58:22
好了,我找到解决方案了。它在Android Docs中进行了描述。
我必须创建额外的文件夹,名为debug和release,以及我想要的任何构建变量,并将依赖于该变量的源文件放在那里。
https://stackoverflow.com/questions/24200802
复制相似问题