I’m currently working on a Flutter project, but encountering build operation errors related to JDK versions. Android toolchain is using JDK 21.0.3, whereas my system has only JDK 18.0.2.1 installed. My JAVA_HOME
and env path are configured to use JDK 18.0.2.1. I suspect that this mismatch might be causing errors with tasks such as generateDebugUnitTestConfig
for multiple plugins (e.g., flutter_plugin_android_lifecycle
, image_picker_android
, etc.) (I might be wrong, please correct me if I’m wrong).
I’m encountering an issue when trying to run my Flutter project with the command flutter run. I receive the following error message:
FAILURE: Build failed with an exception.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':gradle:compileGroovy'.
> BUG! exception in phase 'semantic analysis' in source unit 'C:srcflutterpackagesflutter_toolsgradlesrcmaingroovyapp_plugin_loader.groovy'
Unsupported class file major version 65
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 14s
Running Gradle task 'assembleDebug'... 15.3s
Flutter Fix
[!] Your project's Gradle version is incompatible with the Java version that Flutter is using for Gradle.
If you recently upgraded Android Studio, consult the migration guide at docs.flutter.dev/go/android-java-gradle-error.
Otherwise, to fix this issue, first, check the Java version used by Flutter by running `flutter doctor --verbose.
Then, update the Gradle version specified in E:App
Flutterlearn4_flutterandroidgradlewrappergradle-wrapper.properties to be compatible with that Java version. See the link below for more information on compatible Java/Gradle versions: https://docs.gradle.org/current/userguide/compatibility.html#java
Error: Gradle task assembleDebug failed with exit code 1
What I Tried:
- I ran
flutter doctor -v
, and the output shows that the JDK used by Android toolchain is version 21.0.3. However, I only have JDK version 18.0.2.1 installed on my device.
[√] Flutter (Channel stable, 3.19.1, on Microsoft Windows [Version 10.0.19045.5011], locale en-US)
• Flutter version 3.19.1 on channel stable at C:srcflutter
• Upstream repository ``https://github.com/flutter/flutter.git
• Framework revision abb292a07e (8 months ago), 2024-02-20 14:35:05 -0800
• Engine revision 04817c99c9
• Dart version 3.3.0
• DevTools version 2.31.1
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at C:UsersKenAppDataLocalAndroidsdk
• Platform android-34-ext10, build-tools 34.0.0
• Java binary at: C:Program FilesAndroidAndroid Studiojbrbinjava
• Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
• All Android licenses accepted.
- I attempted to sync the Gradle project, but I received multiple build operation failures related to tasks for various plugins (e.g., flutter_plugin_android_lifecycle, image_picker_android).
Multiple build operations failed.
Could not create task ':flutter_plugin_android_lifecycle:generateDebugUnitTestConfig'.
Could not create task ':image_picker_android:generateDebugUnitTestConfig'.
Could not create task ':local_auth_android:generateDebugUnitTestConfig'.
Could not create task ':path_provider_android:generateDebugUnitTestConfig'.
Could not create task ':shared_preferences_android:generateDebugUnitTestConfig'.
Could not create task ':flutter_plugin_android_lifecycle:generateDebugUnitTestConfig'.
this and base files have different roots: E:App-Flutterlearn4_flutterbuildflutter_plugin_android_lifecycle and C:UsersKenAppDataLocalPubCachehostedpub.devflutter_plugin_android_lifecycle-2.0.19android.
Questions:
- Is the mismatch between JDK 21.0.3 and 18.0.2.1 the root cause of the error?
- Do I need to move my project folder to the path indicated in the error message, or is there a better solution?
- How can I ensure that Android toolchain uses the correct JDK version (18.0.2.1) instead of 21.0.3?
- HOW CAN I FIX THIS PROBLEM?
addition info:
File gradle-wrapper.properties
:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-7.5-all.zip
android/app/build.gradle:
plugins {
id "com.android.application"
id "kotlin-android"
id "dev.flutter.flutter-gradle-plugin"
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'
}
android {
namespace "com.example.learn4_flutter"
compileSdkVersion flutter.compileSdkVersion
ndkVersion flutter.ndkVersion
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.learn4_flutter"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '../..'
}
dependencies {}
android/build.gradle:
buildscript {
ext.kotlin_version = '1.7.10'
repositories {
google()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
rootProject.buildDir = '../build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
Any assistance would be greatly appreciated!
2
Answers
flutter first finds the android studio one, in case java 21, try setting flutter’s java to 17 in:
flutter config --java-dir={YOUR-JAVA-17-HOME}
Use this : flutter config –jdk-dir={YOUR-JAVA-17-HOME}
for me it is : flutter config –jdk-dir=/Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home