Gradle构建失败:找不到drawable/ic_launcher_foreground.xml,该如何解决?
Hey there, let's troubleshoot this missing ic_launcher_foreground.xml issue that's breaking your Gradle build. This is a super common problem when dealing with Android adaptive launcher icons, so let's walk through the most effective fixes step by step:
Start by navigating to your project's app/src/main/res/ directory. Look for ic_launcher_foreground.xml in either the drawable or drawable-v24 folder (adaptive icons often use these directories for vector assets).
- If the file is missing entirely: It might have been accidentally deleted, renamed, or never generated in the first place. Skip to step 2 to regenerate it.
- If the file is in the wrong folder: Move it to
drawableordrawable-v24—Gradle expects it here for launcher icon references.
Android Studio has a built-in tool that will auto-generate all the necessary launcher icon files, including the missing foreground XML. Here's how to use it:
- Right-click your
appmodule in the Project view → New → Image Asset. - In the popup, select "Launcher Icons (Adaptive and Legacy)" from the "Icon Type" dropdown.
- Under the "Foreground Layer" section, choose an image (you can use the default Android icon or upload your own custom asset).
- Click Next then Finish. Android Studio will create
ic_launcher_foreground.xml(and all required background/mipmap files) and place them in the correct directories automatically.
Sometimes the issue isn't the missing file—it's a broken reference in your manifest or icon configs:
- Open
app/src/main/res/mipmap-anydpi-v26/ic_launcher.xmlandic_launcher_round.xml. These files define your adaptive icon structure, so make sure they have a line pointing to the foreground file:
If this line is missing or has a typo, fix it to match the correct file path.<foreground android:drawable="@drawable/ic_launcher_foreground"/> - Check your
AndroidManifest.xmlto ensure the launcher icon references are correct. Theandroid:iconandandroid:roundIconattributes should point to the mipmap icons (not the drawable directly), like@mipmap/ic_launcher. - Look at your module-level
build.gradlefile for any accidental exclusions. If you see a line likeexclude 'drawable/ic_launcher_foreground.xml'in thesourceSetssection, delete it immediately.
Stale build cache can sometimes cause Gradle to "think" the file is missing even after you've fixed it:
- Go to Build → Clean Project to wipe old build artifacts.
- Then run Build → Rebuild Project to generate fresh files.
- If that still doesn't work, invalidate Android Studio's caches: Go to File → Invalidate Caches... → check "Invalidate and Restart". This clears any cached metadata that might be causing conflicts.
If the Image Asset tool fails for some reason, you can create the file manually:
- Right-click the
drawablefolder → New → Vector Asset (or just XML File if you prefer). - Name the file
ic_launcher_foreground.xml. - Paste in a basic vector drawable template (you can adjust the color/path to match your app's style). Here's a simple example:
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="108dp" android:height="108dp" android:viewportWidth="108" android:viewportHeight="108"> <path android:fillColor="#FF2196F3" android:pathData="M39,108h30c16.57,0 30,-13.43 30,-30L99,39c0,-16.57 -13.43,-30 -30,-30L39,9C22.43,9 9,22.43 9,39l0,30C9,94.57 22.43,108 39,108z"/> </vector> - Save the file, then clean and rebuild your project.
内容的提问来源于stack exchange,提问作者Thrinadh Reddy




