Flutter应用编译失败:AAPT2可执行文件缺失问题求助
Hey there, sorry to hear your Flutter app's build started failing with that AAPT2 missing error—since it worked perfectly before, this is almost certainly a corrupted Gradle cache or a minor path mismatch. Let's walk through some tried-and-true fixes that should get you back up and running:
Step 1: Clear Gradle and Flutter Caches
The error points to a missing AAPT2 executable in your Gradle cache folder. Wiping this cache and rebuilding often resolves the issue:
- Run these commands in your project root first:
flutter clean flutter pub get - Then manually delete the problematic Gradle cache directory mentioned in the error:
C:\Users\john\.gradle\caches\transforms-2\ - Restart your IDE after completing these steps.
Step 2: Invalidate IDE Caches (Android Studio/IntelliJ)
IDE-level caches can sometimes get stuck and cause weird build glitches:
- Go to
File > Invalidate Caches... - Check the box for "Invalidate and Restart"
- Let the IDE reload fully, then try building your app again.
Step 3: Update Android Gradle Plugin (AGP) and Kotlin Versions
Your current AGP version (3.5.0) is quite outdated, which might be causing compatibility issues with newer Flutter versions. Try updating your buildscript block to more recent stable versions (and note that JCenter is deprecated, so we'll switch to Maven Central):
buildscript { ext.kotlin_version = '1.7.20' repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.4.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() mavenCentral() } }
After updating, click "Sync Now" in the IDE to apply the changes.
Step 4: Verify Flutter and Android SDK Setup
Run flutter doctor in your terminal to check for any missing components or mismatched versions. Fix any issues it reports (like outdated build tools, missing SDK components, etc.)—this often catches hidden setup problems.
Your Original Error Details
任务':app:mergeDebugResources'执行失败。指定的AAPT2可执行文件不存在:C:\Users\john.gradle\caches\transforms-2\files-2.1\a406eb4ba72ff395bc6c8b1db90b3b1f\aapt2-3.5.0-5435860-windows\aapt2.exe。必须从Maven提供AAPT2或指定自定义路径。可尝试:使用--stacktrace选项获取堆栈跟踪;使用--info或--debug选项获取更多日志输出;使用--scan选项获取完整分析信息。更多帮助请访问https://help.gradle.org。
Your Original buildscript Config
buildscript { ext.kotlin_version = '1.3.50' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.5.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } allprojects { repositories { google() jcenter() } }
内容的提问来源于stack exchange,提问作者CodeFloat

