Android 12及以上点击指定链接唤起App的实现方案咨询
一、无需配置App Link的可行方法
1. 使用自定义Scheme
如果可以修改链接格式,改用自定义Scheme(如com.abc.efg://callback?wjiofjjfie...),这种方式无需App Link验证,Android 12+ 可直接唤起应用。修改Intent Filter如下:
<activity android:name=".UrlActivity" android:screenOrientation="portrait" 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="com.abc.efg" android:path="/callback" /> </intent-filter> </activity>
注意:自定义Scheme链接无法被浏览器直接解析,仅适用于应用内跳转、短信、通知等场景。
2. 修复普通深链接配置(需用户手动选择)
若坚持使用HTTP/HTTPS链接,先修正Intent Filter的匹配规则——你当前的配置缺少路径匹配,导致浏览器无法触发应用选择器。修改后配置如下:
<activity android:name=".UrlActivity" android:screenOrientation="portrait" 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="https" android:host="com.abc.efg" android:pathPrefix="/callback" /> <data android:scheme="http" android:host="com.abc.efg" android:pathPrefix="/callback" /> </intent-filter> </activity>
修改后,用户点击链接时会弹出应用选择器,若之前设置了浏览器为默认打开方式,需前往应用设置清除默认偏好。
二、配置App Link(默认直接唤起应用)
如果需要HTTP/HTTPS链接无需用户选择,直接唤起应用,必须配置App Link,步骤如下:
1. 确认域名控制权
你需要完全控制com.abc.efg域名(注意:该格式更接近包名,正常域名应为abc.com这类格式,请确认你拥有对应域名的管理权限)。
2. 生成assetlinks.json文件
文件需包含应用包名和签名证书的SHA-256指纹,格式示例:
[{ "relation": ["delegate_permission/common.handle_all_urls"], "target": { "namespace": "android_app", "package_name": "你的应用包名", "sha256_cert_fingerprints": ["签名证书的SHA-256指纹"] } }]
- 获取SHA-256指纹:可通过Android Studio的
Build > Generate Signed Bundle/APK查看,或执行命令:keytool -list -v -keystore 你的签名文件路径 -alias 签名别名
3. 上传文件到域名
将assetlinks.json上传至域名的.well-known目录,确保可通过https://com.abc.efg/.well-known/assetlinks.json直接访问(必须使用HTTPS,且无重定向)。
4. 修改Intent Filter
为Intent Filter添加android:autoVerify="true"属性,触发系统自动验证:
<activity android:name=".UrlActivity" android:screenOrientation="portrait" 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="com.abc.efg" android:pathPrefix="/callback" /> </intent-filter> </activity>
注意:App Link仅支持HTTPS链接,可移除HTTP相关配置。
5. 测试验证
安装签名后的应用(正式/debug签名均可,但需与assetlinks.json中的指纹匹配),点击目标链接,系统验证通过后会直接唤起应用。也可通过Android Studio的Tools > App Links Assistant辅助配置和测试。
内容的提问来源于stack exchange,提问作者Abhi

