Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

InaccessibleObjectException: java.base does not open java.io during an Android build

Errorjava.lang.reflect.InaccessibleObjectException: Unable to make field protected java.io.OutputStream java.io.FilterOutputStream.out accessible: module java.base does not "opens java.io" to unnamed module

Short answer

A tool in your build reached into JDK internals by reflection, and JDK 17 and later block that. The proper fix is to update the plugin or tool that does it. If you cannot yet, pass --add-opens=java.base/java.io=ALL-UNNAMED to the process that fails: the Gradle daemon or the Kotlin daemon.

  • Applies to JDK 17 and 21, Android Gradle Plugin 8.x and 9.x, Gradle 8.x and 9.x, Android Studio current stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which JDK Gradle runs on, then read the stack trace to see which process threw: the Gradle daemon or a separate Kotlin daemon.

    ./gradlew --version
    ./gradlew assembleDebug --stacktrace 2>&1 | grep -n -i "InaccessibleObject\|daemon" | head
  2. 2Match what you see, then jump to its fix

    1. 1The stack trace names a plugin, a Gradle script or a library jar just above InaccessibleObjectExceptionAn old plugin or library reflects into JDK internalsFix
    2. 2The failing class is in the Gradle daemon process, and no newer version of the tool existsYou cannot update yet: open the package for the Gradle daemonFix
    3. 3The log mentions the Kotlin daemon or a fallback compile, and the Gradle jvmargs change had no effectThe Kotlin daemon throws it, not the Gradle daemonFix

What does the error look like?

  • java.lang.reflect.InaccessibleObjectException: Unable to make field protected java.io.OutputStream java.io.FilterOutputStream.out accessible: module java.base does not "opens java.io" to unnamed module
  • Unable to make ... accessible: module java.base does not "opens java.lang" to unnamed module

Cause 1 of 3

An old plugin or library reflects into JDK internals

1Check

Since JDK 17 the JDK's internal packages are closed to reflection by default. Code that uses non-public fields of java.* classes throws InaccessibleObjectException. Many tools were updated to avoid this, so an old version of one is the likely source.

./gradlew assembleDebug --stacktrace 2>&1 | grep -B3 -A12 "InaccessibleObject"
./gradlew buildEnvironment | head -60

2Fix

Update the plugin or library named in the trace (Android Gradle plugin, Kotlin plugin, Hilt, a code generator) to a version that supports your JDK. Update Gradle itself too.

# gradle/libs.versions.toml: raise the version of the plugin named in the trace
# gradle/wrapper/gradle-wrapper.properties: use a Gradle version your AGP requires

3Verify

Run the build again and the exception is gone.

./gradlew clean assembleDebug

Confirmed by docs.oracle.com

Did this fix it?

Cause 2 of 3

You cannot update yet: open the package for the Gradle daemon

1Check

org.gradle.jvmargs holds the JVM arguments of the Gradle daemon. Check whether it is set and whether it already carries --add-opens.

grep -rn "jvmargs" gradle.properties ~/.gradle/gradle.properties

2Fix

Add the flag to org.gradle.jvmargs in gradle.properties. The JDK's flag form is --add-opens <module>/<package>=<target-module>; ALL-UNNAMED targets code on the class path. Keep your existing options on the same line.

# gradle.properties
org.gradle.jvmargs=-Xmx2g --add-opens=java.base/java.io=ALL-UNNAMED

3Verify

Stop the old daemon so it restarts with the new arguments, then build.

./gradlew --stop
./gradlew assembleDebug

Confirmed by docs.gradle.org

Did this fix it?

Cause 3 of 3

The Kotlin daemon throws it, not the Gradle daemon

1Check

The Kotlin compiler can run in its own daemon with its own JVM arguments. Options for it are set with kotlin.daemon.jvmargs, separate from org.gradle.jvmargs.

grep -n "kotlin.daemon" gradle.properties

2Fix

Pass the same flag to the Kotlin daemon with kotlin.daemon.jvmargs. Separate several arguments with spaces.

# gradle.properties
kotlin.daemon.jvmargs=-Xmx1500m --add-opens=java.base/java.io=ALL-UNNAMED

3Verify

Stop the daemons and build again.

./gradlew --stop
./gradlew assembleDebug

Confirmed by kotlinlang.org

Did this fix it?

How do I stop it coming back?

  • Treat --add-opens as a short-term workaround; the JDK page says to update the tools first and that internals can change or go away.
  • Update the Android Gradle plugin, Kotlin and Gradle together, using AGP's compatibility table (JDK 17 is the minimum for current AGP).
  • Keep one JDK for Android Studio's Gradle JDK setting and for the command line, so a build behaves the same in both.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Android app crashes because the Apache HTTP classes are missing

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;

From Android 9 the Apache HTTP client is no longer on the default classpath, so code or a library that still uses org.apache.http fails at runtime.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.