Skip to content

ADFA-7142: Keep plugin resource loaders off the activity's Resources - #2250

Closed
davidschachterADFA wants to merge 2 commits into
stagefrom
fix/ADFA-7142
Closed

davidschachterADFA wants to merge 2 commits into
stagefrom
fix/ADFA-7142

Conversation

@davidschachterADFA

@davidschachterADFA davidschachterADFA commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

ADFA-7142

In plugin tabs in the editor's bottom panel (Shell-Hooks' "Hooks", AI Core's "Agent"), text in the views a tab builds on first inflation was drawn at about half its sp size. dp sizes were right, and views added later were normal. Found in ADFA-3570's device QA on a Note 20 Ultra.

Cause

  • Confirmed by reading the code: a plugin built with its own resource package id is non-legacy. For those plugins, PluginResourceContext.getResources()/getTheme() returned the host activity's own Resources. The first time a tab inflated, ensurePluginResourcesAdded called activity.resources.addLoaders(...) in the middle of that inflation. On Android 11+, addLoaders makes the framework build a new ResourcesImpl for the activity and rebase its themes (checked in the SDK sources).
  • Inferred, not confirmed: the rebuilt resources carry the wrong font scale until the next configuration update. EditorActivityKt handles fontScale changes itself, which may matter.

Change

  • On API 30+, getResources() and getTheme() use a copy of the activity's resources: createConfigurationContext(Configuration(activity config)). The plugin's ResourcesLoader goes on the copy. The activity's own Resources are never changed.
  • The copy is cached against the activity (held weakly) and its full configuration. A font-scale, uiMode, density, orientation or locale change rebuilds it, along with the plugin theme.
  • A debug log prints the copy's fontScale, density and scaledDensity when it is built, so a device run can confirm them.
  • Unchanged: the path before Android 11 (in-place addAssetPath; minSdk is 28), legacy plugins, and per-plugin getAssets() (ADFA-6794).

Sibling sweep

  • Checked and unaffected:
    • PluginFragmentHelper.getPluginInflater, PluginManager's registration and its getAssets use for languages.
    • EditorBottomSheetTabAdapter and PluginFragmentFactory: class loaders only.
    • PluginScreenActivity, PluginEditorTabManager, and PluginTabDockableContent's overlay context.
    • The markdown-preview and apk-viewer plugins.
    • PluginDrawableResolver and getIdentifier, which now resolve through the copy. It still holds the host APK.
  • Left alone: the other addAssetPath sites. PluginLoader and PluginDocumentationManager use fresh, plugin-only AssetManagers, and quickbuild and the profiler run in other processes.

Testing

  • New PluginResourceContextActivityResourcesTest (Robolectric, sdk 34) asserts three things: the activity's resources have no loaders after resolving through the plugin context; 15sp gives the same pixels through the plugin and through the activity; and they still agree after a font-scale change.
    • On the unfixed code, the first assertion fails: the activity had the plugin's loader. The other two crash in Robolectric, which has no shadow for the theme rebase that addLoaders triggers. That's a side effect of the same mutation, not the size bug.
    • Robolectric cannot reproduce the half-size text itself. Robolectric 4.11 can't load an APK through ResourcesProvider, so the test injects an empty loader by reflection.
    • Mutation check: leaving the configuration out of the cache key fails the font-scale assertion (81 px expected, 45 px from the stale copy).
  • Two existing tests that asserted resources === app.resources now compare configurations.
  • All 153 plugin-manager tests pass. spotlessCheck is clean, and :app:compileV8DebugKotlin builds.
  • Not verified on a device yet. The shared test phone is in use. The device check: text in the Hooks and Agent tabs at font scale 1.0 and 2.0, the debug log against the activity's own values, and whether plugin themes still look right, since Theme.setTo now copies across AssetManagers.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH

Plugins built with their own resource package id resolved resources
through the host activity's own Resources, and their first use added
the plugin's ResourcesLoader to them. On Android 11+ addLoaders makes
ResourcesManager build a new ResourcesImpl for the activity and rebase
its themes, in the middle of the tab's first LayoutInflater.inflate;
the views built then drew sp text at about half size (Note 20 Ultra,
display size overridden to 720x1544). That the swap carries the wrong
scale is inferred, not confirmed.

PluginResourceContext now adds the loader to a copy made with
createConfigurationContext from the activity's current configuration,
and builds the plugin theme and PluginTheme lookup on that copy. The
copy is cached against the activity (held weakly) and its full
configuration, so a fontScale, uiMode or density change the activity
handles in place rebuilds the copy and the theme. Before Android 11
there is no ResourcesLoader, and the in-place addAssetPath path stays
as it was; the legacy (0x7f) path is untouched. A debug log prints the
copy's fontScale, density and scaledDensity when it is built.

PluginResourceContextActivityResourcesTest pins that a first inflation
through the plugin inflater leaves the activity's Resources with no
loaders and the same AssetManager, puts the loader on the copy, and
that 15sp text through the plugin context matches the activity's,
before and after a font-scale change applied to the activity in place.
Robolectric does not reproduce the half-size text. Before the fix the
loader assertion fails, and the two size tests crash in Robolectric's
applyStyle CHECK because it has no shadow for the native theme rebase
the swap triggers. Dropping the configuration from the cache key fails
the font-scale test (81px vs 45px). Two existing tests that pinned
resources === activity.resources now compare configurations.

Needs a device check: plugin tab text (Shell-Hooks "Hooks", AI Core
"Agent") at font scale 0.8, 1.0 and 2.0 on the Note 20 Ultra at
720x1544, with the logged values compared against the activity's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Summary

  • Plugin resources on Android 11 (API 30) and later now load into a copy of the activity's Resources. The activity's own Resources no longer receives plugin loaders for plugins with a custom package ID.
  • The copy is created from the activity's current configuration. It is cached per PluginResourceContext and reused only when the activity context and configuration both match. A configuration change creates a new copy.
  • The plugin theme is rebuilt when the plugin resources object or the activity theme changes.
  • Plugin getAssets() returns the plugin's own assets. The code comment now states that the activity's and host resources can hold other plugins' APKs.
  • Android 10 and earlier keep the previous behavior. Plugin resources are added to the activity's Resources in place.
  • Legacy plugins are not changed.
  • Debug logging added: Log.d on each cache rebuild, with font scale and density values.
  • Tests added: PluginResourceContextActivityResourcesTest (111 lines, 6 methods). It checks that resolving plugin resources adds no loader to the activity, that 15sp text matches through both contexts, and that the match holds after a font-scale change to 2f.
  • Existing tests changed: PluginResourceContextActivitySnapshotTest and PluginResourceContextAssetsTest now compare resource configurations instead of Resources identity.

Risks and best-practice concerns:

  • The device defect is not confirmed. The cause (a resource rebuild that carries the wrong font scale) is inferred. Robolectric cannot reproduce the half-size text, so the new tests do not prove the fix.
  • Device verification is not complete. Checks at font scales 0.8, 1.0, and 2.0, and plugin theme appearance, are planned, not reported as done.
  • The reported checks are author-reported, not verified here: 153 plugin-manager tests pass, spotlessCheck is clean, and :app:compileV8DebugKotlin builds.
  • Two existing tests now assert configuration equality instead of Resources identity. Their assertions are weaker. They may not detect a regression that the identity check caught.
  • The cache stores a strong reference to a Resources object derived from the activity context. The WeakReference only covers the owner field. The cached copy may keep the activity alive after it is destroyed. This is not verified in the diff and should be checked.
  • The cache fields (hostResourcesOwner, hostResourcesConfig, hostResources) are plain var fields with no synchronization. The change assumes all calls run on the UI thread. This assumption is not documented in code.
  • Each plugin context keeps its own copy and its own loader set. Memory use grows with the number of plugins that use custom package IDs. This is not measured.
  • The Log.d call uses @Suppress("DEPRECATION"). It runs on every cache rebuild and writes to logcat in release builds unless log level is filtered.
  • Behavior differs by API level. Android 11+ and Android 10 and earlier take different code paths. Only Android 11+ has the copy-based path under test.

Custom-package plugins use cached, configuration-pinned resources on Android 11 and later. Older Android versions continue to patch activity resources. Resource and theme lookups use the selected plugin resources. Tests check resource isolation and configuration and font-scale alignment.

Changes: Plugin resource handling

Layer / File(s) Summary
Plugin resource selection and validation
plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt, plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt, plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivitySnapshotTest.kt, plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextAssetsTest.kt
On Android 11 and later, the context reuses cached resources only when the activity context and configuration match; otherwise, it creates resources for the current configuration and adds plugin resources. Custom-package resource and theme lookups use these resources. Tests check that activity resources remain unchanged and that plugin resources track activity configuration and font scale.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: daniel-adfa

Merge Risk: Minimal · up to d8153

The change keeps plugin resource loaders off the host activity's Resources on Android 11 and later, which targets the reported half-size text. Nothing in the reviewed code shows a concrete regression. Device checks at several font scales and of plugin theme appearance are still advisable before release.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the ticket and the main change: plugin resource loaders are kept off the host activity's Resources. The raw summary and objectives both describe this as the primary change, so the titl…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description states the bug (plugin tab text drawn at about half its sp size on first inflation), the cause (plugin loaders added to the host activity's Resources through addLoaders on Android 11+)…
Full details: Title check

Explanation

The title names the ticket and the main change: plugin resource loaders are kept off the host activity's Resources. The raw summary and objectives both describe this as the primary change, so the title is fully related to the changeset.

Full details: Description check

Explanation

The description states the bug (plugin tab text drawn at about half its sp size on first inflation), the cause (plugin loaders added to the host activity's Resources through addLoaders on Android 11+), the change (a configuration-pinned copy of the activity resources, cached per activity and configuration), and the tests and device-verification status. It is specific and clearly related to the changeset, so it passes.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

A rabbit checks the resource cache
And hops through configs, fresh and fair
The plugin loader stays in place
While themes find resources they can share
Font scales rise; the text keeps pace
Then soft ears nod and leave no trace

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Essentials
  • Run ID: 374c5989-e5cc-48c4-87f7-f61fc9518b8a
Commits

Reviewing files that changed from the base of the PR and between 6350ec3 and d815369.

Files selected for processing: 4

  • plugin-manager/src/main/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContext.kt
  • plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivityResourcesTest.kt
  • plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextActivitySnapshotTest.kt
  • plugin-manager/src/test/kotlin/com/itsaky/androidide/plugins/manager/loaders/PluginResourceContextAssetsTest.kt

Included review availability: This review used your included allowance. 3 included reviews remain after this review. Your included PR review attempts over the past 7 days set your current allowance at 5 reviews per hour.

@davidschachterADFA

Copy link
Copy Markdown
Collaborator Author

Device check on the Note 20 Ultra (build C-d-1113-1127, this PR at d815369): this fix breaks AI Core. The PR is converted to draft until a revised fix is checked.

  • Opening the bottom panel crashed AI Core's Agent tab: No package ID 42 found for ID 0x420a0007, from ChatFragment.
    • AI Core resolves some of its own resources through the activity's context, not the plugin context.
    • That only worked because the old code added every plugin's loader to the activity's Resources, which this PR stopped doing.
  • After the crash, the bottom panel showed the wrong page, and Shell Hooks' tab was never created. So this build doesn't show whether the copy fixes the text size.
  • The debug log for AI Core's copy read fontScale=1.0 density=1.875 scaledDensity=1.875, which matches the activity.

Next: a revised fix that keeps the copy for plugin inflation and also adds the loader to the activity's Resources, so plugin code that uses the activity's context keeps working. Then a new device check.

@davidschachterADFA
davidschachterADFA marked this pull request as draft November 13, 2026 12:04
…use it

d815369 stopped adding a plugin's ResourcesLoader to the activity's
own Resources. On the Note 20 Ultra that crashed AI Core's Agent tab
("No package ID 42 found for ID 0x420a0007"): AI Core resolves its own
ids through the activity as well as through the plugin context.
ChatFragment calls Fragment.getString and resources.getInteger, which
go through requireContext(), and builds its dialogs on
ContextThemeWrapper(requireContext(), R.style.PluginTheme), as
FilePickerDialogFragment does. Those lookups only ever worked because
every such plugin's loader sat on the activity's Resources.

The activity's Resources get the loader again, as on stage, and before
the copy is built, so that rebuild is done before any plugin view
exists. getResources() and getTheme() still hand out the per-activity,
per-configuration copy, so plugin views are never built on Resources
that addLoaders has just rebuilt. ensurePluginResourcesAdded's
patchedResources slot now tracks only the activity's Resources; each
copy gets the loader once, through addPluginResources, when it is
built. createConfigurationContext passes the base context's loaders on,
so the copy usually starts with the loader already. The pre-R and
legacy paths are unchanged, and getAssets()'s comment is back to
stage's.

PluginResourceContextActivityResourcesTest now pins: once the plugin
context is used, its loader is on the activity's Resources and on a
ContextThemeWrapper over the activity (AI Core's pattern); the plugin
context's Resources are a different object that also carries it;
inflated plugin views and the plugin theme are built on that copy; and
15sp resolves to the same pixels through both, also after a font-scale
change applied to the activity in place. On d815369's code the
activity-loader test fails and the rest pass. Robolectric cannot load
an APK into a ResourcesLoader, so no plugin id is resolved, and it has
no shadow for the native theme rebase that adding a loader to the
activity triggers. A theme made on the activity's Resources before the
patch then trips a CHECK when copied or used, so sp is compared through
DisplayMetrics, and the inflation test snapshots a host theme made
after the patch.

Needs a device check: the Agent and Hooks tabs open without the
package-id errors, plugin dialogs and strings resolve, plugin text is
the right size at font scale 0.8, 1.0 and 2.0 at 720x1544, and the
plugin theme still looks right now that it is copied from the
activity's theme across AssetManagers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vh2DAZWMkFWJh7CY7P4kCH
@davidschachterADFA

davidschachterADFA commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator Author

Device check on C-d-1113-1314 (Note 20 Ultra): fails. Keep this a draft; this PR's cause is wrong.

  • The Hooks and Agent tab text is still small at font scale 1.0 and 2.0. AI Core no longer crashes. The debug line shows the plugin Resources are right: fontScale=1.0 density=1.875 scaledDensity=1.875.
  • Actual cause: ADFA-4212 (ADFA-4212: scale plugin tab UI text to editor font-size preference #1386). EditorHandlerActivity.applyPluginFontScale multiplies every TextView in a newly created plugin fragment view by EditorPreferences.editorFontScale (editor font size / 14). The phone's editor font size is 6.38 (pinch-zoomed), so the factor is 0.456.
  • A debugger reads hkEmpty.getTextSize() = 12.761933 px. That equals 28 px x 6.3809667 / 14.
  • Views added after creation (AI Core's capability chips, Shell Hooks' run rows) skip that pass. That's the "views added later were normal" in the ticket.

The resource changes here don't address it. The direction is an open question on ADFA-7142.

@davidschachterADFA

Copy link
Copy Markdown
Collaborator Author

Superseded by #2255. The device check showed the cause was ADFA-4212's text scaling, not plugin Resources (see the comment above), so these Resources changes aren't needed.

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.

1 participant