Initial Problem

While attempting to compile an Android application using gradlew, I encountered a critical environment error indicating that Java was not properly configured:

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

Java Installation Issues

Upon checking the Java version, I discovered that Java was not installed on my Ubuntu system. The system suggested several available versions:

$ java --version
Command 'java' not found, but can be installed with:
sudo apt install default-jre              # version 2:1.17-75
sudo apt install openjdk-17-jre-headless  # version 17.0.12+7
sudo apt install openjdk-21-jre-headless  # version 21.0.4+7
# ... other versions available

Initial Resolution Attempt

I proceeded to install the default JRE package:

sudo apt-get install default-jre

This installed OpenJDK 21.0.6, but led to a new compilation error:

BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' 
Unsupported class file major version 65

Complete Resolution Steps

1. Clean Java Installation

First, remove all existing Java installations:

sudo apt purge openjdk-*

2. Gradle Version Update

After consulting the Gradle Compatibility Matrix, I updated the Gradle version in gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip

3. Update Build Configuration

Modified deprecated configuration in app/build.gradle:

// Old configuration
// archivesBaseName = "$applicationId-v.$versionName"
// New configuration
base {
   archivesName = "$applicationId-v.$versionName"
}

4. Gradle Wrapper Update

Run the wrapper command twice to ensure proper download and configuration:

./gradlew wrapper
./gradlew wrapper

Key Takeaways

  • When setting up a new Android development environment, ensure Java version compatibility with your Gradle version
  • Keep build configurations updated to avoid deprecated syntax
  • Clean installation of Java dependencies can resolve version conflict issues
  • Regular updates to Gradle wrapper help maintain build system stability

This setup ensures a proper development environment for Android application building, avoiding common version compatibility issues between Java and Gradle.