运维开发网

Android 插件化处理方案详解

运维开发网 https://www.qedev.com 2021-04-07 08:14 出处:网络 作者: bobby_developer
插件化启动Activity的过程 在宿主里面的androidManifest.xml里面注册一个空的activity

插件化启动Activity的过程

Android 插件化处理方案详解

在宿主里面的androidManifest.xml里面注册一个空的activity

从开始执行execStartActiviwww.cppcns.comty到最终将Activity对象new出来这个过程,系统层会去校验需要启动的activity的合法性[就是是否有在某个应用的AndroidManifest.xml里面注册]以及按启动要求创建activity对象。清晰了这点我们就可以很好的绕过系统的约束,达到我们的目的:【插件中的组件拥有真正生命周期,完全交由系统管理、非反射代理】。 简单来说方案就两步: Step1、在开始startActivity的时候将需要启动的插件组件替换成宿主预先声明号的。

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);
}

Step2、在最终创建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 = http://www.cppcns.comclazz.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.isPlugin编程客栈Process() + ", isStubActivity : "
        + PluginManagerHelper.isStub(orginalClassName) + ", isExact : "
        + PluginManagerHelper.isExact(orginalClassName, PluginDescriptor.ACTIVITY), e);
  }
}

方案确实很简单,不过还有一些收尾工作,就是将创建好的[插件]组件进行一些必要的init操作,比如:在声明周期onCreate之前进行上下文替换等操作,这些都在插件框架提供的PluginInstrumentionWrapper里面进行完成的,看一下代码片段:

@Owww.cppcns.comverride
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 (Hachttp://www.cppcns.comkContextImpl.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在插件启动的时候进行加载。

资源冲突的解决方案

resources.arsc资源描述符详解

Android 插件化处理方案详解

  • packageId: 包名id
  • 资源类型id:string,drawable,layout,color
  • 偏移:某一种类型的偏移值

解决冲突的方案

由于每个插件的包名是不一致的,可以事先规定某个插件的packageId的值固定,然后修改aapt对其进行编译固定,就可以保证每个插件分配的值不一样了。

以上就是Android 插件化处理方案详解的详细内容,更多关于Android 插件化处理方案的资料请关注我们其它相关文章!

0

精彩评论

暂无评论...
验证码 换一张
取 消