如何为不同的 gradle buildType 提供不同的 Android 应用程序图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22875948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:31:16  来源:igfitidea点击:

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

androidgradle

提问by InsanityOnABun

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

我的 gradle 文件中设置了两种构建类型:debugrelease. 我希望能够为debug构建类型设置不同的应用程序图标。有没有办法仅通过构建类型来实现这一点,而无需进入产品口味?build.gradle 文件在下面。

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'])
}

回答by InsanityOnABun

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

弄清楚了。您需要做的是创建一个单独的 src 文件夹,称为debug保存不同的图标。例如,如果您的项目布局如下,并且您的启动器图标名为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.

然后,当您在 debug 构建类型下构建时,它将使用在 debug 文件夹中找到的 ic_launcher。

回答by qinmiao

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

这是一种方便的方法,尽管它有一个重要的缺点……两个启动器都将放入您的 apk。巴特克·利平斯基

The better way: InsanityOnABun's answer

更好的方法:InsanityOnABun 的回答

AndroidManifest.xml

AndroidManifest.xml

<manifest 

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

    ...

    </application>

</manifest>

build.gradle

构建.gradle

android {

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

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

the Github url:Build multi-version App with Gradle

Github 网址:使用 Gradle 构建多版本应用

回答by isyi

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

您也可以在产品风味的部分 AndroidManifest.xml 文件中指定图标:

<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

这将覆盖您在原始 AndroidManifest.xml 中指定的图标

回答by Jose Gómez

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 workwhen multiple dimensions are in use:

只是为了清楚起见,当使用多个维度时,以下内容将不起作用

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

回答by Robert Pal

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

分步解决方案,包括替换 mipmap-anydpi-v26 并保留所有维度的文件:

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

首先在 build.gradle (Module: app) 中定义你的构建类型 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

在项目层次结构上,在 Android 下方,右键单击应用程序 -> 新建 -> 图像资产 -> 在路径中选择您的图标 -> 背景层和传统上的任何其他更改 -> 下一步 -> 在 Res 目录中选择您想要的构建类型(调试、内部、主要等)-> 完成

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

这样,图标将替换您拥有的每个旧图标。