Search code examples
flutterandroid-studioandroid-sdk-managerandroid-sdk-2.3

flutter: How to make Android studio and Android SDK compatible?


It seems that upgrading the Android SDK from within Android studio installs incompatible stuff and I'm left unable to build. Anyone know how to fix this nonsense?

The Java SDK comes with the Android studio is seems. How do I choose a new version exactly? Android studio seems to no provide a JDK update/downgrade version option in the settings.

$ flutter create --platforms android
...
Your application code is in ./lib/main.dart.

The configured version of Java detected may conflict with the Gradle version in your new Flutter app.

To keep the default AGP version Gradle version 8.3, download a compatible Java version
(Java 17 <= (Java 17 <= compatible Java version < Java 21) Java version < Java 21). Configure this Java version
globally for Flutter by running:

  flutter config --jdk-dir=<JDK_DIRECTORY>


Alternatively, to continue using your configured Java version, update the Gradle
version specified in the following file to a compatible Gradle version (compatible Gradle version range: 8.4 - 8.7):
/home/sir/software/.../android/gradle/wrapper/gradle-wrapper.properties

You may also update the Gradle version used by running
`./gradlew wrapper --gradle-version=<COMPATIBLE_GRADLE_VERSION>`.

See
https://docs.gradle.org/current/userguide/compatibility.html#java for details
on compatible Java/Gradle versions, and see
https://docs.gradle.org/current/userguide/gradle_wrapper.html#sec:upgrading_wrapper
for more details on using the Gradle Wrapper command to update the Gradle version
used.

It also seems that some versions of intellij are incompatible with some versions of Java (I have Android Studio Ladybug | 2024.2.1 Patch 2). Starting from which version does Gradle support Java 21?

You'd think the integrated installer would provide guidance. But whatever.

~/my_app$ flutter doctor -v
[✓] Flutter (Channel master, 3.27.0-1.0.pre.25, on Ubuntu 22.04.3 LTS 6.8.0-48-generic, locale en_US.UTF-8)
    • Flutter version 3.27.0-1.0.pre.25 on channel master at /home/sir/software/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c667c97c3f (5 weeks ago), 2024-10-11 17:55:42 -0400
    • Engine revision b39fec6d97
    • Dart version 3.6.0 (build 3.6.0-334.2.beta)
    • DevTools version 2.40.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /home/sir/Android/Sdk
    • Platform android-35, build-tools 34.0.0
    • ANDROID_HOME = /home/sir/Android/Sdk
    • Java binary at: /home/sir/software/android-studio/jbr/bin/java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 14.0.0-1ubuntu1.1
    • cmake version 3.25.1
    • ninja version 1.10.1
    • pkg-config version 0.29.2

[✓] Android Studio (version 2024.2)
    • Android Studio at /home/sir/software/android-studio
    • Flutter plugin version 82.1.3
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • android-studio-dir = /home/sir/software/android-studio
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)

[✓] VS Code (version 1.85.1)
    • VS Code at /usr/share/code
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] VS Code (version 1.86.0-insider)
    • VS Code at /usr/share/code-insiders
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 22.04.3 LTS 6.8.0-48-generic
    • Chrome (web)    • chrome • web-javascript • Google Chrome 117.0.5938.149

[✓] Network resources
    • All expected network resources are available.

The internet suggest upgrading gradle (to 8.11 for Java 21) as follows... but then goes off and uses the incorrect Java.

android$ ./gradlew wrapper --gradle-version=8.11

Welcome to Gradle 8.3!

Here are the highlights of this release:
 - Faster Java compilation
 - Reduced memory usage
 - Support for running on Java 20

For more details see https://docs.gradle.org/8.3/release-notes.html

Starting a Gradle Daemon, 1 busy and 1 incompatible Daemons could not be reused, use --status for details

FAILURE: Build failed with an exception.

* Where:
Build file '/home/sir/software/..../android/app/build.gradle' line: 2

* What went wrong:
An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
      Your current JDK is located in /usr/lib/jvm/java-11-openjdk-amd64
      You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

* 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 22s
4 actionable tasks: 2 executed, 2 up-to-date

and also:

$ ./gradlew wrapper --gradle-version=8.7

Welcome to Gradle 8.11!

Here are the highlights of this release:
 - Parallel load and store for Configuration Cache
 - Java compilation errors at the end of the build output
 - Consolidated report for warnings and deprecations

For more details see https://docs.gradle.org/8.11/release-notes.html

Starting a Gradle Daemon, 1 incompatible and 1 stopped Daemons could not be reused, use --status for details
[Incubating] Problems report is available at: file:///home/sir/software/....app/android/build/reports/problems/problems-report.html

FAILURE: Build failed with an exception.

* Where:
Build file '/home/sir/software/...app/build.gradle' line: 2

* What went wrong:
An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
   > Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
      Your current JDK is located in /usr/lib/jvm/java-11-openjdk-amd64
      You can try some of the following options:
       - changing the IDE settings.
       - changing the JAVA_HOME environment variable.
       - changing `org.gradle.java.home` in `gradle.properties`.

* 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.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.11/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 23s
4 actionable tasks: 2 executed, 2 up-to-date

Solution

  • Have you tried to install Java 17 from Java SE 17 Archive Downloads?

    Then replace the current PATH and JAVA_HOME's directories with Java 17 directory

    Since you encountered an exception,

    Android Gradle plugin requires Java 17 to run.

    So you have to make sure that you're using Java 17 SDK.

    It works for me by restarting my machine after I installed Java 17 and replacing the system variables PATH and JAVA_HOME and also by making sure that your Flutter project is configured for your updated environment configurations.

    I verified it by creating a new Flutter application to see the updated build configuration files. It runs properly as you expected after making the approaches mentioned above.

    Also, check your currently installed Java by executing this command in the command prompt.

    Java --version

    My installed Java has LTS, by the way,.

    To see where your Java is installed based on the system variables' PATH,

    where java

    Note: I hope you don't forget to do File > Invalidate Caches if necessary.

    ADDITIONALS:

    If you're working with multiple Flutter projects, you may read this answer.

    Conclusion:

    I validated the proper configuration of the files in my Flutter project by testing its platform-specific runtime. So I can use it as a reference for my actual project, particularly by inspecting the plugin/packages to see which might cause exceptions.

    Update:

    Since you're using Android Studio,

    Please go to File > Project Structure > Project tab > SDK

    Make sure that you set it to 17, like this one

    SDK

    Or by using a terminal in Android Studio instead, like this one

    PS C:\PATH_TO_YOUR_PROJECT> flutter config --jdk-dir="C:\Program Files\Java\jdk-17"
    

    Again, don't forget to restart your IDE if you find it necessary.

    I hope it helps!