Fix IllegalAccessException reading non-public PreferenceKey/EnvironmentKey (#243)#490
Open
vincentborko wants to merge 1 commit into
Open
Fix IllegalAccessException reading non-public PreferenceKey/EnvironmentKey (#243)#490vincentborko wants to merge 1 commit into
vincentborko wants to merge 1 commit into
Conversation
…ntKey
A custom `PreferenceKey` or `EnvironmentKey` declared `private`/`fileprivate`
transpiles to a package-private Kotlin class. Reading its companion via Kotlin's
`companionObjectInstance` (kotlin.reflect.full) throws on first access:
java.lang.IllegalAccessException: class kotlin.reflect.jvm.internal.KClassImpl$Data
cannot access a member of class skip.ui.<Key> with modifiers "public static final"
The synthesized companion INSTANCE field is public, but its declaring class is
not, and Kotlin reflection reads the field without setting `isAccessible = true`.
Replace the three `companionObjectInstance` call sites (in PreferenceKey.swift
and EnvironmentValues.swift) with a `companionInstance(ofType:)` helper that
resolves the companion via Java reflection with `field.isAccessible = true`.
The regression test in 3a7de72 ("Make test PreferenceKey implementation public,
working around skiptools#243") sidestepped this by making the test EnvironmentKey public.
This restores it to `private`/`fileprivate`, so `testCustomEnvironmentValue`
again reproduces skiptools#243 and now passes with the fix.
skiptools#310 reports what appears to be the same root cause surfacing as a null-companion
cast NPE (a non-public `@AppStorage`/preference key whose companion reflection
fails); this change should resolve it as well, though only skiptools#243 was reproduced
here.
Fixes skiptools#243
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
vincentborko
force-pushed
the
pr/preferencekey-private-reflect
branch
from
July 24, 2026 15:41
1495208 to
a6a5e74
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
A custom
PreferenceKeyorEnvironmentKeydeclaredprivateorfileprivatecrashes the first time its value is read. On Android the key transpiles to a package-private Kotlin class, and reading its companion object via Kotlin reflection throws:Root cause
PreferenceKey.swiftandEnvironmentValues.swiftread the companion viakotlin.reflect.full.companionObjectInstance:Preference.init(key:)—key.companionObjectInstance as! PreferenceKeyCompanion<Value>View.onPreferenceChange(_:perform:)—key.companionObjectInstance as PreferenceKeyCompanion<V>EnvironmentValues.valueCompositionLocal(key:)—key.companionObjectInstance as EnvironmentKeyCompanion<*>The synthesized companion
INSTANCEfield ispublic static final, but the enclosing key class is package-private when the Swift type isprivate/fileprivate. Kotlin'scompanionObjectInstancereads that field without callingisAccessible = true, so the JVM access check fails withIllegalAccessException.Fix
Introduce a small
companionInstance(ofType:)helper that resolves the companion via Java reflection withfield.isAccessible = true, and route the three call sites through it:This is the minimal root-level fix — it removes the one missing
isAccessible = truerather than forcing every custom key to bepublic.Testing
The regression test was previously worked around in 3a7de72 ("Make test PreferenceKey implementation public, working around #243") by making the test
EnvironmentKeyand its accessorpublic. This PR reverts that toprivate/fileprivate, restoringtestCustomEnvironmentValueas a genuine reproduction.private):testCustomEnvironmentValue$SkipUIfails on Robolectric with theIllegalAccessExceptionabove —JUNIT ... PASSED 0 FAILED 1.JUNIT ... PASSED 1 FAILED 0.Verified with the real Skip SDK via
swift test(Robolectric): transpiles,compileDebugKotlinsucceeds, andtestCustomEnvironmentValuepasses with the test keyprivate.Related
#310 reports what looks like the same root cause surfacing as a
null cannot be cast to non-null type ...PreferenceKeyCompanionNPE (a non-public@AppStorage/preference key whose companion reflection fails). This change should resolve it too, though only #243 was reproduced here.Fixes #243