概述

atals中bundle加载过程

  • class
  • resource

注意图中的箭头

  • 实线箭头表示运行在主线程
  • 虚线箭头表示运行在HandlerThread中

将流程分为三大部分

第一部分 触发时机

入口

以在MainActivity中点击secondebundle为例

MainActivity.java

switchActivity 辗调用,会执行到第2步方法中

execStartChildActivityInternal

ActivityGroupDelegate.java

  1. public void execStartChildActivityInternal(ViewGroup container,String key, Intent intent){
  2. String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(componentName);
  3. if(!TextUtils.isEmpty(bundleName)){
  4. BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
  5. if(impl!=null&&impl.checkValidate()) { }else {
  6. //...
  7. asyncStartActivity(container,key,bundleName,intent);
  8. }
  9. }
  • 首先,根据componentName即com.taobao.secondbundle.SecondBundleActivity,查询bundle的名称。在中,我们知道AtlasBundleInfoManager中存储了几乎所有bundle的信息,所以返回 bundleName 就是 com.taobao.secondbundle
  • 然后,根据bundlename,去查询bundle加载后的结构体- BundleImpl。由于secondbundle之前并没有加载进来,所以会执行到第3步asyncStartActivity方法。

asyncStartActivity最终调用了BundleUtil的方法,我们直接看第4步 checkBundleStateAsync

BundleUtil.java

  1. public static boolean checkBundleArrayStateAsync(final String[] bundlesName, final Runnable bundleActivated, final Runnable bundleDisabled){
  2. BundleInstaller installer = BundleInstallerFetcher.obtainInstaller();
  3. installer.installTransitivelyAsync(bundlesName, new BundleInstaller.InstallListener() {
  4. @Override
  5. public void onFinished() {
  6. boolean success = true;
  7. BundleImpl tmp;
  8. for(String bundleName : bundlesName){
  9. if((tmp=((BundleImpl) Atlas.getInstance().getBundle(bundleName)))==null || !tmp.checkValidate()){
  10. success = false;
  11. }else{
  12. tmp.startBundle();
  13. }
  14. }
  15. });
  16. return true;
  17. }

在这一步中,开始异步加载bundle,成功后,在主线程中回调onFinished(后面会提及),调用BundleImpl对象的startBundl方法,开始 第三部分 的初始化过程。

deliveryTask

第5、6步,主要是各种逻辑判断,之后辗转调用到7步。直接看第7步deliveryTask的函数实现。

BundleUtil.java

  1. private void deliveryTask(boolean sync){
  2. Runnable installTask = new Runnable() {
  3. @Override
  4. public void run() {
  5. try{
  6. call();
  7. } catch (Throwable e) {
  8. e.printStackTrace();
  9. } finally {
  10. if (mListener != null) {
  11. new Handler(Looper.getMainLooper()).post(new Runnable() {
  12. @Override
  13. public void run() {
  14. }
  15. //..
  16. };
  17. sBundleHandler.post(installTask);
  18. }
  19. }

函数首先创建了一个异步任务 installTask,之后将任务提交给sBundleHandler。而sBundleHandler实际上关联的是一个HandlerThread,所以,installTask是运行在单独的线程中的。

installTask

  • 调用了call函数
  • 向ui线程提交一个任务,用于回调onFinished

到目前为止,第一部分分析完毕,我们进入第二部分 加载过程

第二部分 加载过程

回顾一下第二部分的运行时序

Atlas之Bundle加载过程 - 图2

call

这里有两个判断条件

  • 剩余存储空间满足
  • 是内置bundle

当两个条件都满足时,执行bundle的安装和optDexFile操作。

installBundleFromApk 又调用了 installNewBundle方法,直接看第9步 - installNewBundle 的实现。

Framework.java

  1. static BundleImpl installNewBundle(final String location, final InputStream in)throws BundleException {
  2. //...
  3. BundleListing.BundleInfo info = AtlasBundleInfoManager.instance().getBundleInfo(location);
  4. BundleImpl bundle = new BundleImpl(bundleDir, location, new BundleContext(), null, file,version,true,-1);
  5. return bundle;
  6. }

函数很简单,获取bundle的信息,之后构造一个BundleImpl对象。

有两个参数需要注意一下

参数 说明
bundleDir /data/data/com.taobao.demo/files/storage/com.taobao.secondbundle
in 指向 lib/armeabi/libcom_taobao_secondbundle.so

BundleImpl

来看BundleImpl的构造函数

BundleImpl.java

  1. BundleImpl(final File bundleDir, final String location, final BundleContext context, final InputStream stream,...) throws BundleException, IOException{
  2. if (stream != null) {
  3. this.archive = new BundleArchive(location,bundleDir, stream,version, dexPatchVersion);
  4. }
  5. if (autoload) {
  6. resolveBundle();
  7. Framework.bundles.put(location, this);
  8. }
  9. }

首先创建了一个BundleArchive对象,bundleArchive持有bundleDir和InputStream的引用,用来做dexOpt的( ? todo ),暂时不关注,往下走

resolveBundle

BundleImpl.java

  1. private synchronized void resolveBundle() throws BundleException {
  2. //...
  3. if ( this.classloader == null){
  4. // create the bundle classloader
  5. List<String> dependencies = AtlasBundleInfoManager.instance().getDependencyForBundle(location);
  6. String nativeLibDir = getArchive().getCurrentRevision().getRevisionDir().getAbsolutePath()+"/lib"+":"
  7. + RuntimeVariables.androidApplication.getApplicationInfo().nativeLibraryDir+":"
  8. +System.getProperty("java.library.path");
  9. if(dependencies!=null) {
  10. for (String str : dependencies) {
  11. BundleImpl impl = (BundleImpl) Atlas.getInstance().getBundle(str);
  12. if (impl != null) {
  13. File dependencyLibDir = new File(impl.getArchive().getCurrentRevision().getRevisionDir(), "lib");
  14. nativeLibDir += dependencyLibDir;
  15. }
  16. }
  17. this.classloader = new BundleClassLoader(this,dependencies,nativeLibDir);
  18. }
  19. // notify the listeners
  20. Framework.notifyBundleListeners(0 /*LOADED*/, this);
  21. }

为bundle创建一个classloader,在创建classloader的过程中

  • 指定bundle自身依赖so的路径
  • 指定依赖bundle所依赖so的路径

比如在demo中 ,nativeLibDir的 值为 /data/user/0/com.taobao.demo/files/storage/com.taobao.secondbundle/version.1/lib:/data/app/com.taobao.demo-1/lib/x86:/vendor/lib:/system/lib

创建完BundlerClassLoader后,在最后一行进行回调,这里的回调实现类是 BundleLifecycleHandler

BundleLifecycleHandler.java

  1. private void loaded(Bundle bundle) { long time = System.currentTimeMillis();
  2. BundleImpl b = (BundleImpl) bundle;
  3. DelegateResources.addBundleResources(
  4. b.getArchive().getArchiveFile().getAbsolutePath()
  5. );
  6. }

回到第10步

BundleImpl.java

  1. BundleImpl(final File bundleDir, final String location...) {
  2. //...
  3. resolveBundle();
  4. Framework.bundles.put(location, this);
  5. }

在resolveBundle函数执行周期内,主要完成了两件事

  • 为bundle创建了独立的ClassLoader
  • 将bundle中的资源添加到Resource上

第二行函数将bundle的信息加入到已加载的列表中。

完成之后,回到第8步,执行optDexFile方法。

。。。

第三部分 初始化过程

整个第三部分运行在 UI 线程中

bundle加载完成后,第7步,在UI线程中回调了完成的接口,调用了startbundle方法又经过辗转调用,到了BundleLifecycleHandler的startd方法中。

BundleLifecycleHandler.java

  1. private void started(Bundle bundle){
  2. BundleImpl b = (BundleImpl) bundle;
  3. BundleListing.BundleInfo info = AtlasBundleInfoManager.instance().getBundleInfo(b.getLocation());
  4. //...
  5. String appClassName = info.getApplicationName();
  6. Application app = newApplication(appClassName, b.getClassLoader());
  7. app.onCreate();
  8. }

在第6行构造出bundle中注册的application对象,之后执行application的onCreate方法。对于appliation来说,似乎还差一个关键的attachBaseContext的入口函数调用,我们接着看构造过程。

BundleLifecycleHandler.java

首先,根据类名加载对应的class,之后反射执行applciation的attach方法。

Atlas之启动过程(二)下中,解释过,application的attach方法最终会调用到attachBaseContext方法。

最后,大概看一下DelegateClassLoader加载bundle中class的过程。

DelegateClassLoader.java

  1. protected Class<?> findClass(String className) throws ClassNotFoundException {
  2. Class<?> clazz = loadFromInstalledBundles(className,false);
  3. //...
  4. return clazz;
  5. }
  6. static Class<?> loadFromInstalledBundles(String className,boolean safe)
  7. throws ClassNotFoundException {
  8. String bundleName = AtlasBundleInfoManager.instance().getBundleForComponet(className);
  9. BundleImpl bundle = (BundleImpl) Atlas.getInstance().getBundle(bundleName);
  10. //...
  11. ClassLoader classloader = bundle.getClassLoader();
  12. Class<?> clazz = classloader.loadClass(className);
  13. return clazz;
  14. }

可以看到,classloader先从bundle上去找的。而loadFromInstalledBundles逻辑如下:

  1. AtlasBundleInfoManager上已加载的列表中找到对应bunde(在第在第10步中添加)