DEV Community

Gabriel Aguirre
Gabriel Aguirre

Posted on

Android Studio Ladybug broke my build.

TL;DR - Updating Android Studio to the latest version can sometimes break your build, especially when using Flutter. I learned this the hard way recently after updating to Android Studio Ladybug (2024.2.1), which uses JDK 21. Unfortunately, this version is incompatible with Gradle below 8.5, causing build failures. To fix this, you can downgrade AS or use the command flutter config --jdk-dir to use a previous JDK version.

A few days ago, after updating one of my favorite tools, Android Studio, I ran into an interesting (time-consuming) problem. I've never thought updating an IDE would also break my build in another editor. I mean, I thought that only happened with Xcode.

I have this bad habit of updating Android Studio as soon as the latest versions are available (Toolbox made it so easy), even though I'm using VS Code a lot more these days.

But anyway, after I updated to AS Ladybug (2024.2.1), I could no longer run my app from VS Code. I've got this message:

Flutter Fix
[!] Your project's Gradle version is incompatible with the Java version that Flutter is using for Gradle.
Enter fullscreen mode Exit fullscreen mode

Okay, Flutter is so cool that it showed me the problem. But how do I fix this? Which version should I use? Why are there two different versions of Gradle?

Gradle and The AGP

First off, Gradle is the build system selected by the Android team at Google as the official tool for generating Android APKs, Bundles, or libraries. Gradle exists outside Android and can be used with other technologies.

The Android team created the Android Gradle Plugin (AGP) to tell Gradle how to build Android-related projects. This plugin will register all tasks needed to generate Android-related content.

You set the version of this plugin inside your project gradle.build file (not the one inside the app folder) using com.android.tools.build:gradle or inside settings.gradle with com.android.application. Something like this:

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version '7.4.0' apply false // AGP
    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

settings.gradle
Enter fullscreen mode Exit fullscreen mode

Inside the gradle/wrapper folder, there is a file called gradle-wrapper.properties. The distribution version of Gradle that our project uses is specified in the distributionUrl param. For example:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Enter fullscreen mode Exit fullscreen mode

Here is the official compatibility matrix between AGP and Gradle. I upgraded these versions to >= 8.5. Let's see if that fixed my build.

The JDK

Just a quick recap: the Java Development Kit (or JDK for friends) is a set of tools, libraries, and components used to build Java applications. In our case, it is used by Gradle to build Android apps. Even though if we use Kotlin (Kotlin produces Java bytecode), we still need to use the JDK, so setting the correct version of the JDK is fundamental. See JDK and Gradle compatibility.

Unfortunately, even after correctly setting the matching versions of Gradle, the AGP, and the JDK, my build was still very much broken. Upgrading to Gradle 8.5+ was a thing that many of my dependencies didn't like. I couldn't set a lower version because they are incompatible with JDK 21.

But wait a minute, what am I using JDK 21? It turns out Android Studio Ladybug comes with JDK 21 included.

So okay, fine. Just downgrade the version globally of the JDK with the JAVA_HOME or whatever, it's not a problem (while doing this I discovered mise, which is a pretty cool tool). But I couldn't make Flutter use other JDK versions, even though the output of java -version was this:

java -version
openjdk version "17.0.12" 2024-07-16 LTS
OpenJDK Runtime Environment Zulu17.52+17-CA (build 17.0.12+7-LTS)
OpenJDK 64-Bit Server VM Zulu17.52+17-CA (build 17.0.12+7-LTS, mixed mode, sharing)
Enter fullscreen mode Exit fullscreen mode

What's going on?

The JDK and Flutter

Well, to pick which JDK to use, Flutter follows these steps:

  • The Android Studio app includes a version of Java, which Flutter uses by default.
  • If you don't have Android Studio installed, Flutter relies on the version defined by your shell script's JAVA_HOME environment variable.
  • If JAVA_HOME isn't defined, Flutter looks for any java executable in your path. Once issue 122609 lands, the flutter doctor command reports which version of Java is used

So, my initial thought was wrong. Flutter doesn't care about your JAVA_HOME as long as there's an Android Studio living in your machine.

To solve this problem, I have to point Flutter to the JDK 17 with this command, which I've found in this nice issue.

flutter config --jdk-dir /Library/Java/JavaVirtualMachines/zulu-17.52.17.jdk/Contents/home 
Enter fullscreen mode Exit fullscreen mode

Also, shout out to flutter analyze --suggestions which was really handy for this kind of issue.

General Info                                                      
│ [✓] App Name: TestApp                                             │
│ [✓] Supported Platforms: android, ios                             │
│ [✓] Is Flutter Package: yes                                       │
│ [✓] Uses Material Design: yes                                     │
│ [✓] Is Plugin: no                                                 │
│ [✗] Java/Gradle/Android Gradle Plugin:                            │
│ Incompatible Java/Gradle versions.                                │
│ Java Version: 21.0.3, Gradle Version: 7.5                         │
│                                                                   │
│ See the link below for more information:                          │
│ https://docs.gradle.org/current/userguide/compatibility.html#java
Enter fullscreen mode Exit fullscreen mode

That's it! I can continue working on my side project app! (and updating Android Studio as soon as possible - I didn't learn).

Top comments (0)