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

如何解决Android Studio中的AAPT2错误及相关Java编译异常?

Fixes for "java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception" in Android Studio

Hey there, I’ve dealt with this frustrating AAPT2 + ExecutionException error more times than I can count in Android Studio. Let’s walk through the most reliable fixes that usually get things back on track:

1. First, Get the Actual Error Details

The default error message is super vague—you need to dig into the build logs to find what’s really breaking AAPT2:

  • Open the Build panel at the bottom of Android Studio
  • Switch to the Build Output tab
  • Look for red-highlighted lines below the generic error message. These will almost always point to a specific issue: a malformed XML tag, missing drawable, invalid resource name, or corrupted image file.

2. Clean & Rebuild Your Project

This is the "turn it off and on again" of Android development, but it works surprisingly often:

  • Go to Build > Clean Project to wipe cached build files
  • Then run Build > Rebuild Project to compile everything from scratch
    Cached resources or partial builds often confuse AAPT2, so a full clean can clear up weird inconsistencies.

3. Audit Your Resource Files (Most Common Culprit!)

AAPT2’s job is to process your app’s resources, so 90% of the time the issue lives here:

  • XML Files: Check all layouts, values, and manifest files for syntax errors—missing closing tags, typos in attribute names (like android:layout_width vs android:layout_witdh), or invalid resource references (e.g., @drawable/my_icon when the file is named My_Icon.png—AAPT2 is case-sensitive!).
  • Image Resources: Verify no drawables are corrupted (try opening them in an image viewer). Avoid using WebP files unless you’ve configured your build.gradle to support them for your target SDK.
  • Resource Naming: Stick to lowercase letters, numbers, underscores, and hyphens for resource filenames. Spaces, uppercase letters, or special characters will break AAPT2.

4. Temporarily Disable AAPT2 (Quick Workaround)

If you need to compile immediately and can’t track down the root cause right now, you can disable AAPT2 temporarily:

  • Open your project’s root gradle.properties file
  • Add this line:
    android.enableAapt2=false
    
  • Sync your project and recompile
    ⚠️ Note: This is a band-aid, not a fix. AAPT2 is the official, optimized resource processor—you’ll want to re-enable it once you resolve the underlying issue.

5. Update Gradle & Android Plugin Versions

Outdated versions of the Android Gradle Plugin or Gradle itself often have known AAPT2 bugs:

  • Go to File > Project Structure > Project
  • Update Gradle Version and Android Gradle Plugin Version to the latest stable releases (match versions per Google’s compatibility table—e.g., Plugin 8.0.x pairs with Gradle 8.0+)
  • Sync your project and try building again

6. Fix Dependency Conflicts

Third-party dependencies can sometimes introduce conflicting resources or libraries that trip up AAPT2:

  • Run the command ./gradlew app:dependencies in your terminal to generate a dependency tree
  • Look for duplicate libraries or version conflicts (e.g., two different versions of support-v4 or material-components)
  • Exclude conflicting modules in your app’s build.gradle like this:
    implementation("com.example:problematic-library:1.0.0") {
        exclude group: "androidx.appcompat", module: "appcompat"
    }
    

7. Check File Encoding Settings

Mismatched or non-UTF-8 encoding can cause AAPT2 to fail parsing resource files:

  • Go to File > Settings > Editor > File Encodings (on macOS: Android Studio > Settings > Editor > File Encodings)
  • Set IDE Encoding, Project Encoding, and Default encoding for properties files all to UTF-8
  • Check the box for Transparent native-to-ascii conversion
  • Restart Android Studio and recompile

8. Invalidate Caches & Restart

Sometimes Android Studio’s own cached data gets corrupted:

  • Go to File > Invalidate Caches...
  • Select Invalidate and Restart
  • Once Studio reopens, try building your project again

If none of these fixes work, try creating a blank new Android project and gradually move your code/resources over to it. This will help you isolate exactly which file or configuration is causing the AAPT2 error.

内容的提问来源于stack exchange,提问作者Shivam Choudhary

火山引擎 最新活动