private static Context newDefaultAppContext(PluginDescriptor pluginDescriptor) { Context newContext = null; if (pluginDescriptor != null && pluginDescriptor.getPluginContext() != null) { Context originContext = pluginDescriptor.getPluginContext(); newContext = PluginCreator.createPluginContext( ((PluginContextTheme) originContext).getPluginDescriptor(), sApplication, originContext.getResources(), (DexClassLoader) originContext.getClassLoader()); newContext.setTheme(pluginDescriptor.getApplicationTheme()); } return newContext; }
/** * 根据当前插件的默认Context, 为当前插件的组件创建一个单独的context * * @param pluginContext * @param base 由系统创建的Context。 其实际类型应该是ContextImpl * @return */ /*package*/ static Context getNewPluginComponentContext( Context pluginContext, Context base, int theme) { Context newContext = null; if (pluginContext != null) { newContext = PluginCreator.createPluginContext( ((PluginContextTheme) pluginContext).getPluginDescriptor(), base, pluginContext.getResources(), (DexClassLoader) pluginContext.getClassLoader()); newContext.setTheme(sApplication.getApplicationContext().getApplicationInfo().theme); } return newContext; }
/** * 构造插件信息 * * @param */ static void ensurePluginInited(PluginDescriptor pluginDescriptor) { if (pluginDescriptor != null) { DexClassLoader pluginClassLoader = pluginDescriptor.getPluginClassLoader(); if (pluginClassLoader == null) { LogUtil.e( "正在初始化插件" + pluginDescriptor.getPackageName() + "Resources, DexClassLoader, Context, Application"); LogUtil.d("是否为独立插件", pluginDescriptor.isStandalone()); Resources pluginRes = PluginCreator.createPluginResource(sApplication, pluginDescriptor); pluginClassLoader = PluginCreator.createPluginClassLoader( pluginDescriptor.getInstalledPath(), pluginDescriptor.isStandalone(), pluginDescriptor.getDependencies()); Context pluginContext = PluginCreator.createPluginContext( pluginDescriptor, sApplication, pluginRes, pluginClassLoader); // 插件Context默认主题设置为插件application主题 pluginContext.setTheme(pluginDescriptor.getApplicationTheme()); pluginDescriptor.setPluginContext(pluginContext); pluginDescriptor.setPluginClassLoader(pluginClassLoader); try { ActivityThread.installPackageInfo(getApplicatoin(), pluginDescriptor.getPackageName()); } catch (ClassNotFoundException e) { e.printStackTrace(); } callPluginApplicationOnCreate(pluginDescriptor); LogUtil.e("初始化插件" + pluginDescriptor.getPackageName() + "完成"); } } }