提交 7857a51c 编写于 作者: S superq_sky

Fixed some null pointer exception.

上级 c5e3a88b
......@@ -253,11 +253,13 @@ public class PluginManager {
}
public LoadedPlugin getLoadedPlugin(Intent intent) {
ComponentName component = PluginUtil.getComponent(intent);
return getLoadedPlugin(component.getPackageName());
return getLoadedPlugin(PluginUtil.getComponent(intent));
}
public LoadedPlugin getLoadedPlugin(ComponentName component) {
if (component == null) {
return null;
}
return this.getLoadedPlugin(component.getPackageName());
}
......
......@@ -88,7 +88,7 @@ public final class LoadedPlugin {
if (Constants.COMBINE_CLASSLOADER) {
try {
DexUtil.insertDex(loader);
DexUtil.insertDex(loader, parent);
} catch (Exception e) {
e.printStackTrace();
}
......
......@@ -18,6 +18,7 @@ package com.didi.virtualapk.internal;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Application;
import android.app.Fragment;
import android.app.Instrumentation;
import android.content.ComponentName;
......@@ -91,30 +92,44 @@ public class VAInstrumentation extends Instrumentation implements Handler.Callba
public Activity newActivity(ClassLoader cl, String className, Intent intent) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
try {
cl.loadClass(className);
Log.i(TAG, String.format("newActivity[%s]", className));
} catch (ClassNotFoundException e) {
ComponentName component = PluginUtil.getComponent(intent);
LoadedPlugin plugin = this.mPluginManager.getLoadedPlugin(component);
if (component == null) {
return mBase.newActivity(cl, className, intent);
}
String targetClassName = component.getClassName();
Log.i(TAG, String.format("newActivity[%s : %s/%s]", className, component.getPackageName(), targetClassName));
if (plugin != null) {
Activity activity = mBase.newActivity(plugin.getClassLoader(), targetClassName, intent);
activity.setIntent(intent);
try {
// for 4.1+
Reflector.with(activity).field("mResources").set(plugin.getResources());
} catch (Exception ignored) {
// ignored.
}
return activity;
LoadedPlugin plugin = this.mPluginManager.getLoadedPlugin(component);
if (plugin == null) {
return mBase.newActivity(cl, className, intent);
}
Activity activity = mBase.newActivity(plugin.getClassLoader(), targetClassName, intent);
activity.setIntent(intent);
try {
// for 4.1+
Reflector.with(activity).field("mResources").set(plugin.getResources());
} catch (Exception ignored) {
// ignored.
}
return activity;
}
return mBase.newActivity(cl, className, intent);
}
@Override
public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return mBase.newApplication(cl, className, context);
}
@Override
public void callActivityOnCreate(Activity activity, Bundle icicle) {
......@@ -159,7 +174,7 @@ public class VAInstrumentation extends Instrumentation implements Handler.Callba
try {
Reflector reflector = Reflector.with(r);
Intent intent = reflector.field("intent").get();
intent.setExtrasClassLoader(VAInstrumentation.class.getClassLoader());
intent.setExtrasClassLoader(mPluginManager.getHostContext().getClassLoader());
ActivityInfo activityInfo = reflector.field("activityInfo").get();
if (PluginUtil.isIntentFromPlugin(intent)) {
......
......@@ -28,58 +28,46 @@ import java.lang.reflect.Field;
import java.util.List;
import dalvik.system.DexClassLoader;
import dalvik.system.PathClassLoader;
public class DexUtil {
private static boolean sHasInsertedNativeLibrary = false;
public static void insertDex(DexClassLoader dexClassLoader) throws Exception {
Object baseDexElements = getDexElements(getPathList(getPathClassLoader()));
public static void insertDex(DexClassLoader dexClassLoader, ClassLoader baseClassLoader) throws Exception {
Object baseDexElements = getDexElements(getPathList(baseClassLoader));
Object newDexElements = getDexElements(getPathList(dexClassLoader));
Object allDexElements = combineArray(baseDexElements, newDexElements);
Object pathList = getPathList(getPathClassLoader());
Object pathList = getPathList(baseClassLoader);
Reflector.with(pathList).field("dexElements").set(allDexElements);
insertNativeLibrary(dexClassLoader);
}
private static PathClassLoader getPathClassLoader() {
PathClassLoader pathClassLoader = (PathClassLoader) DexUtil.class.getClassLoader();
return pathClassLoader;
insertNativeLibrary(dexClassLoader, baseClassLoader);
}
private static Object getDexElements(Object pathList) throws Exception {
return Reflector.with(pathList).field("dexElements").get();
}
private static Object getPathList(Object baseDexClassLoader) throws Exception {
private static Object getPathList(ClassLoader baseDexClassLoader) throws Exception {
return Reflector.with(baseDexClassLoader).field("pathList").get();
}
private static Object combineArray(Object firstArray, Object secondArray) {
Class<?> localClass = firstArray.getClass().getComponentType();
int firstArrayLength = Array.getLength(firstArray);
int allLength = firstArrayLength + Array.getLength(secondArray);
Object result = Array.newInstance(localClass, allLength);
for (int k = 0; k < allLength; ++k) {
if (k < firstArrayLength) {
Array.set(result, k, Array.get(firstArray, k));
} else {
Array.set(result, k, Array.get(secondArray, k - firstArrayLength));
}
}
int secondArrayLength = Array.getLength(secondArray);
Object result = Array.newInstance(localClass, firstArrayLength + secondArrayLength);
System.arraycopy(firstArray, 0, result, 0, firstArrayLength);
System.arraycopy(secondArray, 0, result, firstArrayLength, secondArrayLength);
return result;
}
private static synchronized void insertNativeLibrary(DexClassLoader dexClassLoader) throws Exception {
private static synchronized void insertNativeLibrary(DexClassLoader dexClassLoader, ClassLoader baseClassLoader) throws Exception {
if (sHasInsertedNativeLibrary) {
return;
}
sHasInsertedNativeLibrary = true;
Context context = ActivityThread.currentApplication();
Object basePathList = getPathList(getPathClassLoader());
Object basePathList = getPathList(baseClassLoader);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
Reflector reflector = Reflector.with(basePathList);
List<File> nativeLibraryDirectories = reflector.field("nativeLibraryDirectories").get();
......
......@@ -51,16 +51,22 @@ import java.util.zip.ZipFile;
*/
public class PluginUtil {
public static String getTargetActivity(Intent intent) {
return intent.getStringExtra(Constants.KEY_TARGET_ACTIVITY);
}
public static ComponentName getComponent(Intent intent) {
return new ComponentName(intent.getStringExtra(Constants.KEY_TARGET_PACKAGE),
if (intent == null) {
return null;
}
if (isIntentFromPlugin(intent)) {
return new ComponentName(intent.getStringExtra(Constants.KEY_TARGET_PACKAGE),
intent.getStringExtra(Constants.KEY_TARGET_ACTIVITY));
}
return intent.getComponent();
}
public static boolean isIntentFromPlugin(Intent intent) {
if (intent == null) {
return false;
}
return intent.getBooleanExtra(Constants.KEY_IS_PLUGIN, false);
}
......@@ -166,6 +172,9 @@ public class PluginUtil {
}
public static IBinder getBinder(Bundle bundle, String key) {
if (bundle == null) {
return null;
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return bundle.getBinder(key);
} else {
......
......@@ -28,7 +28,6 @@ import android.util.Pair;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* Created by renyugang on 16/11/10.
......@@ -77,17 +76,22 @@ public class RunUtil {
return AsyncTask.THREAD_POOL_EXECUTOR;
}
public static String getProcessNameByPid(Context context, int pid) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcessList = manager.getRunningAppProcesses();
if (appProcessList != null) {
for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) {
if (pid == appProcessInfo.pid) {
return appProcessInfo.processName;
private static String getProcessNameByPid(Context context, int pid) {
try {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> appProcessList = manager.getRunningAppProcesses();
if (appProcessList != null) {
for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) {
if (pid == appProcessInfo.pid) {
return appProcessInfo.processName;
}
}
}
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册