🚀 Android Deep Link 技术详解与实践指南
在 Android 应用开发中,我们经常会遇到这样的场景:
-
从短信、网页、推送或第三方 App 点击链接后,直接打开自己 App 的指定页面;
-
H5 页面跳转 App 实现无缝衔接;
-
营销活动链接带参数打开应用指定内容。
这些功能的核心实现手段,就是 Deep Link(深度链接)。
一、什么是 Deep Link?
Deep Link 简单来说就是一种"从外部链接直接唤起 App 内特定页面 "的机制。
当用户点击一个符合规则的 URL(例如 myapp://detail?id=123
)时,系统会解析该链接并唤起对应的 App 页面。
类比网页:
普通网页链接跳转是
https://example.com/detail?id=123
Deep Link 则是
myapp://detail?id=123
,它会跳转到 App 内 detail 页面
二、Deep Link 的类型
Deep Link 在 Android 生态中主要分为三种类型:
类型 | 说明 | 示例 |
---|---|---|
Custom URI Scheme | 自定义协议,最基础、最早的 Deep Link 方案 | myapp://product/detail?id=1 |
Android App Links | Android 6.0+ 推出的官方方案,支持 HTTPS 验证,安全性高 | https://www.example.com/product/1 |
Intent Filters with HTTP/HTTPS | 普通的 HTTP 链接匹配,不需要域名验证 | http://m.example.com/detail?id=1 |
本文主要聚焦 Custom URI Scheme + App Links 两种主流实现方式。
三、Custom Scheme Deep Link 实现
1. Manifest 配置
在目标页面的 Activity
中声明一个 Intent Filter:
XML
<activity
android:name=".ui.DetailActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- 自定义协议 -->
<data
android:scheme="myapp"
android:host="detail" />
</intent-filter>
</activity>
2. 链接触发示例
-
网页中:
<a href="myapp://detail?id=10086">打开App详情页</a>
-
ADB 模拟触发:
adb shell am start -a android.intent.action.VIEW -d "myapp://detail?id=10086"
3. 获取参数
在 DetailActivity
中通过 Intent
获取参数:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val data: Uri? = intent.data
val id = data?.getQueryParameter("id")
Log.d("DeepLink", "detail id = $id")
}
四、Android App Links 实现(官方推荐)
相比自定义 scheme,App Links 具有以下优势:
-
使用标准 HTTPS 链接;
-
只有验证过的域名才会触发应用打开;
-
避免了多个 App 注册相同 scheme 冲突的问题。
1. Manifest 配置
XML
<activity
android:name=".ui.DetailActivity"
android:exported="true">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https"
android:host="www.example.com"
android:pathPrefix="/detail" />
</intent-filter>
</activity>
2. 域名验证文件
在服务器根目录放置一个文件:
路径: https://www.example.com/.well-known/assetlinks.json
内容:
[
{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "com.example.myapp",
"sha256_cert_fingerprints": [
"A1:B2:C3:D4:...:ZZ"
]
}
}
]
SHA256 指纹可以通过以下命令获取:
keytool -list -v -keystore myapp.keystore
五、从外部浏览器跳转 App
1️⃣ 若 App 已安装
- 系统自动匹配 Intent Filter 并打开对应 Activity。
2️⃣ 若 App 未安装
-
可配置 fallback 链接(例如跳转到应用商店下载页):
打开应用
六、Deep Link 参数解析与路由封装
在大型项目中,为了统一管理各个页面的跳转,可以封装一个 DeepLink 路由管理器:
Kotlin
object DeepLinkRouter {
fun handleDeepLink(context: Context, uri: Uri) {
when (uri.host) {
"detail" -> {
val id = uri.getQueryParameter("id")
val intent = Intent(context, DetailActivity::class.java)
intent.putExtra("id", id)
context.startActivity(intent)
}
"profile" -> {
val userId = uri.getQueryParameter("userId")
val intent = Intent(context, ProfileActivity::class.java)
intent.putExtra("userId", userId)
context.startActivity(intent)
}
}
}
}
然后在入口 Activity
(例如 MainActivity
)中统一接管:
Kotlin
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
intent?.data?.let { DeepLinkRouter.handleDeepLink(this, it) }
}
七、常见问题与排查技巧
问题 | 原因 | 解决方案 |
---|---|---|
点击链接无反应 | 没有匹配到 Intent Filter | 检查 scheme、host、path 是否完全一致 |
链接被浏览器拦截 | 未声明 BROWSABLE |
添加 <category android:name="android.intent.category.BROWSABLE"/> |
App Links 未验证成功 | assetlinks.json 配置错误 | 用 adb shell am verify-app-links 检查验证状态 |
多个应用注册相同 scheme | 冲突 | 优先使用 App Links(HTTPS)避免冲突 |
八、总结
对比项 | Custom Scheme | App Links |
---|---|---|
安全性 | 弱,易被抢注 | 高,需域名验证 |
使用门槛 | 简单,无需服务器 | 需配置 assetlinks.json |
链接样式 | myapp:// |
https:// |
推荐场景 | 内部渠道唤起、测试 | 生产环境、H5落地页、广告跳转 |
九、结语
Deep Link 是连接 Web 与 App 的桥梁,是移动增长和产品体验的重要一环。
在项目中合理使用 Deep Link,可以极大提升用户转化率与跳转体验。