Analysis of Android arouter routing framework

1、 Arouter introduction and main application scenarios:

1. Introduction:

Arouter is a middleware that provides routing functions for pages and services in Alibaba's open source Android platform. It advocates simplicity and sufficiency.

2. Problems with native routing schemes

First, let's talk about the problems of the original routing scheme and why the routing framework is needed. The native routing scheme we use is generally implemented by explicit intent and implicit intent. In the case of explicit intent, the coupling is very serious because of the direct class dependency; In the case of implicit intent, there will be centralized rule management, which makes collaboration very difficult. Moreover, generally speaking, the configuration rules are in manifest, which leads to poor scalability. In addition, the use of the native routing scheme will cause the problem that the jump process cannot be controlled, because once startactivity() is used, it can not intervene in any of the links and can only be handed over to the system management, which leads to the inability to degrade in case of jump failure, but will directly throw operation level exceptions.

At this time, if you consider using custom routing components, you can solve some of the above problems. For example, you can solve the problem of class dependency through URL index; The problem of centralized management path in implicit intent can be solved through distributed management page configuration; The implementation of the whole routing process by yourself can also have good scalability. It can also solve the problem that the jump process cannot be controlled through AOP. At the same time, it can also provide a very flexible degradation method.

3. Why use routing components

Why use routing components

The problems mentioned above are mainly in development and collaboration, and the use of a routing framework will involve two other major aspects: one is componentization, and the other is the problems of native and H5. What I just mentioned is mainly the problems that developers need to face in development and collaboration. Once an app reaches a certain volume, the business will expand seriously, and the scale of the development team will become larger and larger. At this time, the concept of componentization is generally put forward. Componentization is to split the app into multiple small components according to certain functions and businesses, and different components are responsible by different development teams. In this way, the problems of development and cooperation in the development process of large app can be solved, and these problems can be dispersed into small app. At present, there are many mature schemes for componentization, and the user-defined routing framework can also solve the problem of no coupling between modules after the Componentization of the whole app, because it is certainly impossible to use the original routing scheme when there is no coupling.

Another problem is the problem of native and H5, because today's apps are rarely pure native or H5. Generally, the two are combined. At this time, a very convenient and unified jump scheme is needed, because you can't jump to the native page using startactivity() in H5, and jumping from native to H5 can only be realized by configuring the browser.

4. Characteristics of routing framework

In order to solve the above problems, it is necessary to implement a custom routing framework, which generally has the following three characteristics:

5. Seven advantages of arouter

Advantage 1: directly parse the URL route, parse the parameters and assign them to the page of the corresponding target field. Advantage 2: it supports multi module projects, because few apps are single module projects. Generally, they are multi module and single project. Different teams are responsible for different module development. At this time, it is particularly important to support multi module project development. Advantage 3: it supports instantrun. At present, many routing frameworks do not support instantrun. Instantrun is a new function provided by Google in Android studio 2.0 alpha version. It is similar to the daily update of code. It only aims at the development process. This can reduce the number of development and compilation during the development process, You can simply synchronize code changes to APK immediately, which can reduce the development complexity on a large scale. Advantage 4: it allows you to customize interceptors. Arouter supports interceptors, and interceptors are actually the implementation of AOP. You can customize multiple interceptors to solve some problems in behavior-oriented programming. Advantage 5: arouter can provide IOC container. IOC is actually control inversion. Friends who have done server-side development in this part may know better, because a very important capability that the spring framework often used in server-side development can provide is control inversion. Advantage 6: automatic registration of mapping relationship. For small apps with few pages, automatic registration does not show much advantage, but for large apps, the number of pages may have reached dozens or hundreds. In this case, automatic registration is very important, Because it is impossible to register every page through code. Advantage 7: flexible degradation strategy. Arouter can provide many degradation strategies for users to choose by themselves. However, the original routing scheme cannot be degraded flexibly. Once startactivity() fails, it will throw an operation level exception.

6. Application scenario:

GitHub: https://github.com/alibaba/ARouter

2、 Basic steps for using arouter:

1. Add dependencies and configurations

android {
  defaultConfig {
  ...
  javaCompileOptions {
    annotationProcessorOptions {
    arguments = [ moduleName : project.getName() ]
    }
  }
  }
}

dependencies {
  // 替换成最新版本,需要注意的是api
  // 要与compiler匹配使用,均使用最新版可以保证兼容
  compile 'com.alibaba:arouter-api:x.x.x'
  annotationProcessor 'com.alibaba:arouter-compiler:x.x.x'
  ...
}
// 替换成最新版本,需要注意的是api
// 要与compiler匹配使用,均使用最新版可以保证兼容
compile 'com.alibaba:arouter-api:x.x.x'
annotationProcessor 'com.alibaba:arouter-compiler:x.x.x'

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>