安卓 Oreo - 我该如何在 Cordova 中设置自适应图标?

29

请问是否有人能够在 Cordova 中设置 Android Oreo 的自适应图标?我正在使用 android 6.4.0,我的方形图标会缩小以适应圆形图标,但我希望它不要缩小。我不介意边角被裁剪成圆形。


@CharliePrynn,我的回答有帮助吗? - Darshan Pania
感谢您的帮助,@CharliePrynn。我将在我们应用程序的下一个版本中进行测试。 - jkdude
我已经尝试了建议的答案,但我的图标仍然显示在白色框内,而不是填充整个图标。我的背景是从Android Studio中的drawable-v24生成的。有什么想法吗? - Smartie
9个回答

37

警告:不要使用这个答案。从 Cordova 9 开始,这已经内置支持。请参见 https://dev59.com/pVYN5IYBdhLWcg3wXnT7#55169307

我按照https://developer.android.com/studio/write/image-asset-studio.html#create-adaptive中的说明创建了图标,将它们复制到res/android中,并使用以下配置:

config.xml:

<widget ... xmlns:android="http://schemas.android.com/apk/res/android">
    <platform name="android">
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" />
        </edit-config>
        <resource-file src="res/android/drawable/ic_launcher_background.xml" target="app/src/main/res/drawable/ic_launcher_background.xml" />
        <resource-file src="res/android/drawable/ic_launcher_foreground.xml" target="app/src/main/res/drawable/ic_launcher_foreground.xml" />
        <resource-file src="res/android/mipmap-anydpi-v26/ic_launcher.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" />
        <resource-file src="res/android/mipmap-anydpi-v26/ic_launcher_round.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml" />
        <resource-file src="res/android/mipmap-hdpi/ic_launcher.png" target="app/src/main/res/mipmap-hdpi/ic_launcher.png" />
        <resource-file src="res/android/mipmap-hdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_round.png" />
        <resource-file src="res/android/mipmap-mdpi/ic_launcher.png" target="app/src/main/res/mipmap-mdpi/ic_launcher.png" />
        <resource-file src="res/android/mipmap-mdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_round.png" />
        <resource-file src="res/android/mipmap-xhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher.png" />
        <resource-file src="res/android/mipmap-xhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_round.png" />
        <resource-file src="res/android/mipmap-xxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher.png" />
        <resource-file src="res/android/mipmap-xxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png" />
        <resource-file src="res/android/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher.png" />
        <resource-file src="res/android/mipmap-xxxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png" />
    </platform>    
</widget>

我使用Android Studio创建了资源,将其复制到Phonegap项目根目录下的res/android文件夹中,并在config.xml中添加了您提供的xml。但是我的图标仍然显示为一个小方块图标,位于白色背景圆圈内,就像之前一样。我正在使用Phonegap 7.1.1版本。您有什么想法,我可能做错了什么吗? - Caleb Pitman
3
为了使这个工作正常,我不得不从目标中删除 app/src/main - Jespertheend
5
为使其正常工作,我必须在config.xml的widget标签中添加“xmlns:android="http://schemas.android.com/apk/res/android"”。 - Augustin Bralley
4
如果您选择背景颜色而不是图像,第一个资源文件将是 <resource-file src="res/android/values/ic_launcher_background.xml" target="app/src/main/res/values/ic_launcher_background.xml" />(values 而不是 drawable)。感谢您发布这个解决方案,真的帮了我很大的忙! - MStrutt
1
我正在使用我的遗留应用程序图标.png作为我的“前景”资产图像,并使用“color:white”作为我的背景。但是,当Image Asset开始生成所有内容时,它不会生成一个“icon_launcher_foreground.xml”文件 - 没有这个文件,我的构建将失败,因为“ic_launcher.xml”和“ic_launcher_round.xml”都引用了foregound.xml文件。它确实生成了所有的“ic_launcher_foreground.png”图像。如果我删除foreground.xml引用,我的应用程序将构建,但图标只是白色背景。我开始了一个新项目,得到了相同的结果,我错过了什么关键步骤? - rolinger
显示剩余9条评论

26
警告:不要使用这个答案。截至Cordova 9,已经支持此功能。请参见:https://dev59.com/pVYN5IYBdhLWcg3wXnT7#55169307

虽然以上答案帮助我得出答案,但它们要么过时,要么不完整。因此,为了帮助任何向前移动的人,这是一个完整的答案,包括我能想到的所有细节。

第一步:创建图标

您需要使用图像资源管理器(https://developer.android.com/studio/write/image-asset-studio)进行此操作。有许多指南可以提供帮助。

第二步:将图标移动到您的ionic / cordova项目中

将整个res文件夹复制到您的项目中。以下示例适用于ionic v1。

cp -a AndroidStudioProjects/MyApplication4/app/src/main/res MyIonicProject/myapp/resources/android/adaptiveicon

步骤3:编辑config.xml

首先,要使用图标(其他答案中缺少此项),您需要修改顶部的widget行。您需要添加xmlns:android="schemas.android.com/apk/res/android",以便它看起来像下面这样。这将允许系统更改AndroidMenifest.xml文件。

<widget id="io.ionic.starter" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:android="schemas.android.com/apk/res/android" xmlns:cdv="http://cordova.apache.org/ns/1.0">

接下来,您需要调整config.xml的平台部分。

首先从<platform name="android">部分中删除任何<icon density= ... />的实例。

然后,在Android Manifest文件中添加修改:

<edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
    <application android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" />
</edit-config>

最后,在新的 resources/android/adaptiveicon 文件夹中的每个文件,您都需要添加像这样的一行:

<resource-file src="resources/android/adaptiveicon/<folder>/<file>" target="app/src/main/res/<folder>/<file>" />

确保每个文件都被表示!您的最终platform部分可能会像这样(此示例适用于前景和背景均使用PNG格式的情况):

<platform name="android">
    <allow-intent href="market:*" />
    <splash density="land-ldpi" src="resources/android/splash/drawable-land-ldpi-screen.png" />
    <splash density="land-mdpi" src="resources/android/splash/drawable-land-mdpi-screen.png" />
    <splash density="land-hdpi" src="resources/android/splash/drawable-land-hdpi-screen.png" />
    <splash density="land-xhdpi" src="resources/android/splash/drawable-land-xhdpi-screen.png" />
    <splash density="land-xxhdpi" src="resources/android/splash/drawable-land-xxhdpi-screen.png" />
    <splash density="land-xxxhdpi" src="resources/android/splash/drawable-land-xxxhdpi-screen.png" />
    <splash density="port-ldpi" src="resources/android/splash/drawable-port-ldpi-screen.png" />
    <splash density="port-mdpi" src="resources/android/splash/drawable-port-mdpi-screen.png" />
    <splash density="port-hdpi" src="resources/android/splash/drawable-port-hdpi-screen.png" />
    <splash density="port-xhdpi" src="resources/android/splash/drawable-port-xhdpi-screen.png" />
    <splash density="port-xxhdpi" src="resources/android/splash/drawable-port-xxhdpi-screen.png" />
    <splash density="port-xxxhdpi" src="resources/android/splash/drawable-port-xxxhdpi-screen.png" />
    <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
        <application android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" />
    </edit-config>
    <resource-file src="resources/android/adaptiveicon/drawable/ic_launcher_background.xml" target="app/src/main/res/drawable/ic_launcher_background.xml" />
    <resource-file src="resources/android/adaptiveicon/drawable-v24/ic_launcher_foreground.xml" target="app/src/main/res/drawable-v24/ic_launcher_foreground.xml" />
    <resource-file src="resources/android/adaptiveicon/mipmap-anydpi-v26/ic_launcher.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" />
    <resource-file src="resources/android/adaptiveicon/mipmap-anydpi-v26/ic_launcher_round.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml" />
    <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher.png" target="app/src/main/res/mipmap-hdpi/ic_launcher.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_background.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher.png" target="app/src/main/res/mipmap-mdpi/ic_launcher.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_background.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_background.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_background.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_background.png" />
    <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png" />
</platform>

第四步:保险起见,清理Android平台

运行以下命令以清理平台。

cd myapp
rm -rf platforms/android
ionic cordova prepare

为了更好的效果,在Ionic中修复Android模拟器启动时出现的错误:

wget https://raw.githubusercontent.com/gegenokitaro/cordova-android/8d497784ac4a40a9689e616cd486c4ed07d3e063/bin/templates/cordova/lib/emulator.js -O platforms/android/cordova/lib/emulator.js

第五步:构建!

构建:

ionic cordova build android

模拟或仿真:

ionic cordova emulate android --consolelogs --serverlogs --target "Android8"

你确定第三步仍然是最新的吗?在创建和测试图标后,我在我的清单中找不到以下内容:android:roundIcon="@mipmap/ic_launcher_round"。所以我相信它也可以没有这个。 - pascalzon

14

现在 Cordova Android 8.0.0 支持此功能。请参阅 公告文档

例如,在您的 config.xml 中定义图标,如下所示:

<platform name="android">
        <resource-file src="res/icons/android/colors.xml" target="/app/src/main/res/values/colors.xml" />
        <icon density="ldpi" background="@color/background" foreground="res/icons/android/ldpi-foreground.png" />
        <icon density="mdpi" background="@color/background" foreground="res/icons/android/mdpi-foreground.png" />
        <icon density="hdpi" background="@color/background" foreground="res/icons/android/hdpi-foreground.png" />
        <icon density="xhdpi" background="@color/background" foreground="res/icons/android/xhdpi-foreground.png" />
        <icon density="xxhdpi" background="@color/background" foreground="res/icons/android/xxhdpi-foreground.png" />
        <icon density="xxxhdpi" background="@color/background" foreground="res/icons/android/xxxhdpi-foreground.png" />
    </platform>

颜色文件 colors.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="background">#FFFFFF</color>
</resources>

感谢您的更新和快速示例! - Dmitri R117

13

警告:不要使用此答案。自 Cordova 9 版本起,这已经成为内置功能。请参见https://dev59.com/pVYN5IYBdhLWcg3wXnT7#55169307

最近的 Android 使用了适应性图标,其中包括背景和前景图像以及一些 XML 文件。以下是我在 Ionic 应用程序中设置适应性图标的步骤:

  1. config.xml 中,将 android-minSdkVersion 设置为版本 26。
<preference name="android-minSdkVersion" value="26" />
<preference name="android-targetSdkVersion" value="28" />
  1. config.xml 中,删除 icon density 标签,并移除这些行:
        <icon density="ldpi" src="resources/android/icon/drawable-ldpi-icon.png" />
        <icon density="mdpi" src="resources/android/icon/drawable-mdpi-icon.png" />
        <icon density="hdpi" src="resources/android/icon/drawable-hdpi-icon.png" />
        <icon density="xhdpi" src="resources/android/icon/drawable-xhdpi-icon.png" />
        <icon density="xxhdpi" src="resources/android/icon/drawable-xxhdpi-icon.png" />
        <icon density="xxxhdpi" src="resources/android/icon/drawable-xxxhdpi-icon.png" />
  1. 接下来,我需要创建 Android 自适应图标。我使用 Android Studio 中的 Image Assets 工具。首先,我从 Photoshop 中创建了两个文件:背景图和仅包含透明图标的前景图(均为 png 格式)。之后,我按照以下步骤生成了图标:
  • 打开 Android Studio,新建一个项目或打开现有项目。

  • 在左侧边栏中点击 app -> res。右键单击 res -> New -> Image Assets。

enter image description here

  • 选择 Foreground Layer -> Asset Type 为“Image”,并选择包含透明背景的 logo 图片路径。然后选择 Trim 为 Yes 并根据需要调整大小。

enter image description here

  • 选择 Background Layer -> Asset Type 为“Image”,并选择路径。(另外,也可以设置为“Color”)

enter image description here

  • 点击 Next,然后点击“Finish”。

enter image description here

  • 现在,我右键单击 res 文件夹并选择“在 Finder 中显示”。

enter image description here

  • 我复制了 res 文件夹中的所有文件夹,并将其放置在my-app/resources/android/adaptiveicon/ 下。

enter image description here

  1. 接下来,我只需要将以下代码添加到 config.xml 中即可。确保适应性图标文件夹中的每个文件都正确链接,否则编译时会出现“未找到”错误。
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" />
        </edit-config>
        <resource-file src="resources/android/adaptiveicon/mipmap-anydpi-v26/ic_launcher.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" />
        <resource-file src="resources/android/adaptiveicon/mipmap-anydpi-v26/ic_launcher_round.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml" />
        <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher.png" target="app/src/main/res/mipmap-hdpi/ic_launcher.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_round.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_background.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-hdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher.png" target="app/src/main/res/mipmap-mdpi/ic_launcher.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_round.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_background.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-mdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_round.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_background.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_background.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xxhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher_background.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_background.png" />
        <resource-file src="resources/android/adaptiveicon/mipmap-xxxhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png" />

搞定了,现在应用程序将拥有自适应图标。


4
非常感谢您提供详细的解释。 提示:如果您选择仅更改背景颜色的方式,请从config.xml中删除“ic_launcher_background_png”条目。还需要添加以下行以使构建正常运行:<resource-file src="resources/android/adaptiveicon/values/ic_launcher_background.xml" target="app/src/main/res/values/ic_launcher_background.xml" /> - Stefan Rein
2
只需删除以下目录:mipmap-ldpimipmap-ldpi-v26mipmap-mdpi-v26mipmap-hdpi-v26mipmap-xhdpi-v26mipmap-xxhdpi-v26mipmap-xxxhdpi-v26,位于源代码/平台/安卓/应用/主要/资源。 - Stefan Rein
1
@StefanRein 非常感谢您提供这些有用的评论 #救命稻草。 - sachin rathod
3
感谢您,@StefanRein。如果我的回答有所帮助,我很高兴。 - Neel
1
只需将两个解决方案结合起来:从“cordova resources”中获取<icon density>和手动添加的<resource-file>内容。 - Stefan Sprenger
显示剩余5条评论

2
据我所知,Cordova目前还没有设置自适应图标的功能。但是,在运行Cordova构建之后,手动设置自适应图标很容易。
将AndroidManifest.xml中的android:icon更改为:
<application android:hardwareAccelerated="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:supportsRtl="true">  

在res/drawable中创建ic_launcher.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@drawable/ic_launcher_background"/>
    <foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

然后在res/drawable中添加两个向量文件ic_launcher_background.xml和ic_launcher_foreground.xml。可以使用此工具创建这些文件:http://inloop.github.io/svg2android/

然后就可以开始使用了!希望Cordova很快将其整合到他们的构建中。


它运行良好。AndroidManifest.xml也可以在构建shell脚本中轻松更改,使用以下命令sed -i'' 's#@mipmap/icon#@drawable/ic_launcher#' platforms/android/AndroidManifest.xml - gdorbes
更多测试:它只适用于Oreo设备,而不适用于使用默认Android图标的早期平台。 - gdorbes

2
这里是Ionic v4和Cordova Android 6.4.0的更新。对于0x6368656174的回答,值得注意的更改包括:

  • 从资源目标中删除app/src/main
  • 我的background.xml在一个values文件夹中(我没有前景,因为我使用了一个png)。
  • edit-config清单文件位置与file="AndroidManifest.xml"在同一目录中。

我挣扎了几天,但这就是对我有效的方法:

config.xml

"最初的回答"

<widget ... xmlns:android="http://schemas.android.com/apk/res/android">
    <platform name="android">
    <resource-file src="resources/android/values/ic_launcher_background.xml" target="res/values/ic_launcher_background.xml" />
    <resource-file src="resources/android/mipmap-anydpi-v26/ic_launcher.xml" target="res/mipmap-anydpi-v26/ic_launcher.xml" />
    <resource-file src="resources/android/mipmap-anydpi-v26/ic_launcher_round.xml" target="res/mipmap-anydpi-v26/ic_launcher_round.xml" />
    <resource-file src="resources/android/mipmap-hdpi/ic_launcher.png" target="res/mipmap-hdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-hdpi/ic_launcher_round.png" target="res/mipmap-hdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/mipmap-mdpi/ic_launcher.png" target="res/mipmap-mdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-mdpi/ic_launcher_round.png" target="res/mipmap-mdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/mipmap-xhdpi/ic_launcher.png" target="res/mipmap-xhdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-xhdpi/ic_launcher_round.png" target="res/mipmap-xhdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/mipmap-xxhdpi/ic_launcher.png" target="res/mipmap-xxhdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-xxhdpi/ic_launcher_round.png" target="res/mipmap-xxhdpi/ic_launcher_round.png" />
    <resource-file src="resources/android/mipmap-xxxhdpi/ic_launcher.png" target="res/mipmap-xxxhdpi/ic_launcher.png" />
    <resource-file src="resources/android/mipmap-xxxhdpi/ic_launcher_round.png" target="res/mipmap-xxxhdpi/ic_launcher_round.png" />
    <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/application">
         <application android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" />
    </edit-config>
    </platform>    
</widget>

哎呀


2
最初的回答:
可能我来晚了,但是我为了让自适应图标生效而苦苦挣扎,因为(a)使用PhoneGap Build,(b)手动操作,而不是使用Android Studio。所以,对于那些在家里玩的人,这是我所需要做的所有事情
  1. 在我的config.xml中,<platform name="android">内设置:
        <resource-file src="www/pwa/android/icon-bg.png"               target="app/src/main/res/mipmap/ic_bg.png" />
        <resource-file src="www/pwa/android/icon-fg.png"               target="app/src/main/res/mipmap/ic_fg.png" />
        <resource-file src="www/pwa/ic/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap/ic_launcher.png" />
        <resource-file src="www/pwa/ic/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap/ic_launcher_round.png" />
        <resource-file src="www/pwa/android/ic_launcher.xml"           target="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" />
        <resource-file src="www/pwa/android/ic_launcher_round.xml"     target="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml" />

        <!-- Per https://forums.adobe.com/thread/2576077 -->
        <edit-config file="app/src/main/AndroidManifest.xml" mode="merge" target="/manifest/application">
            <application android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" />
        </edit-config>
  1. 上述配置中的PNG文件是不言自明的。被引用为ic_launcher.xmlic_launcher_round.xml的XML文件是相同的,我只是在源位置创建了这个文件,然后通过上面的资源标签进行复制。以下是这两个XML文件的内容,可以参考public/pwa/android/ic_launcher.xmlic_launcher_round.xml的src:
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@mipmap/ic_bg"/>
    <foreground android:drawable="@mipmap/ic_fg"/>
</adaptive-icon>
注意,我正在针对phonegap ver 8.1.1 (<preference name="phonegap-version" value="cli-8.1.1" />) 进行操作。在https://forums.adobe.com/thread/2576077中提到,您必须根据使用的cli版本,在<resource-file标记上使用不同的target路径。

希望这有所帮助,如果有任何问题,请随时问我。谢谢!

将“Original Answer”翻译成“最初的回答”。

谢谢!终于成功将 AI 添加到我的 PGB 项目中了! - Leonardo Gandini

0

Android icon-android-foreground.png 432*432像素 72dpi

iOS 无透明度的png格式 72dpi

Android参考资料 https://developer.android.com/guide/practices/ui_guidelines/icon_design_adaptive

  const CordovaRes = require('cordova-res');
  const iconFolder = {
    dev: 'test',
    test: 'test',
    beta: 'beta',
    betaInternal: 'beta',
    betaRelease: 'beta',
    production: 'production'
  }[ENV];
  const sourcePath = `resources/_environments/${iconFolder}/`;
  const options = {
    resourcesDirectory: 'resources',
    platforms: {
      ios: { icon: { sources: [`${sourcePath}/icon-ios.png`] } },
      android: {
        'adaptive-icon': {
          icon: { sources: [`${sourcePath}/icon-android.png`] },
          foreground: { sources: [`${sourcePath}/icon-android-foreground.png`] },
          background: {
            sources: [{ type: 'color', color: '#b5c429' }]
          }
        }
      }
    }
  };
 CordovaRes(options);


-4

首先,您需要将Cordova更新到7.0版本。Cordova 7.0支持Android Oreo。一旦您使用Cordova 7创建了一个应用程序,那么您就必须使用本机Android代码手动创建自适应图标。这个Medium博客会帮助您完成这个过程。一旦您创建了自适应图标,您可以像这样将它们添加到您的config.xml中 -

<platform name="android">
    <!--
        ldpi    : 36x36 px
        mdpi    : 48x48 px
        hdpi    : 72x72 px
        xhdpi   : 96x96 px
        xxhdpi  : 144x144 px
        xxxhdpi : 192x192 px
    -->
    <icon src="res/android/ldpi.png" density="ldpi" />
    <icon src="res/android/mdpi.png" density="mdpi" />
    <icon src="res/android/hdpi.png" density="hdpi" />
    <icon src="res/android/xhdpi.png" density="xhdpi" />
    <icon src="res/android/xxhdpi.png" density="xxhdpi" />
    <icon src="res/android/xxxhdpi.png" density="xxxhdpi" />
</platform>

1
自适应图标使用前景和背景 .xml 文件,这些文件可以通过 Image Asset Studio 生成。如何通过 config.xml 将这些文件加载到 Cordova 中? - Augustin Bralley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接