上一篇文章我们记录了在编译期ARoute将目标注解生成文件保存的过程,这一节我们来探究下在用户使用时ARoute做了什么
两条路径保证
在官方README中我们能看见关于AutoRegister
的介绍,借由这个gradle插件能实现路由表的自动加载。这节我们先不讲这个,关心下普通状态下的加载过程。
前期准备工作
初始化
在使用ARoute之前,我们需要进行初始化ARouter.init
,初始化期间会将储存信息读取到静态变量中,使我们能顺序完成跳转,因此先来看下初始化的内容:
ARoute的初始化代码:
public static void init(Application application) {
// 忽略不重要的过程
hasInit = _ARouter.init(application);
}
// _ARouter.init
protected static synchronized boolean init(Application application) {
// 主要是进行数据仓库初始化
LogisticsCenter.init(mContext, executor);
}
初始化过程都是基操,维护一个主线程Handler用于线程切换、保存Context 等
接下来我们主要看下LogisticsCenter.init
,这里会进行一次判断
if (ARouter.debuggable() || PackageUtils.isNewVersion(context)) {
logger.info(TAG, "Run with debug mode or new install, rebuild router map.");
// These class was generated by arouter-compiler.
routerMap = ClassUtils.getFileNameByPackageName(mContext, ROUTE_ROOT_PAKCAGE);
if (!routerMap.isEmpty()) {
context.getSharedPreferences(AROUTER_SP_CACHE_KEY, Context.MODE_PRIVATE).edit().putStringSet(AROUTER_SP_KEY_MAP, routerMap).apply();
}
PackageUtils.updateVersion(context); // Save new version name when router map update finishes.
} else {
logger.info(TAG, "Load router map from cache.");
routerMap = new HashSet<>(context.getSharedPreferences(AROUTER_SP_CACHE_KEY, Context.MODE_PRIVATE).getStringSet(AROUTER_SP_KEY_MAP, new HashSet<String>()));
}
}
这里看到先做了一次预加载,如果是debug模式或者版本号升级的话预先做一次加载。但是使用版本号来判断是否需要重新读取组件,在未升级版本号的情况下可能会出现问题。
getFileNameByPackageName
这个函数的目的是读取安装包中现有的class,这些class都是以包名开头的,我们来看下他是如何实现的:
public static Set<String> getFileNameByPackageName(Context context, final String packageName) throws PackageManager.NameNotFoundException, IOException, InterruptedException {
final Set<String> classNames = new HashSet<>();
//获取源文件路径,先着重看下这个
List<String> paths = getSourcePaths(context);
、、、暂时忽略
public static List<String> getSourcePaths(Context context) throws PackageManager.NameNotFoundException, IOException {
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
// app 根目录
File sourceApk = new File(applicationInfo.sourceDir);
List<String> sourcePaths = new ArrayList<>();
// 应用存放数据目录
sourcePaths.add(applicationInfo.sourceDir); //add the default apk path
//the prefix of extracted file, ie: test.classes
String extractedFilePrefix = sourceApk.getName() + EXTRACTED_NAME_EXT;
// 如果VM已经支持了MultiDex,就不要去Secondary Folder加载 Classesx.zip了,那里已经么有了
// 通过是否存在sp中的multidex.version是不准确的,因为从低版本升级上来的用户,是包含这个sp配置的
if (!isVMMultidexCapable()) {
//the total dex numbers
int totalDexNumber = getMultiDexPreferences(context).getInt(KEY_DEX_NUMBER, 1);
File dexDir = new File(applicationInfo.dataDir, SECONDARY_FOLDER_NAME);
for (int secondaryNumber = 2; secondaryNumber <= totalDexNumber; secondaryNumber++) {
//for each dex file, ie: test.classes2.zip, test.classes3.zip...
String fileName = extractedFilePrefix + secondaryNumber + EXTRACTED_SUFFIX;
File extractedFile = new File(dexDir, fileName);
if (extractedFile.isFile()) {
sourcePaths.add(extractedFile.getAbsolutePath());
//we ignore the verify zip part
} else {
throw new IOException("Missing extracted secondary dex file '" + extractedFile.getPath() + "'");
}
}
}
if (ARouter.debuggable()) { // Search instant run support only debuggable
sourcePaths.addAll(tryLoadInstantRunDexFile(applicationInfo));
}
return sourcePaths;
}
private static boolean isVMMultidexCapable() {
boolean isMultidexCapable = false;
String vmName = null;
try {
if (isYunOS()) { // YunOS需要特殊判断(我怀疑你在打广告)
vmName = "'YunOS'";
isMultidexCapable = Integer.valueOf(System.getProperty("ro.build.version.sdk")) >= 21;
} else { // 非YunOS原生Android
vmName = "'Android'";
String versionString = System.getProperty("java.vm.version");
if (versionString != null) {
Matcher matcher = Pattern.compile("(\\d+)\\.(\\d+)(\\.\\d+)?").matcher(versionString);
if (matcher.matches()) {
try {
int major = Integer.parseInt(matcher.group(1));
int minor = Integer.parseInt(matcher.group(2));
isMultidexCapable = (major > VM_WITH_MULTIDEX_VERSION_MAJOR)
|| ((major == VM_WITH_MULTIDEX_VERSION_MAJOR)
&& (minor >= VM_WITH_MULTIDEX_VERSION_MINOR));
} catch (NumberFormatException ignore) {
// let isMultidexCapable be false
}
}
}
}
} catch (Exception ignore) {
}
Log.i(Consts.TAG, "VM with name " + vmName + (isMultidexCapable ? " has multidex support" : " does not have multidex support"));
return isMultidexCapable;
}
代码中会到安装目录下读取apk包( /data/app/com.alibaba.android.arouter.demo-S8YOQ-yxwK8cUikcaWvAVA==/base.apk),这里还有分包的可能
这里的是否支持分包isVMMultidexCapable
是通过判断版本号来决定的,version>2.1,这是一个可以学习的点。
拿到apk后,开启CountDownLatch,多线程查询所有文件
List<String> paths = getSourcePaths(context);
final CountDownLatch parserCtl = new CountDownLatch(paths.size());
for (final String path : paths) {
DefaultPoolExecutor.getInstance().execute(new Runnable() {
@Override
public void run() {
DexFile dexfile = null;
try {
if (path.endsWith(EXTRACTED_SUFFIX)) {
//NOT use new DexFile(path), because it will throw "permission error in /data/dalvik-cache"
dexfile = DexFile.loadDex(path, path + ".tmp", 0);
} else {
dexfile = new DexFile(path);
}
Enumeration<String> dexEntries = dexfile.entries();
while (dexEntries.hasMoreElements()) {
String className = dexEntries.nextElement();
if (className.startsWith(packageName)) {
classNames.add(className);
}
}
} catch (Throwable ignore) {
Log.e("ARouter", "Scan map file in dex files made error.", ignore);
} finally {
if (null != dexfile) {
try {
dexfile.close();
} catch (Throwable ignore) {
}
}
parserCtl.countDown();
}
}
});
}
parserCtl.await();
Log.d(Consts.TAG, "Filter "
使用DexFile读取文件,遍历文件中的class,拿到所有包名开头的;
接下来就是根据读到的信息,获取三类我们想要的文件:IRouteRoot、IInterceptorGroup、IProviderGroup 读取进内存;这里我们比较关心的是IRouteRoot,他就是我们上节提到的ARouter$$Root$$app
,这里包含了所有我们的组信息:
for (String className : routerMap) {
if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_ROOT)) {
// This one of root elements, load root.
((IRouteRoot) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.groupsIndex);
} else if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_INTERCEPTORS)) {
// Load interceptorMeta
((IInterceptorGroup) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.interceptorsIndex);
} else if (className.startsWith(ROUTE_ROOT_PAKCAGE + DOT + SDK_NAME + SEPARATOR + SUFFIX_PROVIDERS)) {
// Load providerIndex
((IProviderGroup) (Class.forName(className).getConstructor().newInstance())).loadInto(Warehouse.providersIndex);
}
}
初始化结束,我们的组信息储存在Warehouse.groupsIndex
中
路由定位
最简单的形式:
ARouter.getInstance().build("/test/activity2").navigation();
build会生成一个PostCard对象
new Postcard(path, group)
,其中的组信息group也是根据简单的第一个/分割而来
接下来的navigation自然也是交给Postcard来处理了,事实上他只是充当了数据model的作用,保存了组信息,调用转发给了ARoute
ARouter.getInstance().navigation(context, this, -1, callback);
接下来交给数据仓库尝试路由:
try {
LogisticsCenter.completion(postcard);
}
首先会在已经解析的Route信息中查找,如果找不到则到groupsIndex 中找对应的组信息,然后实例化组信息记录类,加载数据:
RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath());
if (null == routeMeta) { // Maybe its does't exist, or didn't load.
Class<? extends IRouteGroup> groupMeta = Warehouse.groupsIndex.get(postcard.getGroup()); // Load route meta.
if (null == groupMeta) {
throw new NoRouteFoundException(TAG + "There is no route match the path [" + postcard.getPath() + "], in group [" + postcard.getGroup() + "]");
} else {
// Load route and cache it into memory, then delete from metas.
try {
if (ARouter.debuggable()) {
logger.debug(TAG, String.format(Locale.getDefault(), "The group [%s] starts loading, trigger by [%s]", postcard.getGroup(), postcard.getPath()));
}
IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance();
iGroupInstance.loadInto(Warehouse.routes);
Warehouse.groupsIndex.remove(postcard.getGroup());
if (ARouter.debuggable()) {
logger.debug(TAG, String.format(Locale.getDefault(), "The group [%s] has already been loaded, trigger by [%s]", postcard.getGroup(), postcard.getPath()));
}
} catch (Exception e) {
throw new HandlerException(TAG + "Fatal exception when loading group meta. [" + e.getMessage() + "]");
}
completion(postcard); // Reload:这里是重新调了一次自身
}
}
在找到的情况下,填充自身数据:
// 比较重要的是这个,记录目标class
postcard.setDestination(routeMeta.getDestination());
postcard.setType(routeMeta.getType());
postcard.setPriority(routeMeta.getPriority());
postcard.setExtra(routeMeta.getExtra());
接下来回到_ARoute中,找到记录就告知上层:
if (null != callback) {
callback.onFound(postcard);
}
然后是实际的跳转:
return _navigation(context, postcard, requestCode, callback);
我们看下Activity的处理情况:
case ACTIVITY:
// Build intent
final Intent intent = new Intent(currentContext, postcard.getDestination());
// 我们在外层调用withString等方法传递参数的时候,就会保存到PostCard的mBundle中
intent.putExtras(postcard.getExtras());
// Set flags.
int flags = postcard.getFlags();
if (-1 != flags) {
intent.setFlags(flags);
} else if (!(currentContext instanceof Activity)) { // Non activity, need less one flag.
// 应该有很多朋友遇到这种奔溃,非Activity源启动需要加上New_Task标志
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
// Set Actions
String action = postcard.getAction();
if (!TextUtils.isEmpty(action)) {
intent.setAction(action);
}
// Navigation in main looper.
runInMainThread(new Runnable() {
@Override
public void run() {
startActivity(requestCode, currentContext, intent, postcard, callback);
}
});
break;
这段代码不难看明白,基本是我们平时跳转的基操。
课后题:通过插件扫描dex是怎么实现的
在LogisticsCenter
有这么一个方法loadRouterMap
,我们在调初始化的时候会调用它,所以可否在这里入手呢?
com.alibaba.arouter
注册的插件PluginLaunch
会执行 RegisterTransform
的转换操作,经过层层调用,最终是使用asm来修改现有代码的,RouteMethodVisitor
会修改LogisticsCenter. loadRouterMap
在其中注入 LogisticsCenter. register
方法,把扫描到的目标类IRoute作为参数注入。