简体   繁体   English

安卓摇篮。 如何将 Flavors 与 buildTypes 结合起来

[英]Android Gradle. How to combine Flavors with buildTypes

I'm working on a white brand app.我正在开发一个白色品牌的应用程序。

We create a different flavor per client and each client has Debug and Production APIs, so I'm trying to set them up in the Gradle.我们为每个客户端创建了不同的风格,每个客户端都有DebugProduction API,所以我试图在 Gradle 中设置它们。

How should I do that?我该怎么做?

Here is what I've tried:这是我尝试过的:

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/\"")
    }
}

EDIT: I would like to be able to define a different BASE_URL per each Flavor and Buildtype.编辑:我希望能够为每个 Flavor 和 Buildtype 定义不同的BASE_URL

Flavor company1, BuildType debug风味公司1,BuildType调试

https://app.company1.com/devApi/

Flavor company1, BuildType release Flavor company1, BuildType 发布

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

Flavor company2, BuildType debug风味公司2,BuildType调试

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

Flavor company2, BuildType release Flavor company2, BuildType 发布

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

For my specific problem, where the URLs differ a lot one to another, I couldn't make it work with Flavours and BuildTypes.对于我的具体问题,URL 之间存在很大差异,我无法使其与 Flavors 和 BuildTypes 一起使用。

I was able to define the debug/production URLs by using specific strings.xml for each build variant (which is each combination of flavour and build type):我能够通过为每个构建变体(这是风格和构建类型的每种组合)使用特定的strings.xml来定义调试/生产 URL:

These are the folder structures to do so:这些是这样做的文件夹结构:

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

and

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

EXTRA: This can also be used to host different google-services.json files额外:这也可用于托管不同的google-services.json文件

Try something like this:尝试这样的事情:

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/\"")
    }
}

And use it like:并像这样使用它:

String BASE_URL = BuildConfig.BASE_URL_DOMAIN + BuildConfig.BASE_URL_PATH字符串 BASE_URL = BuildConfig.BASE_URL_DOMAIN + BuildConfig.BASE_URL_PATH

Here is the working example这是工作示例

// Specifies one flavor dimension. // 指定一种风味维度。

flavorDimensions "version"

productFlavors {
dev {
    dimension "version"
    applicationId "com.example.lk"
    versionCode 1
    versionName "1.0.1"
    buildConfigField("String", "BASE_URL_PATH", "\"https://app.dev.com/api/v1/\"")
}

prod {
    dimension "version"
    applicationId "com.example.in"
    versionCode 1
    versionName "1.0.1"
    buildConfigField("String", "BASE_URL_PATH", "\"https://app.prod.com/api/v1/\"")

}
staging {
    dimension "version"
    applicationId "com.example.other"
    versionCode 1
    versionName "1.0.1"
    buildConfigField("String", "BASE_URL_PATH", "\"https://app.staging.com/api/v1/\"")

}}




buildTypes {
    release {
      minifyEnabled true
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }

you can access it like this你可以像这样访问它

String baseURL =BuildConfig.BASE_URL_PATH;

You can use flavors to add basic configurations for your application, that ranges from app url , API keys , master password etc.您可以使用风格为您的应用程序添加基本配置,包括app urlAPI keysmaster password等。

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"
        }
    }

Now if you check at your build varients you will get something like this:现在,如果您检查build varients您将得到如下信息:

在此处输入图片说明

Your main problem is that you do not correctly place your buildTypes for flavors and no correct params inside each company and also I suggest you read more about Gradle setups( developer.android ).你的主要问题是你没有正确地将你的 buildTypes 放置在每个公司内部,并且没有正确的参数,我建议你阅读更多关于 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")


    }

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

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