Android 深层链接路径、pathPrefix 或 pathPattern 不起作用

问题描述

我正在尝试使 Android 深层链接仅适用于特定网址,例如:

所以 http://books.com/about 一直链接到网页

我的 AndroidManifest.xml 中的此配置有效并在我的应用中打开正确的页面。问题是这与 book.com 上的每个页面相匹配

<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="@string/custom_url_scheme" />

    <data android:scheme="https" android:host="books.com" />
</intent-filter>

当我将数据部分更改为:

<data android:scheme="https" android:host="books.com" android:path="/book/nice-book-1" />

<data android:scheme="https" android:host="books.com" android:pathPrefix="/book" />

<data android:scheme="https" android:host="books.com" android:pathPattern="/book/..*" />

仅匹配 /book 后跟一个 slug,它不起作用。

我已经在 Stackoverflow 上发现了许多类似的问题,但它们要么已经过时,要么无法处理 url 与 slugs 一起使用的情况。

是否有人遇到过与 slugs 仅匹配几个 url 的相同问题?

解决方法

我花了很长时间才弄明白。所以我会回答我自己的问题,以节省其他人的宝贵时间。

事实证明,当通过 Android studio 在设备上运行 Android 应用程序时

<data android:scheme="https" android:host="books.com" />

是唯一可行的路径。

使用时:

<data android:scheme="https" android:host="books.com" android:path="/book/nice-book-1" />

<data android:scheme="https" android:host="books.com" android:pathPrefix="/book" />

这些仅在您的设备上使用 签名 版本的应用程序时有效。这可能是因为在您的 assetlinks.json 中有一个 sha256_cert_fingerprints,它只能在使用签名的应用程序时进行验证。