Commit 65224ed2 authored by vvb2060's avatar vvb2060 Committed by John Wu

Fix NPE when apk could not be parsed

parent 0a28dfe1
...@@ -6,6 +6,7 @@ import android.content.ContentResolver; ...@@ -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 { ...@@ -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 { ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment