添加 cordova-android@11 时出现 Ionic android 12 启动错误

Nel*_*ali 9 cordova ionic-framework android-12

由于 Google 在 Google Play 控制台上将最低 sdk 提高到 31,我不得不对 Android 12 进行一些更改和更新,但是当我尝试添加新的 Android 平台 android@11 时,我遇到了关于已更改的 Flash 的错误科尔多瓦11

\n

有人可以帮助我吗?

\n

预先非常感谢

\n

这是错误:\xc2\xa0

\n
Cannot read properties of null (reading 'find')\nTypeError: Cannot read properties of null (reading 'find')\n\xc2\xa0 \xc2\xa0 at E:\\DEV\\IONIC\\donateApp_cord\\node_modules\\cordova-android\\lib\\prepare.js:387:49\n\xc2\xa0 \xc2\xa0 at Array.forEach (<anonymous>)\n\xc2\xa0 \xc2\xa0 at updateProjectSplashScreen (E:\\DEV\\IONIC\\donateApp_cord\\node_modules\\cordova-android\\lib\\prepare.js:384:7)\n\xc2\xa0 \xc2\xa0 at updateProjectAccordingTo (E:\\DEV\\IONIC\\donateApp_cord\\node_modules\\cordova-android\\lib\\prepare.js:269:5)\n\xc2\xa0 \xc2\xa0 at E:\\DEV\\IONIC\\donateApp_cord\\node_modules\\cordova-android\\lib\\prepare.js:67:21\n\xc2\xa0 \xc2\xa0 at processTicksAndRejections (node:internal/process/task_queues:96:5)\n\xc2\xa0 \xc2\xa0 at async Promise.all (index 0)\n[ERROR] An error occurred while running subprocess cordova.\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 cordova.cmd platform add android@11 --verbose exited with exit code 1.\n
Run Code Online (Sandbox Code Playgroud)\n

引发错误的文件是:\n E:\\DEV\\IONIC\\donateApp_cord\\node_modules\\cordova-android\\lib\\prepare.js

\n
function updateProjectSplashScreen (platformConfig, locations) {\n\n\xc2\xa0 \xc2\xa0 // res/values/themes.xml\n\n\xc2\xa0 \xc2\xa0 const themes = xmlHelpers.parseElementtreeSync(locations.themes);\n\n\xc2\xa0 \xc2\xa0 const splashScreenTheme = themes.find('style[@name="Theme.App.SplashScreen"]');\n\n\xc2\xa0 \xc2\xa0 [\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 'windowSplashScreenAnimatedIcon',\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 'windowSplashScreenAnimationDuration',\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 'windowSplashScreenBackground',\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 'windowSplashScreenBrandingImage',\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 'windowSplashScreenIconBackgroundColor',\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 'postSplashScreenTheme'\n\n\xc2\xa0 \xc2\xa0 ].forEach(themeKey => {\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 const cdvConfigPrefKey = 'Android' + themeKey.charAt(0).toUpperCase() + themeKey.slice(1);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 const cdvConfigPrefValue = platformConfig.getPreference(cdvConfigPrefKey, this.platform);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 let themeTargetNode = splashScreenTheme.find(`item[@name="${themeKey}"]`);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 switch (themeKey) {\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 case 'windowSplashScreenBackground':\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // use the user defined value for "colors.xml"\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 updateProjectSplashScreenBackgroundColor(cdvConfigPrefValue, locations);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // force the themes value to `@color/cdv_splashscreen_background`\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 themeTargetNode.text = '@color/cdv_splashscreen_background';\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 break;\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 case 'windowSplashScreenAnimatedIcon':\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // handle here the cases of "png" vs "xml" (drawable)\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // If "png":\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // \xc2\xa0- Clear out default or previous set "drawable/ic_cdv_splashscreen.xml" if exisiting.\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // \xc2\xa0- Copy png in correct mipmap dir with name "ic_cdv_splashscreen.png"\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // If "xml":\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // \xc2\xa0- Clear out "{mipmap}/ic_cdv_splashscreen.png" if existing.\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // \xc2\xa0- Copy xml into drawable dir with name "ic_cdv_splashscreen.xml"\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // updateProjectSplashScreenIcon()\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // value should change depending on case:\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // If "png": "@mipmap/ic_cdv_splashscreen"\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // If "xml": "@drawable/ic_cdv_splashscreen"\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 updateProjectSplashScreenImage(locations, themeKey, cdvConfigPrefKey, cdvConfigPrefValue);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 break;\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 case 'windowSplashScreenBrandingImage':\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // display warning only when set.\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 if (cdvConfigPrefValue) {\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 events.emit('warn', `"${themeKey}" is currently not supported by the splash screen compatibility library. https://issuetracker.google.com/issues/194301890`);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 }\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 updateProjectSplashScreenImage(locations, themeKey, cdvConfigPrefKey, cdvConfigPrefValue);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // force the themes value to `@color/cdv_splashscreen_icon_background`\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 if (!cdvConfigPrefValue && themeTargetNode) {\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 splashScreenTheme.remove(themeTargetNode);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 } else if (cdvConfigPrefValue) {\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // if there is no current node, create a new node.\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 if (!themeTargetNode) {\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 themeTargetNode = themes.getroot().makeelement('item', { name: themeKey });\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 splashScreenTheme.append(themeTargetNode);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 }\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // set the user defined color.\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 themeTargetNode.text = '@drawable/ic_cdv_splashscreen_branding';\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 }\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 break;\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 case 'windowSplashScreenIconBackgroundColor':\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // use the user defined value for "colors.xml"\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 updateProjectSplashScreenIconBackgroundColor(cdvConfigPrefValue, locations);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // force the themes value to `@color/cdv_splashscreen_icon_background`\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 if (!cdvConfigPrefValue && themeTargetNode) {\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // currentItem.remove();\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 splashScreenTheme.remove(themeTargetNode);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 } else if (cdvConfigPrefValue) {\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // if there is no current color, create a new node.\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 if (!themeTargetNode) {\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 themeTargetNode = themes.getroot().makeelement('item', { name: themeKey });\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 splashScreenTheme.append(themeTargetNode);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 }\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 // set the user defined color.\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 themeTargetNode.text = '@color/cdv_splashscreen_icon_background';\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 }\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 break;\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 case 'windowSplashScreenAnimationDuration':\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 themeTargetNode.text = cdvConfigPrefValue || '200';\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 break;\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 case 'postSplashScreenTheme':\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 themeTargetNode.text = cdvConfigPrefValue || '@style/Theme.AppCompat.NoActionBar';\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 break;\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 default:\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 events.emit('warn', `The theme property "${themeKey}" does not exist`);\n\n\xc2\xa0 \xc2\xa0 \xc2\xa0 \xc2\xa0 }\n\n\xc2\xa0 \xc2\xa0 });\n\n\xc2\xa0 \xc2\xa0 fs.writeFileSync(locations.themes, themes.write({ indent: 4 }), 'utf-8');\n\n  \xc2\xa0 events.emit('verbose', 'Wrote out Android application themes to ' + locations.themes);\n}\n
Run Code Online (Sandbox Code Playgroud)\n

关于foreach上的splashScreenTheme.find函数,有人可以帮助我吗?

\n

Igu*_*aut 8

如果它对任何人有帮助,我发现我正在使用的插件之一(特别是 cordova-plugin-telerik-imagepicker)正在写入自己的themes.xml文件,app/src/main/res/values该文件会覆盖Cordova 生成的模板。

这就是为什么 Cordova 在尝试更新themes.xml不再存在的元素时崩溃的原因。

我通过分叉插件并删除其themes.xml. 不确定这对插件有什么影响,但我不太怀疑......

  • 拯救了我的日子,tysm!:) (2认同)

Mik*_*ike 5

实际上,我解决了编译问题,但该应用程序仅在 Android 12 上运行,并且在所有其他 Android 版本上的启动屏幕上崩溃。

错误发生在node_modules\cordova-android\lib\prepare.js中的方法:updateProjectSplashScreen(...)中,因为varsplashScreenThemenull,因为theme(= style[@name=" Theme.App.SplashScreen "])未找到或 var themeTargetNode为 null,因为未找到主题项。

我在platforms/android/app/src/main/res/values目录中的themes.xml(&colors.xml)中手动添加了主题,内容如下:

<?xml version='1.0' encoding='utf-8'?>
<resources>
    <style name="Theme.App.SplashScreen" parent="@android:style/Theme.DeviceDefault.NoActionBar">
        <!-- Set the splash screen background -->
        <!-- <item name="android:windowBackground">@android:color/white</item> -->
        <item name="windowSplashScreenBackground">@color/cdv_splashscreen_background</item>
        
        <!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an animated drawable. One of these is required. -->
        <item name="windowSplashScreenAnimatedIcon">@drawable/ic_cdv_splashscreen</item>
        
        <!-- Required for animated icons -->
        <item name="windowSplashScreenAnimationDuration">200</item>
        
        <!-- Required: Set the theme of the Activity that directly follows your splash screen. -->
        <!-- <item name="postSplashScreenTheme">@style/Theme.App</item> -->
        <item name="postSplashScreenTheme">@style/Theme.AppCompat.NoActionBar</item>
    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

color.xml的内容

<?xml version='1.0' encoding='utf-8'?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <color name="cdv_splashscreen_background">#FFFFFF</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

这解决了构建期间的错误,但应用程序启动时在启动屏幕上崩溃,没有错误。我想我错误配置了主题。我希望这里的任何人都可以更正themes.xml 文件。

PS:同样的问题发布在离子论坛:https://forum.ionicframework.com/t/ionic-android-12-splash-error-while-adding-cordova-android-11/225896

PPS:我必须解决 ionic 迁移指南中未提及的另外 2 个问题(https://ionic.zendesk.com/hc/en-us/articles/7891143965975-Migration-to-Cordova-Android-11):

  • 没有删除cordova-plugin-androidx-adapter以防止出现以下错误:包 android.support.v4.content 不存在或包 android.support.v7.app 不存在。
  • 更改了一些旧插件 gradle 文件中的编译实现(在platforms/android/ cordova-plugin-badge /XXX-badge.gradle、com-sarriaroman-photoviewer /XXX-photoviewer.gradlephonegap-plugin-barcodescanner /XXX-barcodescanner中) .gradle)

我通过在package.json中使用“ before ionic build script ”来自动化所有更正,例如:

 "scripts": { "ionic:build:before": "./scripts/fix_android.sh", ... }
Run Code Online (Sandbox Code Playgroud)

scripts/fix_android.sh的内容:

#!/usr/bin/env bash
echo ""
echo "Executing fix_android.sh"

## Copy resources/android/colors.xml & themes.xml to platforms/android/app/src/main/res/values
target=platforms/android/app/src/main/res/values
if [ -d "$target" ]; then
    file4="resources/android/colors.xml"
    if [ -f "$file4" ]; then
      cp -rvf "$file4" platforms/android/app/src/main/res/values
      echo "Copied colors.xml"
    else
      echo ${file4}" not found!"
    fi
    file5="resources/android/themes.xml"
    if [ -f "$file5" ]; then
      cp -rvf "$file5" platforms/android/app/src/main/res/values
      echo "Copied themes.xml"
    else
      echo ${file5}" not found!"
    fi
else
    echo ${target}" not found!"
fi

## Change compile to implementation in old plugins' gradle files
file6="platforms/android/cordova-plugin-badge/XXX-badge.gradle"
if [ -f "$file6" ]; then
    if grep -lr "compile" "$file6"; then
        sed -i '' "s/compile/implementation/g" "$file6"
        echo "Changed compile to implementation in XXX-badge.gradle"
    else
        echo "$file6"
        echo "File already corrected!"  
    fi
else
    echo ${file6}" not found!"
fi
file7="platforms/android/com-sarriaroman-photoviewer/XXX-photoviewer.gradle"
if [ -f "$file7" ]; then
    if grep -lr "compile" "$file7"; then
        sed -i '' "s/compile/implementation/g" "$file7"
        echo "Changed compile to implementation in XXX-photoviewer.gradle"
    else
        echo "$file7"
        echo "File already corrected!"
    fi  
else
    echo ${file7}" not found!"
fi
file8="platforms/android/phonegap-plugin-barcodescanner/XXX-barcodescanner.gradle"
if [ -f "$file8" ]; then
    if grep -lr "compile" "$file8"; then
        sed -i '' "s/compile/implementation/g" "$file8"
        echo "Changed compile to implementation in XXX-barcodescanner.gradle"
    else
        echo "$file8"
        echo "File already corrected!"  
    fi
else
    echo ${file8}" not found!"
fi

## Remove all <splash> tags from config.xml
file9="config.xml"
if [ -f "$file9" ]; then
    if grep -lr "<splash" "$file9"; then
        sed -i '' "s/<splash .* \/>//g" "$file9"
        echo "Removed <splash> tags in config.xml"
    else
        echo "$file9"
        echo "File already corrected!"  
    fi
else
    echo ${file9}" not found!"
fi

echo ""
echo ""
Run Code Online (Sandbox Code Playgroud)

  • 您能告诉我您是否得到任何解决方案吗? (2认同)