Search code examples
javafluttergradle

where in Android Studio do I control which Java / Gradle-Version is in use?


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:\EigeneProgramme\flutter\API\flutter\packages\flutter_tools\gradle\src\main\groovy\app_plugin_loader.groovy' Unsupported class file major version 67

* 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 12s

┌─ 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                         │
│ https://flutter.dev/to/java-gradle-incompatibility.                                             │
│                                                                                                 │
│ 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                                                    │
│ ...\android\gradle\wrapper\gradle-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

This is the terminating statement.

Issuing 'flutter doctor --verbose' states:

[√] Flutter (Channel stable, 3.27.1, on Microsoft Windows [Version 10.0.26100.2605], locale de-DE)• Flutter version 3.27.1 on channel stable at C:\EigeneProgramme\flutter\API\flutter• Upstream repository https://github.com/flutter/flutter.git%E2%80%A2 Framework revision 17025dd882 (3 weeks ago), 2024-12-17 03:23:09 +0900• Engine revision cb4b5fff73• Dart version 3.6.0• DevTools version 2.40.2

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)• Android SDK at C:\Android-SDK• Platform android-35, build-tools 35.0.0• ANDROID_SDK_ROOT = C:\Android-SDK• Java binary at: C:\Program Files\Java\jdk-23\bin\java• Java version Java(TM) SE Runtime Environment (build 23.0.1+11-39)• All Android licenses accepted.

[√] Chrome - develop for the web• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Build Tools 2019 16.11.21)• Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools• Visual Studio Build Tools 2019 version 16.11.33027.164• Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 2024.2)• Android Studio at C:\Program Files\Android\Android Studio• Flutter plugin can be installed from:https://plugins.jetbrains.com/plugin/9212-flutter%E2%80%A2 Dart plugin can be installed from:https://plugins.jetbrains.com/plugin/6351-dart%E2%80%A2 Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)

[√] Connected device (4 available)• SM S918B (mobile) • R5CW10CTPAR • android-arm64 • Android 14 (API 34)• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.26100.2605]• Chrome (web) • chrome • web-javascript • Google Chrome 131.0.6778.205• Edge (web) • edge • web-javascript • Microsoft Edge 131.0.2903.70

[√] Network resources• All expected network resources are available.

• No issues found!

As far as I believe Java-Version 23 is in use, whereas .../android/settings.gradle uses Gradle-Version 8.1.0.

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.1.0" apply false
    id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

include ":app"

Following link provides a list stating Java-Versions with compatible Gradle-Versions - this is why I believe I habe a incompatibility-issue.

Ma question is: where in Android Studio do I control, which Java- / Gradle-Version is in use?

Following did not help:

  1. Creating a 'New Flutter-Project' leads to the same problem.
  2. After uninstalling Java and Android-Studio inclusive all folders and reinstallation after restart of Windows 11 did not change anything.
  3. the article at StackOverflow did not help either: Link

Solution

  • JAVA:

    In Windows: Java version used from the environment variables

    In MacOS: The JAVA_HOME variables in .bashrc or .zshrc file in MacOS.

    So to make sure you're using the right version or changing the version, you need to change these variables.

    Then in app-level gradle file build.gradle, ensure that compileOptions have your desired Java version like this:

    android {
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_21
            targetCompatibility JavaVersion.VERSION_21
        }
    }
    

    Then in Android Studio, go to File > Project Structure > SDK Location and under JDK Location, set the path to the desired JDK.

    Gradle:

    1. Open gradle/wrapper/gradle-wrapper.properties.

    2. Update the distributionUrl with the desired Gradle version: properties

      distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip

    3. Sync Gradle

    4. Update Gradle Plugin Version by opening build.gradle (Project-level)

    dependencies { classpath 'com.android.tools.build:gradle:8.2.1' }