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

Fixed some null pointer exception.

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