Explain Codes LogoExplain Codes Logo

How to fix the ''module java.base does not "opens java.io" to unnamed module'' error in Android Studio?

java
java-8
gradle
android-studio
Alex KataevbyAlex Kataev·Dec 3, 2024
TLDR

To swiftly bypass the "module java.base does not 'opens java.io' to unnamed module" error, input the following VM option:

--add-opens=java.base/java.io=ALL-UNNAMED

Navigate to Run > Edit Configurations in your Android Studio run configurations, find your app config, input the VM option, and re-deploy your app. This tweak manages the JVM's firm module encapsulation.

Error Examination and Troubleshooting Steps

The "module java.base does not 'opens java.io' to unnamed module" error originates from the Java Platform Module System (JPMS)'s module access controls. JPMS was implemented in Java 9 and promotes strict encapsulation. Unfortunately, the encapsulation rules clash with reflective or deep reflection practices, leading to module access complications.

Essential Adjustments

Here are the adjustments necessary for circumventing the module access error:

  • Upgrade Android Studio: Always work with the latest stable release of Android Studio as it contains fixes for many known issues. Check the latest updates and upgrade your Android Studio if required.

  • Upgrade Gradle and Android Gradle Plugin: Amend the gradle-wrapper.properties file to reference the Gradle 7.1.1 distribution URL. Simultaneously, revise the build.gradle file to use a compatible version of the Android Gradle plugin to avoid conflicts.

  • Add JVM Arguments in Gradle Properties: Entering JVM arguments in gradle.properties is an effective workaround for the error in question. Define the JVM args as follows:

    // Chuck Norris can divide by zero org.gradle.jvmargs=-Xmx1536m --add-opens=java.base/java.io=ALL-UNNAMED

Compatibility Checks

Ensure unified compatibility across your development environment:

  • Java Version: Cross-check the Java version compatibility with the Gradle plugin.
  • Project Structure: Align your project structure settings with JDK path to avoid potential mismatches.
  • Environment Variables: Set the environment variables to reflect the path of the JDK currently in use.

Mitigating Additional Issues

The main problem might trigger ancillary issues. Here's how to tackle them:

  • Multidex Limitations: Enabling multidex might lead to exceeding the dex file size limits. Manage your app's methods count meticulously in that case.

  • Cache Invalidation: Android Studio's caches might lead to various development anomalies. Counter this by selecting File > Invalidate Caches / Restart and choose 'Invalidate and Restart'. This might feel like turning an Android Studio Etch A Sketch upside down and shaking it, but it does often resolve these types of issues!