关于android:Android-阿里路由框架ARouter

4次阅读

共计 4450 个字符,预计需要花费 12 分钟才能阅读完成。

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

结尾:常识学习,由大到小

正文完
 0