在宿主裏面的AndroidManifest.xml裏面註冊一個空的activitymarkdown
從開始執行execStartActivity到最終將Activity對象new出來這個過程,系統層會去校驗須要啓動的activity的合法性[就是是否有在某個應用的AndroidManifest.xml裏面註冊]以及按啓動要求建立activity對象。清晰了這點咱們就能夠很好的繞過系統的約束,達到咱們的目的:【插件中的組件擁有真正生命週期,徹底交由系統管理、非反射代理】。 簡單來講方案就兩步: Step一、在開始startActivity的時候將須要啓動的插件組件替換成宿主預先聲明號的。app
public ActivityResult execStartActivity(Context who, IBinder contextThread, IBinder token, Activity target,
Intent intent, int requestCode, Bundle options) {
//若是啓動的是插件的activity組件,這裏面將會被替換成宿主預先聲明的
PluginIntentResolver.resolveActivity(intent);
return hackInstrumentation.execStartActivity(who, contextThread, token, target, intent, requestCode, ptions);
}
複製代碼
Step二、在最終建立activity對象的時候改回成插件組件的。框架
@Override
public Activity newActivity(ClassLoader cl, String className, Intent intent) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
ClassLoader orignalCl = cl;
String orginalClassName = className;
String orignalIntent = intent.toString();
if (ProcessUtil.isPluginProcess()) {
// 將PluginStubActivity替換成插件中的activity
if (PluginManagerHelper.isStub(className)) {
String action = intent.getAction();
if (action != null && action.contains(PluginIntentResolver.CLASS_SEPARATOR)) {
String[] targetClassName = action.split(PluginIntentResolver.CLASS_SEPARATOR);
String pluginClassName = targetClassName[0];
final String pid = intent.getStringExtra(PluginIntentResolver.INTENT_EXTRA_PID).trim();
PluginDescriptor pluginDescriptor = TextUtils.isEmpty(pid) ? PluginManagerHelper.getPluginDescriptorByClassName(pluginClassName) : PluginManagerHelper.getPluginDescriptorByPluginId(pid);
Class<?> clazz = PluginLoader.loadPluginClassByName(pluginDescriptor, pluginClassName);
if (clazz != null) {
className = pluginClassName;
cl = clazz.getClassLoader();
intent.setExtrasClassLoader(cl);
if (targetClassName.length > 1) {
// 以前爲了傳遞classNae,intent的action被修改過
// 這裏再把Action還原到原始的Action
intent.setAction(targetClassName[1]);
} else {
intent.setAction(null);
}
// 添加一個標記符
intent.addCategory(RELAUNCH_FLAG + className);
} else {
throw new ClassNotFoundException("pluginClassName : " + pluginClassName, new Throwable());
}
} else if (PluginManagerHelper.isExact(className, PluginDescriptor.ACTIVITY)) {
// 這個邏輯是爲了支持外部app喚起配置了stub_exact的插件Activity
PluginDescriptor pluginDescriptor = PluginManagerHelper.getPluginDescriptorByClassName(className);
if (pluginDescriptor != null) {
boolean isRunning = PluginLauncher.instance().isRunning(pluginDescriptor.getPackageName());
if (!isRunning) {
return waitForLoading(pluginDescriptor);
}
}
Class<?> clazz = PluginLoader.loadPluginClassByName(pluginDescriptor, className);
if (clazz != null) {
cl = clazz.getClassLoader();
} else {
throw new ClassNotFoundException("className : " + className, new Throwable());
}
} else {
// 進入這個分支多是由於activity重啓了,好比橫豎屏切換,因爲上面的分支已經把Action還原到原始到Action了
// 這裏只能經過以前添加的標記符來查找className
boolean found = false;
Set<String> category = intent.getCategories();
if (category != null) {
Iterator<String> itr = category.iterator();
while (itr.hasNext()) {
String cate = itr.next();
if (cate.startsWith(RELAUNCH_FLAG)) {
className = cate.replace(RELAUNCH_FLAG, "");
PluginDescriptor pluginDescriptor = PluginManagerHelper.getPluginDescriptorByClassName(className);
if (pluginDescriptor != null) {
boolean isRunning = PluginLauncher.instance().isRunning(
pluginDescriptor.getPackageName());
if (!isRunning) {
return waitForLoading(pluginDescriptor);
}
}
Class<?> clazz = PluginLoader.loadPluginClassByName(pluginDescriptor, className);
cl = clazz.getClassLoader();
found = true;
break;
}
}
}
if (!found) {
throw new ClassNotFoundException(
"className : " + className + ", intent : " + intent.toString(), new Throwable());
}
}
} else {
if (cl instanceof PluginClassLoader) {
PluginIntentResolver.resolveActivity(intent);
} else {
// Do Nothing
}
}
}
try {
Activity activity = super.newActivity(cl, className, intent);
if (activity instanceof PluginContainer) {
((PluginContainer) activity).setPluginId(intent.getStringExtra(PluginContainer.FRAGMENT_PLUGIN_ID));
}
return activity;
} catch (ClassNotFoundException e) {
// 收集狀態,便於異常分析
throw new ClassNotFoundException(" orignalCl : " + orignalCl.toString() + ", orginalClassName : "
+ orginalClassName + ", orignalIntent : " + orignalIntent + ", currentCl : " + cl.toString()
+ ", currentClassName : " + className + ", currentIntent : " + intent.toString() + ", process : "
+ ProcessUtil.isPluginProcess() + ", isStubActivity : "
+ PluginManagerHelper.isStub(orginalClassName) + ", isExact : "
+ PluginManagerHelper.isExact(orginalClassName, PluginDescriptor.ACTIVITY), e);
}
}
複製代碼
方案確實很簡單,不過還有一些收尾工做,就是將建立好的[插件]組件進行一些必要的init操做,好比:在聲明週期onCreate以前進行上下文替換等操做,這些都在插件框架提供的PluginInstrumentionWrapper裏面進行完成的,看一下代碼片斷:ide
@Override
public void callActivityOnCreate(Activity activity, Bundle icicle) {
PluginInjector.injectActivityContext(activity);
Intent intent = activity.getIntent();
if (intent != null) {
intent.setExtrasClassLoader(activity.getClassLoader());
}
if (icicle != null) {
icicle.setClassLoader(activity.getClassLoader());
}
if (ProcessUtil.isPluginProcess()) {
installPluginViewFactory(activity);
if (activity instanceof WaitForLoadingPluginActivity) {
// NOTHING
} else {
}
if (activity.isChild()) {
// 修正TabActivity中的Activity的ContextImpl的packageName
Context base = activity.getBaseContext();
while (base instanceof ContextWrapper) {
base = ((ContextWrapper) base).getBaseContext();
}
if (HackContextImpl.instanceOf(base)) {
HackContextImpl impl = new HackContextImpl(base);
String packageName = PluginLoader.getApplication().getPackageName();
// String packageName1 = activity.getPackageName();
impl.setBasePackageName(packageName);
impl.setOpPackageName(packageName);
}
}
}
super.callActivityOnCreate(activity, icicle);
monitor.onActivityCreate(activity);
}
複製代碼
到這插件activity組件就被順序的啓動起來了,而且是系統在維護具有完整的生命週期。 組件service、Receiver也是同樣的,只是這兩個組件的攔截點在ActivityThread的Handler成員的回調Callback裏面進行的。Application和provider在插件啓動的時候進行加載。spa
因爲每一個插件的包名是不一致的,能夠事先規定某個插件的packageId的值固定,而後修改aapt對其進行編譯固定,就能夠保證每一個插件分配的值不同了。插件