You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

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:

1. First, check if the file exists in the right spot

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 drawable or drawable-v24—Gradle expects it here for launcher icon references.
2. Regenerate the launcher icon (the easiest, most reliable fix)

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 app module in the Project view → NewImage 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.
3. Double-check your configuration files

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.xml and ic_launcher_round.xml. These files define your adaptive icon structure, so make sure they have a line pointing to the foreground file:
    <foreground android:drawable="@drawable/ic_launcher_foreground"/>
    
    If this line is missing or has a typo, fix it to match the correct file path.
  • Check your AndroidManifest.xml to ensure the launcher icon references are correct. The android:icon and android:roundIcon attributes should point to the mipmap icons (not the drawable directly), like @mipmap/ic_launcher.
  • Look at your module-level build.gradle file for any accidental exclusions. If you see a line like exclude 'drawable/ic_launcher_foreground.xml' in the sourceSets section, delete it immediately.
4. Clean, rebuild, and invalidate caches

Stale build cache can sometimes cause Gradle to "think" the file is missing even after you've fixed it:

  • Go to BuildClean Project to wipe old build artifacts.
  • Then run BuildRebuild Project to generate fresh files.
  • If that still doesn't work, invalidate Android Studio's caches: Go to FileInvalidate Caches... → check "Invalidate and Restart". This clears any cached metadata that might be causing conflicts.
5. Manually create the foreground file (as a last resort)

If the Image Asset tool fails for some reason, you can create the file manually:

  • Right-click the drawable folder → NewVector 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

火山引擎 最新活动