Android 阿里路由框架ARouter
一:前言
随着我的项目越来越大,有的我的项目曾经超过100M,就会带来很多问题,随着代码量的减少,AS编译工夫减少,代码的耦合性减少,Android组件化能很好的解决这2点问题,组件化带来问题,咱们如何解决组件的通信问题,组件间的页面调用问题等问题,阿里路由ARouter提供了一个很好解决方案
二:ARouter应用
1.增加依赖:
第一步:在根底的moudle_base库中增加
dependencies {
compile 'com.alibaba:arouter-api:1.5.0'
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
}
第二步:在业务组件中例如:moudle_login等模块中增加
dependencies {
annotationProcessor 'com.alibaba:arouter-compiler:1.2.2'
}
第三步:在模块中build.gradle下的android的defaultConfig配置
android{
defaultConfig {
//Arouter路由配置,第三步配置信息
javaCompileOptions {
annotationProcessorOptions {
arguments = [AROUTER_MODULE_NAME: project.getName()]
includeCompileClasspath = true
}
}
}
}
2.在App模块中Application中初始化ARouter
private void initRouter() {
//这两行必须写在init之前,否则这些配置在init过程中有效
if (AppUtils.isAppDebug()) {//第三方工具判断是否是Debug 或者BuildConfig.DEBUG
//打印日志
ARouter.openLog();
//开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!线上版本须要敞开,否则有平安危险)
ARouter.openDebug();
}
ARouter.init(mApplication);
}
3.流程剖析
界面跳转到B界面,arouter做了以下工作:
从上图流程中,咱们能够发现Arouter中原理:
1.通过apt技术利用注解编译时生成类,封装指标界面类的类信息。
2.在初始化时,把编译生成的类通过key-value的形式存储在arouter中。
3.发送操作者通过key获取到指标界面类的信息。
4.把发送操作者的信息与指标界面类信息进行联合或者关联在一起。
5.实现跳转性能。
其实简略概括:将须要互相跳转的界面信息传递至arouter中存储关联 & 实现跳转。
4.路由应用
4.1跳转页面不带参
发送跳转操作
// 1. 一般跳转
ARouter.getInstance().build("/test/LoginActivity").navigation();
指标页面
// 在反对路由的页面上增加注解(必选)
// 这里的门路须要留神的是至多须要有两级,/xx/xx
@Route(path = "/test/LoginActivity")
public class LoginActivity extend Activity {
...
}
咱们定义一个RouterUtils工具类来对立治理这些路由跳转门路path
public class RouterUtils {
/**
* 定义一个RouterUtils工具类
*/
//定义一个Group,ARouter路由第一个层级代表一个组别
public static final String LOGIN_GROUP = "/login";
public static final String LoginActivity=LOGIN_GROUP+"/LoginActivity";
public static final String WebViewActivity=LOGIN_GROUP+"/WebViewActivity";
}
5.2跳转界面带参数
发送跳转
ARouter.getInstance().build(RouterUtils.WebViewActivity).withString("url", getString(R.string.help_center)).navigation();
指标页面
@Route(path = RouterUtils.WebViewActivity)
public class WebViewActivity extends BaseActivity {
//获取数据三种形式
//1.通过Autowired注解表明key & 须要在onCreate中调用ARouter.getInstance().inject(this);配合应用
@Autowired(name = "key1")
public long data;
//2.通过Autowired注解 & 将key1作为属性的名称 & 须要在onCreate中调用ARouter.getInstance().inject(this);配合应用
@Autowired()
public long key1;
//前两种没怎么用过
//3.获取数据,通过Bundle
String url=getIntent().getStringExtra("url");
LogUtils.d("initData: url=" + url);
}
5.3 Uri跳转
界面配置
<activity android:name=".activity.SchameFilterActivity">
<!-- Schame -->
<intent-filter>
<data
android:host="m.aliyun.com"
android:scheme="arouter"/>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>
</activity
发送跳转操作
Uri testUriMix = Uri.parse("arouter://m.aliyun.com/test/activity2");
ARouter.getInstance().build(testUriMix)
.withString("key1", "value1")
.navigation();
指标页面
@Route(path = "/test/activity2")
public class Test2Activity extends AppCompatActivity {
}
5.4跳转页面后果监听
ARouter.getInstance()
.build("/test/activity2")
.navigation(this, new NavCallback() {
@Override
public void onArrival(Postcard postcard) {
}
@Override
public void onInterrupt(Postcard postcard) {
Log.d("ARouter", "被拦挡了");
}
});
5.5申明拦截器(拦挡跳转过程,面向切面编程)
// 比拟经典的利用就是在跳转过程中解决登陆事件,这样就不须要在指标页反复做登陆查看
// 拦截器会在跳转之间执行,多个拦截器会按优先级程序顺次执行
@Interceptor(priority = 8, name = "测试用拦截器")
public class TestInterceptor implements IInterceptor {
@Override
public void process(Postcard postcard, InterceptorCallback callback) {
...
callback.onContinue(postcard); // 解决实现,交还控制权
// callback.onInterrupt(new RuntimeException("我感觉有点异样")); // 感觉有问题,中断路由流程
// 以上两种至多须要调用其中一种,否则不会持续路由
}
@Override
public void init(Context context) {
// 拦截器的初始化,会在sdk初始化的时候调用该办法,仅会调用一次
}
}
5.6 为指标页面申明更多信息
// 咱们常常须要在指标页面中配置一些属性,比方说"是否须要登陆"之类的
// 能够通过 Route 注解中的 extras 属性进行扩大,这个属性是一个 int值,换句话说,单个int有4字节,也就是32位,能够配置32个开关
// 剩下的能够自行施展,通过字节操作能够标识32个开关,通过开关标记指标页面的一些属性,在拦截器中能够拿到这个标记进行业务逻辑判断
@Route(path = "/test/activity", extras = Consts.XXXX)
5.7依赖注入解耦
注册须要依赖注入的对象
@Route(path = "/provider/testP")
public class TestProvider implements IProvider {
@Override
public void init(Context context) {
}
public String test(){
return ("Hello Provider!");
}
}
获取对象 & 应用
ARouter.getInstance().navigation(TestProvider.class).test();
ARouter 的GitHub:https://github.com/alibaba/AR…有很多路由的应用
三:路由ARouter构造解析
ARouter次要由三局部组成,包含对外提供的api调用模块、注解模块以及编译时通过注解生产相干的类模块。
- arouter-annotation注解的申明和信息存储类的模块
- arouter-compiler编译期解析注解信息并生成相应类以便进行注入的模块
- arouter-api外围调用Api性能的模块
annotation模块
Route(path)、Interceptor(拦挡)、Autowired(数据获取)都是在开发是须要的注解
compiler模块,这是编译产生的文件
AutoWiredProcessor(主动拆卸处理器)、InterceptorProcessor(拦截器处理器)、RouteProcessor(路由门路处理器)别离为annotation模块对应的Autowired、Interceptor、Route在我的项目编译时产生相干的类文件。
api模块
次要是ARouter具体实现和对外裸露应用的api
结尾:常识学习,由大到小
发表回复