skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    Here's how I resolved the issue

    1. I ran the following command to gather detailed information:
        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.
    
    1. 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
      
    2. I found the path to Java 17 in the output:

      /Users/user/Library/Java/JavaVirtualMachines/openjdk-17.0.2/Contents/Home
      
    3. 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
      

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search