Android Gradle. 如何将Flavors与buildTypes结合使用

9

我正在开发一个白标应用。

我们为每个客户创建不同的风格,每个客户都有DebugProduction API,因此我正在尝试在Gradle中设置它们。

我该怎么做?

以下是我尝试过的:

buildTypes {
    debug {
        // some configurations
    }
    release {
        // some configurations
    }
}

flavorDimensions "client"
productFlavors {
    company1{
        dimension "client"
        buildConfigField("String", "BASE_URL", "\"https://app.company1/devApi/\"")
    }

    company2 {
        dimension "client"
        buildConfigField("String", "BASE_URL", "\"https://app.company2/devApi/\"")
    }
}

我希望您能为我翻译以下内容:编辑: 我希望能够为每个Flavor和Buildtype定义不同的BASE_URL
Flavor company1,BuildType debug
https://app.company1.com/devApi/

Flavor公司1,BuildType发布版

https://app.company1.com/prodApi/

Flavor公司2,BuildType为debug

https://dev.company2.com/api/

口味公司2,构建类型为发布版本

https://prod.company2.com/api/

你的具体要求是什么?你想要表达什么意思? - Umair
检查一下我的答案 @kike,你可以根据需要进行修改。 - Tufan
嗨,@kike,你现在有最好的解决方案了吗?我今天遇到了同样的问题。不幸的是,我只用Gradle无法得到令人满意的解决方案。最终我不得不在每个flavour中定义两个变量,一个用于debug,另一个用于release。然后在代码中,我检查buildType以获取正确的值。有任何更新吗? - Freddie
@Freddie 如果这些变量非常不同(例如,您无法组合字符串),则必须将它们设置在单独的strings.xml中。请查看我的下面的答案,如果您有更多问题,请告诉我。 - kike
https://dev59.com/GLroa4cB1Zd3GeqPdx2K - Jemshit Iskenderov
4个回答

5

对于我的具体问题,因为每个URL差异很大,我无法使用Flavours和BuildTypes来解决。

通过为每个构建变量(即每个风味和构建类型的组合)使用特定的strings.xml,我能够定义调试/生产URL:

以下是实现此目的的文件夹结构:

src/flavour1Debug/res/values/strings.xml 
src/flavour1Release/res/values/strings.xml 

并且

src/flavour2Debug/res/values/strings.xml 
src/flavour2Release/res/values/strings.xml 

额外提示:

这也可以用来托管不同的google-services.json文件。


3

尝试类似于这样的代码:

buildTypes {
    debug {
        buildConfigField("String", "BASE_URL_PATH", "\"devApi/\"")
    }
    release {
        buildConfigField("String", "BASE_URL_PATH", "\"prodApi/\"")
    }
}

flavorDimensions "client"
productFlavors {
    company1{
        dimension "client"
        buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company1/\"")
    }

    company2 {
        dimension "client"
        buildConfigField("String", "BASE_URL_DOMAIN", "\"https://app.company2/\"")
    }
}

使用方式如下:

String BASE_URL = BuildConfig.BASE_URL_DOMAIN + BuildConfig.BASE_URL_PATH


1
谢谢你的回答。对于某些情况它是可行的,但在我的情况下,我需要整个BASE_URL。该URL不取决于我们,可能是company1.com/apiDebugdebug.company2.com/api - kike
我的想法是将整个URL分成几个部分,并将它们放入Flavours和BuildTypes中。即使有两种以上的组合,这也可以工作。例如:Company1-Mobile-Debug,Company2-Tablet-Prod等。如果你认为这里一切都不确定,那么最好在你的Java中使用字符串常量,并使用Build Type和Product Flavours以及一些Switch语句来在运行时构建实际的URL。 - Housefly

0

您可以使用Flavors为您的应用程序添加基本配置,包括应用URLAPI密钥主密码等。

flavorDimensions "Mobile"
     productFlavors {
        Production {
            dimension "Mobile"   // dimension can be mobile, kiosks, tv, miniKiosks etc

            resValue "string", "API_KEY", "Just to give the idea"
            resValue "string", "SERVICE_IP", "Your service IP"
            resValue "string", "SERVICE_BASE_URL", ""
            resValue "string", "APK_BASE_URL", "base url"
            resValue "string", "MASTER_PASSWORD", ""

        }
        Demo {
            dimension "Mobile"

            resValue "string", "API_KEY", "Just to give the idea"
            resValue "string", "SERVICE_IP", "Your service IP"
            resValue "string", "SERVICE_BASE_URL", "services/v1/"
            resValue "string", "APK_BASE_URL", "base url"
            resValue "string", "MASTER_PASSWORD", ""
        }

    Local {
            dimension "Mobile"

            resValue "string", "API_KEY", ""
//            resValue "string", "app_name", ""
            resValue "string", "SERVICE_IP", ""
//            resValue "string", "SERVICE_IP", ""
            resValue "string", "SERVICE_BASE_URL", ""
            resValue "string", "APK_BASE_URL", ""
            resValue "string", "MASTER_PASSWORD", "a"
        }
    }

现在如果你检查你的构建变量(build variants),你会得到类似这样的结果:

enter image description here


0
你的主要问题是没有正确地为不同的公司设置构建类型和参数,我建议你阅读更多关于Gradle设置的资料(developer.android)。
    buildTypes {
        debug {
            // some configurations
        }
        release {
            // some configurations
        }
    }

    flavorDimensions "version", "brand"
    productFlavors {
        dev {
            versionName += "dev"
            dimension "version"

            buildConfigField "String", "BASE_API_URL", "\...\""
        }

        prod {
            dimension "version"

            buildConfigField "String", "BASE_API_URL", "\...\""
        }
        company1{
            dimension "brand"
            versionName "1.0.0"
            buildConfigField("int", "CLONE_ID", "1")
            **here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "true")
        }

        company2 {
            dimension "brand" 
            versionName "1.0.0"
            buildConfigField("int", "CLONE_ID", "2")
            **here you can set some params, for current clone id: examlpe ->** buildConfigField("boolean", "SHOW_CREDIT_BUY_IN_PROFILE", "false")


    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接