Skip to content

Java Module Error: 'opens java.io' Fix

Problem Overview

The error module java.base does not "opens java.io" to unnamed module occurs when using newer Java versions (Java 9+) with Android development tools. This is caused by Java's module system restrictions that prevent reflection-based access to internal Java classes, which some Android build tools depend on.

The error typically appears when:

  • Using Java 16+ with older Gradle versions
  • Running projects with reflection-based libraries
  • Having incompatible Gradle and JDK combinations

Solutions

Solution 1: JDK and Gradle Version Compatibility

The most reliable solution is ensuring JDK and Gradle version compatibility:

properties
# For JDK 17
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip

# For JDK 11
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
gradle
// For Gradle 7.3.3
classpath 'com.android.tools.build:gradle:7.2.1'

// For Gradle 6.7.1  
classpath 'com.android.tools.build:gradle:4.2.2'

WARNING

Always check the official compatibility matrix:

Solution 2: Add JVM Arguments

For temporary fixes or when downgrading isn't possible, add these JVM arguments to your gradle.properties:

properties
org.gradle.jvmargs=-Xmx1536M \
--add-exports=java.base/sun.nio.ch=ALL-UNNAMED \
--add-opens=java.base/java.lang=ALL-UNNAMED \
--add-opens=java.base/java.lang.reflect=ALL-UNNAMED \
--add-opens=java.base/java.io=ALL-UNNAMED \
--add-exports=jdk.unsupported/sun.misc=ALL-UNNAMED

Solution 3: Set Specific Java Version

If you have multiple JDK versions installed, specify which one to use:

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

# macOS/Linux
org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk-11.0.13.jdk/Contents/Home
bash
# Run with specific JDK
JAVA_HOME=/path/to/jdk11 ./gradlew build

Solution 4: Android Studio Configuration

Update Android Studio settings to use compatible JDK:

  1. Go to File > Project Structure
  2. Under SDK Location, set JDK location to compatible version (JDK 8 or 11)
  3. Under Project Settings, set SDK to appropriate version
  4. Invalidate caches and restart (File > Invalidate Caches / Restart)

For most Android projects, we recommend:

  1. JDK 11 - Most stable for Android development
  2. Gradle 7.0+ - For better compatibility with newer JDKs
  3. Android Gradle Plugin 7.0+ - Updated to handle module system changes

TIP

If working with Flutter projects, ensure your Android directory is properly configured by either:

  • Creating a new Flutter project and migrating code
  • Updating Gradle versions and properties as shown above

Troubleshooting Steps

  1. Check current versions:

    bash
    java -version
    ./gradlew --version
  2. Clean project:

    bash
    ./gradlew clean
  3. Refresh dependencies:

    bash
    ./gradlew --refresh-dependencies
  4. Verify Android Studio configuration in File > Settings > Build, Execution, Deployment > Build Tools > Gradle

When to Use Each Solution

ScenarioRecommended Solution
New projectsUse JDK 11 + Gradle 7.0+
Legacy projectsDowngrade to JDK 8
Temporary fixAdd JVM arguments
Multiple JDK installationsSet specific Java home

DANGER

Avoid using JDK 16+ with Gradle versions below 7.0, as this combination frequently causes module system conflicts.

By following these solutions, you should be able to resolve the module opening error and continue with Android development without downgrading your entire development environment.