ARouter原理(面对初学者)

作者: 键盘上的麒麟臂 | 来源:发表于2021-08-10 18:29 被阅读0次

    写这篇文章的原因是昨天有个刚入门的小老弟问我ARouter的原理是怎样的。当时就把我搞不会了,当时我也不记得我以前有没有看过ARouter的源码。但是我还是老油条的回了一句“上百度找,网上很多都说得很清楚”,本以为这事就到处结束,结果他说网上别人写的他看得不是很懂。
    那帮人帮到底,我只能带他慢慢过一遍ARouter,当然这篇文章肯定不会写得比较详细,因为我大致浏览下,能看到很多的关于ARouter的文章其实都写得很好,这是是为了更方便初学者,更慢的去过一遍ARouter

    一. 简述ARouter原理

    1. 为什么要使用ARouter

    为什么要使用ARouter,无非就是做了组件化,如果module之间的相互引用,就会导致组件之间很混乱,使用ARouter能更好的实现解耦,并且让代码更可观。

    2. ARouter的实现

    我这边就只拿Activity举例。如果不使用ARouter,要在各个module之间做页面跳转。无非只能各个module之间相互引用,然后直接做Intent。要么使用隐式Intent等等,但这些方法都不太好。
    ARouter的作用简单来说是在module之间不相互引用的基础上能实现显示Intent来跳转页面。
    那么它是怎么实现的?很多新手其实卡在这里,既要不相互引用,又能使用显示Intent,这不相互矛盾吗?
    其实不然,我们平时写Intent

                        Intent intent = new Intent(this, xxxx.class);
                        startActivity(intent)
    

    加入在moduleA中写这段代码,但是xxxx是moduleB中的类,不应用的情况下肯定报红,你编译不过。
    但其实这里的Intent构造方法是传的Class<?> 对象,所以我们可以不在这明确写xxxx.class。我可以在某个地方Class<?> cls = xxxx.class,然后传这个cls

    ARouter的原理就是所有的moudle都引用ARouter,然后再moudle中去生成一个映射表,然后再把这个映射表传到ARouter中。如果还是不是很理解,可以先继续往下看

    二. 源码分析

    关于源码我这边只简单的讲一讲,详细的可以去网上找更细致的分析,我的目的主要是让大家了解大概的一个流程是怎样的

    1. 映射表生成

    我们一般配置ARouter会这样写

    @Route(path = xxx/xxx)
    public class xxx{
        ......
    }
    

    这个会在编译时生成一个文件,在build/intermediates/javac/....../classes/com/alibaba/....../routes/下,会看到一堆生成的.class文件


    随便打开一个看看

    看得出这就是一个映射表Map<String, RouteMeta>
    某个module引用ARouter并且使用@Route注释的话都会生成一个这样的文件。
    原理就在这里,想必猜都能猜得出,在ARouter中有提个Map<String, RouteMeta> 对象的映射表,然后在某个时候,会调用所有module中的这些文件的loadInto方法,把这个映射表传过来,做put操作。也就是下层的对象在上层传入Class<?>
    如果到这里还是看不懂,建议多捋几次,这就是ARouter能实现module之间不引用的情况下又能实现显示Intent的办法。如果让你自己写一个ARouter框架,也是利用上面这招来弄。

    2. 路由初始化

    初始化调用

    ARouter.init(context)
    

    内部主要调用

                hasInit = _ARouter.init(application);
    
                if (hasInit) {
                    _ARouter.afterInit();
                }
    

    往下看,之后调用LogisticsCenter.init

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

    接着往下看,我把不重要的代码屏蔽掉

        public synchronized static void init(Context context, ThreadPoolExecutor tpe) throws HandlerException {
            ......
            try {
               
                ......
                    Set<String> routerMap;
    
                    // It will rebuild router map every times when debuggable.
                    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>()));
                    }
    
                    logger.info(TAG, "Find router map finished, map size = " + routerMap.size() + ", cost " + (System.currentTimeMillis() - startInit) + " ms.");
                    startInit = System.currentTimeMillis();
    
                    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);
                        }
                    }
                ......
            } catch (Exception e) {
                throw new HandlerException(TAG + "ARouter init logistics center exception! [" + e.getMessage() + "]");
            }
        }
    

    先判断版本号,PackageUtils.isNewVersion(context),这里主要是做一个缓存的操作,第一次进来肯定进判断。
    ClassUtils.getFileNameByPackageName(mContext, ROUTE_ROOT_PAKCAGE)方法是获取所有生成的com/alibaba/....../routes/下的class文件,如果还是不清楚,可以直接加个打印,就知道获取到的数组是什么了。
    下面的for (String className : routerMap)循环,就是给这些文件分类,并调用loadInto方法来配置路由表(这个在上面就讲过),Warehouse这个类就是存放路由表的地方。
    如果你细心,你会发现其实这里配置路由表的映射并不完整,因为没有给Warehouse的routes对象进行赋值,而我们的跳转的核心就是routes这个HashMap,没关系,我们继续往下看。

    3. 路由跳转

    看_ARouter.afterInit();的源码也行,看路由跳转的源码也行。
    无非都是

    ARouter.getInstance().build("xxxxxx").navigation()
    

    build方法往下看

        protected Postcard build(String path) {
            if (TextUtils.isEmpty(path)) {
                throw new HandlerException(Consts.TAG + "Parameter is invalid!");
            } else {
                PathReplaceService pService = ARouter.getInstance().navigation(PathReplaceService.class);
                if (null != pService) {
                    path = pService.forString(path);
                }
                return build(path, extractGroup(path));
            }
        }
    

    这里正常流程pService会为空,直接执行build(path, extractGroup(path));

        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);
            }
        }
    

    还是为空,所以能看出这步最终是为了创建一个Postcard对象。

    然后看navigation方法,我这边也把和流程无关紧要的代码屏蔽掉

        protected Object navigation(final Context context, final Postcard postcard, final int requestCode, final NavigationCallback callback) {
            ......
    
            try {
                LogisticsCenter.completion(postcard);
            } catch (NoRouteFoundException ex) {
                ......
            }
    
            if (null != callback) {
                callback.onFound(postcard);
            }
    
            ......
                return _navigation(context, postcard, requestCode, callback);
            
    
            return null;
        }
    

    关键在于 LogisticsCenter.completion(postcard);

        public synchronized static void completion(Postcard postcard) {
            if (null == postcard) {
                throw new NoRouteFoundException(TAG + "No 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 {
                        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
                }
            } 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;
                }
            }
        }
    

    这里的代码都是有用的。RouteMeta routeMeta = Warehouse.routes.get(postcard.getPath());
    上面说了,每给Warehouse.routes配置映射关系,所以第一次进来这里是为空,执行下面操作

    IRouteGroup iGroupInstance = groupMeta.getConstructor().newInstance();
                        iGroupInstance.loadInto(Warehouse.routes);
                        Warehouse.groupsIndex.remove(postcard.getGroup());
    

    简单来说就是把group中的映射关系转变为具体routes的映射关系,再调用一次自身completion赋值给Postcard

                postcard.setDestination(routeMeta.getDestination());
                postcard.setType(routeMeta.getType());
                postcard.setPriority(routeMeta.getPriority());
                postcard.setExtra(routeMeta.getExtra());
    

    再接下来的switch (routeMeta.getType()) 中处理Type为PROVIDER和FRAGMENT的逻辑。

    然后接着上面看_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;
        }
    

    我们一般写的跳转就是Type为ACTIVITY,能看到里面就是直接调用显示intent。

    相关文章

      网友评论

        本文标题:ARouter原理(面对初学者)

        本文链接:https://www.haomeiwen.com/subject/ltnivltx.html