fluttercommunity / fluttercommunity/plus_plugins

How to fix Android Compilation issues

Aperta
#3,303 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
all android compile
Lingua principale
Dart
Stelle
1.9k
Fork
1.3k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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:

- See: https://docs.flutter.dev/release/breaking-changes/flutter-gradle-plugin-apply

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

- See: https://developer.android.com/build/releases/gradle-plugin

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

- https://github.com/flutter/flutter/issues/156111

### 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:

```groovy
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:

```groovy
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`

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia esaminando la guida alla migrazione collegata nell’issue, i requisiti di Android Gradle Plugin e i passaggi per la risoluzione dei problemi relativi a android/app/build.gradle, android/gradle/wrapper/gradle-wrapper.properties e android/settings.gradle. Il lavoro sarà completo quando sarà disponibile una spiegazione chiara e mantenuta dei controlli di compilazione Android, NDK e JDK e le modifiche di versione documentate saranno state verificate con flutter doctor -v e una compilazione release riuscita.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
android, dart, flutter, java
Ambito
documentation, mobile-dev
Tipo di issue
Documentazione
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
32/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.