应用链接是指使用 HTTP 或 HTTPS 架构且经过 Android 验证与您的网站相关联的深层链接。如需注册以处理应用链接,请按以下步骤操作:
- 在应用清单中添加一个或多个 intent 过滤器,以指定您的网站网域或网址。
- 将
autoVerify="true"attribute添加到 intent 过滤器元素。这会向系统发出信号,表明系统应尝试根据您网站的assetlinks.json配置验证架构和主机网域。 - 声明网站关联性。
以下是应用链接声明的示例,其中包含架构和主机以及
autoVerify="true":
<activity
android:name=".MainActivity"
android:exported="true"
...>
<!-- Make sure you explicitly set android:autoVerify to "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" />
<!-- If a user clicks on a link that uses the "http" scheme, your
app should be able to delegate that traffic to "https". -->
<!-- Do not include other schemes, as this will prevent verification. -->
<data android:scheme="http" />
<data android:scheme="https" />
<!-- Include one or more domains that should be verified. -->
<data android:host="www.example.com" />
<data android:host="*.example.com" />
</intent-filter>
</activity>
代码要点
- AutoVerify:应用链接必须使用
android:autoVerify="true" 属性。它会向系统发出信号,表明系统应尝试验证应用与 架构和网域之间的关联,这些架构和网域在<data>标记中指定。建议将autoVerify="true” 添加到您希望可验证的每个 intent 过滤器。 - 数据元素:每个应用链接 intent 过滤器都必须包含一个或多个
<data>元素,这些元素用于指定与您的 可验证网站网域匹配的架构和主机格式。 - 架构:intent 过滤器必须包含
<data>元素,用于http和https架构。 主机:您可以选择添加
<data>元素来匹配一个或多个 主机。使用通配符 (*) 来匹配多个子网域(例如*.example.com)。系统会尝试根据您的 assetlinks.json 文件验证每个主机。请注意,任何路径级路由都应由 assetlinks.json 文件处理(请参阅下面的最佳实践部分)。多个主机:如果您声明了多个主机网域,系统(在 Android 12 及更高版本上)会尝试验证每个主机网域。如果验证了任何主机,应用将成为来自该已验证主机的链接的默认处理程序。在 Android 11 及更低版本上,如果即使有一个主机无法验证,验证也会失败。
多个 intent 过滤器:如果您想要声明唯一网址(例如特定的架构和主机组合),则创建单独的过滤器很重要,因为同一 intent 过滤器中的多个
<data>元素会合并在一起,以涵盖合并后属性的所有变体。
清单过滤规则注意事项
如果您要在 Android 15 及更高版本中设置过滤器以用于动态应用链接,请务必记住,在服务器端 assetlinks.json 文件中声明的动态规则无法扩展您在应用清单中静态声明的网址规则的范围。
因此,我们建议使用以下方法:
- 在应用清单中,设置尽可能广泛的范围,例如仅声明架构和网域
- 依靠服务器端 assetlinks.json 规则进行进一步优化,例如路径级路由。
通过这种理想的配置,您可以根据需要在 assetlinks.json 文件中动态添加新的应用链接路径,并知道它们将适合您在应用清单中设置的广泛范围。
支持多个主机的应用链接
系统必须能够对照该 intent 过滤器中相应网域上托管的 Digital Asset Links 文件验证应用网址 intent 过滤器的数据元素中指定的主机。如果验证失败,系统会默认采用标准行为解析相应 intent,具体如创建指向应用内容的深层链接中所述。不过,应用仍可以验证为处理应用其他 intent 过滤器中定义的任何网址格式的默认处理程序。
例如,如果在
https://www.example.com/.well-known/assetlinks.json中找到了assetlinks.json文件,但在
https://www.example.net/.well-known/assetlinks.json中未找到,则具有以下 intent 过滤器的应用仅会通过针对https://www.example.com的验证:
<application>
<activity android:name=”MainActivity”>
<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" />
<data android:scheme="https" />
<data android:host="www.example.com" />
</intent-filter>
</activity>
<activity android:name="SecondActivity">
<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" />
<data android:host="www.example.net" />
</intent-filter>
</activity>
</application>
支持多个子网域的应用链接
Digital Asset Links 协议将 intent 过滤器中的子网域视为唯一的独立主机。因此,如果您的 intent 过滤器列出多个包含不同子网域的主机,您必须在每个网域上分别发布一个有效的 assetlinks.json。
例如,以下 intent 过滤器包含 www.example.com 和 mobile.example.com 作为接受的 intent 网址主机。因此,必须在 https://www.example.com/.well-known/assetlinks.json
和 https://mobile.example.com/.well-known/assetlinks.json 中发布有效的 assetlinks.json
。
<application>
<activity android:name="MainActivity">
<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" />
<data android:scheme="https" />
<data android:host="www.example.com" />
<data android:host="mobile.example.com" />
</intent-filter>
</activity>
</application>
或者,如果您使用通配符(例如
*.example.com)声明主机名,则必须在根
主机名 (example.com) 上发布 assetlinks.json 文件。例如,只要将 assetlinks.json 文件发布到
https://example.com/.well-known/assetlinks.json,具有以下 intent 过滤器的应用就会通过针对 example.com 的任何子域名(如
foo.example.com)的验证:
<application>
<activity android:name="MainActivity">
<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" />
<data android:host="*.example.com" />
</intent-filter>
</activity>
</application>
检查与同一网域关联的多个应用
如果您发布了多个应用,其中每个应用都与同一网域相关联,则它们都可以成功地通过验证。不过,如果这些应用可以解析完全相同的网域主机和路径,就像一个应用的精简版和完整版一样,则只有最近安装的应用才能解析该网域的网络 intent。
在这种情况下,请检查用户的设备上是否有可能发生冲突的应用,
前提是具有必要的 软件包可见性。然后,在您的应用中,
显示一个自定义选择器对话框,其中包含调用
queryIntentActivities所得到的结果。用户可以从该对话框中显示的匹配应用的列表中选择他们偏好的应用。
Android 14 及更低版本的动态应用链接向后兼容性
动态应用链接功能(包括
assetlinks.json中的高级匹配规则以及<uri-relative-filter-group>的使用)仅在 Android 15(API 级别 35)及更高版本上完全
受支持。
在 Android 14(API 级别 34)及更低版本上,系统仅考虑在清单的 <data> 元素中声明的 scheme
和 host 进行应用链接
验证。系统不会应用来自 assetlinks.json 的路径专用规则、排除项和动态更新。
这意味着,如果您的清单仅指定了 scheme 和 host,则无论您在 Android 15 及更高版本的 assetlinks.json 中定义了哪些路径专用规则,您的应用都可能会在 Android 14 及更低版本上意外捕获已验证网域的所有路径。
为较低 Android 版本配置回退策略,以便不使用深层链接
为了防止您的应用在 Android 14 及更低版本上处理网域的所有链接,而您打算在 Android 15 及更高版本上使用动态应用链接来处理更具体的路径,请在清单的 intent 过滤器中添加不匹配的路径。
添加一个 <data> 元素,其中包含一个 android:path 属性,该属性不太可能
成为链接的有效路径。这样可确保 intent 过滤器不会匹配较低版本上的所有路径。
示例:
<activity
android:name=".MainActivity"
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="http" />
<data android:scheme="https" />
<data android:host="www.example.com" />
<!-- Add a non-matching path for backward compatibility -->
<data android:path="/no_match_for_older_android_versions" />
<uri-relative-filter-group android:allow="true">
<data android:pathPattern="/.*"/>
</uri-relative-filter-group>
</intent-filter>
</activity>
通过添加 <data android:path="/no_match_for_older_android_versions" />,您可以确保在 Android 14 及更低版本上,此 intent 过滤器不会匹配任何传入链接,同时仍允许根据 assetlinks.json 规则中的高级匹配规则验证网域,以便在 Android 15 及更高版本上使用动态应用链接。
迁移现有应用链接
如果您已在清单中设置了具有特定路径规则(例如
android:pathPrefix)的应用链接,并且想要开始在 Android 15 及更高版本上使用动态应用链接
,则可以直接将 <uri-relative-filter-group>
元素安全地添加到现有 intent 过滤器。
由于 Android 14 及更低版本会忽略 <uri-relative-filter-group> 元素,
您现有的应用链接在运行较低 Android 版本的设备上会继续按其当前工作方式正常运行。
不过,您必须仔细考虑 Android 15 及更高版本如何评估“混合”配置:
- 双层过滤 :在 Android 15 及更高版本上,系统会将 intent 过滤器评估为并集。如果网址满足
您的旧版静态
<data>标记或<uri-relative-filter-group>中的广泛规则,则该网址会通过清单检查。网址通过此初始清单检查后,系统会应用在assetlinks.json文件中定义的动态规则,作为第二层精细过滤。这意味着,服务器端 JSON 规则最终决定了哪些匹配的网址实际上会打开应用。
混合配置示例 :
<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" />
<data android:host="www.example.com" />
<!-- Legacy rule: Android 14 and lower use this. Android 15 and higher
also use this. -->
<data android:pathPrefix="/store" />
<!--
Dynamic rule: Android 14 and lower ignore this. Android 15 and higher
evaluate this as a union between all paths and the configuration
specified in the assetlinks.json file. Make sure to apply further
refinements in the assetlinks.json file to prevent all URL paths from
opening in the app.
-->
<uri-relative-filter-group android:allow="true">
<data android:pathPrefix="/" />
</uri-relative-filter-group>
</intent-filter>