Fix NPE when apk could not be parsed

This commit is contained in:
vvb2060 2021-11-25 13:13:30 +08:00 committed by John Wu
parent 0a28dfe1e2
commit 65224ed22b

View File

@ -6,6 +6,7 @@ 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.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@ -59,8 +60,31 @@ public class InjectAPK {
if (apk.exists()) { if (apk.exists()) {
ClassLoader cl = new InjectedClassLoader(apk); ClassLoader cl = new InjectedClassLoader(apk);
PackageManager pm = context.getPackageManager(); PackageManager pm = context.getPackageManager();
ApplicationInfo info = pm.getPackageArchiveInfo(apk.getPath(), 0).applicationInfo; PackageInfo pkgInfo = pm.getPackageArchiveInfo(apk.getPath(), 0);
try { try {
return createApp(context, cl, pkgInfo.applicationInfo);
} catch (Exception e) {
Log.e(InjectAPK.class.getSimpleName(), "", e);
apk.delete();
}
// fallthrough
}
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;
}
private static Application createApp(Context context, ClassLoader cl, ApplicationInfo info)
throws ReflectiveOperationException {
// Create the receiver Application // Create the receiver Application
Object app = cl.loadClass(info.className) Object app = cl.loadClass(info.className)
.getConstructor(Object.class) .getConstructor(Object.class)
@ -81,24 +105,6 @@ public class InjectAPK {
} }
return (Application) app; return (Application) app;
} catch (Exception e) {
Log.e(InjectAPK.class.getSimpleName(), "", e);
apk.delete();
}
// fallthrough
}
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