Android Studio 3.1创建项目触发AAPT2构建错误求助
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.pngtoheader_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 inandroid:layout_widthcan trigger this error. - Look for duplicate resource names across different directories (e.g., same filename in
drawableanddrawable-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 selectBuild > 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.propertiesfile, add this line:android.enableAapt2=false - Click
Sync Nowin 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 Locationand 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 theSDK Toolstab, check the compatible build tools version, and clickApplyto 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




