[Android] Change the app name and app icon for each Flavor

When developing an Android application, you may set Flavor for each environment such as development environment, staging environment, and production environment. Here's a summary of how to change the app name and app icon so that you can tell at a glance which apps have different environments.

Set Flavor

Set Flavor in build.gradle of the app. This time, as an example, we will prepare three environments: dev (development), stg (staging), and prod (production).

build.gradle


android {

    //abridgement

    //Add from here
    flavorDimensions "environment"
    productFlavors {
        dev {
            dimension "environment"
            applicationIdSuffix ".dev"
            versionNameSuffix ".dev"
        }
        stg {
            dimension "environment"
            applicationIdSuffix ".stg"
            versionNameSuffix ".stg"
        }
        prod {
            dimension "environment"
        }
    }
    //Add up to here
}

Change the app name for each Flavor

Create a strings.xml file for each Flavor. Right-click res / values-> New-> Values Resource File as shown below. スクリーンショット 2020-10-30 1.54.18.png

Then set File name to strings and select dev for Source set. This dev was set in Flavor earlier. スクリーンショット 2020-10-30 1.55.45.png

This will create a strings.xml for the dev environment. スクリーンショット 2020-10-30 1.56.40.png

You can change the app name by setting the app name for each environment in ʻapp_name of this strings.xml`. スクリーンショット 2020-10-30 2.02.45.png

Similarly, create strings.xml for stg and set the app name. (You must set Build Variants to stgDebug to see strings.xml (stg)) The prod environment wants to use the default strings.xml, so we don't create a separate strings.xml.

As shown below, it is OK if the application name is changed for each environment. スクリーンショット 2020-10-30 2.11.49.png

Change the app icon for each Flavor

This time, I used a library called easylauncher-gradle-plugin.

First, add the settings to the project's build.gradle.

build.gradle


buildscript {
    
    //abridgement

    dependencies {

        //abridgement

        classpath 'com.akaita.android:easylauncher:1.3.1'  // <-Add here
    }
}

Then add the settings to your app's build.gradle.

build.gradle


plugins {

    //abridgement

    id 'com.akaita.android.easylauncher'  // <-Add here
}

android {

    //abridgement

    //Flavor settings added earlier
    flavorDimensions "environment"
    productFlavors {
        dev {
            dimension "environment"
            applicationIdSuffix ".dev"
            versionNameSuffix ".dev"
        }
        stg {
            dimension "environment"
            applicationIdSuffix ".stg"
            versionNameSuffix ".stg"
        }
        prod {
            dimension "environment"
        }
    }
}

//Add from here
easylauncher {
    iconNames "@mipmap/ic_launcher_foreground"
    foregroundIconNames "@mipmap/ic_launcher_foreground"

    defaultFlavorNaming = true

    buildTypes {
        debug {}
        release {}
    }

    productFlavors {
        dev {
            filters = grayRibbonFilter()
        }
        stg {
            filters = greenRibbonFilter()
        }
        prod {
            enable false  //Do not label in production
        }
    }
}
//Add up to here

//abridgement

With the settings up to this point, the icon will change for each app. This time, we have also set an appropriate icon. スクリーンショット 2020-10-30 3.01.49.png

It's very easy to understand! Please refer to Documentation for other settings of the library.

Recommended Posts

[Android] Change the app name and app icon for each Flavor
Change the default timezone for the rails app
Change the injection target for each environment with Spring Boot 2
default value for android: exported
Default values for MaxHeapSize and InitialHeapSize
Change the default timezone for the rails app
Change the file name and output destination of the JavaVM error file
Beginner-friendly Android app publishing procedure, struggling for the first release⁈
Technical causes and countermeasures for the points I was addicted to with the first Android app & Kotlin
[Rails] How to change the page title of the browser for each page
[Android Studio] [For beginners] Let's roughly explain the screen and directories
Introduction to Android App Development 1 Installing JDK and Android Studio for mac
[Android] Cause and remedy for TransactionTooLargeException
Android Studio shows only one method for button onClick and Java throws an exception and ruins the app
Best practice to change settings for each environment in iOS app (Swift)
The story of releasing the Android app to the Play Store for the first time.
The Android app crashes. Just click a button and it will fall.
[Android 9.0 Pie Java] RecyclerView Press and hold to display a context menu whose contents change for each element.
[Java] for Each and sorted in Lambda
Output the sum of each name and its contents from a multiple array
Get YouTube video information with Retrofit and keep it in the Android app.
[Rails] Get the path name of the URL before the transition and change the link destination
Change the port name and execution authority of the microcomputer connected to Ubuntu and LiDAR
If you are using Android Room and want to change the column definition
Resize or hide Button and EditText placed on the screen for each page
I changed the column name, but it said Undefined method for the column before the change.