简体   繁体   English

如何根据构建类型和构建风格管理配置

[英]How to manage configuration based on build type and build flavor

In my app I have 2 build types release and debug , below is the code snippet.在我的应用程序中,我有 2 种构建类型releasedebug ,下面是代码片段。

buildTypes {
    getByName("debug") {
        applicationIdSuffix = ".debug"
        versionNameSuffix = ".debug"
        buildConfigField("boolean", "DEBUG_MODE", "true")
    }

    getByName("release") {
        proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
        buildConfigField("boolean", "DEBUG_MODE", "false")
    }
}

and I also have 2 product flavors below is the code snippet.我还有 2 种产品口味,下面是代码片段。

flavorDimensions("version")
productFlavors {
    create("flavor1") {
        setDimension("version")
    }
    create("flavor2") {
        setDimension("version")
        versionCode = 1
        versionName = "1.0"
    }
}

Now in total I have 4 product types现在我总共有 4 种产品类型

  • flavor1debug风味1调试
  • flavor1release风味1释放
  • flavor2debug风味2调试
  • flavor2release风味2释放

Now I need to create a variable app_name so that the app name can be different for different product types.现在我需要创建一个变量 app_name 以便不同产品类型的应用程序名称可以不同。

  • flavor1debug ---------> app_name : flavor1 (debug)风味1调试---------> app_name : 风味1(调试)
  • flavor1release ---------> app_name : flavor1风味1发布---------> app_name : 风味1
  • flavor2debug ---------> app_name : flavor2 (debug)风味2调试---------> app_name : 风味2(调试)
  • flavor2release ---------> app_name : flavor2风味2发布---------> app_name : 风味2

How do I achieve the same using configuration or strings.xml我如何使用配置或 strings.xml 实现相同的目标

Edit: Let's assume, I am using a third party library and I want to use different authentication key, how can I achieve this using Flavor & buildType combination?编辑:让我们假设,我正在使用第三方库并且我想使用不同的身份验证密钥,我如何使用FlavorbuildType组合来实现这一点?

ie I need 4 different keys based on following configuration即我需要基于以下配置的 4 个不同的键

  1. Flavor1 + debug ========= Key1 Flavor1 + 调试 ========== Key1
  2. Flavor1 + release ========= Key2风味 1 + 发布 ========== Key2
  3. Flavor2 + debug =========== Key3 Flavor2 + 调试 ============ Key3
  4. Flavor2 + release ========== Key4 Flavor2 + 发布 ========== Key4

any help would be appreciated.任何帮助,将不胜感激。

在每个源集中声明strings.xml,然后你可以为不同的构建类型和风格设置不同的值检查这个

Try this,


productFlavors {
   flavorA {
        setDimension("version")
        manifestPlaceholders = [appLabel : "FlavourA"]

    }
   flavorB {
        setDimension("version")
        versionCode = 1
        versionName = "1.0"
        manifestPlaceholders = [appLabel : "FlavourB"]

    }
}


In android manifest file set this appLabel.

 <application
        android:name=".ApplicationClass"
        android:icon="@mipmap/logo_launcher"
        android:label="${appLabel}"    // Add this line.
        android:largeHeap="true"
        android:theme="@style/AppTheme"
/>

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

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