Skip to content

How to fix Android Compilation issues #3303

Open
@miquelbeltran

Description

@miquelbeltran

This ticket contains some tips to help mitigate recent Android compilation issues.

Before attempting any solution, ensure that your project has been migrated to the new Android project structure:

You can also find a reference to the different Android Gradle Plugin (AGP) and Gradle versions here:

There is also a main ticket on flutter/flutter discussing this issue:

What causes the issue

The majority of issues come from using the latest Android Studio (Ladybug 2024.2.1), which ships the Java SDK 21.

e.g. running flutter doctor -v:

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /home/miquel/Android/Sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /home/miquel/Android/Sdk
    • Java binary at:
      /home/miquel/.local/share/JetBrains/Toolbox/apps/android-studio/jbr/bin/ja
      va
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

The simplest solution is to downgrade Android Studio and use Java 17, now, let's attempt to fix this with the latest Android Studio version.

Compilation issues

The first compilation issue you will see with a newly created project with Flutter 3.24.3 and adding connectivity_plus (or any other Android plugin) is:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':connectivity_plus:compileReleaseJavaWithJavac'.
> Could not resolve all files for configuration ':connectivity_plus: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: /home/miquel/Android/Sdk/platforms/android-34/core-for-system-modules.jar.
         > Error while executing process /home/miquel/.local/share/JetBrains/Toolbox/apps/android-studio/jbr/bin/jlink with arguments {--module-path /home/miquel/.gradle/caches/transforms-3/4a46fc89ed5f9adfe3afebf74eb8bfeb/transformed/output/temp/jmod --add-modules java.base --output /home/miquel/.gradle/caches/transforms-3/4a46fc89ed5f9adfe3afebf74eb8bfeb/transformed/output/jdkImage --disable-plugin system-modules}

Checking project dependencies

The project comes with the following AGP:

id "com.android.application" version "8.1.0" apply false

And the following gradle wrapper:

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

Checking plugin requirements

The plugins README lists the following version requirements:

    Flutter >=3.7.0
    Dart >=3.2.0 <4.0.0
    iOS >=12.0
    MacOS >=10.14
    Android compileSDK 34
    Java 17
    Android Gradle Plugin >=8.3.0
    Gradle wrapper >=8.4

Upgrading dependencies

To apply these, change the following:

Open android/app/build.gradle and change:

    compileOptions {
      sourceCompatibility JavaVersion.VERSION_17
      targetCompatibility JavaVersion.VERSION_17
    }

    kotlinOptions {
        jvmTarget = JavaVersion.VERSION_17
    }

Open android/gradle/wrapper/gradle-wrapper.properties and change:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-all.zip

Open android/settings.gradle and change:

    id "com.android.application" version "8.3.1" apply false

All these follow the minimum version requirements as specified by the plugin README file.

Now the project compiles again:

Running Gradle task 'assembleRelease'...                           13.4s
✓ Built build/app/outputs/flutter-apk/app-release.apk (18.7MB)

NDK Error

You may see the following error on screen:

Your project is configured with Android NDK 23.1.7779620, but the following plugin(s) depend on a different Android NDK version:
- connectivity_plus requires Android NDK 25.1.8937393
Fix this issue by using the highest Android NDK version (they are backward compatible).
Add the following to /home/miquel/tmp/connectivity_error_2/android/app/build.gradle:

    android {
        ndkVersion = "25.1.8937393"
        ...
    }

Seems that you can ignore this error, as the project still compiles.

Changing the Java SDK

You can try to set the Jdk path using the flutter config --jdk-dir command. I haven't tried that but it's something you can consider if everything else fails.

  1. Download and install the JDK 17.
  2. Use flutter config --jdk-dir to configure it.
  3. Check the version again with flutter doctor -v

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions