安卓路由技术选型调研

本文侧重调研安卓路由框架,以及简单使用对比,适合不熟悉的人快速上手查字典。

一、路由库选型数据

目前看起来基本的功能都过得去,框架也是大体差不多,互相借鉴的,也都支持路由拦截,回调式onActivityResult等便捷功能。所以这里只做数据展示

选型 所属 更新时间 维护状态 Star 技术文章
ARouter 阿里 2020 - 10 功能冻结 / 基本停更GitHub,最高兼容 AGP 7.x 14.5k 稀土掘金
TheRouter 货拉拉 2026 - 03 活跃维护,支持 Android 15、AGP 9 1.3k 知乎稀土掘金
WMRouter 美团 2021 稳定维护,支持 Kotlin 协程 2.4k 美团技术团队
DRouter 滴滴 2023 - 12 基本停更,功能稳定 777 稀土掘金

目前看起来只有TheRouter维护比较积极,所以后面只好简单分析下TheRouter

二、TheRouter快速上手

TheRouter最简单demo示例

1、基础使用

1、添加依赖

app/build.gradle新增依赖

groovy 复制代码
plugins {
    id 'therouter'
}
dependencies {
    implementation 'cn.therouter:router:1.3.2'
    annotationProcessor 'cn.therouter:apt:1.3.2'
}

根目录build.gradle

groovy 复制代码
// 语法要求buildscript必须放最plugins顶部
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "cn.therouter:plugin:1.3.2"
    }
}
2、框架初始化

Application onCreate中初始化

java 复制代码
TheRouter.init(this);
TheRouter.setDebug(true);

页面 onCreate注册 (建议在BaseActivity、BaseFragment统一处理)

复制代码
TheRouter.inject(this);
3、使用

要跳转的页面注解@Route

java 复制代码
import com.therouter.demo.R;
import com.therouter.router.Autowired;
import com.therouter.router.Route;

@Route(path = "router://home")
public class HomeActivity extends BaseActivity {

    @Autowired
    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        TextView tvName = findViewById(R.id.tv_name);
        tvName.setText("欢迎:" + name);
    }
}
4、页面跳转回调功能
java 复制代码
TheRouter.build("router://home")
        .withString("name", "TheRouter Java Demo")
        .navigation(this,new NavigationCallback(){
            
        });
5、统一拦截功能
java 复制代码
// 拦截器
NavigatorKt.setRouterInterceptor(new RouterInterceptor() {
    @Override
    public void process(@NonNull RouteItem routeItem, @NonNull InterceptorCallback interceptorCallback) {
        if (false){
            Toast.makeText(MainActivity.this, "未登录", Toast.LENGTH_SHORT).show();
        }
        interceptorCallback.onContinue(routeItem);
    }
});

2、跑通官方demo

扫码下载 (张涛没有提供二维码,自己打的包)

最小改动运行TheRouter官方demo

官方库

app/build.gradle.kts启动多dex支持

groovy 复制代码
defaultConfig {
    multiDexEnabled = true
}

根目录settings.gradle.kts替换

groovy 复制代码
pluginManagement {
    repositories {
        maven(url = "https://maven.aliyun.com/repository/google")
        maven(url = "https://maven.aliyun.com/repository/gradle-plugin")
        maven(url = "https://maven.aliyun.com/repository/public")
        maven(url = "https://maven.therouter.cn:8443/repository/maven-public/")
    }
}
plugins {
    id("org.gradle.toolchains.foojay-resolver-convention") version "1.0.0"
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        maven(url = "https://maven.aliyun.com/repository/google")
        maven(url = "https://maven.aliyun.com/repository/central")
        maven(url = "https://maven.therouter.cn:8443/repository/maven-public/")
    }
}

rootProject.name = "TheRouter"

// Include all modules directly
include(":app")
include(":business-a")
include(":business-b")
include(":business-base")
include(":compose")
include(":router")
include(":apt")
相关推荐
Rytter1 天前
某气骑士 libtprt.so 反 Frida 机制分析与绕过
android·安全·网络安全
alexhilton1 天前
揭密:Compose应用如何做到启动提升34%
android·kotlin·android jetpack
沐言人生1 天前
React Native 源码分析1——HybridData 机制深度分析
android·react native
程序员陆业聪1 天前
跨平台框架全景图:Flutter/KMP/KuiKly/RN的2026年格局
android
码云数智-园园1 天前
Fibers(纤程)来了:打破阻塞,实现纯PHP下的异步非阻塞IO
android
shaoming37761 天前
检查系统硬件配置是否满足PyCharm最低要求
android·spring boot·mysql
一起搞IT吧1 天前
高通Camx功能feature分析之十五:insensor zoom介绍及实现
android·智能手机·相机
aqi001 天前
一文读懂 HarmonyOS 6.1 带来的十大重要升级
android·华为·harmonyos·鸿蒙·harmony
秋91 天前
MySQL 9.7.0 使用详解:新特性、实战与避坑指南
android·数据库·mysql
狼与自由1 天前
clickhouse ReplacingMergeTree
android·clickhouse