Android Oreo - how do I set Adaptive Icons in Cordova?

前端 未结 9 1615
死守一世寂寞
死守一世寂寞 2020-12-24 13:57

Just wondering if anyone been able to set adaptive icons on Cordova for Android Oreo? I\'m using the android 6.4.0 and my square icon shrinks to fit the circle. I just want

相关标签:
9条回答
  • 2020-12-24 14:39

    As far as I know, Cordova doesn't set Adaptive Icons yet. But it's easy enough to do manually after you've run Cordova build.

    Change android:icon in AndroidManifest.xml to:

    <application android:hardwareAccelerated="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:supportsRtl="true">  
    

    Create ic_launcher.xml in res/drawable with:

    <?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>
    

    Then add your two vector files ic_launcher_background.xml and ic_launcher_foreground.xml in res/drawable. These can be created with this tool: http://inloop.github.io/svg2android/

    And away you go! I hope Cordova bakes this into their builds soon.

    0 讨论(0)
  • 2020-12-24 14:39

    So here's an update for Ionic v4 and Cordova Android: 6.4.0. Notable changes to 0x6368656174's answer were:

    • Removing app/src/main from the resource targets.
    • My background.xml was in a values folder (i did not have a foreground as i used a png)
    • The edit-config Manifest file location is in the same directory file="AndroidManifest.xml"

    I struggled for a few days but this is what works for me:

    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>
    

    PHEW

    0 讨论(0)
  • 2020-12-24 14:41

    You will first need to update Cordova to 7.0. Cordova 7.0 gives support for Android Oreo. Once you have created an app using Cordova 7, then you will have to manually create the adaptive icons using the native Android code. This Medium blog will help you with that. Once you have created your Adaptive Icons, you can add them to your config.xml like this -

    <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>
    
    0 讨论(0)
  • WARNING: Don't use this answer. This is now supported out of the box as of Cordova 9. See https://stackoverflow.com/a/55169307/2947592

    I created the icons as described in https://developer.android.com/studio/write/image-asset-studio.html#create-adaptive, copied them to res/android and use the following configuration:

    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>
    
    0 讨论(0)
  • 2020-12-24 14:48

    WARNING: Don't use this answer. This is now supported out of the box as of Cordova 9. See https://stackoverflow.com/a/55169307/2947592

    Recent Android uses Adaptive Icons which has background and foreground images along with some xml files. This is what I did to set-up Adaptive Icons in my ionic application:

    1. In config.xml, I set android-minSdkVersion to version 26.
    <preference name="android-minSdkVersion" value="26" />
    <preference name="android-targetSdkVersion" value="28" />
    
    1. In config.xml, delete the icon density tags and remove these lines:
            <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. Next, I had to create the Android Adaptive Icons. For this I used Image Assets that comes as part of Android Studio. At first, I created 2 the background image and the transparent icon only image to use as foreground in png format from photoshop. After that, I did these steps to generate the icons:
    • Open Android Studio and create a new project or open the existing project.

    • Click on app -> res in the left sidebar. Right clicked on res -> New -> Image Assets

    • Selected Foreground Layer -> Asset Type "Image" & selected Path with the icon only logo image and transparent background. Then selected Trim to Yes and Resize if needed.

    • Selected Background Layer -> Asset Type "Image" & selected path. (Alternative, you can even set "Color")

    • Click Next and clicked "Finish".

    • Now, I right clicked on res folder and selected "Reveal in Finder".

    • I copied all the folders inside the res folder and placed it inside: my-app/resources/android/adaptiveicon/

    1. Next, I just need to add the below codes to the config.xml. Make sure each of every file in the adaptiveicon folder is linked here correctly, else it will throw "not found" errors during build.
            <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" />
    

    Thats it. Now the App will have adaptive icons.

    0 讨论(0)
  • 2020-12-24 14:48

    This is now supported by Cordova Android 8.0.0. See the announcement and the documentation.

    For example, define the icons as follows in your 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>
    

    With the colors.xml looking like this:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="background">#FFFFFF</color>
    </resources>
    
    0 讨论(0)
提交回复
热议问题