{"id":25046025,"url":"https://github.com/wangchenyan/crouter","last_synced_at":"2025-10-27T19:48:53.689Z","repository":{"id":98874970,"uuid":"207708657","full_name":"wangchenyan/crouter","owner":"wangchenyan","description":"支持组件化的 Android 路由框架","archived":false,"fork":false,"pushed_at":"2024-06-12T04:31:22.000Z","size":282,"stargazers_count":26,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T17:35:22.074Z","etag":null,"topics":["android","interceptor","modularization","router"],"latest_commit_sha":null,"homepage":"https://juejin.im/post/6844903923606618126","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wangchenyan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-11T02:48:30.000Z","updated_at":"2025-01-05T05:08:02.000Z","dependencies_parsed_at":"2024-06-06T09:39:37.868Z","dependency_job_id":"326bb362-9113-4211-bc67-7a8d9131d373","html_url":"https://github.com/wangchenyan/crouter","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangchenyan%2Fcrouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangchenyan%2Fcrouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangchenyan%2Fcrouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangchenyan%2Fcrouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wangchenyan","download_url":"https://codeload.github.com/wangchenyan/crouter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248815453,"owners_count":21165929,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","interceptor","modularization","router"],"created_at":"2025-02-06T06:34:25.468Z","updated_at":"2025-10-27T19:48:53.581Z","avatar_url":"https://github.com/wangchenyan.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# crouter\n\n[![](https://jitpack.io/v/wangchenyan/crouter.svg)](https://jitpack.io/#wangchenyan/crouter)\n\n## 系列文章\n\n- [手撸一个 Router 框架（上）：熟悉 APT](https://juejin.im/post/6844903923606618126)\n- [手撸一个 Router 框架（下）：路由拦截机制](https://juejin.im/post/6844904193866596365)\n\n## Features\n\n1. 支持直接解析标准 URL 进行跳转，参数在目标页面通过 getIntent 获取\n2. 支持多模块工程使用\n3. 支持添加多个拦截器，自定义拦截顺序\n4. Activity 自动注册（使用 [AutoRegister](https://github.com/luckybilly/AutoRegister) 实现自动注册）\n5. 自动处理 `startActivityForResult` 回调\n6. 支持获取 Fragment\n7. 支持启动 Fragment（需要设置 `RouterClient#fragmentContainerIntent`，可参考 sample）\n8. 支持 Kotlin\n\n## Change Log\n\n`v3`\n\n- 支持 APG 8+\n\n`v2`\n\n- 优化 Api 调用\n- 新增获取 Fragment\n- 新增启动 Fragment\n\n`v1`\n\n- 完成路由功能\n\n## Dependency\n\n### 1. 添加 Jitpack 仓库\n\n```kotlin\n// settings file\npluginManagement {\n    repositories {\n        maven(\"https://jitpack.io\")\n    }\n}\n\ndependencyResolutionManagement {\n    repositories {\n        maven(\"https://jitpack.io\")\n    }\n}\n```\n\n### 2. 添加 crouter-plugin 插件，用于字节码注入\n\n\u003e 注意：插件仅支持 AGP 7.4+\n\n```kotlin\n// root build file\nbuildscript {\n    dependencies {\n        classpath(\"com.github.wangchenyan.crouter:crouter-plugin:${latestVersion}\")\n    }\n}\n```\n\n```kotlin\n// app build file\nplugins {\n    id(\"crouter-plugin\")\n}\n```\n\n### 3. 添加 router 依赖和注解处理器\n\n\u003e 注意：`2.4.0` 版本开始使用 `ksp`，建议尽快迁移，如果仍想使用 `kapt`，请使用 `2.3.0` 版本\n\n```kotlin\n// app build file\nplugins {\n    id(\"com.google.devtools.ksp\")\n}\n\nksp {\n    // 使用默认\n    arg(\"moduleName\", project.name)\n    // 默认 scheme\n    arg(\"defaultScheme\", \"(http|https|native|host)\")\n    // 默认 host\n    arg(\"defaultHost\", \"(\\\\w+\\\\.)*host\\\\.com\")\n}\n\ndependencies {\n    ksp(\"com.github.wangchenyan.crouter:crouter-processor:${latestVersion}\")\n    implementation(\"com.github.wangchenyan.crouter:crouter-api:${latestVersion}\")\n}\n```\n\n## Usage\n\n### 1. 初始化，设置路由客户端\n\n建议在 Application 的 onCreate，或第一个 Activity 的 onCreate 中执行\n\n```kotlin\nCRouter.setRouterClient(\n    RouterClient.Builder()\n        // 设置登录拦截器，可选\n        .loginProvider { context, callback -\u003e\n            CRouter.with(context)\n                .url(\"scheme://host/login\")\n                .startForResult {\n                    if (it.isSuccess()) {\n                        callback.invoke()\n                    }\n                }\n        }\n        // 启动 Fragment 时，需要设置 Fragment 容器 Activity 的 Intent\n        .fragmentContainerIntent(Intent(this, FragmentContainerActivity::class.java))\n        .build()\n)\n```\n\n### 2. 在 `BaseActivity` 中配置 `getIntent` 包装\n\n```kotlin\nabstract class BaseActivity : AppCompatActivity() {\n    override fun getIntent(): Intent {\n        return RouterIntent(super.getIntent())\n    }\n}\n```\n\n### 3. 配置 Activity 路由注解\n\n```kotlin\n// path 使用正则匹配，注意转义\n@Route(\"/target\\\\.html\", needLogin = true)\nclass TargetActivity : BaseActivity() {\n}\n```\n\n### 4. 尽情使用吧\n\n```kotlin\n// 不关心结果\nCRouter.with(this)\n    .url(\"scheme://host/target.html\")\n    .start()\n\n// 关心结果\nCRouter.with(this)\n    .url(\"scheme://host/target.html\")\n    .startForResult {\n        if (it.isSuccess(\"key\")) {\n            val value = it.data?.getStringExtra(\"key\")\n            alert(\"跳转取值\", value)\n        }\n    }\n```\n\n**更多用法请参考 [sample](https://github.com/wangchenyan/crouter/tree/master/sample) 代码**\n\n## Interceptor\n\n在 CRouter 中使用拦截器和 OkHttp 的用法一致，这里以添加一个 H5 页面拦截器为例，当识别到网页链接，用内置浏览器打开\n\n```kotlin\nclass H5Interceptor : Interceptor {\n    override fun intercept(chain: Interceptor.Chain): Response {\n        val request = chain.request()\n        val uri = request.uri()\n        val response = chain.proceed(request)\n        if (response.intent() == null \u0026\u0026 uri != null \u0026\u0026 (uri.scheme == \"http\" || uri.scheme == \"https\")) {\n            val context = request.context()\n            val intent = Intent(context, BrowserActivity::class.java)\n            intent.putExtra(Extra.URL, uri.toString())\n            return Response.Builder()\n                .context(context)\n                .request(request)\n                .intent(intent)\n                .build()\n        }\n        return response\n    }\n}\n```\n\n在初始化时添加拦截器\n\n```kotlin\nCRouter.setRouterClient(\n    RouterClient.Builder()\n        .addInterceptor(H5Interceptor())\n        .build()\n)\n```\n\n## ProGuard\n\n无\n\n## About me\n\n掘金：https://juejin.im/user/2313028193754168\n\n微博：https://weibo.com/wangchenyan1993\n\n## License\n\n    Copyright 2019 wangchenyan\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n       http://www.apache.org/licenses/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangchenyan%2Fcrouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwangchenyan%2Fcrouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangchenyan%2Fcrouter/lists"}