TheRouter 基础能力介绍


功能介绍

TheRouter 核心功能具备四大能力:

一、快速上手

1.1 Gradle 引入

最新版本号,请访问:(github/releases)

// 项目根目录 build.gradle 引入
classpath 'cn.therouter:plugin:1.1.4-rc4'

// app module 中引入
apply plugin: 'therouter'

// 依赖
kapt "cn.therouter:apt:1.1.4-rc4"
implementation "cn.therouter:router:1.1.4-rc4"

1.2 初始化

框架内部包含自动初始化功能,详见单模块自动初始化能力
无需任何初始化代码。但推荐你根据业务设置否为Debug环境,用以查看日志信息。
Application.attachBaseContext() 方法中尽可能早设置当前是否为Debug环境。

@Override
protected void attachBaseContext(Context base) {
    TheRouter.setDebug(true or false);
    super.attachBaseContext(base);  
}

1.3 页面参数注入

ActivityFragmentonCreate()方法中调用,建议直接在BaseActivity(BaseFragment)中做

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TheRouter.inject(this);
}

1.4 页面跳转

关于注解@Route的参数含义,请查看文档:页面导航跳转能力

@Route(path = "http://therouter.com/home", action = "action://scheme.com",
        description = "第二个页面", params = {"hello", "world"})
public class HomeActivity extends BaseActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TheRouter.build("要跳转的目标页Path")
            .withInt("intValue", 12345678) // 传 int 值
            .withString("str_123_Value", "传中文字符串")
            .withBoolean("boolValue", true) 
            .withLong("longValue", 123456789012345L)
            .withChar("charValue", 'c') 
            .withDouble("double", 3.14159265358972)
            .withFloat("floatValue", 3.14159265358972F)
            .navigation();
    }
}

二、编译器配置

可在local.properties配置编译期属性(warning输出日志,error直接报错)

三、混淆配置

# 如果使用了 Fragment 路由,需要保证类名不被混淆
# -keep public class * extends android.app.Fragment
# -keep public class * extends androidx.fragment.app.Fragment
# -keep public class * extends android.support.v4.app.Fragment

-keep class androidx.annotation.Keep
-keep @androidx.annotation.Keep class * {*;}
-keepclassmembers class * {
    @androidx.annotation.Keep *;
}
-keepclasseswithmembers class * {
    @androidx.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
    @androidx.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
    @androidx.annotation.Keep <init>(...);
}
-keepclasseswithmembers class * {
    @com.therouter.router.Autowired <fields>;
}

四、从其他路由迁移至 TheRouter

4.1 迁移工具一键迁移

可使用迁移工具一键迁移:

如果项目中使用了ARouterIProvider.init()方法,可能需要手动处理初始化逻辑。

如下图:

五、源码运行与调试

5.1 工程模块描述

TheRouter
  ├─app
  │   └──代码使用示例Demo
  ├─business-a
  │   └──用于模块化业务模块的演示模块
  ├─business-b
  │   └──用于模块化业务模块的演示模块
  ├─business-base
  │   └──用于模块化基础模块的演示模块
  ├─apt
  │   └──注解处理器相关代码
  ├─plugin
  │   └──编译期 Gradle 插件源码
  └─router
      └──路由库核心代码

5.2 项目运行

  1. 打开local.properties,声明你想要调试的模块 例如希望源码调试apt模块,则声明apt=true即可
  2. 同步 Gradle 变更

5.3 plugin 源码调试

plugin调试比较特殊,需要修改module名。

  1. 修改plugin文件夹名为buildSrc(注意大小写)
  2. 注释根目录build.gradle中的classpath引用(不需要了)
  3. 同步 Gradle 变更

相关推荐:

TheRouterSwift iOS 路由介绍TheRouterSwift iOS 路由介绍

TheRouterSwift是货拉拉TheRouter系列开源框架的Swift版本,为日益增多的Swift开发者提供一高可用路由框架。TheRouterSwift用于模块间解耦和通信,基于Swift协议进行动态懒加载注册路由与打开路由的工具。同时支持通过Service-Protocol寻找对应的模块,并用 protocol进行依赖注入和模块通信。

16 mins
TheRouter iOS 路由介绍TheRouter iOS 路由介绍

TheRouter 是货拉拉打造的一款同时支持 Android 及 iOS 的轻量级路由中间件,在iOS端吸取了其他其他语言的特性,支持注解功能,极大提升了路由在iOS端的使用体感。摒弃了传统 iOSer 的 target-action 或 protocol 理念,向更广的后台或 Android 应用对齐。

4 mins
TheRouter 常见问题处理TheRouter 常见问题处理

如无法解决你的问题,建议登记应用,获取优先技术支持 [https://github.com/HuolalaTech/hll-wp-therouter-an...

5 mins