Migrating from ARouter to TheRouter

ARouter has been used in the previous project, but it has been very stuck. I specially used adb to check the initialization of ARouter 131ms. Recently, I found that Huo Lala has open sourced a more advanced routing framework TheRouter, and also advertised that it can switch seamlessly, so I quickly tried it out during the Mid-Autumn Festival holiday, it's really delicious! !

Let's take a look at the official introduction: TheRouterit is a set of solution frameworks written in Kotlin for Android modular development.
Github project address: github.com/HuolalaTech… ,
official website documentation: therouter.cn/

Run the migration tool

  1. Download the TheRouterofficial migration tool, which supports Windows and Mac: See the official documentation for tool download
  2. Select the routing framework of the current project. Currently, the migration tool seems to only support the migration of ARouter to TheRouter.
  3. Select the TheRouterversion number to use
  4. Select the project path and directly select the root directory

1-1.png5. Start the conversion.
6. After the transfer, a change log will be generated, ARouterbecause Providerthere is an init method, TheRouterbut if there is no such method, you need to judge the code logic by yourself. I just adjust the init first when the interface method is called.
7. After the log is generated, if there is no part that needs to be manually modified, you don't need to worry about it. If there is, you can choose to export the log and put it in Sublime for a clearer view.

View git modification records

It can be seen that TheRouterthe migration tool is still very smart, and the places that need to be changed are automatically changed. ARouterYou can also comment out the useless methods directly . You can try it yourself, if you write it Providerbut there is no init code in it, it will be deleted directly.

1-3.png

1-4.png

Single module auto-initialization capability

And the support TheRouterfor modular development is very complete, according to the description of the official document: TheRouterit is a set of solutions provided completely for modular development. In modular development, each module may have its own code that needs to be initialized. The previous practice is to Applicationdeclare all these codes in it, but this may require modification Applicationof the module each time with business changes. TheRouterThe single-module automatic initialization capability is to solve such a situation. After the initialization method is declared in the current module, it will be automatically called in the business scenario.

Each @FlowTaskannotated method will be parsed at compile time to generate a corresponding Task object, which contains information about the initialization method, such as whether to execute asynchronously, task name, and whether to rely on other tasks to execute first.

When all aars are compiled and all Tasks are generated, they will Gradlebe aggregated through plugins in the main app. At this Tasktime , all aars will be checked once, and a directed acyclic graph will be constructed to prevent the occurrence of circular references in Tasks. .

After each application starts, when the route is initialized, all of the directed graph will be Taskloaded in order according to the dependencies.

At the beginning, many colleagues in our group said that this automatic initialization capability was useless, but when we actually used it, we found that it was absolutely fragrant . Especially when doing privacy compliance rectification before, the initialization code of many modules had to be changed. Now, this set of custom automatic initialization actual framework is used, and all the direct code is integrated back into its own module, and there is no cross-module modification code at all. situation.

there are some pits

The overall migration tool is not bad, but after using it, there are still some small pits.
The ARouter custom interceptor cannot be migrated. After one-click migration, the code cannot be compiled. It is necessary to manually change the code of the interceptor to the corresponding interceptor of TheRouter.

Summarize

In short, the feeling of using it is - silky smooth!

ARouterIt took three days for the boss to evaluate it directly and seamlessly TheRouter. I finished it in half a day, and I said it after a day of testing.
By the way, I also changed the previous initialization logic. When I develop later, I basically don’t need to care about the modules developed by others. I will get off work after I finish my get off work, bye~

おすすめ

転載: juejin.im/post/7142687645980688397