title : Atlas之bundle加载过程

time : 2017-05-05 10:00:00

概述

atals中bundle加载过程

  • class
  • resource

Atlas之Bundle加载过程 - 图1

注意图中的箭头

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

将流程分为三大部分

部分 说明 对应步骤
第一部分 加载时机的触发逻辑 1-7
第二部分 bundle加载过程 8-15
第三部分 bundle代码初始化 16-19

第一部分 触发时机

入口

以在MainActivity中点击secondebundle为例

MainActivity.java

1. case R.id.navigation_dashboard:
2. switchToActivity("second","com.taobao.secondbundle.SecondBundleActivity")

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

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的名称。在atlas启动过程(下)中,我们知道AtlasBundleInfoManager中存储了几乎所有bundle的信息,所以返回 bundleName 就是 com.taobao.secondbundle。
  • 然后,根据bundlename,去查询bundle加载后的结构体- BundleImpl。由于secondbundle之前并没有加载进来,所以会执行到第3步asyncStartActivity方法。

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

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. synchronized (BundleInstaller.this) {
6. try{
7. call();
8. } catch (Throwable e) {
9. e.printStackTrace();
10. } finally {
11. if (mListener != null) {
12. new Handler(Looper.getMainLooper()).post(new Runnable() {
13. @Override
14. public void run() {
15. mListener.onFinished();
16. }
17. //..
18. };
19. sBundleHandler.post(installTask);

21. }
22. }

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

在 installTask 中

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

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

第二部分 加载过程

需要注意的是,第二部分整体是运行在 HandlerThread 中的。

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

Atlas之Bundle加载过程 - 图2

call

1. call(){
2. //...
3. if (FileUtils.getUsableSpace(Environment.getDataDirectory()) >= 5) {
4. //has enough space
5. if(AtlasBundleInfoManager.instance().isInternalBundle(bundleName)) {
6. bundle = installBundleFromApk(bundleName);
7. if (bundle != null) {
8. ((BundleImpl) bundle).optDexFile();
9. }
10. }
11. } else {
12. throw new LowDiskException("no enough space");
13. }
14. //...
15. }

这里有两个判断条件

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

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

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

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. }

6. if (autoload) {
7. resolveBundle();
8. Framework.bundles.put(location, this);
9. }
10. }

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

resolveBundle

BundleImpl.java

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

23. // notify the listeners
24. Framework.notifyBundleListeners(0 /*LOADED*/, this);
25. }

为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. public void bundleChanged(final BundleEvent event){
2. switch (event.getType()) {
3. case 0:/* LOADED */
4. loaded(event.getBundle());
5. }

loaded

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. }

todo 资源处理实现说明

回到第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方法。

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

1. protected static Application newApplication(String applicationClassName, ClassLoader cl) throws ApplicationInitException {
2. final Class<?> applicationClass = cl.loadClass(applicationClassName);
3. Application app = (Application) applicationClass.newInstance();
4. AtlasHacks.Application_attach.invoke(app, RuntimeVariables.androidApplication);
5. return app;
6. }

首先,根据类名加载对应的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. }

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

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

  1. 从AtlasBundleInfoManager上已加载的列表中找到对应bunde(在第在第10步中添加)
  2. 从对应bundle上的classloader上加载对应的class。

到这里为止,整个bundle的加载、初始化过程分析完毕。