现在
Android Studio使用Gradle Build System:是否可以为调试和发布版本提供不同的源文件或编译器开关.
事实上,在我的项目中存在一些差异 – 例如当我的应用程序在调试模式下构建时,我想要有不同的端点URL或bugreport url.
据我所知,在普通的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端点和我的分析帐户不同.
它非常烦人,当我不得不提醒在每次构建之前手动进行这些更改时,存在潜在的错误.
那么如何在Android Studio和/或Gradle中自动执行此操作?
最佳答案 好的,我找到了解决方案.它在
Android Docs中描述.
我必须创建名为debug和release的其他文件夹以及我想要的任何构建变量,并将sourcefiles依赖于那个变体.