简体   繁体   English

相同类别的两种不同的产品风味

[英]This same class for two different product flavor

I have 2 product flavors: 我有2种产品口味:

productFlavors {
    first{}
    second{}
}

and I have a class Http with constant value like on screen: 而且我在屏幕上有一个具有常量值的Http类:

在此处输入图片说明

My problem is: 我的问题是:

If I have set build first , I want Http class with CODE = 2000 如果我已构建first ,我想Http带班CODE = 2000
but if I have set build second I want Http class with CODE = 1000 但是如果我设置了构建second我想要CODE = 1000 Http

I have seen such a project which looked like : repo(first) and after change build repo(second) and class http was different depending on the current build, but can't replicate it :/ 我看过这样的项目,看起来像:repo(first),更改后的版本repo(second)和类http根据当前版本而有所不同,但是无法复制它:/

If your classes are the same but the only difference is the constant, then you do not need to duplicate the code and all you need to do is create gradle constants that you access via the BuildConfig class: 如果您的类相同,但唯一的区别是常量,那么您无需复制代码,只需创建可通过BuildConfig类访问的gradle常量即可:

productFlavors {
    flavor1 {
        buildConfigField 'int', 'CODE', '1000'
    }

    flavor2 {
        buildConfigField 'int', 'CODE', '2000'
    }
}

Then you would use 那你会用

BuildConfig.CODE

in your HTTP calls, that would hold 1000 or 2000 depending on the flavor. 在您的HTTP调用中,根据类型的不同,可以容纳1000或2000。

Create two copies of your Http class, one in src/first/com.androidapp.testproject/repo and the other one in src/second/com.androidapp.testproject/repo , and remove the copy from src/main/... . 创建Http类的两个副本,一个在src/first/com.androidapp.testproject/repo ,另一个在src/second/com.androidapp.testproject/repo ,然后从src/main/...删除该副本src/main/...

The first version of your class in src/first/... will get built for your flavor first , and the one in src/second/... will get built for your flavor second . 你在课堂上的第一个版本src/first/...将获得专为您的口味first ,和一个在src/second/...将获得专为您的口味second Of course, you have to adjust the value for CODE in the appropriate copy of your classes. 当然,您必须在相应类的副本中调整CODE的值。

See the following answer for more information https://stackoverflow.com/a/16746755/3286819 有关更多信息,请参见以下答案https://stackoverflow.com/a/16746755/3286819

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM