升级Android Studio 4.1.1后出现:app:compileDebugJavaWithJavac执行失败问题
:app:compileDebugJavaWithJavac Error Post Android Studio 4.1.1 Upgrade Hey there, sorry to hear you're stuck with this compilation error after upgrading to Android Studio 4.1.1. The core issue you're seeing is org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:compileDebugJavaWithJavac', and the stack trace points to a breakdown in Gradle's Java compilation task. Let's walk through some less common but effective fixes since you've already tried standard solutions:
1. Align Gradle Plugin Version with Android Studio 4.1.1
Android Studio 4.1.1 requires the Gradle Plugin 4.1.1 (or compatible minor versions) to work correctly. Check your project-level build.gradle file and ensure the plugin version is set properly:
buildscript { repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.1.1" // Match this to your AS version } }
Also, update your Gradle wrapper (gradle/wrapper/gradle-wrapper.properties) to use a compatible distribution—for plugin 4.1.1, Gradle 6.5 or newer works best:
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
2. Enforce JDK 11 for Compilation
Android Studio 4.1.x shifted to JDK 11 as the default, but your project might still target an older JDK. Add these compileOptions to your app-level build.gradle to explicitly set JDK 11:
android { // ... existing config compileOptions { sourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11 } // If you use Kotlin, add this too: kotlinOptions { jvmTarget = "11" } }
3. Resolve Wearable Dependency Conflicts
Looking at your dependencies, you're using androidx.wear:wear:1.0.0 alongside compileOnly 'com.google.android.wearable:wearable:2.6.0'—these might conflict with the newer Android Studio version. Try updating the androidx.wear dependency to a compatible version:
implementation 'androidx.wear:wear:1.1.0' // Updated version matching Wearable 2.6.0 compileOnly 'com.google.android.wearable:wearable:2.6.0'
If this is a companion app for Wear OS, also ensure you have the wearApp configuration:
android { // ... wearApp project(':wear') // Include if you have a separate Wear module }
4. Perform a Deep Clean & Invalidate Caches (Thorough Version)
Since basic fixes didn't work, let's do a full reset:
- Close Android Studio
- Delete the
.gradlefolder in your project root - Delete the
buildfolders in both your app module and project root - Reopen Android Studio, go to
File > Invalidate Caches...and select "Invalidate and Restart"
5. Uncover Hidden Compilation Errors
The Gradle stack trace often hides the actual root cause (like a missing import or deprecated API). Run this terminal command in your project root to get detailed compilation logs:
./gradlew compileDebugJavaWithJavac --stacktrace -info
This will print raw output that might reveal specific failing classes or syntax issues the IDE isn't showing.
内容的提问来源于stack exchange,提问作者TotalBlankness

