Cordova Android构建生成空v26 mipmap文件夹问题求助
关于Cordova Android构建生成空v26图标文件夹的问题
使用cordova-android": "11.0.0"版本构建Android应用时,我已在config.xml中配置了指定图标资源,但构建后仍生成mipmap-ldpi-v26、mipmap-hdpi-v26等无资源文件的空文件夹,查阅资料未发现同类问题。
我的config.xml配置如下:
<preference name="android-minSdkVersion" value="21" /> <preference name="android-targetSdkVersion" value="33" /> <resource-file src="resources-android/values/ic_launcher_background.xml" target="app/src/main/res/values/ic_launcher_background.xml" /> <resource-file src="resources-android/mipmap-anydpi-v26/ic_launcher.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml" /> <resource-file src="resources-android/mipmap-anydpi-v26/ic_launcher_round.xml" target="app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml" /> <resource-file src="resources-android/mipmap-mdpi/ic_launcher.png" target="app/src/main/res/mipmap-mdpi/ic_launcher.png" /> <resource-file src="resources-android/mipmap-mdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_round.png" /> <resource-file src="resources-android/mipmap-mdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png" /> <resource-file src="resources-android/mipmap-hdpi/ic_launcher.png" target="app/src/main/res/mipmap-hdpi/ic_launcher.png" /> <resource-file src="resources-android/mipmap-hdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_round.png" /> <resource-file src="resources-android/mipmap-hdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png" /> <resource-file src="resources-android/mipmap-xhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher.png" /> <resource-file src="resources-android/mipmap-xhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_round.png" /> <resource-file src="resources-android/mipmap-xhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png" /> <resource-file src="resources-android/mipmap-xxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher.png" /> <resource-file src="resources-android/mipmap-xxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png" /> <resource-file src="resources-android/mipmap-xxhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png" /> <resource-file src="resources-android/mipmap-xxxhdpi/ic_launcher.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher.png" /> <resource-file src="resources-android/mipmap-xxxhdpi/ic_launcher_round.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png" /> <resource-file src="resources-android/mipmap-xxxhdpi/ic_launcher_foreground.png" target="app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png" />
原因分析
- Cordova Android 11.x的默认构建模板会自动生成符合Android资源规范的目录结构,针对API 26+的密度目录即使没有对应资源,也会被创建出来
- 你仅配置了
mipmap-anydpi-v26的矢量图标配置文件,未提供其他密度的v26版本图标,但构建流程仍会按规范生成对应空目录
解决办法
1. 直接忽略空目录
这些空目录不会影响应用功能,Android打包工具会自动忽略空资源目录,不会增加最终APK的体积,完全可以不用处理。
2. 添加对应资源(可选)
如果不想看到空目录,可以为每个密度创建v26版本的图标资源,放入对应的mipmap-xxx-v26文件夹,然后在config.xml中添加对应的<resource-file>配置,比如:
<resource-file src="resources-android/mipmap-hdpi-v26/ic_launcher.png" target="app/src/main/res/mipmap-hdpi-v26/ic_launcher.png" />
3. 通过构建钩子删除空目录
自定义Cordova构建钩子,在构建完成后自动删除这些空目录:
- 在项目根目录创建
hooks/after_prepare文件夹 - 新建脚本文件
remove_empty_v26_dirs.js,内容如下:
const fs = require('fs'); const path = require('path'); const resDir = path.join(__dirname, '../../platforms/android/app/src/main/res'); if (!fs.existsSync(resDir)) return; const dirs = fs.readdirSync(resDir); dirs.forEach(dir => { if (dir.includes('-v26')) { const dirPath = path.join(resDir, dir); const files = fs.readdirSync(dirPath); if (files.length === 0) { fs.rmdirSync(dirPath); console.log(`Deleted empty directory: ${dir}`); } } });
- 给脚本添加执行权限:
chmod +x hooks/after_prepare/remove_empty_v26_dirs.js
内容的提问来源于stack exchange,提问作者Dunny
相关产品推荐
相关产品推荐

