Skip to content

Commit 7857a51

Browse files
committed
Fixed some null pointer exception.
1 parent c5e3a88 commit 7857a51

File tree

6 files changed

+74
-56
lines changed

6 files changed

+74
-56
lines changed

CoreLibrary/src/main/java/com/didi/virtualapk/PluginManager.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,13 @@ public void loadPlugin(File apk) throws Exception {
253253
}
254254

255255
public LoadedPlugin getLoadedPlugin(Intent intent) {
256-
ComponentName component = PluginUtil.getComponent(intent);
257-
return getLoadedPlugin(component.getPackageName());
256+
return getLoadedPlugin(PluginUtil.getComponent(intent));
258257
}
259258

260259
public LoadedPlugin getLoadedPlugin(ComponentName component) {
260+
if (component == null) {
261+
return null;
262+
}
261263
return this.getLoadedPlugin(component.getPackageName());
262264
}
263265

CoreLibrary/src/main/java/com/didi/virtualapk/internal/LoadedPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private static ClassLoader createClassLoader(Context context, File apk, File lib
8888

8989
if (Constants.COMBINE_CLASSLOADER) {
9090
try {
91-
DexUtil.insertDex(loader);
91+
DexUtil.insertDex(loader, parent);
9292
} catch (Exception e) {
9393
e.printStackTrace();
9494
}

CoreLibrary/src/main/java/com/didi/virtualapk/internal/VAInstrumentation.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.annotation.TargetApi;
2020
import android.app.Activity;
21+
import android.app.Application;
2122
import android.app.Fragment;
2223
import android.app.Instrumentation;
2324
import android.content.ComponentName;
@@ -91,30 +92,44 @@ private void injectIntent(Intent intent) {
9192
public Activity newActivity(ClassLoader cl, String className, Intent intent) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
9293
try {
9394
cl.loadClass(className);
95+
Log.i(TAG, String.format("newActivity[%s]", className));
96+
9497
} catch (ClassNotFoundException e) {
9598
ComponentName component = PluginUtil.getComponent(intent);
96-
LoadedPlugin plugin = this.mPluginManager.getLoadedPlugin(component);
99+
100+
if (component == null) {
101+
return mBase.newActivity(cl, className, intent);
102+
}
103+
97104
String targetClassName = component.getClassName();
98-
99105
Log.i(TAG, String.format("newActivity[%s : %s/%s]", className, component.getPackageName(), targetClassName));
100-
101-
if (plugin != null) {
102-
Activity activity = mBase.newActivity(plugin.getClassLoader(), targetClassName, intent);
103-
activity.setIntent(intent);
104-
105-
try {
106-
// for 4.1+
107-
Reflector.with(activity).field("mResources").set(plugin.getResources());
108-
} catch (Exception ignored) {
109-
// ignored.
110-
}
111-
112-
return activity;
106+
107+
LoadedPlugin plugin = this.mPluginManager.getLoadedPlugin(component);
108+
109+
if (plugin == null) {
110+
return mBase.newActivity(cl, className, intent);
111+
}
112+
113+
Activity activity = mBase.newActivity(plugin.getClassLoader(), targetClassName, intent);
114+
activity.setIntent(intent);
115+
116+
try {
117+
// for 4.1+
118+
Reflector.with(activity).field("mResources").set(plugin.getResources());
119+
} catch (Exception ignored) {
120+
// ignored.
113121
}
122+
123+
return activity;
114124
}
115125

116126
return mBase.newActivity(cl, className, intent);
117127
}
128+
129+
@Override
130+
public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
131+
return mBase.newApplication(cl, className, context);
132+
}
118133

119134
@Override
120135
public void callActivityOnCreate(Activity activity, Bundle icicle) {
@@ -159,7 +174,7 @@ public boolean handleMessage(Message msg) {
159174
try {
160175
Reflector reflector = Reflector.with(r);
161176
Intent intent = reflector.field("intent").get();
162-
intent.setExtrasClassLoader(VAInstrumentation.class.getClassLoader());
177+
intent.setExtrasClassLoader(mPluginManager.getHostContext().getClassLoader());
163178
ActivityInfo activityInfo = reflector.field("activityInfo").get();
164179

165180
if (PluginUtil.isIntentFromPlugin(intent)) {

CoreLibrary/src/main/java/com/didi/virtualapk/utils/DexUtil.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -28,58 +28,46 @@
2828
import java.util.List;
2929

3030
import dalvik.system.DexClassLoader;
31-
import dalvik.system.PathClassLoader;
3231

3332
public class DexUtil {
3433
private static boolean sHasInsertedNativeLibrary = false;
3534

36-
public static void insertDex(DexClassLoader dexClassLoader) throws Exception {
37-
Object baseDexElements = getDexElements(getPathList(getPathClassLoader()));
35+
public static void insertDex(DexClassLoader dexClassLoader, ClassLoader baseClassLoader) throws Exception {
36+
Object baseDexElements = getDexElements(getPathList(baseClassLoader));
3837
Object newDexElements = getDexElements(getPathList(dexClassLoader));
3938
Object allDexElements = combineArray(baseDexElements, newDexElements);
40-
Object pathList = getPathList(getPathClassLoader());
39+
Object pathList = getPathList(baseClassLoader);
4140
Reflector.with(pathList).field("dexElements").set(allDexElements);
4241

43-
insertNativeLibrary(dexClassLoader);
44-
45-
}
46-
47-
private static PathClassLoader getPathClassLoader() {
48-
PathClassLoader pathClassLoader = (PathClassLoader) DexUtil.class.getClassLoader();
49-
return pathClassLoader;
42+
insertNativeLibrary(dexClassLoader, baseClassLoader);
5043
}
5144

5245
private static Object getDexElements(Object pathList) throws Exception {
5346
return Reflector.with(pathList).field("dexElements").get();
5447
}
5548

56-
private static Object getPathList(Object baseDexClassLoader) throws Exception {
49+
private static Object getPathList(ClassLoader baseDexClassLoader) throws Exception {
5750
return Reflector.with(baseDexClassLoader).field("pathList").get();
5851
}
5952

6053
private static Object combineArray(Object firstArray, Object secondArray) {
6154
Class<?> localClass = firstArray.getClass().getComponentType();
6255
int firstArrayLength = Array.getLength(firstArray);
63-
int allLength = firstArrayLength + Array.getLength(secondArray);
64-
Object result = Array.newInstance(localClass, allLength);
65-
for (int k = 0; k < allLength; ++k) {
66-
if (k < firstArrayLength) {
67-
Array.set(result, k, Array.get(firstArray, k));
68-
} else {
69-
Array.set(result, k, Array.get(secondArray, k - firstArrayLength));
70-
}
71-
}
56+
int secondArrayLength = Array.getLength(secondArray);
57+
Object result = Array.newInstance(localClass, firstArrayLength + secondArrayLength);
58+
System.arraycopy(firstArray, 0, result, 0, firstArrayLength);
59+
System.arraycopy(secondArray, 0, result, firstArrayLength, secondArrayLength);
7260
return result;
7361
}
7462

75-
private static synchronized void insertNativeLibrary(DexClassLoader dexClassLoader) throws Exception {
63+
private static synchronized void insertNativeLibrary(DexClassLoader dexClassLoader, ClassLoader baseClassLoader) throws Exception {
7664
if (sHasInsertedNativeLibrary) {
7765
return;
7866
}
7967
sHasInsertedNativeLibrary = true;
8068

8169
Context context = ActivityThread.currentApplication();
82-
Object basePathList = getPathList(getPathClassLoader());
70+
Object basePathList = getPathList(baseClassLoader);
8371
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
8472
Reflector reflector = Reflector.with(basePathList);
8573
List<File> nativeLibraryDirectories = reflector.field("nativeLibraryDirectories").get();

CoreLibrary/src/main/java/com/didi/virtualapk/utils/PluginUtil.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,22 @@
5151
*/
5252
public class PluginUtil {
5353

54-
public static String getTargetActivity(Intent intent) {
55-
return intent.getStringExtra(Constants.KEY_TARGET_ACTIVITY);
56-
}
57-
5854
public static ComponentName getComponent(Intent intent) {
59-
return new ComponentName(intent.getStringExtra(Constants.KEY_TARGET_PACKAGE),
55+
if (intent == null) {
56+
return null;
57+
}
58+
if (isIntentFromPlugin(intent)) {
59+
return new ComponentName(intent.getStringExtra(Constants.KEY_TARGET_PACKAGE),
6060
intent.getStringExtra(Constants.KEY_TARGET_ACTIVITY));
61+
}
62+
63+
return intent.getComponent();
6164
}
6265

6366
public static boolean isIntentFromPlugin(Intent intent) {
67+
if (intent == null) {
68+
return false;
69+
}
6470
return intent.getBooleanExtra(Constants.KEY_IS_PLUGIN, false);
6571
}
6672

@@ -166,6 +172,9 @@ public static void putBinder(Bundle bundle, String key, IBinder value) {
166172
}
167173

168174
public static IBinder getBinder(Bundle bundle, String key) {
175+
if (bundle == null) {
176+
return null;
177+
}
169178
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
170179
return bundle.getBinder(key);
171180
} else {

CoreLibrary/src/main/java/com/didi/virtualapk/utils/RunUtil.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.List;
2929
import java.util.concurrent.CountDownLatch;
3030
import java.util.concurrent.Executor;
31-
import java.util.concurrent.ThreadPoolExecutor;
3231

3332
/**
3433
* Created by renyugang on 16/11/10.
@@ -77,17 +76,22 @@ public static Executor getThreadPool() {
7776
return AsyncTask.THREAD_POOL_EXECUTOR;
7877
}
7978

80-
public static String getProcessNameByPid(Context context, int pid) {
81-
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
82-
List<ActivityManager.RunningAppProcessInfo> appProcessList = manager.getRunningAppProcesses();
83-
if (appProcessList != null) {
84-
for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) {
85-
if (pid == appProcessInfo.pid) {
86-
return appProcessInfo.processName;
79+
private static String getProcessNameByPid(Context context, int pid) {
80+
try {
81+
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
82+
List<ActivityManager.RunningAppProcessInfo> appProcessList = manager.getRunningAppProcesses();
83+
if (appProcessList != null) {
84+
for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessList) {
85+
if (pid == appProcessInfo.pid) {
86+
return appProcessInfo.processName;
87+
}
8788
}
8889
}
90+
91+
} catch (Throwable e) {
92+
e.printStackTrace();
8993
}
90-
94+
9195
return null;
9296
}
9397

0 commit comments

Comments
 (0)