Android Gradle Plugin Java Version Fix
## 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
Recommended Solutions
1. Update Gradle JDK in Android Studio
!> Best for standard Android development
This is the preferred solution for most users:
- Download Java 17 JDK
- Install using default settings
- In Android Studio:
- File → Settings → Build, Execution, Deployment → Build Tools → Gradle
- Under Gradle JDK, select Download JDK → Choose Java 17
- Sync project with Gradle files
2. Set JAVA_HOME
Environment Variable
?> System-wide solution for all projects
Windows:
- Press Win+R →
sysdm.cpl
→ Advanced → Environment Variables - Under System variables:
- Create new variable
JAVA_HOME
- Set value to JDK 17 path (e.g.,
C:\Program Files\Java\jdk-17.0.10
)
- Create new variable
- Edit
Path
variable → Add%JAVA_HOME%\bin
- Restart terminal/IDEs
Mac/Linux:
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
:
# 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
- Run diagnostics:
npx react-native doctor
- Install required JDK (Mac):
brew install --cask zulu@17
- Confirm JDK path:
brew info --cask zulu@17
# Typical location: /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home
- Remove old
JAVA_HOME
references in environment variables
5. Ubuntu/Linux Installation
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
- Close Unity
- Copy contents from
C:\Program Files\Java\jdk-17
- Replace files in Unity's reported JDK folder (e.g.,
C:\Program Files\Java\jdk-16
) - Reopen Unity and rebuild
7. Clear Gradle Cache (Final Step)
After making changes:
# 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:
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.