ARouter是阿里團(tuán)隊(duì)開(kāi)源的一款提供路由功能的中間件合愈,項(xiàng)目地址:https://github.com/alibaba/ARouter待侵。
路由功能對(duì)android組件化開(kāi)發(fā)的模塊解耦有重要意義兵志,通過(guò)本文我們可以先了解ARouter的使用方法以及使用中的常見(jiàn)問(wèn)題椎镣。
使用方法
一艺晴、在提供路由功能的模塊的build.gradle文件中冗美,添加下述信息
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'
...
}
當(dāng)前的最新版本在github首頁(yè)展示,如下圖
二粉洼、在提供路由功能的模塊中节预,在支持路由的頁(yè)面上添加注解(必選)。這里的路徑需要注意的是至少需要有兩級(jí)漆改,/xx/xx
@Route(path = "/test/activity")
public class YourActivity extend Activity {
...
}
三心铃、宿主App中,初始化ARouter
if (isDebug()) { // 這兩行必須寫(xiě)在init之前挫剑,否則這些配置在init過(guò)程中將無(wú)效
ARouter.openLog(); // 打印日志
ARouter.openDebug(); // 開(kāi)啟調(diào)試模式(如果在InstantRun模式下運(yùn)行去扣,必須開(kāi)啟調(diào)試模式!線上版本需要關(guān)閉,否則有安全風(fēng)險(xiǎn))
}
ARouter.init(mApplication); // 盡可能早樊破,推薦在Application中初始化
四愉棱、發(fā)起路由操作
// 1. 應(yīng)用內(nèi)簡(jiǎn)單的跳轉(zhuǎn)(通過(guò)URL跳轉(zhuǎn)在'進(jìn)階用法'中)
ARouter.getInstance().build("/test/activity").navigation();
// 2. 跳轉(zhuǎn)并攜帶參數(shù)
ARouter.getInstance().build("/test/1")
.withLong("key1", 666L)
.withString("key3", "888")
.withObject("key4", new Test("Jack", "Rose"))
.navigation();
使用問(wèn)題
There's no router matched!
在組件化開(kāi)發(fā)過(guò)程中,我在宿主App模塊引用模塊C中的Activity哲戚,一直不能成功奔滑,界面及日志提示"W/ARouter::: ARouter::There is no route match the path [/xxx/xxx], in group [xxx][ ]"
最終定位原因是需要在宿主App的build.gradle中,把需要路由的模塊(上例中的模塊C)引入進(jìn)來(lái)
//builde.gradle文件
compile project(":moudlec")
compile project(":moudled")
在組件化開(kāi)發(fā)中顺少,各模塊建議不要有依賴關(guān)系朋其。宿主App在打包編譯時(shí)可依賴各組件,組件之間可以使用ARouter進(jìn)行界面跳轉(zhuǎn)脆炎。上例中模塊c和d之間無(wú)依賴關(guān)系梅猿,但是再宿主app中可以進(jìn)行跳轉(zhuǎn)。
ARouter::Extract the default group failed
根據(jù)github的使用文檔秒裕,路由路徑至少需要有兩級(jí):/xx/xx袱蚓。
使用路由啟動(dòng)Activity時(shí),犯了一個(gè)錯(cuò)誤几蜻,路徑名稱忘記寫(xiě)首字符/喇潘,導(dǎo)致出現(xiàn)了如下報(bào)錯(cuò)体斩,引以為戒。
09-06 15:13:45.408 2550-2550/com.example.chenbin.helloas E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chenbin.helloas, PID: 2550
com.alibaba.android.arouter.exception.HandlerException: ARouter::Extract the default group failed, the path must be start with '/' and contain more than 2 '/'!
at com.alibaba.android.arouter.launcher._ARouter.extractGroup(_ARouter.java:228)
at com.alibaba.android.arouter.launcher._ARouter.build(_ARouter.java:189)
at com.alibaba.android.arouter.launcher.ARouter.build(ARouter.java:140)
at com.example.chenbin.helloas.MainActivity$1.onClick(MainActivity.java:37)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21177)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)