自定义Flutter应用图标后图标消失/空白问题求助
我按照flutter_launcher_icons的教程配置了pubspec.yaml,添加依赖并设置图标路径,执行了flutter clean、flutter pub get、flutter pub run flutter_launcher_icons命令,过程中虽提示部分包过时,但图标生成日志显示成功。但将APP通过USB调试安装到安卓设备后,图标变成空白(之前显示的是Flutter默认图标)。
我试过更换执行命令(flutter pub run flutter_launcher_icons:main、dart run flutter_launcher_icons)、移除图标Alpha通道、重新配置更完整的pubspec.yaml,执行命令后卸载重装APP,甚至构建release包安装,问题依然存在。不知道该怎么进一步调试。
初始pubspec.yaml配置
dev_dependencies: flutter_test: sdk: flutter flutter_launcher_icons: "^0.14.1" # The "flutter_lints" package below contains a set of recommended lints to # encourage good coding practices. The lint set provided by the package is # activated in the `analysis_options.yaml` file located at the root of your # package. See that file for information about deactivating specific lint # rules and activating additional ones. flutter_lints: ^4.0.0 flutter_launcher_icons: android: "launcher_icon" ios: true image_path: "assets/images/icon2.png"
依赖获取提示信息
Resolving dependencies... Downloading packages... collection 1.18.0 (1.19.0 available) leak_tracker 10.0.5 (10.0.7 available) leak_tracker_flutter_testing 3.0.5 (3.0.8 available) material_color_utilities 0.11.1 (0.12.0 available) meta 1.15.0 (1.16.0 available) string_scanner 1.2.0 (1.3.0 available) test_api 0.7.2 (0.7.3 available) Got dependencies! 7 packages have newer versions incompatible with dependency constraints. Try `flutter pub outdated` for more information.
图标生成成功日志
Deprecated. Use `dart run` instead. Building package executable... Built flutter_launcher_icons:flutter_launcher_icons. ════════════════════════════════════════════ FLUTTER LAUNCHER ICONS (v0.14.1) ════════════════════════════════════════════ • Creating default icons Android • Adding a new Android launcher icon • Creating mipmap xml file Android WARNING: Icons with alpha channel are not allowed in the Apple App Store. Set "remove_alpha_ios: true" to remove it. • Overwriting default iOS launcher icon with new icon No platform provided ✓ Successfully generated launcher icons
修改后的pubspec.yaml配置
# flutter pub run flutter_launcher_icons flutter_launcher_icons: image_path: "assets/images/icon2.png" android: "launcher_icon" # image_path_android: "assets/icon/icon.png" min_sdk_android: 21 # android min sdk min:16, default 21 # adaptive_icon_background: "assets/icon/background.png" # adaptive_icon_foreground: "assets/icon/foreground.png" # adaptive_icon_monochrome: "assets/icon/monochrome.png" ios: true # image_path_ios: "assets/icon/icon.png" remove_alpha_channel_ios: true # image_path_ios_dark_transparent: "assets/icon/icon_dark.png" # image_path_ios_tinted_grayscale: "assets/icon/icon_tinted.png" # desaturate_tinted_to_grayscale_ios: true web: generate: true image_path: "assets/images/icon2.png" background_color: "#000000" theme_color: "#3d9bd5" windows: generate: true image_path: "assets/images/icon2.png" icon_size: 48 # min:48, max:256, default: 48 macos: generate: true image_path: "assets/images/icon2.png"
排查与解决步骤
检查安卓Manifest配置
打开android/app/src/main/AndroidManifest.xml,确认application标签的android:icon属性是@mipmap/launcher_icon,而非默认的@mipmap/ic_launcher。如果不符,手动修改后重新构建项目。验证生成的图标文件
进入android/app/src/main/res/目录,检查各分辨率的mipmap文件夹(如mipmap-hdpi、mipmap-xhdpi)下是否存在launcher_icon.png。如果缺失,删除所有与launcher图标相关的mipmap文件,重新执行dart run flutter_launcher_icons。清理安卓端构建缓存
仅执行flutter clean可能不够,还需清理安卓gradle缓存:cd android && ./gradlew clean回到项目根目录后,重新执行
flutter pub get和图标生成命令。调整flutter_launcher_icons版本
当前使用的0.14.1版本可能存在兼容性问题,尝试切换到最新稳定版或回退到0.13.1版本,修改pubspec.yaml中的依赖后重新获取依赖并生成图标。排查图标文件问题
确保图标是1024x1024分辨率的不透明PNG,尝试替换为一张已知可用的测试图标,排除原图标文件损坏或格式不兼容的问题。清除设备Launcher缓存
部分安卓设备的桌面Launcher会缓存图标,卸载APP后仍可能残留。可以重启设备,或进入设置→应用管理→找到当前Launcher→清除缓存和数据。
内容的提问来源于stack exchange,提问作者Xaver

