How to provide different Android app icons for different gradle buildTypes?

AndroidGradle

Android Problem Overview


I have two build types set in my gradle file: debug and release. I'd like to be able to set a different app icon for the debug build type. Is there any way to this just through the build type, without getting into product flavors? build.gradle file is below.

apply plugin: 'android'

//...

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 30
        versionName "2.0"
    }
    buildTypes {
        debug {
            packageNameSuffix '.debug'
            versionNameSuffix '-SNAPSHOT'
        }
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Android Solutions


Solution 1 - Android

Figured it out. What you need to do is create a separate src folder called debug that holds the different icons. For example, if your project layout is as follows, and your launcher icon is called ic_launcher.png:

[Project Root]
  -[Module]
    -src
      -main
        -res
          -drawable-*
            -ic_launcher.png

Then to add a separate icon for the debug build type, you add:

[Project Root]
  -[Module]
    -src
      -main
        -res
          -drawable-*
            -ic_launcher.png
      -debug
        -res
          -drawable-*
            -ic_launcher.png

Then, when you build under the debug build type, it will use the ic_launcher found in the debug folder.

Solution 2 - Android

> This is a handy approach although it has an important downside... both launchers will be put into your apk.Bartek Lipinski

The better way: InsanityOnABun's answer

AndroidManifest.xml

<manifest 

    ...
        <application
        android:allowBackup="true"
        android:icon="${appIcon}"
        android:roundIcon="${appIconRound}"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

    ...

    </application>

</manifest>

build.gradle

android {

    ...
        productFlavors{
        Test{
            versionName "$defaultConfig.versionName" + ".test"
            resValue "string", "app_name", "App-Test"
            manifestPlaceholders = [
                    appIcon: "@mipmap/ic_launcher_test",
                    appIconRound: "@mipmap/ic_launcher_test_round"
            ]
        }

        Product{
            resValue "string", "app_name", "App"
            manifestPlaceholders = [
                    appIcon: "@mipmap/ic_launcher",
                    appIconRound: "@mipmap/ic_launcher_round"
            ]
        }
    }
}

the Github url:Build multi-version App with Gradle

Solution 3 - Android

You can specify the icon in the product flavor's partial AndroidManifest.xml file as well:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">
    <application
        tools:replace="android:icon"
        android:icon="@drawable/alternative_icon" />
</manifest>

This will overwrite the icon that you specify in the original AndroidManifest.xml

Solution 4 - Android

For getting different icons while using different flavors with multiple dimensions, such as:

flavorDimensions "color", "size"
productFlavors {
    black {
        dimension "color"
    }
    white {
        dimension "color"
    }

    big {
        dimension "size"
    }
    small {
        dimension "size"
    }
}

This can be achieved as:

First, put the debug resources in separate folders, such as:

src/blackDebug/res
src/whiteDebug/res

Second, put the key with multiple flavor dimensions is that the sourceset name must contain all the possible flavor combinations, even if some of these dimensions do not affect the icon.

sourceSets {
    // Override the icons in debug mode
    blackBigDebug.res.srcDir 'src/blackDebug/res'
    blackSmallDebug.res.srcDir 'src/blackDebug/res'
    whiteBigDebug.res.srcDir 'src/whiteDebug/res'
    whiteSamllDebug.res.srcDir 'src/whiteDebug/res'
}

Just to make it clear, the following will not work when multiple dimensions are in use:

sourceSets {
    // Override the icons in debug mode
    blackDebug.res.srcDir 'src/blackDebug/res'
    whiteDebug.res.srcDir 'src/whiteDebug/res'
}

Solution 5 - Android

Step by step solution, including replacing mipmap-anydpi-v26 and keeping files for all dimensions:

First define in build.gradle (Module: app) your build type in android -> buildTypes -> debug, internal, etc

On the project hierarchy, below Android, right click on app -> New -> Image Asset -> in Path choose your icon -> any other changes on Background Layer and Legacy -> Next -> in Res Directory choose your desired build type (debug, internal, main, etc) -> Finish

That way the icons will replace every old icon you had.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionInsanityOnABunView Question on Stackoverflow
Solution 1 - AndroidInsanityOnABunView Answer on Stackoverflow
Solution 2 - AndroidqinmiaoView Answer on Stackoverflow
Solution 3 - AndroidisyiView Answer on Stackoverflow
Solution 4 - AndroidJose GómezView Answer on Stackoverflow
Solution 5 - AndroidRobert PalView Answer on Stackoverflow