|
42 | 42 | import androidx.pluginmgr.utils.PluginManifestUtil;
|
43 | 43 | import androidx.pluginmgr.verify.PluginNotFoundException;
|
44 | 44 | import androidx.pluginmgr.verify.PluginOverdueVerifier;
|
| 45 | +import android.content.pm.ApplicationInfo; |
| 46 | +import android.app.*; |
| 47 | +import java.lang.reflect.Field; |
| 48 | +import android.content.*; |
| 49 | +import androidx.pluginmgr.environment.PluginContext; |
45 | 50 |
|
46 | 51 | /**
|
47 | 52 | * 插件管理器
|
@@ -110,7 +115,8 @@ private PluginManager(Context context) {
|
110 | 115 | }
|
111 | 116 | DelegateActivityThread delegateActivityThread = DelegateActivityThread.getSingleton();
|
112 | 117 | Instrumentation originInstrumentation = delegateActivityThread.getInstrumentation();
|
113 |
| - delegateActivityThread.setInstrumentation(new PluginInstrumentation(originInstrumentation)); |
| 118 | + pluginInstrumentation = new PluginInstrumentation(originInstrumentation); |
| 119 | + delegateActivityThread.setInstrumentation(pluginInstrumentation); |
114 | 120 | }
|
115 | 121 |
|
116 | 122 |
|
@@ -239,7 +245,7 @@ public Collection<PlugInfo> loadPlugin(final File pluginSrcDirFile)
|
239 | 245 | pluginPkgToInfoMap.clear();
|
240 | 246 | }
|
241 | 247 | File[] pluginApkFiles = pluginSrcDirFile.listFiles(this);
|
242 |
| - if (pluginApkFiles == null || pluginApkFiles.length < 1) { |
| 248 | + if (pluginApkFiles == null || pluginApkFiles.length == 0) { |
243 | 249 | throw new FileNotFoundException("could not find plugins in:"
|
244 | 250 | + pluginSrcDirFile);
|
245 | 251 | }
|
@@ -290,11 +296,40 @@ private PlugInfo buildPlugInfo(File pluginApk, String pluginId,
|
290 | 296 | PluginClassLoader pluginClassLoader = new PluginClassLoader(info, dexPath, dexOutputPath
|
291 | 297 | , getPluginLibPath(info).getAbsolutePath(), ClassLoader.getSystemClassLoader().getParent());
|
292 | 298 | info.setClassLoader(pluginClassLoader);
|
293 |
| - |
| 299 | + ApplicationInfo appInfo = info.getPackageInfo().applicationInfo; |
| 300 | + String appClassName = null; |
| 301 | + if(appInfo != null){ |
| 302 | + appClassName = appInfo.name; |
| 303 | + } |
| 304 | + Application app = makeApplication(pluginClassLoader,appClassName); |
| 305 | + attachBaseContext(info,app); |
| 306 | + info.setApplication(app); |
294 | 307 | Log.i(TAG, "buildPlugInfo: " + info);
|
295 | 308 | return info;
|
296 | 309 | }
|
297 | 310 |
|
| 311 | + private void attachBaseContext(PlugInfo info, Application app) |
| 312 | + { |
| 313 | + try{ |
| 314 | + Field mBase = ContextWrapper.class.getDeclaredField("mBase"); |
| 315 | + mBase.setAccessible(true); |
| 316 | + mBase.set(app, new PluginContext(app.getBaseContext(), info)); |
| 317 | + }catch(Throwable e){ |
| 318 | + |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + private Application makeApplication(PluginClassLoader pluginClassLoader, String appClassName) |
| 323 | + { |
| 324 | + if(appClassName != null) { |
| 325 | + try{ |
| 326 | + return (Application) pluginClassLoader.loadClass(appClassName).newInstance(); |
| 327 | + }catch(Throwable ignored){} |
| 328 | + } |
| 329 | + |
| 330 | + return new Application(); |
| 331 | + } |
| 332 | + |
298 | 333 |
|
299 | 334 | /**
|
300 | 335 | * 将Apk复制到私有目录
|
|
0 commit comments