function updateIconResourceForAdaptive()

in lib/prepare.js [759:916]


function updateIconResourceForAdaptive (preparedIcons, resourceMap, platformResourcesDir) {
    const android_icons = preparedIcons.android_icons;
    const default_icon = preparedIcons.default_icon;

    // The source paths for icons are relative to
    // project's config.xml location, so we use it as base path.
    let background;
    let foreground;
    let monochrome;
    let targetPathBackground;
    let targetPathForeground;
    let targetPathMonochrome;

    for (const density in android_icons) {
        let backgroundVal = '@mipmap/ic_launcher_background';
        let foregroundVal = '@mipmap/ic_launcher_foreground';
        const monochromeVal = '@mipmap/ic_launcher_monochrome';

        background = android_icons[density].background;
        foreground = android_icons[density].foreground;
        monochrome = android_icons[density].monochrome;

        const hasAdaptiveIcons = !!background && !!foreground;
        let hasMonochromeIcon = !!monochrome;

        if (hasMonochromeIcon && !hasAdaptiveIcons) {
            // If we have a monochrome icon, but no adaptive icons,
            // then warn that in order to use monochrome, the adaptive icons
            // must be supplied. We will ignore monochrome and proceed with the
            // icon preparation however.
            hasMonochromeIcon = false;
            monochrome = undefined;
            events.emit('warn', dedent`
                Monochrome icon found but without adaptive properties.
                Monochrome icon requires the adaptive background and foreground assets.
                See https://cordova.apache.org/docs/en/latest/config_ref/images.html fore more information.
            `);
        }

        if (!hasAdaptiveIcons) {
            // This icon isn't an adaptive icon, so skip it
            continue;
        }

        if (background.startsWith('@color')) {
            // Colors Use Case
            backgroundVal = background; // Example: @color/background_foobar_1
        } else if (path.extname(path.basename(background)) === '.xml') {
            // Vector Use Case
            targetPathBackground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_background.xml', path.basename(android_icons[density].background));
            resourceMap[targetPathBackground] = android_icons[density].background;
        } else if (path.extname(path.basename(background)) === '.png') {
            // Images Use Case
            targetPathBackground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_background.png', path.basename(android_icons[density].background));
            resourceMap[targetPathBackground] = android_icons[density].background;
        }

        if (foreground.startsWith('@color')) {
            // Colors Use Case
            foregroundVal = foreground;
        } else if (path.extname(path.basename(foreground)) === '.xml') {
            // Vector Use Case
            targetPathForeground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_foreground.xml', path.basename(android_icons[density].foreground));
            resourceMap[targetPathForeground] = android_icons[density].foreground;
        } else if (path.extname(path.basename(foreground)) === '.png') {
            // Images Use Case
            targetPathForeground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_foreground.png', path.basename(android_icons[density].foreground));
            resourceMap[targetPathForeground] = android_icons[density].foreground;
        }

        if (hasMonochromeIcon) {
            if (path.extname(path.basename(monochrome)) === '.xml') {
                // Vector Use Case
                targetPathMonochrome = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_monochrome.xml', path.basename(android_icons[density].monochrome));
                resourceMap[targetPathMonochrome] = android_icons[density].monochrome;
            } else if (path.extname(path.basename(monochrome)) === '.png') {
                // Images Use Case
                targetPathMonochrome = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', density, 'ic_launcher_monochrome.png', path.basename(android_icons[density].monochrome));
                resourceMap[targetPathMonochrome] = android_icons[density].monochrome;
            }
        }

        // create an XML for DPI and set color
        let icLauncherTemplate = '';
        if (hasMonochromeIcon) {
            icLauncherTemplate = dedent`
                <?xml version="1.0" encoding="utf-8"?>
                <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
                    <background android:drawable="${backgroundVal}" />
                    <foreground android:drawable="${foregroundVal}" />
                    <monochrome android:drawable="${monochromeVal}" />
                </adaptive-icon>
            `;
        } else {
            icLauncherTemplate = dedent`
                <?xml version="1.0" encoding="utf-8"?>
                <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
                    <background android:drawable="${backgroundVal}" />
                    <foreground android:drawable="${foregroundVal}" />
                </adaptive-icon>
            `;
        }

        const launcherXmlPath = path.join(platformResourcesDir, 'mipmap-' + density + '-v26', 'ic_launcher.xml');

        // Remove the XML from the resourceMap so the file does not get removed.
        delete resourceMap[launcherXmlPath];

        fs.writeFileSync(path.resolve(launcherXmlPath), icLauncherTemplate);
    }

    // There's no "default" drawable, so assume default == mdpi.
    if (default_icon && !android_icons.mdpi) {
        let defaultTargetPathBackground;
        let defaultTargetPathForeground;
        let defaultTargetPathMonochrome;

        if (background.startsWith('@color')) {
            // Colors Use Case
            targetPathBackground = default_icon.background;
        } else if (path.extname(path.basename(background)) === '.xml') {
            // Vector Use Case
            defaultTargetPathBackground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_background.xml', path.basename(default_icon.background));
            resourceMap[defaultTargetPathBackground] = default_icon.background;
        } else if (path.extname(path.basename(background)) === '.png') {
            // Images Use Case
            defaultTargetPathBackground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_background.png', path.basename(default_icon.background));
            resourceMap[defaultTargetPathBackground] = default_icon.background;
        }

        if (foreground.startsWith('@color')) {
            // Colors Use Case
            targetPathForeground = default_icon.foreground;
        } else if (path.extname(path.basename(foreground)) === '.xml') {
            // Vector Use Case
            defaultTargetPathForeground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_foreground.xml', path.basename(default_icon.foreground));
            resourceMap[defaultTargetPathForeground] = default_icon.foreground;
        } else if (path.extname(path.basename(foreground)) === '.png') {
            // Images Use Case
            defaultTargetPathForeground = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_foreground.png', path.basename(default_icon.foreground));
            resourceMap[defaultTargetPathForeground] = default_icon.foreground;
        }

        if (monochrome) {
            if (path.extname(path.basename(monochrome)) === '.xml') {
                // Vector Use Case
                defaultTargetPathMonochrome = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_monochrome.xml', path.basename(default_icon.monochrome));
                resourceMap[defaultTargetPathMonochrome] = default_icon.monochrome;
            } else if (path.extname(path.basename(monochrome)) === '.png') {
                // Images Use Case
                defaultTargetPathMonochrome = getAdaptiveImageResourcePath(platformResourcesDir, 'mipmap', 'mdpi', 'ic_launcher_monochrome.png', path.basename(default_icon.monochrome));
                resourceMap[defaultTargetPathMonochrome] = default_icon.monochrome;
            }
        }
    }

    return resourceMap;
}