简体   繁体   中英

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.

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.

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

Now in total I have 4 product types

  • flavor1debug
  • flavor1release
  • flavor2debug
  • flavor2release

Now I need to create a variable app_name so that the app name can be different for different product types.

  • flavor1debug ---------> app_name : flavor1 (debug)
  • flavor1release ---------> app_name : flavor1
  • flavor2debug ---------> app_name : flavor2 (debug)
  • flavor2release ---------> app_name : flavor2

How do I achieve the same using configuration or 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?

ie I need 4 different keys based on following configuration

  1. Flavor1 + debug ========= Key1
  2. Flavor1 + release ========= Key2
  3. Flavor2 + debug =========== Key3
  4. Flavor2 + release ========== 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"
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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