After updating Android Studio, many Flutter developers suddenly find their previously working project refuses to build โ whether they're running it from VS Code, the terminal, or Android Studio itself. The error looks something like this for flutter gradle build failed:
Launching lib/main.dart on RMX3381 in debug mode... WARNING: A restricted method in java.lang.System has been called WARNING: java.lang.System::load has been called by net.rubygrapefruit.platform.internal.NativeLibraryLoader in an unnamed module WARNING: Use --enable-native-access=ALL-UNNAMED to avoid a warning for callers in this module WARNING: Restricted methods will be blocked in a future release unless native access is enabled FAILURE: Build failed with an exception. * What went wrong: 25.0.2 * 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. BUILD FAILED in 2s Error: Gradle task assembleDebug failed with exit code 1
At first glance this error is confusing โ it doesn't clearly say "wrong Java version." The stray 25.0.2 under "What went wrong" and the System::load restricted-method warnings are the real clues.
Root Cause
Every new major release of Android Studio ships with its own bundled JDK (JBR โ JetBrains Runtime), and Google has been steadily moving this bundled JDK to newer Java versions (Java 21, and now builds shipping Java 25 previews).
Your Flutter project's Android build, however, is driven by Gradle, and a specific Gradle version (in this case Gradle 8.14) is only tested and guaranteed to work with a specific range of JDK versions โ typically JDK 17. When Android Studio's update silently swaps out the JDK that gets picked up during the build (via JAVA_HOME or an internal Android Studio setting), Gradle tries to run on a Java version it was never designed for, and the build fails with an unhelpful, truncated error like 25.0.2.
This is especially confusing because:
- Your terminal's
java -versionmight correctly show JDK 17 (because your shell'sJAVA_HOMEis set separately) - But VS Code / Flutter / Android Studio may still be invoking a different, newer JDK bundled with Android Studio itself
- The two environments disagree, and Gradle picks the wrong one
The Fix: Force Gradle to Use JDK 17
The reliable fix is to explicitly tell Gradle which JDK to use, rather than letting it infer this from the environment.
Step 1: Confirm you have JDK 17 installed
If you're on macOS and use Homebrew:
bash
brew install openjdk@17
Step 2: Find the exact path to JDK 17
bash
/usr/libexec/java_home -v 17
This will print something like:
/opt/homebrew/Cellar/openjdk@17/17.0.17/libexec/openjdk.jdk/Contents/Home
Copy this path โ you'll need it in the next step.
Step 3: Point Gradle to that JDK explicitly
Open android/gradle.properties in your Flutter project and add this line (using the path from Step 2):
properties
org.gradle.jvmargs=-Xmx8G -XX:MaxMetaspaceSize=4G -XX:ReservedCodeCacheSize=512m -XX:+HeapDumpOnOutOfMemoryError android.useAndroidX=true org.gradle.java.home=/opt/homebrew/Cellar/openjdk@17/17.0.17/libexec/openjdk.jdk/Contents/Home
This single line overrides whatever JDK Android Studio, VS Code, or your shell environment might otherwise be passing to Gradle โ Gradle will now always use JDK 17 for this project, regardless of what changed in your IDE.
Step 4: Kill any running Gradle daemons and clean the build
A stale Gradle daemon may still be running with the old, broken JDK cached in memory. Clear everything out:
bash
cd android ./gradlew --stop ./gradlew clean cd .. flutter clean flutter pub get
Step 5: Run your project again
bash
flutter run
The build should now complete successfully.
Why This Happens Specifically After Updating Android Studio
Android Studio bundles its own copy of the JDK to ensure a consistent development experience across all its features (not just Flutter/Gradle builds, but also its own Java/Kotlin tooling). Each major Android Studio release tends to bump this bundled JDK to a newer major version to keep up with JetBrains' and Google's tooling requirements.
Unfortunately, Gradle (and by extension, Flutter's Android build pipeline) doesn't always keep pace with the very latest JDK releases โ especially preview or early-adopter versions like Java 21+ or Java 25. Until your project's Gradle and Android Gradle Plugin (AGP) versions are explicitly upgraded to versions that support the newer JDK, you need to pin your project to a known-compatible JDK version, which is exactly what org.gradle.java.home does.
Quick Reference Checklist
- Confirm JDK 17 is installed (
brew install openjdk@17on macOS) - Get the exact path with
/usr/libexec/java_home -v 17 - Add
org.gradle.java.home=<path>toandroid/gradle.properties - Run
./gradlew --stop && ./gradlew cleaninside theandroid/folder - Run
flutter clean && flutter pub get - Run
flutter runagain
Alternative: Upgrade Gradle and AGP Instead
If you'd rather use the newer JDK that ships with your updated Android Studio (instead of pinning to JDK 17), the alternative fix is to upgrade both:
- Your Gradle version, in
android/gradle/wrapper/gradle-wrapper.properties - Your Android Gradle Plugin (AGP) version, in
android/settings.gradleorandroid/build.gradle
to versions that officially support the newer JDK. This is a more involved fix and carries a higher risk of breaking other parts of your build configuration, so pinning to JDK 17 via org.gradle.java.home is usually the faster, safer path for most projects.