Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter build error: "invalid source release: 17" when building app bundle

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.

like image 368
Gladiator Avatar asked Jan 28 '26 06:01

Gladiator


2 Answers

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
    
like image 181
Gladiator Avatar answered Jan 30 '26 00:01

Gladiator


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'
like image 21
oyenigun Avatar answered Jan 29 '26 23:01

oyenigun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!