flutterandroid-studio

Android studio 2024.2.1 flutter build error app_plugin_loader.groovy Unsupported class file major version 65


Upgraded to Android studio Android studio 2024.2.1. Even did a fresh install.

Made a new Flutter project. When I run it, I get this error.

Launching lib\main.dart on Pixel 8 in debug mode...
Running Gradle task 'assembleDebug'...

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:\Users\Rok\Flutter SDK\flutter\packages\flutter_tools\gradle\src\main\groovy\app_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 5s

┌─ 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/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                                                    │
│ C:\Users\Rok\FlutterProjects\Test\test20241009\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

Can run Android Java project without problems.

Tried a full uninstall and reinstall of Android Studio.


Solution

  • The same happened to me today, after installing Android Studio Ladybug 2024.2.1.

    As far as I understand, this is related to the new Java 21 version installed by AS.

    c:\android-studio\jbr\bin\java.exe --version
    openjdk 21.0.3 2024-04-16
    OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    OpenJDK 64-Bit Server VM (build 21.0.3+-12282718-b509.11, mixed mode)
    

    Solution

    1. Update gradle-wrapper.properties

    C:\project_path\android\gradle\wrapper\gradle-wrapper.properties

    distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip
    
    1. Update android/build.gradle to gradle:8.7.0
    buildscript {
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            THIS ->
            classpath 'com.android.tools.build:gradle:8.7.0'
        }
    }
    
    1. Update settings.gradle to gradle:8.7.0
    plugins {
        id "dev.flutter.flutter-plugin-loader" version "1.0.0"
        
        THIS ->
        id "com.android.application" version "8.7.0" apply false
        
        id "org.jetbrains.kotlin.android" version "1.9.0" apply false
        id "com.google.gms.google-services" version "4.4.1" apply false
        id "com.google.firebase.crashlytics" version "3.0.0" apply false
        id 'com.google.firebase.firebase-perf' version '1.4.2' apply false
        id "com.huawei.agconnect" version "1.9.1.303" apply false
    }
    
    1. Update NDK in Android Studio if you get a build error like below
    A problem occurred configuring project ':fvp'.
    > com.android.builder.sdk.InstallFailedException: Failed to install the following SDK components:
          ndk;27.0.12077973 NDK (Side by side) 27.0.12077973
      Install the missing components using the SDK manager in Android Studio.
    
    1. Manually enter the new NDK (if required)
    android {
        namespace 'my app.com'
        
        compileSdkVersion 34
                            
        ndkVersion "28.0.12433566"
    ...
    
    1. Update libs (if required) ->

    pubspec.yaml

    audio_session: ^0.1.21
    chewie: ^1.8.5
    video_player: ^2.9.2
    just_audio: ^0.9.41
    just_audio_background: ^0.0.1-beta.13
    
    Execution failed for task ':audio_session:compileDebugJavaWithJavac'.
    
    1. Manually modify video_player_android (if required)
    c:\Users\<username>\AppData\Local\Pub\Cache\hosted\pub.dev\video_player_android-2.4.12\android\build.gradle 
    
    see below link
    https://github.com/flutter/flutter/issues/148478#issuecomment-2358072537
    
    THIS -> def args = ["-Xlint:deprecation","-Xlint:unchecked"]
    instead do def args = ["-Xlint:deprecation","-Xlint:unchecked","-Werror"]
    
    THIS -> warningsAsErrors false
    instead of warningsAsErrors true
    
    Warnung: [options] Verwenden Sie -Xlint:-options, um Warnungen zu veralteten Optionen zu unterdrücken.
    Fehler: Warnungen gefunden und -Werror angegeben
    1 Fehler
    3 Warnungen
    
    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Execution failed for task ':video_player_android:compileDebugJavaWithJavac'.
    > Compilation failed; see the compiler error output for details.
    
    1. Build project
    flutter clean
    flutter pub get
    flutter run
    

    First build time may take longer (about 30 minutes for my project), so be patient.

    1. Sometimes you may need to manually delete old packages from cache c:\Users<username>\AppData\Local\Pub\Cache\hosted\pub.dev\

    2. You are done! Easy peasy!