Reputation: 644
I'm encountering an issue while trying to build an Android app bundle (appbundle
) for release in my Flutter project on Android studio. I'm getting the following error message:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':package_info_plus:compileReleaseJavaWithJavac'.
> error: invalid source release: 17
Here's some details of my setup:
Java Home:
/usr/libexec/java_home -V
Matching Java Virtual Machines (3):
20.0.1 (x86_64) "Oracle Corporation" - "Java SE 20.0.1" /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home
17.0.2 (x86_64) "Oracle Corporation" - "OpenJDK 17.0.2" /Users/user/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home
1.8.391.13 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home
Environment Configuration (echo $JAVA_HOME
):
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Flutter Version:
flutter --version
Flutter 3.19.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 54e66469a9 (2 days ago) • 2024-04-17 13:08:03 -0700
Engine • revision c4cd48e186
Tools • Dart 3.3.4 • DevTools 2.31.1
Android Project Configuration (android/app/build.gradle
):
android {
compileSdkVersion 34
android {
ndkVersion "25.2.9519653"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
I have been facing this issue after upgrading to macOS Sonoma 14.4.1
, and I'm using an M3 Mac
.
I've tried various solutions from stack overflow Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio
Could someone please provide guidance on how to resolve this issue? Any insights or suggestions would be greatly appreciated.
Upvotes: 15
Views: 21174
Reputation: 1055
This issue occurs when a plugin requires JDK version 17 or higher, but your Flutter setup is using a lower JDK version.
You can check the JDK version Flutter is using with the following command:
flutter doctor -v
Check the output:
This is the JDK version Flutter is currently using. If it's lower than 17, you need to update Flutter's JDK version.
Download JDK 17 and configure the JAVA_HOME
environment variable to point to the path
of JDK 17.
then verify it with the command:
javac --version
Reconfigure the JDK for Flutter with the command:
flutter config --jdk-dir=$JAVA_HOME
Verify again using: (You may need to restart your IDE to apply the changes).
flutter doctor -v
If the JDK version is now 17, congratulations! You have successfully updated the JDK.
Upvotes: 0
Reputation: 371
For those who are experiencing this issue on Github actions, try checking If JDK is installed and It's version with (Note that It checks if version is 17 apply changes for your needs) :
- name: Check java found
id: check_java
shell: bash
run: |
if java --version 2>&1 | grep -q "17"; then
echo "installed=true" >> "$GITHUB_OUTPUT"
else
echo "installed=false" >> "$GITHUB_OUTPUT"
fi
Also handle logic like this:
- name: Install java
if: steps.check_java.outputs.installed == 'false'
uses: actions/setup-java@v3
with:
distribution: "zulu"
java-version: "17.x"
cache: "gradle"
Upvotes: 0
Reputation: 329
I have fixed this error with flutter config --jdk-dir /your/path/jvm/java-17-openjdk/
This is a global config and it changes the jdk for all of your projects. You can unset the config with flutter config --jdk-dir ''
Upvotes: 1
Reputation: 21
I found the error is caused by the Android Gradle Plugin (AGP) version in the Flutter app, as defined in android/settings.gradle: plugins { ...... id "com.android.application" version "8.1.0" apply false ...... } The version "8.1.0" requires Java 17 to build, changing it to "7.0.0" will work with Java 8.
Upvotes: 0
Reputation: 169
So in my experience, all the SO posts claim a resolution is to configure via Gradle/project settings OR via JAVA_HOME env variable. I found that I could open my flutter projects android folder and configure the Gradle JDK and have a successful build, but when opening the greater flutter project I still could not have a successful build. All my configurations correctly pointed to the correct JDK, but when the flutter project would build it would still be stuck on the incorrect version! Finally I came across a random GitHub issue which referenced a "flutter config" command. This command contains information that seems to explain why everyone has to update Android studio in order to resolve the issue for flutter projects, specifically. If this setting is left unconfigured, the following precedence takes effect:
Default - JDK bundled with Android studio
THEN JAVA_HOME env variable
THEN directory containing java binary found in the users path
Upvotes: 1
Reputation: 1
I faced this issue when I rerun my app after a long time.
I resolved this error by adding this line into gradle.properties org.gradle.java.home=/<path to your .jdks folder>/.jdks/corretto-17.0.11
Upvotes: 0
Reputation: 9
Upvotes: 0
Reputation: 637
I encountered the same error on my Azure Pipeline. I fixed it by changing the JDK version.
I modified Java Tool Installer task:
- task: JavaToolInstaller@0
inputs:
versionSpec: '11'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
to the following:
- task: JavaToolInstaller@0
inputs:
versionSpec: '17'
jdkArchitectureOption: 'x64'
jdkSourceOption: 'PreInstalled'
Upvotes: 4
Reputation: 19
I downgraded my current java version from 21 to 17 and the problem was solved for me.
my app/build gradle
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = '17'
}
kotlin {
jvmToolchain(17)
}
my gradle.properties
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-
17.0.11.jdk/Contents/Home
my gradle wrapper properties
distributionUrl=https://services.gradle.org/distributions/gradle-8.5-all.zip
Upvotes: 0
Reputation: 644
Here's how I resolved the issue
cd android
./gradlew assembleDebug --info
What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.application'.
> Android Gradle plugin requires Java 11 to run. You are currently using Java 1.8.
Your current JDK is located in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changing org.gradle.java.home in gradle.properties.
Locate Java installation directory on my computer by running the following command:
/usr/libexec/java_home -V
Matching Java Virtual Machines (3):
20.0.1 (x86_64) "Oracle Corporation" - "Java SE 20.0.1" /Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home
17.0.2 (x86_64) "Oracle Corporation" - "OpenJDK 17.0.2" /Users/user/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home
1.8.391.13 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
/Library/Java/JavaVirtualMachines/jdk-20.jdk/Contents/Home
I found the path to Java 17 in the output:
/Users/user/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home
Then, I set the Java 17 path by adding the following line to the gradle.properties
file in my Android project directory:
org.gradle.java.home=/Users/user/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home
Upvotes: 7
Reputation: 307
Are you using two Java-related environment configurations in your '.zshrc' or '.bash_profile'?
Note: export JAVA_HOME=$(/usr/libexec/java_home -v 1.8) sets Java_home to Java 1.8 and export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home sets to OpenJDK 17, error maybe due to conflicting java version or configurations
you can resolve it by,
1 Check Java version using echo $JAVA_HOME make sure it uses Java 1.8 version
2 Update your Gradle Conf in your Android project's 'build.gradle' file, ensure that the 'compileOptions' and 'kotlinOptions' are set to use Java 1.8:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
3 Remove Conflicting JAVA_HOME Since there are two 'JAVA_HOME' configurations in your code remove or comment out the one that sets it to OpenJDK 17 in your '.zshrc' or '.bash_profile'
4 Restart Android studio
Upvotes: 0