等待许久的淘宝Atlas框架终于开源了
Github地址:https://github.com/alibaba/atlas
atlas-core: This is client-side core library, it's job is to install each bundle, load the classes and resources on-demand when runtime.
atlas-update: This is client-side update library, which provide dexmerge capacity for update or upgrade.
atlas-gradle-plugin: This is Android Studio Gradle Plugin for engineers developing in project period, because we change some android default package mechanisms, include android aapt atlas-aapt.
比较重要的是atlas-core、atlas-update、atlas-gradle-plugin三个工程,将分三篇文章分析
本篇着重分析atlas-core这个工程,atlas-core主要的职责是安装bundle和加载bundle运行所需的类和资源两个功能。
从目录上看,这个core工程包含了淘宝模块化bundle机制的框架,包含了自定义的framework、bundle基本组件、所有bundle运行有关的App组件,下面我们一点一点分析。
1.Bundle体系
先看看最主要的bundle是什么。
/**
* An installed bundle in the Framework.
*
* <p>A bundle can be in one of six states:
* <ul>
* <li>{@link #UNINSTALLED}
* <li>{@link #INSTALLED}
* <li>{@link #RESOLVED}
* <li>{@link #STARTING}
* <li>{@link #STOPPING}
* <li>{@link #ACTIVE}
* </ul>
*/
public abstract interface Bundle
{
/**
* This bundle is uninstalled and may not be used.
*
*/
public static final int UNINSTALLED = 0x00000001;
/**
* This bundle is installed but not yet resolved.
*
*/
public static final int INSTALLED = 0x00000002;
/**
* This bundle is resolved and is able to be started.
*
*/
public static final int RESOLVED = 0x00000004;
/**
* This bundle is in the process of starting.
*
*/
public static final int STARTING = 0x00000008;
/**
* This bundle is in the process of stopping.
*
*/
public static final int STOPPING = 0x00000010;
/**
* This bundle is now running.
*
*/
public static final int ACTIVE = 0x00000020;
/**
* Returns this bundle's current state.
*
*/
public abstract int getState();
/**
* Starts this bundle.
*
*/
public abstract void start() throws BundleException;
/**
* Stops this bundle.
*
*/
public abstract void stop() throws BundleException;
/**
* Updates this bundle from an <tt>File</tt>.
*
*/
public abstract void update(File file,String version,long dexPathVersion) throws BundleException;
/**
* Uninstalls this bundle.
*
*/
public abstract void uninstall() throws BundleException;
/**
* Returns this bundle's Manifest headers and values.
* This method returns all the Manifest headers and values
* from the main section of the bundle's Manifest file; that is, all lines prior
* to the first blank line.
*
* <p>Manifest header names are case-insensitive. The methods of the returned
* <tt>Dictionary</tt> object will operate on header names in a case-insensitive manner.
*
* <p>For example, the following Manifest headers and values are included
* if they are present in the Manifest file:
* <pre>
* Bundle-Name
* Bundle-Vendor
* Bundle-Version
* Bundle-Description
* Bundle-DocURL
* Bundle-ContactAddress
* </pre>
*/
public abstract Dictionary<String,String> getHeaders();
/**
* Returns this bundle's identifier. The bundle is assigned a unique identifier by the Framework
* when it is installed in the OSGi environment.
*
*/
public abstract long getBundleId();
/**
* Returns this bundle's location identifier.
*
*/
public abstract String getLocation();
/**
* Determines if this bundle has the specified permissions.
*/
public abstract boolean hasPermission(Object permission);
/**
* Find the specified resource in this bundle.
*/
public abstract URL getResource(String name);
}
Bundle接口定义了bundle运行时所有的状态以及Bundle的主要方法,下面接着看Bundle的实现类BundleImpl。
/**
* the bundle location.
*/
final String location;
/**
* the storage location.
*/
final File bundleDir;
/**
* the bundle revision.
*/
long revisionNUM = 0;
long dexPatchVersion = 0;
/**
* the bundle archive file.
*/
Archive archive;
/**
* the bundle state.
*/
int state;
/**
* the headers from the manifest.
*/
Hashtable<String, String> headers;
/**
* the bundle classloader.
*/
BundleClassLoader classloader;
/**
* the bundle context.
*/
private final BundleContext context;
/**
* the protection domain of this bundle.
*/