javaflutterdartandroid-studio

Flutter 3.24.3 problem with Android Studio Ladybug | 2024.2.1


When I update Android studio to last version "Ladybug 2024.2.1" an error occur whenever I run application in android emulator or physical phone, this problem shows an error with a specific library but in fact it occur with all libraries, I know that because every library tell me there is an error with, I just try to remove it then try running the code again.

This is the error:

FAILURE: Build failed with an exception.

What went wrong: Execution failed for task ':google_sign_in_android:compileDebugJavaWithJavac'. Could not resolve all files for configuration ':google_sign_in_android:androidJdkImage'. Failed to transform core-for-system-modules.jar to match attributes {artifactType=_internal_android_jdk_image, org.gradle.libraryelements=jar, org.gradle.usage=java-runtime}. Execution failed for JdkImageTransform: /Users/zaydahmad/Library/Android/sdk/platforms/android-34/core-for-system-modules.jar. Error while executing process /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/jlink with arguments {--module-path /Users/zaydahmad/.gradle/caches/transforms-3/fd4e2b69c2691a24d99d2b3c10c60f94/transformed/output/temp/jmod --add-modules java.base --output /Users/zaydahmad/.gradle/caches/transforms-3/fd4e2b69c2691a24d99d2b3c10c60f94/transformed/output/jdkImage --disable-plugin system-modules}

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 9s Error: Gradle task assembleDebug failed with exit code 1

I have update all the libraries to the last version. I have remove every library appear with that error, it doesn't work.

maybe these files help you solve the problem:

android/app/build.gradle

android {
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

android/gradle/wrapper/gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip

android/build.gradle

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = "../build"
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(":app")
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

Solution

  • The issue comes from Android Studio Ladybug having bundled a version of Java that isn't fully backward compatible with Flutter's plugin ecosystem. The solution is to separately install a version of Java (such as Java 17) and configure your project to use it.

    1. Install Java SDK 17 using your method of choice* (either manually or using a runtime manager such as asdf).

    2. Run this command pointing at the location where you installed Java SDK 17:

      flutter config --jdk-dir <path-to-java-sdk-17-home-directory>
      
    3. (Optional, but some plugins may require it) Open android/app/build.gradle and update your Java compatibility options:

      android {
          compileOptions {
            sourceCompatibility JavaVersion.VERSION_17
            targetCompatibility JavaVersion.VERSION_17
          }
      
          // If using Kotlin
          kotlinOptions {
              jvmTarget = JavaVersion.VERSION_17
          }
      }
      

    Further reading:


    *: If you install the JDK via Homebrew, you may need to update your PATH and/or JAVA_HOME as laid out in @prensj's answer.