Skip to content

feat: Allow users to provide Android context instance to AndroidContextUtil instance#361

Closed
lgoldstein wants to merge 3 commits into
tony19:mainfrom
lgoldstein:external-android-context
Closed

feat: Allow users to provide Android context instance to AndroidContextUtil instance#361
lgoldstein wants to merge 3 commits into
tony19:mainfrom
lgoldstein:external-android-context

Conversation

@lgoldstein

Copy link
Copy Markdown

Description

In order to support various special properties the code requires access to an Android context instance. By default, the framework uses a workaround based on reflection that seems to work for the time being. However, in view of Google's restrictions on non-SDK interfaces this code might not work anymore. Therefore, the framework should provide a special API that enables the application to provide an Android context instance that will be used instead of the workaround. See the README file for description as to how this feature would be used if necessary.

Linked Issues

None

Additional context

Note:

  • The usage of Context instead of ContextWrapper (also related to next issue)
  • The usage of application context instead of the original context - as it is durable and does not cause possible memory leaks.
  • The fix to package version code retrieval according to current API level
  • Raised min. Java level to 7 and API version to 21 since getting Java 6 level no longer supported. This should not be an issue as it is highly unlikely that the code will run on (extremely) old versions. However, if backward compatibility to Java 6 is still required, this setting can be reverted (though still need to fix the compilation error).

@lgoldstein lgoldstein changed the title Allow users to provide Android context instance to AndroidContextUtil instance feat: Allow users to provide Android context instance to AndroidContextUtil instance Sep 21, 2023
@lgoldstein lgoldstein force-pushed the external-android-context branch from f5640e6 to 5d93955 Compare September 21, 2023 05:11
@lgoldstein

Copy link
Copy Markdown
Author

Reverted min. API level to 9, but left Java JDK at 7

@tony19 tony19 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, @lgoldstein — providing the Context explicitly instead of relying on the AppGlobals.getInitialApplication() reflection hack is a solid improvement, especially given Google's non-SDK interface restrictions. The design (reduce to application context, hold in an AtomicReference, fall back to reflection) is clean.

A few edits I'd suggest before merging:

  1. README references a non-existent class. The docs (and both code snippets) call AndroidContextUtils.setApplicationContext(...) — plural — but the class is AndroidContextUtil (singular), so the examples won't compile as written. Should be AndroidContextUtil.setApplicationContext(...).

  2. No test coverage for the new API. Worth adding tests that (a) a provided context is used by getContext(), and (b) clearing it (setApplicationContext(null)) falls back to the reflection workaround. Because CONTEXT_HOLDER is static, the holder should also be reset in the test setup/teardown so it can't leak between tests.

Two things that are fine as-is, for the record: the fully-qualified android.content.Context references are necessary (importing it would collide with ch.qos.logback.core.Context), and the sourceCompatibility 1.6 → 1.7 bump is needed for the new AtomicReference<>() diamond operator.

I've applied edits 1 and 2 on top of your change in #391 (draft) so the maintainer can review a ready version — full credit to you for the original work. Happy to fold these back here instead if you'd prefer.


Generated by Claude Code

@lgoldstein

Copy link
Copy Markdown
Author

Thanks for finally getting back on this - this is something I personally look forward to have in the upcoming version

tony19 added a commit that referenced this pull request Jul 9, 2026
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.


Claude-Session: https://claude.ai/code/session_01BBrtdK6G1WtVPvCxenWAXz

Co-authored-by: Claude <noreply@anthropic.com>
@tony19

tony19 commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Closed by #391

@tony19 tony19 closed this Jul 9, 2026

tony19 commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Thanks again for this, @lgoldstein. Your change has landed on main via #391, which builds directly on your work (with the README example corrected and test coverage added) — full credit to you for the feature. Closing this as superseded by #391. 🙏


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants