Skip to content

Commit 59ca1e2

Browse files
committed
[tinker] Fix patched resources is not found after inflating webview on Android 15.
1 parent d96ae92 commit 59ca1e2

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

tinker-android/tinker-android-loader/src/main/java/com/tencent/tinker/loader/TinkerResourcePatcher.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -282,18 +282,36 @@ public static void monkeyPatchExistingResources(Context context, String external
282282
resources.updateConfiguration(resources.getConfiguration(), resources.getDisplayMetrics());
283283
}
284284

285-
try {
286-
if (resourceImpls != null) {
287-
for (WeakReference<Object> wr : resourceImpls.values()) {
288-
final Object resourceImpl = wr.get();
285+
if (resourceImpls != null) {
286+
try {
287+
Field implAssetsField = null;
288+
Field mResDirField = null;
289+
for (Map.Entry<Object, WeakReference<Object>> pair : resourceImpls.entrySet()) {
290+
final Object resKey = pair.getKey();
291+
if (mResDirField == null) {
292+
mResDirField = findField(resKey.getClass(), "mResDir");
293+
}
294+
String origResDir = (String) mResDirField.get(resKey);
295+
if (!appInfo.sourceDir.equals(origResDir)) {
296+
continue;
297+
}
298+
299+
if (Build.VERSION.SDK_INT >= 35) {
300+
mResDirField.set(resKey, externalResourceFile);
301+
}
302+
303+
final WeakReference<Object> resValueRef = pair.getValue();
304+
final Object resourceImpl = resValueRef.get();
289305
if (resourceImpl != null) {
290-
final Field implAssets = findField(resourceImpl, "mAssets");
291-
implAssets.set(resourceImpl, newAssetManager);
306+
if (implAssetsField == null) {
307+
implAssetsField = findField(resourceImpl, "mAssets");
308+
}
309+
implAssetsField.set(resourceImpl, newAssetManager);
292310
}
293311
}
312+
} catch (Throwable thr) {
313+
throw new TinkerRuntimeException("Fail to hack resourceImpls field.");
294314
}
295-
} catch (Throwable ignored) {
296-
// Ignored.
297315
}
298316

299317
// Handle issues caused by WebView on Android N.
@@ -305,8 +323,8 @@ public static void monkeyPatchExistingResources(Context context, String external
305323
if (publicSourceDirField != null) {
306324
publicSourceDirField.set(context.getApplicationInfo(), externalResourceFile);
307325
}
308-
} catch (Throwable ignore) {
309-
// Ignored.
326+
} catch (Throwable thr) {
327+
ShareTinkerLog.printErrStackTrace(TAG, thr, "fail to process publicSourceDirField field hack.");
310328
}
311329
}
312330

0 commit comments

Comments
 (0)