Android APP Links教程以及踩坑指南 - Jerrysun0227/TechnicalArticles GitHub Wiki

Android APP-Links

1.添加URL Intent Filters

在AndroidManifest文件中需要处理对应URL的Activity中添加Intent Filters,如下所示(两种形式均可):

<activity>
    ...
    <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="http"
                android:host="www.jerrysun0227.com" android:pathPrefix="/example/" />
        <data
                android:scheme="https"
                android:host="www.jerrysun0227.com" android:pathPrefix="/example/" />
    </intent-filter>
    ...
</activity>

<activity>
    ...
    <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="http"
                android:host="www.jerrysun0227.com" android:pathPrefix="/example/" />
    </intent-filter>
    <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.jerrysun0227.com" android:pathPrefix="/example/" />
    </intent-filter>
    ...
</activity>

scheme的值可以是http,https二选其一,也可以全部都加上。host是URL中的域名部分。pathPrefix/path是可选项,如果想要URL对应的更精准,则可以添加pathPrefix/path。此外,intent-filters要添加android:autoVerify="true"这个属性,只有添加了这个属性以后,在Android6.0及以上的系统上安装APP的时候会触发系统对APP里和URL有关的每一个域名的验证。验证的步骤如下:

  1. 系统会检查所有包含以下特征的intent filter:Action为 android.intent.action.VIEW、Category为android.intent.category.BROWSABLE和android.intent.category.DEFAULT、Data scheme为http或https
  2. 对于在上述intent filter里找到的每一个唯一的域名,Android系统会到对应的域名下查找数字资产文件,地址是:https://your.domain/.well-known/assetlinks.json

只有当系统为AndroidManifest里找到的每一个域名都找到了对应的assetlinks.json文件,系统才会把该APP设置为特定的默认处理器

2.生成assetlinks.json文件

Android Studio 2.3及以上版本提供了可视化的工具"APP Links Assistant"用来生成assetlinks.json文件,如下图所示: APP Links Assistant 点击第三步中的"Open Digital Asset Links File Generator"按钮,会在编辑区域打开如下编辑页面: Open Digital Asset Links File Generator 在Site domain下面的文本框中输入域名,在Application ID文本框中输入APP的packagename,SHA256 Fingerprint选择相应版本的签名文件即可,然后点击"Generate Digital Asset Links File"按钮,会在下方的Preview中显示生成的文件的内容,如下图所示: Generate Digital Asset Links File 然后点击"Save file"按钮将文件保存,并上传至域名/.well-known文件夹下,需要确保上传至网站的文件的content-type为application/json,否则不能通过验证 最后点击"Link and Verify"按钮,验证文件是否正确,验证通过后,会在strings.xml文件中增加如下代码:

<resources>
    ...
    <string name="asset_statements">
        [{
        \"relation\": [\"delegate_permission/common.share_location\"],
        \"target\": {
        \"namespace\": \"web\",
        \"site\": \"https://www.jerrysun0227.com\"
        }
        }]
    </string>
    ...
</resources>

同时在AndroidManifest中也会增加如下代码:

<application>
    ...
    <meta-data android:name="asset_statements" android:resource="@string/asset_statements" />
    ...
</application>

至此,APP Links已经设置成功,安装APP后等待约20秒左右,通过如下命令可以查看系统中APP Links的认证状态

adb shell dumpsys package d

如果显示

Package: com.jerrysun0227.applinks
Domains: www.jerrysun0227.com
Status:  always : 200000007

则说明认证成功,每次点击相应的URL时会自动调起APP Status的状态值如下:

  • undefined — app没有在manifest中启用链接自动验证功能
  • ask — app验证失败(会通过打开方式对话框询问用户)
  • always — app通过了验证(点击这个域名总是打开这个app)

小结

  1. 存放assetlinks.json文件的域名,不能加端口号,否则会导致验证失败
  2. 如果strings.xml文件中没有添加"asset_statements",则需要科学上网才能通过验证,反之,则可以直接通过验证
  3. 如果希望APP能处理多个域名,则需要在每个域名下分别放置一份assetlinks.json文件,才可以通过验证

参考资料

  1. https://developer.android.com/studio/write/app-link-indexing
  2. https://juejin.im/post/59b3eab25188257e8d77c991
  3. https://github.com/hehonghui/android-tech-frontier/blob/master/issue-15/Android-M%E7%9A%84App-Links%E5%AE%9E%E7%8E%B0%E8%AF%A6%E8%A7%A3.md
⚠️ **GitHub.com Fallback** ⚠️