@@ -282,18 +282,36 @@ public static void monkeyPatchExistingResources(Context context, String external
282
282
resources .updateConfiguration (resources .getConfiguration (), resources .getDisplayMetrics ());
283
283
}
284
284
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 ();
289
305
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 );
292
310
}
293
311
}
312
+ } catch (Throwable thr ) {
313
+ throw new TinkerRuntimeException ("Fail to hack resourceImpls field." );
294
314
}
295
- } catch (Throwable ignored ) {
296
- // Ignored.
297
315
}
298
316
299
317
// Handle issues caused by WebView on Android N.
@@ -305,8 +323,8 @@ public static void monkeyPatchExistingResources(Context context, String external
305
323
if (publicSourceDirField != null ) {
306
324
publicSourceDirField .set (context .getApplicationInfo (), externalResourceFile );
307
325
}
308
- } catch (Throwable ignore ) {
309
- // Ignored.
326
+ } catch (Throwable thr ) {
327
+ ShareTinkerLog . printErrStackTrace ( TAG , thr , "fail to process publicSourceDirField field hack." );
310
328
}
311
329
}
312
330
0 commit comments