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

Android Studio 3.1创建项目触发AAPT2构建错误求助

Troubleshooting AAPT2 Build Failure in Android Studio 3.1

Hey there, sorry to hear you're stuck with this build error in Android Studio 3.1. AAPT2 issues almost always tie back to resource problems or tooling configuration glitches, so let's walk through some practical fixes to get your project up and running:

Step-by-Step Fixes to Try

1. Check Your Resource Files for Issues

AAPT2 is strict about resource rules—even tiny mistakes can break the build:

  • Ensure all resource filenames use only lowercase letters, with underscores/hyphens for spacing (e.g., rename HeaderLogo.png to header_logo.png). No uppercase letters, spaces, or special characters allowed.
  • Audit XML resources (layouts, drawables, value files) for syntax errors: unclosed tags, misspelled attributes, or invalid values. A missing > or a typo in android:layout_width can trigger this error.
  • Look for duplicate resource names across different directories (e.g., same filename in drawable and drawable-xhdpi).

2. Clean & Rebuild the Project

Corrupted build cache is a super common culprit:

  • Go to the top menu: Build > Clean Project, wait for it to finish, then select Build > Rebuild Project.
  • Or run these commands in your project's root directory via terminal:
    # For macOS/Linux
    ./gradlew clean
    ./gradlew assembleDebug
    
    # For Windows
    gradlew clean
    gradlew assembleDebug
    

3. Disable AAPT2 Temporarily (Quick Workaround)

Android Studio 3.1 enables AAPT2 by default. If you need to get your project working while you debug the root cause, you can turn it off:

  • Open your project's root gradle.properties file, add this line:
    android.enableAapt2=false
    
  • Click Sync Now in the top-right corner, then try building again.

4. Verify SDK Build Tools Compatibility

Mismatched build tools versions often cause AAPT2 errors:

  • Go to File > Project Structure > SDK Location and check the Android SDK Build Tools version. For Android Studio 3.1, versions like 27.0.3 are known to work reliably.
  • If you don't have the correct version, open the SDK Manager (Tools > SDK Manager), go to the SDK Tools tab, check the compatible build tools version, and click Apply to download it.

5. Get Detailed Error Logs

The generic error message doesn't tell the full story. Run this command to get a stack trace that points to the exact problem file:

# macOS/Linux
./gradlew assembleDebug --stacktrace

# Windows
gradlew assembleDebug --stacktrace

Look for lines that mention specific resource paths—this will directly tell you which file is causing the failure.

If none of these steps resolve the issue, share the full stacktrace output and details about your project's resource structure, and we can dig deeper!

内容的提问来源于stack exchange,提问作者Seçkin Çebi

火山引擎 最新活动