Skip to content

fix: resolve #1194 — 【已解决,请切换到1.9.14版本】反射隔离Application和ApplicationLike类导致getResources方法耗时显著增加#1791

Open
Nam0101 wants to merge 3 commits into
Tencent:devfrom
Nam0101:contribai/improve/quality/replace-reflection-based-delegation-with
Open

fix: resolve #1194 — 【已解决,请切换到1.9.14版本】反射隔离Application和ApplicationLike类导致getResources方法耗时显著增加#1791
Nam0101 wants to merge 3 commits into
Tencent:devfrom
Nam0101:contribai/improve/quality/replace-reflection-based-delegation-with

Conversation

@Nam0101
Copy link
Copy Markdown

@Nam0101 Nam0101 commented May 16, 2026

Summary

fix: resolve #1194 — 【已解决,请切换到1.9.14版本】反射隔离Application和ApplicationLike类导致getResources方法耗时显著增加

Problem

Severity: High | File: tinker-android/tinker-android-anno/src/main/resources/TinkerAnnoApplication.tmpl

This template generates the user's Application class that delegates to ApplicationLike. Since 1.9.9, calls like getResources() were routed through reflection to prevent the compiler from inlining ApplicationLike methods into the Application class (which would break hot-fix capability for those methods). However, reflection on every getResources() call is extremely costly because Android framework code calls getResources() very frequently (e.g., during view inflation, resource lookup, etc.). The fix should use a non-reflection mechanism that still defeats inlining.

Solution

Modify the template so that overrides like getResources(), getAssets(), getBaseContext(), getClassLoader(), etc. call ApplicationLike methods directly (e.g., applicationLike.getResources()) rather than via Method.invoke(). To still prevent inlining across the Application/ApplicationLike boundary on systems where this is a concern, rely on the fact that ApplicationLike is loaded via a different ClassLoader (the patched dex ClassLoader) at runtime, which naturally prevents the ART compiler from inlining across ClassLoader boundaries. Alternatively, mark the relevant ApplicationLike methods as non-final and ensure they remain in a separately-loaded dex. For older Android versions where this is insufficient, a lightweight indirection (e.g., a volatile field read or an interface dispatch) is far cheaper than reflection. Branch the generated code by Build.VERSION.SDK_INT if behavior must differ per system version.

Changes

  • tinker-android/tinker-android-anno/src/main/resources/TinkerAnnoApplication.tmpl (modified)
  • tinker-android/tinker-android-anno/src/test/java/com/tencent/tinker/anno/test/AnnotationProcessorTest.java (new)
  • tinker-android/tinker-android-lib/src/main/java/com/tencent/tinker/lib/app/ApplicationLike.java (new)

Testing

  • Existing tests pass
  • Manual review completed
  • No new warnings/errors introduced

Note: this change was drafted with AI assistance and reviewed locally before submission.

Nam0101 added 3 commits May 17, 2026 04:10
…tionLike类导致getResources方法耗时显著增加

Fixes Tencent#1194

Signed-off-by: Nguyen Van Nam <nam.nv205106@gmail.com>
…tionLike类导致getResources方法耗时显著增加

Fixes Tencent#1194

Signed-off-by: Nguyen Van Nam <nam.nv205106@gmail.com>
…tionLike类导致getResources方法耗时显著增加

Fixes Tencent#1194

Signed-off-by: Nguyen Van Nam <nam.nv205106@gmail.com>
@tencent-adm
Copy link
Copy Markdown
Member

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.9.14版本】反射隔离Application和ApplicationLike类导致getResources方法耗时显著增加

2 participants