Skip to content

请问,使用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

Closed
yanglw opened this issue Jul 8, 2014 · 7 comments

Comments

@yanglw
Copy link

yanglw commented Jul 8, 2014

我现在使用AS 0.8.1版本。想实现release和debug模式下调用不同的服务器接口。因此需要实现release和debug模式下调用不同的类。

目前我有一种实现方式,就是在productFlavors进行设置。之所以没有在buildTypes中设置,是因为gradle编译时会提醒类不存在,而且java文件夹也不会标记为java Folder?如果在productFlavors中进行设置,是可以编辑成功的,而且是调用起来确实是不同的接口。请问这个应该怎么解决?

@promeG
Copy link

promeG commented Jul 8, 2014

如果使用了Dagger等依赖注入框架,可以根据buildtype的不同,动态的Provides不同的服务器接口。
示例:

    @Provides
    @Singleton
    public BitCoinInterface provideBitCoinInterface(RestAdapter restAdapter) {
        if(BuildConfig.DEBUG)
            return restAdapter.create(DebugBitCoinInterface.class);
        else
            return restAdapter.create(ReleaseBitCoinInterface.class);
    }

@rengwuxian
Copy link
Contributor

gradle在编译之前,会对工程进行合并(merge),其中Java代码的merge方式是目录整合。具体是把main目录的Java文件 + 当前buildType目录的Java文件 + 当前productFlavor目录的文件直接整合到同一个目录。你说类文件不存在,建议你查看一下你的build variant当前是什么,它所对应的BuildType和ProductFlavor中是否含有缺少的那个类。

@yanglw
Copy link
Author

yanglw commented Jul 8, 2014

这个是我src目录下的文件结构。我在buildTypes中有两个2分支,一个是free,一个是pro。这两个分支对应的文件夹中都有Utils.java。我在main中有类使用到了这个Utils.java。productFlavors中也有两个分支,一个是baidu,一个是wandoujia。这两个分支对应的文件夹中没有类,只有AndroidManifest.xml,用于处理平台id。

image1

我在MainActivity中是这么调用的:
image2

我的build.gradle中关于buildTypes和productFlavors内容如下:
image3

在这种配置下,在进行编译的时候,他首先会提示我,类未定义

Error:Execution failed for task ':app:compileBaiduDebugJava'.
> Compilation failed; see the compiler error output for details.

当我添加debug文件夹后,添加了Utils类后,又会提示我,类未定义

Error:Execution failed for task ':app:compileBaiduReleaseJava'.
> Compilation failed; see the compiler error output for details.

于是,我又为release添加了Utils类后,又会提示我,类重复

'Error:Execution failed for task ':app:compileBaiduFreeJava'.
> Compilation failed; see the compiler error output for details.

@yanglw
Copy link
Author

yanglw commented Jul 8, 2014

@promeG 我大概了解了你的方法,经过测试,确实是可以这样。但是会有文件冗余。release中的类和debug中的类都要放在main中。有解决这种冗余问题的解决办法吗?

@rengwuxian
Copy link
Contributor

任何代码都不用改,你点击AS左下角的Build Variants,打开之后在Build Variant中选择baiduFree或者baiduPro,总之不要选择baiduDebug,然后再试试,应该就好了。gradle有两个默认的builtType,分别是debug和release,默认是debug。你只在free和pro中写了Utils,没有在Debug中写,程序在选中Debug的时候当然会报错了。是这个道理吧?

@rengwuxian rengwuxian reopened this Jul 8, 2014
@yanglw
Copy link
Author

yanglw commented Jul 9, 2014

@rengwuxian 目前已经实现了该功能。在这里进行总结一下。

  1. 若要在free和pro中使用不同的类(本例中的Utils类,下同),那么main中不能出现这个类,否则会出现类重复定义的错误。
  2. 因为只在free和pro中存在要使用的类,所以build的时候,只能使用free和pro这两个buildTypes。如果编译了默认的debug和release,会因为不存在该类,而编译失败。
  3. Build Variant中需要选择free或者pro(本例中有4个选择:baiduFree,wandoujiaFree,baiduPro,wandoujiaPro),否则代码中会提示错误(类不存在或者无法导入类)。该错误会导致IDE提示错误以及无法运行程序(不是build apk)。这个原因是:运行程序时,会选择一个buildTypes和一个productFlavors进行组合build。默认的buildTypes为debug。
  4. 如果要实现本功能,gradle不能直接使用build命令,而是进行assemblePro和assembleFree命令(原因见2)。

谢谢各位的回答。 @rengwuxian @promeG

@dev4mobile
Copy link

不错,学习了,之前还真搞懂这些。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants