Flutter Android启动页配置问题:@mipmap/ic_launcher资源无法解析
Hey there! Let's tackle this frustrating issue where your launcher icon works perfectly in the AndroidManifest.xml but won't resolve in launch_background.xml. Here are the most common fixes to get your launch screen showing the icon correctly:
1. Double-Check the Icon Resource Name & Location
First, let's confirm you're referencing the right resource:
- Navigate to
android/app/src/main/resand expand allmipmap-*folders (likemipmap-hdpi,mipmap-mdpi). - Verify every folder contains a file named
ic_launcher.png(or matching image format). Sometimes the Image Asset wizard generates a rounded version namedic_launcher_roundby default—if that's the case, update yourlaunch_background.xmlto use@mipmap/ic_launcher_roundinstead. - Make sure
launch_background.xmlis saved directly inandroid/app/src/main/res/drawable(not a version-specific subfolder likedrawable-v21unless intended for targeted API levels).
2. Clear Android Studio's Cache & Rebuild
Android Studio's cached resources often cause false "resource not found" errors. Try these steps:
- Go to the top menu bar and select
Build→Clean Projectto delete all compiled build files. - Next, select
Build→Rebuild Projectto recompile everything from scratch. - If the error persists, go to
File→Invalidate Caches..., check "Clear file system cache and local history", then clickInvalidate and Restartto clear deep caches and restart the IDE.
3. Verify XML Syntax & Namespace
Even a tiny typo can break resource resolution. Double-check your launch_background.xml:
- Ensure the root
layer-listtag includes the correct Android namespace:<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center"> - Confirm the
bitmaptag'sandroid:srcattribute is spelled correctly:<bitmap android:src="@mipmap/ic_launcher" />
4. Confirm Flutter's Android Module Configuration
Since this is a Flutter project, make sure your launch theme is properly linked:
- Verify the
android:themeattribute in the<activity>tag points to yourLaunchTheme:<activity android:name=".MainActivity" android:theme="@style/LaunchTheme" ...> - Double-check the
android:iconattribute in the<application>tag uses the same@mipmap/ic_launcherreference that works for your app icon.
After trying these steps, your launch_background.xml should correctly resolve the icon resource, and your launch screen will display the icon as expected!
内容的提问来源于stack exchange,提问作者daveoncode

