重装最新版Android Studio后出现NoSuchElementException,无法运行应用
Hey there, let's tackle this frustrating issue you're facing after reinstalling Android Studio. That org.gradle.api.tasks.TaskExecutionException tied to :app:transformDexWithInstantRunDependenciesApkForDebug is a frequent culprit when there's a conflict between old cached data and your fresh AS setup. Here are the most reliable fixes to get your app running smoothly again:
禁用Instant Run(快速临时修复)
Instant Run tends to act up after AS updates or reinstalls. Let's turn it off first:- Open Android Studio's Settings (File > Settings on Windows/Linux, Android Studio > Settings on Mac)
- Navigate to Build, Execution, Deployment > Instant Run
- Uncheck the box labeled "Enable Instant Run to hot swap code/resource changes on deploy"
- Hit Apply and OK, then run Build > Clean Project before launching your app again.
清理Gradle与项目缓存
Old cached files from your previous AS installation are probably clashing with the new setup:- Clear project-specific cache: Go to File > Invalidate Caches... > Check "Invalidate and Restart" and wait for AS to reboot.
- Clear global Gradle cache: Locate your Gradle cache folder (Windows:
C:\Users\<YourUsername>\.gradle\caches, Mac/Linux:~/.gradle/caches), delete all contents inside, then go back to AS and sync your project again.
校验Gradle插件与Gradle版本兼容性
The new AS might have auto-selected a Gradle plugin version that doesn't match your project's Gradle runtime:- Open the project-level
build.gradlefile (the one in your root project directory) - Ensure the
com.android.tools.build:gradleversion aligns with your AS version (e.g., AS Arctic Fox and newer require plugin version 7.0+; if you're unsure, follow the sync prompt to upgrade to the recommended version) - Double-check the
gradle-wrapper.propertiesfile in yourgradlefolder: thedistributionUrlshould point to a Gradle version compatible with your plugin (e.g., plugin 7.0 works with Gradle 7.0+).
- Open the project-level
删除Instant Run的临时项目文件
Leftover Instant Run files in your project can also cause this error:- Close Android Studio completely
- Navigate to your project's
app/build/intermediates/instant-runandapp/build/outputs/instant-runfolders - Delete all contents inside these folders
- Reopen AS, clean your project, and try running your app again.
内容的提问来源于stack exchange,提问作者kairatawer

