ARouter原始碼分析(1)-基本路由流程
前面作者分析了一遍 WMRouter
的原始碼,趁著思路比較活躍,就打算把 ARouter
的原始碼也看一下,然後對這兩個路由方案做一個對比。
文章是作者學習 ARouter
的原始碼的重點紀要。 ARouter官方文件 : ofollow,noindex">https://github.com/alibaba/ARouter/blob/master/README_CN.md
先通過一張圖來了解 ARouter
的路由過程以及相關類:

ARouter路由流程.png
本文先不對路由表的生成做詳細瞭解,也不對 InterceptorService
、 GlobalDegradeService
的載入做仔細分析,我們就先根據原始碼來大致看一遍路由的基本過程。
基本路由流程
在 ARouter
中如果使用一個 @Route(path = "/test/activity1")
註解標註在了一個 Activity
上,那麼這個 Activity
就是可被路由的。我們可以通過呼叫下面程式碼實現頁面跳轉:
ARouter.getInstance() .build("/test/activity1") .navigation(context, new NavigationCallback());
ARouter
只是一個門面類,實際功能的實現是由 _ARouter
完成的。來看一下 _ARouter.navigation()方法
。注意為了只看主流程,我刪去了一些邏輯
protected Object navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) { try { LogisticsCenter.completion(postcard); //裝配 postcard } catch (NoRouteFoundException ex) { logger.warning(Consts.TAG, ex.getMessage()); if (null != callback) { callback.onLost(postcard); //頁面降級 } else { DegradeService degradeService = ARouter.getInstance().navigation(DegradeService.class); degradeService.onLost(context, postcard); //全域性降級 } return null; } if (null != callback) callback.onFound(postcard); //新開執行緒,非同步執行攔截器 interceptorService.doInterceptions(postcard, new InterceptorCallback() { @Override public void onContinue(Postcard postcard) { _navigation(context, postcard, requestCode, callback); } @Override public void onInterrupt(Throwable exception) { callback.onInterrupt(postcard); } }); return null; }
LogisticsCenter 裝配 Postcard
public synchronized static void completion(Postcard postcard) { //從路由表中獲取該路由的RouteMeta RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath()); if (null == routeMeta) {//路由表中不存在就嘗試載入 Class<? extends IRouteGroup> groupMeta = Warehouse.groupsIndex.get(postcard.getGroup());// 首先把這次路由對應的路由組的表加載出來 if (null == groupMeta) { throw new NoRouteFoundException(TAG + "There is no route match the path [" + postcard.getPath() + "], in group [" + postcard.getGroup() + "]"); } else { try { IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance(); iGroupInstance.loadInto(Warehouse.routes); Warehouse.groupsIndex.remove(postcard.getGroup()); } catch (Exception e) { throw new HandlerException(TAG + "Fatal exception when loading group meta. [" + e.getMessage() + "]"); } completion(postcard);// Reload } } else { //把對用的RouteMeta資訊設定到Postcard中 postcard.setDestination(routeMeta.getDestination()); // destination這裡就是Activity類物件 postcard.setType(routeMeta.getType()); .... postcard.withString(ARouter.RAW_URI, postcard.getUri().toString()); ... } }
來總結一下這個方法的主要流程:
- 嘗試從路由表
Warehouse.routes
獲取postcard.getPath()
對應的路由資訊。 - 如果不存在
RouteMeta
,則載入postcard.getPath()
對應的路由組表Warehouse.groupsIndex
。如果根本不存在對應的組,則路由失敗 - 例項化
Warehouse.groupsIndex
, 然後把表內的資訊載入到Warehouse.routes
表中,再次呼叫completion()
- 獲取到
postcard.getPath()
對應的RouteMeta
,使用RouteMeta
完善Postcard
我們先來看一眼如何把 Warehouse.groupsIndex
的資訊載入到 Warehouse.routes
中:
這其實 IRouteGroup
介面的例項是動態生成的:
public class ARouter$$Group$$test implements IRouteGroup { @Override public void loadInto(Map<String, RouteMeta> atlas) { atlas.put("/test/activity1", RouteMeta.build(RouteType.ACTIVITY, Test1Activity.class, "/test/activity1", "test", -1, -2147483648)); atlas.put("/test/activity2", RouteMeta.build(RouteType.ACTIVITY, Test2Activity.class, "/test/activity2", "test", -1, -2147483648)); } }
即 loadInto()
實際上是把路由資訊放入到 Warehouse.routes
中。
淺嘗輒止,我們繼續往下看,這裡我們直接看攔截器例項:
攔截器
@Route(path = "/arouter/service/interceptor") public class InterceptorServiceImpl implements InterceptorService { @Override public void doInterceptions(final Postcard postcard, final InterceptorCallback callback) { if (null != Warehouse.interceptors && Warehouse.interceptors.size() > 0) { ..確保攔截器初始化完畢 LogisticsCenter.executor.execute(new Runnable() { @Override public void run() { //依次執行每一個攔截器 _excute(0, interceptorCounter, postcard); } }); } else { callback.onContinue(postcard); } } }
@Route(path = "/arouter/service/interceptor")
標註,其實這個攔截器會在執行時動態載入。可以看到大致邏輯就是:
在非同步執行緒中依次執行每一個攔截器 。其實在這裡可以看出 ARouter
的攔截器是全域性的,即每一次路由如果不設定不被攔截的標誌,則都會把攔截器走一遍。
在Postcard組裝完畢和攔截器執行完畢後,就會呼叫 _navigation()
private Object _navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) { final Context currentContext = null == context ? mContext : context; switch (postcard.getType()) { case ACTIVITY: // intent中設定一些資訊。 // Navigation in main looper. runInMainThread(new Runnable() { @Override public void run() { startActivity(requestCode, currentContext, intent, postcard, callback); } }); break; case ... } return null; }
到這裡,一次Activity的路由算是完成了。不過這其中我們還有很多細節沒有仔細討論:1
-
Warehouse.routes
路由表和Warehouse.groupsIndex
路由組表是如何載入的 -
InterceptorService
、DegradeService
是如何載入的 - ARouter是支援在執行時期獲取不在同一個庫的類例項的,比如
Fragment
,這是怎麼實現的呢? - ..
這些點,我們在後續文章再繼續分析。