flutterandroid-studiogradleandroid-gradle-pluginflutter-dependencies

Why does Flutter fail to compile as soon as any package is added to the pubspec.yaml in Android Studio?


I have a basic Flutter project in Android Studio, but whenever I add any external package (example: google_fonts), the compilation fails with a Gradle error.

Launching lib\main.dart on AOSP on IA Emulator in debug mode...
Running Gradle task 'assembleDebug'...

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':path_provider_android:compileDebugJavaWithJavac'.
> Could not resolve all files for configuration ':path_provider_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: C:\Users\dimitri\AppData\Local\Android\sdk\platforms\android-34\core-for-system-modules.jar.
         > Error while executing process C:\Program Files\Android\Android Studio\jbr\bin\jlink.exe with arguments {--module-path C:\Users\dimitri\.gradle\caches\transforms-3\4a46fc89ed5f9adfe3afebf74eb8bfeb\transformed\output\temp\jmod --add-modules java.base --output C:\Users\dimitri\.gradle\caches\transforms-3\4a46fc89ed5f9adfe3afebf74eb8bfeb\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 26s
Error: Gradle task assembleDebug failed with exit code 1

Here are the details about the installed versions:
Flutter/Dart Versions:

flutter --version
Flutter 3.24.5 • channel stable • https://github.com/flutter/flutter.git
Framework • revision dec2ee5c1f (5 days ago) • 2024-11-13 11:13:06 -0800
Engine • revision a18df97ca5
Tools • Dart 3.5.4 • DevTools 2.37.3

Gradle/Java versions:

PS E:\DevEnv\flutter\projects\doit\lesson01\android> ./gradlew -version

------------------------------------------------------------
Gradle 8.3
------------------------------------------------------------

Build time:   2023-08-17 07:06:47 UTC
Revision:     8afbf24b469158b714b36e84c6f4d4976c86fcd5

Kotlin:       1.9.0
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          17.0.12 (Oracle Corporation 17.0.12+8-LTS-286)
OS:           Windows 11 10.0 amd64

Pubspec.Yaml

name: lesson01
description: "DoIt Lesson Project."
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
  sdk: ">=3.5.0 <4.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.8
  google_fonts: ^6.2.1

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^5.0.0

flutter:
  uses-material-design: true

Kindly advice where is the problem source and how can i fix it?


Solution

  • I can't reproduce the error in my side by simply adding the google_fonts because I didn't encountered the exception after I add that plugin to my pubspec.yaml (i.e., built successful).

    So I've decided to represent to you my configurations.

    Under the gradle-wrapper.properties:

    distributionUrl=https://services.gradle.org/distributions/gradle-8.10.2-bin.zip, or use the latest one, or if necessary, downgrade it. Here is the reference

    TIPS:

    I also do this way to update the distributionUrl,

    PS: C:\to_your_project_directory> cd android

    PS: C:\to_your_project_directory\android> .\gradlew clean

    PS: C:\to_your_project_directory\android> ./gradlew wrapper --gradle-version version_here(x.x.x) --distribution-type all

    or

    PS: C:\to_your_project_directory\android> ./gradlew wrapper --gradle-version version_here(x.x.x)

    or

    PS: C:\to_your_project_directory\android> ./gradlew wrapper --gradle-version latest

    Under the settings.gradle:

    id "org.jetbrains.kotlin.android" version "2.0.21" apply false //if necessary, downgrade it. Here is the reference

    Under the android\build.gradle:

    buildscript {
        ext.kotlin_version = '2.0.21' //if necessary, downgrade it.
        repositories {
            google()
            mavenCentral()
        }
        dependencies {
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    

    Also, consider this compatibility matrix

    Update:

    Please use these approaches if necessary:

    In your Android Studio, Go to File > Invalidate Caches

    Then you can do some troubleshooting practices:

    PS: C:\to_your_project_directory> flutter clean

    PS: C:\to_your_project_directory> flutter pub get

    PS: C:\to_your_project_directory> flutter pub cache repair

    PS: C:\to_your_project_directory> cd android

    PS: C:\to_your_project_directory\android> .\gradlew clean

    Additionals:

    PS: C:\to_your_project_directory\android> ./gradlew build --stacktrace --info

    To see what plugins or packages introduces the issues

    NOTE:

    The goal is to make sure that after you configure build.gradle, gradle-wrapper.properties, etc. You also clean the caches, then rebuild them.

    I hope it helps!