ARouter使用之坑

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fwt336/article/details/84660074

1.使用方法

1.1添加依赖和配置

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

dependencies {
    // 替换成最新版本, 需要注意的是api
    // 要与compiler匹配使用,均使用最新版可以保证兼容
    compile 'com.alibaba:arouter-api:x.x.x'
    annotationProcessor 'com.alibaba:arouter-compiler:x.x.x'
    ...
}
// 旧版本gradle插件(< 2.2),可以使用apt插件,配置方法见文末'其他#4'
// Kotlin配置参考文末'其他#5'

1.2添加注解

// 在支持路由的页面上添加注解(必选)
// 这里的路径需要注意的是至少需要有两级,/xx/xx
@Route(path = "/test/activity")
public class YourActivity extend Activity {
    ...
}

1.3初始化SDK

if (isDebug()) {           // 这两行必须写在init之前,否则这些配置在init过程中将无效
    ARouter.openLog();     // 打印日志
    ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本需要关闭,否则有安全风险)
}
ARouter.init(mApplication); // 尽可能早,推荐在Application中初始化

1.4发起路由操作

// 1. 应用内简单的跳转(通过URL跳转在'进阶用法'中)
ARouter.getInstance().build("/test/activity").navigation();

// 2. 跳转并携带参数
ARouter.getInstance().build("/test/1")
			.withLong("key1", 666L)
			.withString("key3", "888")
			.withObject("key4", new Test("Jack", "Rose"))
			.navigation();

2.dependencies坑

There is no route match the path错误

如果每个module都按照以上步骤写了不会报错,问题是,当我们在一个baseLib中按照步骤1中的写好了,然后在每个module中引用了baseLib,但是在module中就没有在dependency arouter库了。这个时候就会出现上面的错误。

解决办法:每个module在dependencies中都添加以下代码:

compile 'com.alibaba:arouter-api:x.x.x'

annotationProcessor 'com.alibaba:arouter-compiler:x.x.x'

问题解决!

猜你喜欢

转载自blog.csdn.net/fwt336/article/details/84660074
今日推荐