Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fix Android Compilation issues #3303

Open
miquelbeltran opened this issue Oct 5, 2024 · 1 comment
Open

How to fix Android Compilation issues #3303

miquelbeltran opened this issue Oct 5, 2024 · 1 comment
Labels
all android compile Compilation error

Comments

@miquelbeltran
Copy link
Member

miquelbeltran commented Oct 5, 2024

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
@miquelbeltran miquelbeltran added the question Further information is requested label Oct 5, 2024
@miquelbeltran miquelbeltran changed the title [Question]: placeholder Android Compilation issues Oct 5, 2024
@fluttercommunity fluttercommunity locked as resolved and limited conversation to collaborators Oct 5, 2024
@miquelbeltran miquelbeltran pinned this issue Oct 5, 2024
@miquelbeltran miquelbeltran added all android compile Compilation error and removed question Further information is requested labels Oct 5, 2024
@miquelbeltran miquelbeltran reopened this Oct 5, 2024
@miquelbeltran miquelbeltran changed the title Android Compilation issues How to fix Android Compilation issues Oct 5, 2024
@miquelbeltran
Copy link
Member Author

This ticket is locked to avoid discussion in here. If you have done everything in this guide and still face compilation issues, create a ticket using the Compile template: https://github.com/fluttercommunity/plus_plugins/issues/new?assignees=&labels=compile%2Ctriage&projects=&template=COMPILATION.yaml&title=%5BCompile%5D%3A+ and include all your gradle files and the gradlew version number.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
all android compile Compilation error
Projects
None yet
Development

No branches or pull requests

1 participant