Update stub implementation

Prevent some potential issues
This commit is contained in:
topjohnwu 2021-04-17 22:14:54 -07:00
parent 7e01f9c95e
commit 4c1ea0e421
4 changed files with 35 additions and 26 deletions

View File

@ -4,10 +4,12 @@ import com.topjohnwu.magisk.utils.DynamicClassLoader;
import java.io.File; import java.io.File;
class InjectedClassLoader extends DynamicClassLoader { // Wrap the actual classloader as we only want to resolve classname
// mapping when loading from platform (via LoadedApk.mClassLoader)
class InjectedClassLoader extends ClassLoader {
InjectedClassLoader(File apk) { InjectedClassLoader(File apk) {
super(apk); super(new DynamicClassLoader(apk));
} }
@Override @Override

View File

@ -12,12 +12,11 @@ import android.content.Intent;
@SuppressLint("NewApi") @SuppressLint("NewApi")
public class DelegateComponentFactory extends AppComponentFactory { public class DelegateComponentFactory extends AppComponentFactory {
static DelegateComponentFactory INSTANCE;
ClassLoader loader; ClassLoader loader;
AppComponentFactory receiver; AppComponentFactory receiver;
public DelegateComponentFactory() { public DelegateComponentFactory() {
INSTANCE = this; InjectAPK.componentFactory = this;
} }
@Override @Override
@ -53,7 +52,6 @@ public class DelegateComponentFactory extends AppComponentFactory {
@Override @Override
public ContentProvider instantiateProvider(ClassLoader cl, String className) public ContentProvider instantiateProvider(ClassLoader cl, String className)
throws ClassNotFoundException, IllegalAccessException, InstantiationException { throws ClassNotFoundException, IllegalAccessException, InstantiationException {
if (loader == null) loader = cl;
if (receiver != null) if (receiver != null)
return receiver.instantiateProvider(loader, className); return receiver.instantiateProvider(loader, className);
return create(className); return create(className);

View File

@ -5,6 +5,8 @@ import android.app.Application;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper; import android.content.ContextWrapper;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
@ -17,6 +19,12 @@ import java.lang.reflect.Field;
public class InjectAPK { public class InjectAPK {
static Object componentFactory;
private static DelegateComponentFactory getComponentFactory() {
return (DelegateComponentFactory) componentFactory;
}
@SuppressWarnings("ResultOfMethodCallIgnored") @SuppressWarnings("ResultOfMethodCallIgnored")
static Application setup(Context context) { static Application setup(Context context) {
// Get ContextImpl // Get ContextImpl
@ -28,7 +36,6 @@ public class InjectAPK {
File update = DynAPK.update(context); File update = DynAPK.update(context);
if (update.exists()) if (update.exists())
update.renameTo(apk); update.renameTo(apk);
Application result = null;
if (!apk.exists()) { if (!apk.exists()) {
// Try copying APK // Try copying APK
Uri uri = new Uri.Builder().scheme("content") Uri uri = new Uri.Builder().scheme("content")
@ -48,42 +55,47 @@ public class InjectAPK {
} }
if (apk.exists()) { if (apk.exists()) {
ClassLoader cl = new InjectedClassLoader(apk); ClassLoader cl = new InjectedClassLoader(apk);
PackageManager pm = context.getPackageManager();
ApplicationInfo info = pm.getPackageArchiveInfo(apk.getPath(), 0).applicationInfo;
try { try {
// Create the receiver Application // Create the receiver Application
Object app = cl.loadClass(Mapping.get("APP")).getConstructor(Object.class) Object app = cl.loadClass(info.className)
.getConstructor(Object.class)
.newInstance(DynAPK.pack(dynData())); .newInstance(DynAPK.pack(dynData()));
// Create the receiver component factory // Create the receiver component factory
Object factory = null; Object factory = null;
if (Build.VERSION.SDK_INT >= 28) { if (Build.VERSION.SDK_INT >= 28) {
factory = cl.loadClass(Mapping.get("ACF")).newInstance(); factory = cl.loadClass(info.appComponentFactory).newInstance();
} }
setClassLoader(context, cl); setClassLoader(context, cl);
// Finally, set variables // Finally, set variables
result = (Application) app;
if (Build.VERSION.SDK_INT >= 28) { if (Build.VERSION.SDK_INT >= 28) {
DelegateComponentFactory.INSTANCE.loader = cl; getComponentFactory().loader = cl;
DelegateComponentFactory.INSTANCE.receiver = (AppComponentFactory) factory; getComponentFactory().receiver = (AppComponentFactory) factory;
} }
return (Application) app;
} catch (Exception e) { } catch (Exception e) {
Log.e(InjectAPK.class.getSimpleName(), "", e); Log.e(InjectAPK.class.getSimpleName(), "", e);
apk.delete(); apk.delete();
} }
} else { // fallthrough
ClassLoader cl = new RedirectClassLoader();
try {
setClassLoader(context, cl);
if (Build.VERSION.SDK_INT >= 28) {
DelegateComponentFactory.INSTANCE.loader = cl;
}
} catch (Exception e) {
// Should never happen
Log.e(InjectAPK.class.getSimpleName(), "", e);
}
} }
return result;
ClassLoader cl = new RedirectClassLoader();
try {
setClassLoader(context, cl);
if (Build.VERSION.SDK_INT >= 28) {
getComponentFactory().loader = cl;
}
} catch (Exception e) {
Log.e(InjectAPK.class.getSimpleName(), "", e);
}
return null;
} }
// Replace LoadedApk mClassLoader // Replace LoadedApk mClassLoader

View File

@ -34,9 +34,6 @@ public class Mapping {
for (Map.Entry<String, String> e : map.entrySet()) { for (Map.Entry<String, String> e : map.entrySet()) {
inverseMap.put(e.getValue(), e.getKey()); inverseMap.put(e.getValue(), e.getKey());
} }
map.put("APP", "com.topjohnwu.magisk.core.App");
map.put("ACF", "androidx.core.app.CoreComponentFactory");
} }
public static String get(String name) { public static String get(String name) {