系列文章:
路由框架ARouter使用及源碼解析(一)
路由框架ARouter使用及源碼解析(二)
上一篇主要介紹了ARouter的使用,本篇來分析一下ARouter的實現原理
ARouter解析註解
在上一篇中,我們知道在使用ARouter時,需要在build.config裏配置:
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
並且知道annotationProcessor用來聲明註解解析器,arouter-compiler用來解析ARouter中的各個註解並自動生成class類,那麼我們就來看一下到底生成了哪些類:
其生成的5個索引文件有4大類(Group類有2個文件,按Group區分開了),他們都實現了ARouter中的接口:
至於他們都代表什麼,我們後面一一分析。
ARouter初始化
在自定義Application中進行初始化:
// 儘可能早,推薦在Application中初始化
ARouter.init(Application.this);
點進去看一下:
//ARouter.java
public static void init(Application application) {
if (!hasInit) {
logger = _ARouter.logger;
_ARouter.logger.info(Consts.TAG, "ARouter init start.");
hasInit = _ARouter.init(application);
if (hasInit) {
_ARouter.afterInit();
}
_ARouter.logger.info(Consts.TAG, "ARouter init over.");
}
}
哦,ARouter又調用了_ARouter.init(application)去初始化,再點進去:
//_ARouter.java
private volatile static ThreadPoolExecutor executor = DefaultPoolExecutor.getInstance();
protected static synchronized boolean init(Application application) {
mContext = application;
LogisticsCenter.init(mContext, executor);
logger.info(Consts.TAG, "ARouter init success!");
hasInit = true;
mHandler = new Handler(Looper.getMainLooper());
return true;
}
哦,_ARouter
在init
初始化方法裏除了初始化一些變量和一個handler
,又調用了LogisticsCenter.init(mContext, executor)
, 其中executor
是一個線程池, 繼續跟到LogisticsCenter
裏去:
/**
* LogisticsCenter contains all of the map.
* 1. Creates instance when it is first used.
* 2. Handler Multi-Module relationship map(*)
* 3. Complex logic to solve duplicate group definition
*/
//LogisticsCenter.java
public synchronized static void init(Context context, ThreadPoolExecutor tpe) throws HandlerException {
Set<String> routerMap;
//1、遍歷“com.alibaba.android.arouter.routes”路徑下的類並把其加入到set中
if (ARouter.debuggable() || PackageUtils.isNewVersion(context)) {
// 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();
}
// Save new version name when router map update finishes.
PackageUtils.updateVersion(context);
} 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>()));
}
//2、遍歷set,將root、group、provider分類並填充到Warehouse路由表中
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);
}
}
}
}
LogisticsCenter.init
方法比較長,上面只保留了核心代碼,ARouter
優先使用arouter-auto-register
插件去解析並填充Warehouse
路由表,忽略這種方式。我們來看上面這種加載方式,PackageUtils.isNewVersion(context)
中判斷SharedPreferences(後面簡稱sp)裏面是否有存儲versionName
及versionCode
,如果沒有或者他們有更新的時候,需要重新加載一次com.alibaba.android.arouter.routes
這個路徑下的類名並填充到Set中,否則直接從sp中取數據並賦值到Set中去。接着就開始遍歷這個Set,並通過Class.forName(className)
這種反射方式去實例化類並調用類中的loadInto
方法將註解對應的索引信息添加到Warehouse
路由表中。畫個圖來總結一下:
ARouter跳轉
ARouter跳轉時,直接使用ARouter.getInstance().build("xxx/xxx").navigation()
即可完成跳轉,那我們就來看一下源碼,看看裏面都做了什麼,首先是build
方法:
/**
* Build postcard by path and default group
*/
protected Postcard build(String path) {
if (TextUtils.isEmpty(path)) {
throw new HandlerException(Consts.TAG + "Parameter is invalid!");
} else {
PathReplaceService pService = navigation(PathReplaceService.class);
if (null != pService) {
path = pService.forString(path);
}
return build(path, extractGroup(path));
}
}
/**
* Build postcard by uri
*/
protected Postcard build(Uri uri) {
if (null == uri || TextUtils.isEmpty(uri.toString())) {
throw new HandlerException(Consts.TAG + "Parameter invalid!");
} else {
PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
if (null != pService) {
uri = pService.forUri(uri);
}
return new Postcard(uri.getPath(), extractGroup(uri.getPath()), uri, null);
}
}
/**
* Build postcard by path and group
*/
protected Postcard build(String path, String group) {
if (TextUtils.isEmpty(path) || TextUtils.isEmpty(group)) {
throw new HandlerException(Consts.TAG + "Parameter is invalid!");
} else {
PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
if (null != pService) {
path = pService.forString(path);
}
return new Postcard(path, group);
}
}
三個方法中都有,PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
那麼這個PathReplaceService是幹啥的,點進去看看:
/**
* Preprocess your path
*/
public interface PathReplaceService extends IProvider {
/**
* For normal path.
*
* @param path raw path
*/
String forString(String path);
/**
* For uri type.
*
* @param uri raw uri
*/
Uri forUri(Uri uri);
}
看它的介紹就知道了,原來這個類是用來預處理path和uri的,調用方需要實現PathReplaceService就可以做預處理,如果不實現,默認pService==null,那麼直接走下面的去初始化Postcard實體類。
接着來看navigation
方法,因爲build
方法返回的是PostCard
類,所以調用的是PostCard
類的navigation
方法,經過一系列跳轉,最終來到_ARouter.getInstance().navigation(mContext, postcard, requestCode, callback)
:
protected Object navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
try {
LogisticsCenter.completion(postcard);
} catch (NoRouteFoundException ex) {
logger.warning(Consts.TAG, ex.getMessage());
}
return null;
}
if (null != callback) {
callback.onFound(postcard);
}
if (!postcard.isGreenChannel()) {
// It must be run in async thread, maybe interceptor cost too mush time made ANR.
interceptorService.doInterceptions(postcard, new InterceptorCallback() {
@Override
public void onContinue(Postcard postcard) {
_navigation(context, postcard, requestCode, callback);
}
@Override
public void onInterrupt(Throwable exception) {
if (null != callback) {
callback.onInterrupt(postcard);
}
}
});
} else {
return _navigation(context, postcard, requestCode, callback);
}
return null;
}
去除了部分無關代碼,只保留了核心代碼,首先調用了LogisticsCenter.completion
方法,我們追進去看看:
//LogisticsCenter.java
/**
* Completion the postcard by route metas
*
* @param postcard Incomplete postcard, should complete by this method.
*/
public synchronized static void completion(Postcard postcard) {
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 {
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 {
postcard.setDestination(routeMeta.getDestination());
postcard.setType(routeMeta.getType());
postcard.setPriority(routeMeta.getPriority());
postcard.setExtra(routeMeta.getExtra());
Uri rawUri = postcard.getUri();
if (null != rawUri) { // Try to set params into bundle.
Map<String, String> resultMap = TextUtils.splitQueryParameters(rawUri);
Map<String, Integer> paramsType = routeMeta.getParamsType();
if (MapUtils.isNotEmpty(paramsType)) {
// Set value by its type, just for params which annotation by @Param
for (Map.Entry<String, Integer> params : paramsType.entrySet()) {
setValue(postcard,
params.getValue(),
params.getKey(),
resultMap.get(params.getKey()));
}
// Save params name which need auto inject.
postcard.getExtras().putStringArray(ARouter.AUTO_INJECT, paramsType.keySet().toArray(new String[]{}));
}
// Save raw uri
postcard.withString(ARouter.RAW_URI, rawUri.toString());
}
switch (routeMeta.getType()) {
case PROVIDER: // if the route is provider, should find its instance
// Its provider, so it must implement IProvider
Class<? extends IProvider> providerMeta = (Class<? extends IProvider>) routeMeta.getDestination();
IProvider instance = Warehouse.providers.get(providerMeta);
if (null == instance) { // There's no instance of this provider
IProvider provider;
try {
provider = providerMeta.getConstructor().newInstance();
provider.init(mContext);
Warehouse.providers.put(providerMeta, provider);
instance = provider;
} catch (Exception e) {
throw new HandlerException("Init provider failed! " + e.getMessage());
}
}
postcard.setProvider(instance);
postcard.greenChannel(); // Provider should skip all of interceptors
break;
case FRAGMENT:
postcard.greenChannel(); // Fragment needn't interceptors
default:
break;
}
}
}
這個類很長,但是邏輯還是很清晰的:首先從Warehouse
路由表的routes
中獲取RouteMeta
,但是第一次獲取的時候爲空(因爲init時只填充了Warehouse路由表的groupsIndex、interceptorsIndex、providersIndex,還記得嗎?),接着從Warehouse.groupsIndex
中根據group
的名字找到對應的group
索引,並將生成的索引類的map
數據加載到Warehouse.routes
中,然後把Warehouse.groupsIndex
中對應的group
刪除掉,以免重複加載數據,然後調用了completion(postcard)
進行重新加載。此時Warehouse.routes
已經不爲空,根據path獲取對應的RouteMeta
,就會走到else
邏輯中,先是對PostCard
設置了一堆屬性,最後對IProvider
的子類進行了初始化並加載到Warehouse.providers
中,同時也設置到PostCard
中,並給PROVIDER
和FRAGMENT
設置了綠色通道(不會被攔截)。總結一下:主要邏輯就是通過Warehouse.groupsIndex找到對應的group並進行加載,實現了分組加載路由表。
我們繼續回到navigation
方法中往下走,首先通過postcard.isGreenChannel()
判斷是否會攔截,如果攔截,就會走interceptorService
的邏輯(interceptorService是在afeterInit中初始化的),否則就走到了_navigation
邏輯中,那麼來看_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:
// Build intent
final Intent intent = new Intent(currentContext, postcard.getDestination());
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.
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;
case PROVIDER:
return postcard.getProvider();
case BOARDCAST:
case CONTENT_PROVIDER:
case FRAGMENT:
Class fragmentMeta = postcard.getDestination();
try {
Object instance = fragmentMeta.getConstructor().newInstance();
if (instance instanceof Fragment) {
((Fragment) instance).setArguments(postcard.getExtras());
} else if (instance instanceof android.support.v4.app.Fragment) {
((android.support.v4.app.Fragment) instance).setArguments(postcard.getExtras());
}
return instance;
} catch (Exception ex) {
logger.error(Consts.TAG, "Fetch fragment instance error, " + TextUtils.formatStackTrace(ex.getStackTrace()));
}
case METHOD:
case SERVICE:
default:
return null;
}
return null;
}
private void startActivity(int requestCode, Context currentContext, Intent intent, Postcard postcard, NavigationCallback callback) {
if (requestCode >= 0) {
// Need start for result
if (currentContext instanceof Activity) {
ActivityCompat.startActivityForResult((Activity) currentContext, intent, requestCode, postcard.getOptionsBundle());
}
} else {
ActivityCompat.startActivity(currentContext, intent, postcard.getOptionsBundle());
}
if ((-1 != postcard.getEnterAnim() && -1 != postcard.getExitAnim()) && currentContext instanceof Activity) { // Old version.
((Activity) currentContext).overridePendingTransition(postcard.getEnterAnim(), postcard.getExitAnim());
}
if (null != callback) { // Navigation over.
callback.onArrival(postcard);
}
}
哦,原來ARouter
跳轉Activity
最終也是用原生的Intent
實現的,如果navigation()
不傳入context
,則使用初始化時Application
作爲context
,如果是FRAGMENT、PROVIDER、CONTENT_PROVIDER、BOARDCAST
,通過反射方式初始化並返回即可。
嘗試畫個圖來總結一下navigation:
嗯,到這裏ARouter內部的主要流程就分析完了~
ARouter跳轉原理:ARouter路由跳轉本質上也是通過原生的startActiviy及startActivityForResult來實現的,只不過ARouter通過APT形式將編譯期通過解析註解生成的索引加載到Warehouse路由表中,從而製造跳轉規則。並且可以在跳轉之前設置攔截或過濾。