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

后端 未结 5 1066
隐瞒了意图╮
隐瞒了意图╮ 2020-11-29 19:12

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

相关标签:
5条回答
  • 2020-11-29 19:42

    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

    0 讨论(0)
  • 2020-11-29 19:47

    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

    0 讨论(0)
  • 2020-11-29 19:54

    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'
    }
    
    0 讨论(0)
  • 2020-11-29 19:57

    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.

    0 讨论(0)
  • 2020-11-29 20:03

    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.

    0 讨论(0)
提交回复
热议问题