-
Notifications
You must be signed in to change notification settings - Fork 65
请问,使用buildTypes怎么根据release和debug模式调用不同的类或者jar? #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
如果使用了Dagger等依赖注入框架,可以根据buildtype的不同,动态的Provides不同的服务器接口。 @Provides
@Singleton
public BitCoinInterface provideBitCoinInterface(RestAdapter restAdapter) {
if(BuildConfig.DEBUG)
return restAdapter.create(DebugBitCoinInterface.class);
else
return restAdapter.create(ReleaseBitCoinInterface.class);
} |
gradle在编译之前,会对工程进行合并(merge),其中Java代码的merge方式是目录整合。具体是把main目录的Java文件 + 当前buildType目录的Java文件 + 当前productFlavor目录的文件直接整合到同一个目录。你说类文件不存在,建议你查看一下你的build variant当前是什么,它所对应的BuildType和ProductFlavor中是否含有缺少的那个类。 |
@promeG 我大概了解了你的方法,经过测试,确实是可以这样。但是会有文件冗余。release中的类和debug中的类都要放在main中。有解决这种冗余问题的解决办法吗? |
任何代码都不用改,你点击AS左下角的Build Variants,打开之后在Build Variant中选择baiduFree或者baiduPro,总之不要选择baiduDebug,然后再试试,应该就好了。gradle有两个默认的builtType,分别是debug和release,默认是debug。你只在free和pro中写了Utils,没有在Debug中写,程序在选中Debug的时候当然会报错了。是这个道理吧? |
@rengwuxian 目前已经实现了该功能。在这里进行总结一下。
谢谢各位的回答。 @rengwuxian @promeG 。 |
不错,学习了,之前还真搞懂这些。 |
我现在使用AS 0.8.1版本。想实现release和debug模式下调用不同的服务器接口。因此需要实现release和debug模式下调用不同的类。
目前我有一种实现方式,就是在productFlavors进行设置。之所以没有在buildTypes中设置,是因为gradle编译时会提醒类不存在,而且java文件夹也不会标记为java Folder?如果在productFlavors中进行设置,是可以编辑成功的,而且是调用起来确实是不同的接口。请问这个应该怎么解决?
The text was updated successfully, but these errors were encountered: