3030import android .os .Bundle ;
3131import android .os .IBinder ;
3232import android .util .ArrayMap ;
33+ import android .view .ContextThemeWrapper ;
3334
3435import java .io .File ;
3536import java .io .IOException ;
@@ -187,7 +188,7 @@ private static Object makeDexElement(File pkg, boolean isDirectory, DexFile dexF
187188 }
188189 if (sDexElementConstructor == null ) {
189190 if (Build .VERSION .SDK_INT >= 26 ) {
190- sDexElementConstructor = sDexElementClass .getConstructors ()[ 1 ]; // ( DexFile, File)
191+ sDexElementConstructor = sDexElementClass .getConstructor ( new Class []{ DexFile . class , File . class });
191192 } else {
192193 sDexElementConstructor = sDexElementClass .getConstructors ()[0 ];
193194 }
@@ -465,12 +466,23 @@ public static int addAssetPath(AssetManager assets, String path) {
465466 }
466467
467468 public static int [] addAssetPaths (AssetManager assets , String [] paths ) {
468- if (sAssetManager_addAssetPaths_method == null ) {
469- sAssetManager_addAssetPaths_method = getMethod (AssetManager .class ,
470- "addAssetPaths" , new Class []{String [].class });
469+ if (Build .VERSION .SDK_INT < 28 ) {
470+ if (sAssetManager_addAssetPaths_method == null ) {
471+ sAssetManager_addAssetPaths_method = getMethod (AssetManager .class ,
472+ "addAssetPaths" , new Class []{String [].class });
473+ }
474+ if (sAssetManager_addAssetPaths_method == null ) return null ;
475+ return invoke (sAssetManager_addAssetPaths_method , assets , new Object []{paths });
476+ } else {
477+ // `AssetManager#addAssetPaths` becomes unavailable since android 9.0,
478+ // use recursively `addAssetPath` instead.
479+ int N = paths .length ;
480+ int [] ids = new int [N ];
481+ for (int i = 0 ; i < N ; i ++) {
482+ ids [i ] = addAssetPath (assets , paths [i ]);
483+ }
484+ return ids ;
471485 }
472- if (sAssetManager_addAssetPaths_method == null ) return null ;
473- return invoke (sAssetManager_addAssetPaths_method , assets , new Object []{paths });
474486 }
475487
476488 public static void mergeResources (Application app , Object activityThread , String [] assetPaths ) {
@@ -484,9 +496,13 @@ public static void mergeResources(Application app, Object activityThread, String
484496 addAssetPaths (newAssetManager , assetPaths );
485497
486498 try {
487- Method mEnsureStringBlocks = AssetManager .class .getDeclaredMethod ("ensureStringBlocks" , new Class [0 ]);
488- mEnsureStringBlocks .setAccessible (true );
489- mEnsureStringBlocks .invoke (newAssetManager , new Object [0 ]);
499+ if (Build .VERSION .SDK_INT < 28 ) {
500+ Method mEnsureStringBlocks = AssetManager .class .getDeclaredMethod ("ensureStringBlocks" , new Class [0 ]);
501+ mEnsureStringBlocks .setAccessible (true );
502+ mEnsureStringBlocks .invoke (newAssetManager , new Object [0 ]);
503+ } else {
504+ // `AssetManager#ensureStringBlocks` becomes unavailable since android 9.0
505+ }
490506
491507 Collection <WeakReference <Resources >> references ;
492508
@@ -737,6 +753,34 @@ public static Intent getIntentOfLaunchActivityItem(Object item) {
737753 return getValue (f , item );
738754 }
739755
756+ public static void resetResourcesAndTheme (Activity activity , int themeId ) {
757+ AssetManager newAssetManager = activity .getApplication ().getAssets ();
758+ Resources resources = activity .getResources ();
759+
760+ // Set the activity resources assets to the application one
761+ try {
762+ Field mResourcesImpl = Resources .class .getDeclaredField ("mResourcesImpl" );
763+ mResourcesImpl .setAccessible (true );
764+ Object resourceImpl = mResourcesImpl .get (resources );
765+ Field implAssets = resourceImpl .getClass ().getDeclaredField ("mAssets" );
766+ implAssets .setAccessible (true );
767+ implAssets .set (resourceImpl , newAssetManager );
768+ } catch (Throwable e ) {
769+ android .util .Log .e ("Small" , "Failed to update resources for activity " + activity , e );
770+ }
771+
772+ // Reset the theme
773+ try {
774+ Field mt = ContextThemeWrapper .class .getDeclaredField ("mTheme" );
775+ mt .setAccessible (true );
776+ mt .set (activity , null );
777+ } catch (Throwable e ) {
778+ android .util .Log .e ("Small" , "Failed to update existing theme for activity " + activity , e );
779+ }
780+
781+ activity .setTheme (themeId );
782+ }
783+
740784 //______________________________________________________________________________________________
741785 // Private
742786
0 commit comments