Skip to content

Android Gradle Plugin Java Version Fix

markdown
## Problem: Java Version Compatibility Error with Android Gradle Plugin

When creating a new Android project or running Gradle commands, you may encounter this critical error:

```plaintext
Failed to apply plugin 'com.android.internal.application'.
Android Gradle plugin requires Java 17 to run. You are currently using Java 11.

This occurs because:

  • You're using Java 11 or lower
  • Your Android Gradle plugin (AGP) version requires Java 17+
  • Tools configuration points to the incorrect Java location

The error typically appears when:

  • Creating new Android Studio projects
  • Running Gradle commands (./gradlew)
  • Building React Native/Unity Android projects
  • Migrating to newer AGP versions

1. Update Gradle JDK in Android Studio

!> Best for standard Android development
This is the preferred solution for most users:

  1. Download Java 17 JDK
  2. Install using default settings
  3. In Android Studio:
    • File → Settings → Build, Execution, Deployment → Build Tools → Gradle
    • Under Gradle JDK, select Download JDK → Choose Java 17
  4. Sync project with Gradle files

2. Set JAVA_HOME Environment Variable

?> System-wide solution for all projects

Windows:

  1. Press Win+R → sysdm.cpl → Advanced → Environment Variables
  2. Under System variables:
    • Create new variable JAVA_HOME
    • Set value to JDK 17 path (e.g., C:\Program Files\Java\jdk-17.0.10)
  3. Edit Path variable → Add %JAVA_HOME%\bin
  4. Restart terminal/IDEs

Mac/Linux:

bash
export JAVA_HOME=$(/usr/libexec/java_home -v 17)
# Add to ~/.zshrc or ~/.bashrc to make permanent

3. Configure gradle.properties

?> Project-specific override

Add to your project's gradle.properties:

properties
# Windows
org.gradle.java.home=C\:\\Program Files\\Java\\jdk-17

# Mac/Linux
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home

4. React Native Specific Solution

  1. Run diagnostics:
bash
npx react-native doctor
  1. Install required JDK (Mac):
bash
brew install --cask zulu@17
  1. Confirm JDK path:
bash
brew info --cask zulu@17
# Typical location: /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
  1. Remove old JAVA_HOME references in environment variables

5. Ubuntu/Linux Installation

bash
sudo apt update
sudo apt install openjdk-17-jdk
sudo update-alternatives --config java  # Select Java 17

6. Unity Workaround (Risky!)

?> Last-resort solution for Unity projects only

  1. Close Unity
  2. Copy contents from C:\Program Files\Java\jdk-17
  3. Replace files in Unity's reported JDK folder (e.g., C:\Program Files\Java\jdk-16)
  4. Reopen Unity and rebuild

7. Clear Gradle Cache (Final Step)

After making changes:

bash
# Windows
rmdir /s /q %USERPROFILE%\.gradle\caches

# Mac/Linux
rm -rf ~/.gradle/caches

Version-Specific Notes

  • AGP 7.0+ requires Java 11-17
  • AGP 8.0+ requires Java 17
  • React Native 0.73+ requires Java 21
  • Always check official requirements

Verification Steps

Confirm successful configuration:

bash
java -version        # Should show "17.x"
javac -version       # Should match Java version
./gradlew --version  # Check Gradle JDK value

For Android builds, ensure JDK location in Android Studio matches your system configuration. Double-check Project Structure settings (File → Project Structure) if issues persist.