InaccessibleObjectException: java.base does not open java.io during an Android build
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 moduleShort 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.
Fix it fast
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" | head2Match what you see, then jump to its fix
- 1
The stack trace names a plugin, a Gradle script or a library jar just above InaccessibleObjectExceptionAn old plugin or library reflects into JDK internalsFix - 2
The 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
The 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
- 1
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 moduleUnable 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 -602Fix
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 requires3Verify
Run the build again and the exception is gone.
./gradlew clean assembleDebugConfirmed by docs.oracle.com
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.properties2Fix
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-UNNAMED3Verify
Stop the old daemon so it restarts with the new arguments, then build.
./gradlew --stop
./gradlew assembleDebugConfirmed by docs.gradle.org
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.properties2Fix
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-UNNAMED3Verify
Stop the daemons and build again.
./gradlew --stop
./gradlew assembleDebugConfirmed by kotlinlang.org
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 fixMobile apps
AGP 9 marks the android block as deprecated or fails on BaseExtension
java.lang.ClassCastException: class com.android.build.gradle.internal.dsl.ApplicationExtensionImpl$AgpDecorated_Decorated cannot be cast to class com.android.build.gradle.BaseExtension
AGP 9.0 uses only the new public DSL interfaces.
3 causes, each with a fixMobile apps
The editor cannot find R, so every R.string and R.id turns red
Unresolved reference: R
R is a class that the build generates from your res folder, in the package named by the module's namespace.
4 causes, each with a fixMobile apps
Android Studio says it could not identify the launch activity when you press Run
Run > Edit Configurations > Launch Options is set to Nothing or to a class that no longer exists
Android Studio launches the activity marked with a MAIN action and a LAUNCHER category in the merged manifest.
3 causes, each with a fixMobile apps
Gradle cannot apply the Android application or library plugin
Failed to apply plugin 'com.android.internal.application'.
Gradle loaded the Android Gradle plugin but could not run it.
2 causes, each with a fixMobile apps
FirebaseInstanceIdService and FirebaseInstanceId.getToken() no longer work
Deprecation warning on FirebaseInstanceId.getInstance().getToken()
Move token handling into a class that extends FirebaseMessagingService and override onNewToken, which runs when a token is first created and whenever it changes.
2 causes, each with a fix