2020-12-29 01:44:02 -08:00
|
|
|
package com.topjohnwu.magisk;
|
|
|
|
|
|
|
|
import android.app.AppComponentFactory;
|
|
|
|
import android.app.Application;
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
2021-01-26 03:40:25 -08:00
|
|
|
import android.content.ContextWrapper;
|
2021-04-17 22:14:54 -07:00
|
|
|
import android.content.pm.ApplicationInfo;
|
2021-11-25 13:13:30 +08:00
|
|
|
import android.content.pm.PackageInfo;
|
2021-04-17 22:14:54 -07:00
|
|
|
import android.content.pm.PackageManager;
|
2020-12-29 01:44:02 -08:00
|
|
|
import android.net.Uri;
|
2021-01-26 03:40:25 -08:00
|
|
|
import android.os.Build;
|
2020-12-29 01:44:02 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2021-01-26 03:40:25 -08:00
|
|
|
import java.lang.reflect.Field;
|
2020-12-29 01:44:02 -08:00
|
|
|
|
2021-09-02 21:31:33 -07:00
|
|
|
import io.michaelrocks.paranoid.Obfuscate;
|
|
|
|
|
|
|
|
@Obfuscate
|
2020-12-29 01:44:02 -08:00
|
|
|
public class InjectAPK {
|
|
|
|
|
2021-04-17 22:14:54 -07:00
|
|
|
static Object componentFactory;
|
|
|
|
|
|
|
|
private static DelegateComponentFactory getComponentFactory() {
|
|
|
|
return (DelegateComponentFactory) componentFactory;
|
|
|
|
}
|
|
|
|
|
2021-01-26 03:40:25 -08:00
|
|
|
@SuppressWarnings("ResultOfMethodCallIgnored")
|
2020-12-29 01:44:02 -08:00
|
|
|
static Application setup(Context context) {
|
2021-01-26 03:40:25 -08:00
|
|
|
// Get ContextImpl
|
|
|
|
while (context instanceof ContextWrapper) {
|
|
|
|
context = ((ContextWrapper) context).getBaseContext();
|
|
|
|
}
|
|
|
|
|
2020-12-29 01:44:02 -08:00
|
|
|
File apk = DynAPK.current(context);
|
|
|
|
File update = DynAPK.update(context);
|
|
|
|
if (update.exists())
|
|
|
|
update.renameTo(apk);
|
|
|
|
if (!apk.exists()) {
|
|
|
|
// Try copying APK
|
|
|
|
Uri uri = new Uri.Builder().scheme("content")
|
|
|
|
.authority("com.topjohnwu.magisk.provider")
|
|
|
|
.encodedPath("apk_file").build();
|
|
|
|
ContentResolver resolver = context.getContentResolver();
|
2021-01-26 03:40:25 -08:00
|
|
|
try (InputStream src = resolver.openInputStream(uri)) {
|
|
|
|
if (src != null) {
|
2020-12-29 01:44:02 -08:00
|
|
|
try (OutputStream out = new FileOutputStream(apk)) {
|
|
|
|
byte[] buf = new byte[4096];
|
2021-01-26 03:40:25 -08:00
|
|
|
for (int read; (read = src.read(buf)) >= 0;) {
|
2020-12-29 01:44:02 -08:00
|
|
|
out.write(buf, 0, read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 03:40:25 -08:00
|
|
|
} catch (Exception ignored) {}
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
|
|
|
if (apk.exists()) {
|
2021-01-26 03:40:25 -08:00
|
|
|
ClassLoader cl = new InjectedClassLoader(apk);
|
2021-04-17 22:14:54 -07:00
|
|
|
PackageManager pm = context.getPackageManager();
|
2021-11-25 13:13:30 +08:00
|
|
|
PackageInfo pkgInfo = pm.getPackageArchiveInfo(apk.getPath(), 0);
|
2020-12-29 01:44:02 -08:00
|
|
|
try {
|
2021-11-25 13:13:30 +08:00
|
|
|
return createApp(context, cl, pkgInfo.applicationInfo);
|
2020-12-29 01:44:02 -08:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(InjectAPK.class.getSimpleName(), "", e);
|
|
|
|
apk.delete();
|
|
|
|
}
|
2021-04-17 22:14:54 -07:00
|
|
|
// fallthrough
|
|
|
|
}
|
|
|
|
|
|
|
|
ClassLoader cl = new RedirectClassLoader();
|
|
|
|
try {
|
|
|
|
setClassLoader(context, cl);
|
|
|
|
if (Build.VERSION.SDK_INT >= 28) {
|
|
|
|
getComponentFactory().loader = cl;
|
2021-01-26 03:40:25 -08:00
|
|
|
}
|
2021-04-17 22:14:54 -07:00
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(InjectAPK.class.getSimpleName(), "", e);
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
2021-04-17 22:14:54 -07:00
|
|
|
|
|
|
|
return null;
|
2021-01-26 03:40:25 -08:00
|
|
|
}
|
|
|
|
|
2021-11-25 13:13:30 +08:00
|
|
|
private static Application createApp(Context context, ClassLoader cl, ApplicationInfo info)
|
|
|
|
throws ReflectiveOperationException {
|
|
|
|
// Create the receiver Application
|
|
|
|
Object app = cl.loadClass(info.className)
|
|
|
|
.getConstructor(Object.class)
|
|
|
|
.newInstance(DynAPK.pack(dynData()));
|
|
|
|
|
|
|
|
// Create the receiver component factory
|
|
|
|
Object factory = null;
|
|
|
|
if (Build.VERSION.SDK_INT >= 28) {
|
|
|
|
factory = cl.loadClass(info.appComponentFactory).newInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
setClassLoader(context, cl);
|
|
|
|
|
|
|
|
// Finally, set variables
|
|
|
|
if (Build.VERSION.SDK_INT >= 28) {
|
|
|
|
getComponentFactory().loader = cl;
|
|
|
|
getComponentFactory().receiver = (AppComponentFactory) factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Application) app;
|
|
|
|
}
|
|
|
|
|
2021-01-26 03:40:25 -08:00
|
|
|
// Replace LoadedApk mClassLoader
|
|
|
|
private static void setClassLoader(Context impl, ClassLoader cl)
|
|
|
|
throws NoSuchFieldException, IllegalAccessException {
|
|
|
|
Field mInfo = impl.getClass().getDeclaredField("mPackageInfo");
|
|
|
|
mInfo.setAccessible(true);
|
|
|
|
Object loadedApk = mInfo.get(impl);
|
|
|
|
Field mcl = loadedApk.getClass().getDeclaredField("mClassLoader");
|
|
|
|
mcl.setAccessible(true);
|
|
|
|
mcl.set(loadedApk, cl);
|
2020-12-29 01:44:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static DynAPK.Data dynData() {
|
|
|
|
DynAPK.Data data = new DynAPK.Data();
|
|
|
|
data.version = BuildConfig.STUB_VERSION;
|
2021-01-22 20:45:37 -08:00
|
|
|
data.classToComponent = Mapping.inverseMap;
|
2020-12-29 01:44:02 -08:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|