<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gabriel Aguirre</title>
    <description>The latest articles on DEV Community by Gabriel Aguirre (@gabiektx).</description>
    <link>https://dev.to/gabiektx</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2252803%2F1c6e5749-f4cb-4570-99dc-0463a315f799.jpeg</url>
      <title>DEV Community: Gabriel Aguirre</title>
      <link>https://dev.to/gabiektx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gabiektx"/>
    <language>en</language>
    <item>
      <title>Android Studio Ladybug broke my build.</title>
      <dc:creator>Gabriel Aguirre</dc:creator>
      <pubDate>Wed, 23 Oct 2024 11:32:41 +0000</pubDate>
      <link>https://dev.to/gabiektx/android-studio-ladybug-broke-my-build-5a43</link>
      <guid>https://dev.to/gabiektx/android-studio-ladybug-broke-my-build-5a43</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;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 &lt;code&gt;flutter config --jdk-dir&lt;/code&gt; to use a previous JDK version.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Flutter Fix
[!] Your project's Gradle version is incompatible with the Java version that Flutter is using for Gradle.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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?&lt;/p&gt;

&lt;h2&gt;
  
  
  Gradle and The AGP
&lt;/h2&gt;

&lt;p&gt;First off, &lt;a href="https://gradle.org/" rel="noopener noreferrer"&gt;Gradle&lt;/a&gt; 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.&lt;/p&gt;

&lt;p&gt;The Android team created the Android Gradle Plugin (AGP) to tell Gradle how to build Android-related projects. This plugin will register all &lt;a href="https://developer.android.com/build/gradle-build-overview#is-build?" rel="noopener noreferrer"&gt;tasks&lt;/a&gt; needed to generate Android-related content. &lt;/p&gt;

&lt;p&gt;You set the version of this plugin inside your project &lt;code&gt;gradle.build&lt;/code&gt; file (not the one inside the &lt;code&gt;app&lt;/code&gt; folder) using &lt;code&gt;com.android.tools.build:gradle&lt;/code&gt; or inside &lt;code&gt;settings.gradle&lt;/code&gt; with &lt;code&gt;com.android.application&lt;/code&gt;. Something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the official &lt;a href=""&gt;compatibility matrix&lt;/a&gt; between AGP and Gradle. I upgraded these versions to &amp;gt;= 8.5. Let's see if that fixed my build.&lt;/p&gt;

&lt;h2&gt;
  
  
  The JDK
&lt;/h2&gt;

&lt;p&gt;Just a quick recap: the &lt;a href="https://developer.android.com/build/jdks#glossary/" rel="noopener noreferrer"&gt;Java Development Kit&lt;/a&gt; (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 &lt;a href="https://docs.gradle.org/current/userguide/compatibility.html#java_runtime" rel="noopener noreferrer"&gt;JDK and Gradle compatibility&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

&lt;p&gt;But wait a minute, what am I using JDK 21? It turns out Android Studio Ladybug comes with JDK 21 included.&lt;/p&gt;

&lt;p&gt;So okay, fine. Just downgrade the version globally of the JDK with the &lt;code&gt;JAVA_HOME&lt;/code&gt; or whatever, it's not a problem (while doing this I discovered &lt;a href="https://mise.jdx.dev/" rel="noopener noreferrer"&gt;mise&lt;/a&gt;, which is a pretty cool tool). But I couldn't make Flutter use other JDK versions, even though the output of &lt;code&gt;java -version&lt;/code&gt; was this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's going on?&lt;/p&gt;

&lt;h2&gt;
  
  
  The JDK and Flutter
&lt;/h2&gt;

&lt;p&gt;Well, to pick which JDK to use, Flutter follows these &lt;a href="https://docs.flutter.dev/release/breaking-changes/android-java-gradle-migration-guide#notes" rel="noopener noreferrer"&gt;steps&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;The Android Studio app includes a version of Java, which Flutter uses by default.&lt;/li&gt;
&lt;li&gt;If you don't have Android Studio installed, Flutter relies on the version defined by your shell script's &lt;code&gt;JAVA_HOME&lt;/code&gt; environment variable.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;JAVA_HOME&lt;/code&gt; isn't defined, Flutter looks for any &lt;code&gt;java&lt;/code&gt; executable in your path. Once &lt;a href="https://github.com/flutter/flutter/issues/122609" rel="noopener noreferrer"&gt;issue 122609&lt;/a&gt; lands, the &lt;code&gt;flutter doctor&lt;/code&gt; command reports which version of Java is used&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, my initial thought was wrong.  Flutter doesn't care about your &lt;code&gt;JAVA_HOME&lt;/code&gt; as long as there's an Android Studio living in your machine. &lt;/p&gt;

&lt;p&gt;To solve this problem, I have to point Flutter to the JDK 17 with this command, which I've found in this nice &lt;a href="https://github.com/fluttercommunity/plus_plugins/issues/3303" rel="noopener noreferrer"&gt;issue&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;flutter config --jdk-dir /Library/Java/JavaVirtualMachines/zulu-17.52.17.jdk/Contents/home 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, shout out to &lt;code&gt;flutter analyze --suggestions&lt;/code&gt; which was really handy for this kind of issue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

</description>
    </item>
  </channel>
</rss>
