feat: allow apps to provide Android Context externally#391
Merged
Conversation
Adds AndroidContextUtil.setApplicationContext(Context) so an application can supply the Android Context instead of relying on the reflection-based AppGlobals.getInitialApplication() workaround, which may break under Google's non-SDK interface restrictions. The provided context is reduced to its application context and held in a static AtomicReference; getContext() prefers it and falls back to reflection when none was provided. Also modernizes hardcoded API-level literals to Build.VERSION_CODES constants and uses getLongVersionCode() on API 28+. This builds on the work from #361 by lgoldstein, adding test coverage for the new API (including static-holder reset between tests) and fixing the README example that referenced a non-existent "AndroidContextUtils" class. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BBrtdK6G1WtVPvCxenWAXz
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.
Summary
This is a reviewed and touched-up version of #361 (by @lgoldstein), opened as a draft for your review as part of the open-PR triage.
It adds a public API —
AndroidContextUtil.setApplicationContext(Context)— so an application can supply the AndroidContextexplicitly instead of relying on the reflection-basedAppGlobals.getInitialApplication()workaround, which may stop working under Google's non-SDK interface restrictions.AtomicReference.getContext()now prefers the externally provided context and falls back to the existing reflection workaround when none was set.android.content.Context(a supertype of the oldContextWrapper, so existing source callers keep compiling).Build.VERSION_CODES.*, andgetVersionCode()usesgetLongVersionCode()on API 28+.What I changed on top of #361
AndroidContextUtilsclass (plural); corrected toAndroidContextUtilso the snippets compile.AndroidContextUtilTestfor the new API: that a provided context is used, that the no-arg constructor picks it up, and that clearing it falls back to reflection. The static holder is now reset in@Before/@Afterso it can't leak between tests.Notes / things to confirm
build.gradlebumpssourceCompatibility/targetCompatibilityfrom Java 1.6 → 1.7 (carried over from feat: Allow users to provide Android context instance to AndroidContextUtil instance #361, and required by thenew AtomicReference<>()diamond operator). This is harmless on all supported Android versions but is a deliberate baseline change worth a nod.Credit for the original change goes to @lgoldstein (#361).
🤖 Generated with Claude Code
https://claude.ai/code/session_01BBrtdK6G1WtVPvCxenWAXz
Generated by Claude Code