安卓应用链接

了解如何配置 Android 应用链接以从标准 Web URL 打开你的 Expo 应用。


要为你的应用配置 Android 应用链接,你需要:

¥To configure Android App Links for your app, you need to:

  • 在项目的应用配置中添加 intentFilters 并将 autoVerify 设置为 true

    ¥Add intentFilters and set autoVerify to true in your project's app config

  • 设置双向关联以验证你的网站和原生应用

    ¥Set up two-way association to verify your website and native app

Watch: Set up Android App Links with Expo Router
Watch: Set up Android App Links with Expo Router

intentFilters 添加到应用配置

¥Add intentFilters to the app config

通过添加 android.intentFilters 属性并将 autoVerify 属性设置为 true 来配置你的应用配置。指定 autoVerify 是 Android App Links 正常工作所必需的。

¥Configure your app config by adding the android.intentFilters property and setting the autoVerify attribute to true. Specifying autoVerify is required for Android App Links to work correctly.

以下示例显示了一个基本配置,使你的应用可以出现在标准 Android 对话框中,作为处理指向 webapp.io 域的任何链接的选项。它还使用常规 https 方案,因为 安卓应用链接支持的 SDK 版本 不同。

¥The following example shows a basic configuration that enables your app to appear in the standard Android dialog as an option for handling any links to the webapp.io domain. It also uses the regular https scheme since Android App Links are different from standard deep links.

app.json
{
  "expo": {
    "android": {
      "intentFilters": [
        {
          "action": "VIEW",
          "autoVerify": true,
          "data": [
            {
              "scheme": "https",
              "host": "*.webapp.io",
              "pathPrefix": "/records"
            }
          ],
          "category": ["BROWSABLE", "DEFAULT"]
        }
      ]
    }
  }
}

在应用/扩展之间共享数据库(iOS)

¥Set up two-way association

要设置网站和 Android 应用之间的双向关联,你需要以下内容:

¥To setup two-way association between the website and Android app, you will need the following:

  • 网站验证:这需要在 /.well-known 目录中创建一个 assetlinks.json 文件并将其托管在目标网站上。此文件用于验证从给定链接打开的应用是否是正确的应用。

    ¥Website verification: This requires creating a assetlinks.json file inside the /.well-known directory and hosting it on the target website. This file is used to verify that the app opened from a given link is the correct app.

  • 原生应用验证:这需要某种形式的引用目标网站域 (URL) 的代码签名。

    ¥Native app verification: This requires some form of code signing that references the target website domain (URL).

创建 assetlinks.json 文件

¥Create assetlinks.json file

1

在 /.well-known/assetlinks.json 中为网站验证创建 assetlinks.json 文件(也称为 数字资源链接 文件)。此文件用于验证应用是否针对给定链接打开。

¥Create an assetlinks.json file for the website verification (also known as digital asset links file) at /.well-known/assetlinks.json. This file is used to verify that the app opened for a given link.

如果你使用 Expo Router 来构建你的网站(或任何其他现代 React 框架,如 Remix、Next.js 等),请在 public/.well-known/assetlinks.json 创建 assetlinks.json。对于旧版 Expo webpack 项目,请在 web/.well-known/assetlinks.json 创建文件。

¥If you're using Expo Router to build your website (or any other modern React framework such as Remix, Next.js, and so on), create assetlinks.json at public/.well-known/assetlinks.json. For legacy Expo webpack projects, create the file at web/.well-known/assetlinks.json.

2

从你的应用配置中的 android.package 下获取 package_name 的值。

¥Get the value of package_name from your app config, under android.package.

3

从你的应用的签名证书中获取 sha256_cert_fingerprints 的值。如果你使用 EAS 构建 构建你的 Android 应用,创建构建后:

¥Get the value of sha256_cert_fingerprints from your app's signing certificate. If you're using EAS Build to build your Android app, after creating a build:

  • 运行 eas credentials -p android 命令,然后选择构建配置文件以获取其指纹值。

    ¥Run eas credentials -p android command, and select the build profile to get its fingerprint value.

  • 复制 SHA256 Fingerprint 下列出的指纹值。

    ¥Copy the fingerprint value listed under SHA256 Fingerprint.

Alternate method to obtain the SHA256 certificate fingerprint from Google Play Console

如果你没有使用 EAS 来管理代码签名,你可以通过手动构建和提交你的应用到 谷歌游戏控制台 来找到 sha256_cert_fingerprints:

¥If you're not using EAS to manage code signing, you can find the sha256_cert_fingerprints by building and submitting your app manually to the Google Play Console:

  • 在 Google Play 控制台的仪表板中,转到发布 > 设置 > 应用签名。

    ¥Inside the Google Play Console's dashboard, go to Release > Setup > App Signing.

  • 为你的应用找到正确的数字资源链接 JSON 片段。

    ¥Find the correct Digital Asset Links JSON snippet for your app.

  • 复制看起来像 14:6D:E9:83... 的值并将其粘贴到 sha256_cert_fingerprints 下的 public/.well-known/assetlinks.json 文件中。

    ¥Copy the value looks like 14:6D:E9:83... and paste it into your public/.well-known/assetlinks.json file under sha256_cert_fingerprints.

4

package_namesha256_cert_fingerprints 添加到 assetlinks.json 文件中:

¥Add package_name and sha256_cert_fingerprints to the assetlinks.json file:

public/.well-known/assetlinks.json
[
{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target": {
    "namespace": "android_app",
    "package_name": "com.example",
    "sha256_cert_fingerprints": [
      // Supports multiple fingerprints for different apps and keys
      "14:6D:E9:83:51:7F:66:01:84:93:4F:2F:5E:E0:8F:3A:D6:F4:CA:41:1A:CF:45:BF:8D:10:76:76:CD"
    ]
  }
}
]
你可以向 sha256_cert_fingerprints 数组添加多个指纹以支持应用的不同变体。欲了解更多信息,请参阅 有关如何声明网站关联的 Android 文档

托管 assetlinks.json 文件

¥Host assetlinks.json file

使用你域中的 Web 服务器托管 assetlinks.json 文件。此文件必须使用内容类型 application/json 提供,并且可通过 HTTPS 连接访问。通过在地址栏中输入完整的 URL,验证你的浏览器是否可以访问此文件。

¥Host the assetlinks.json file using a web server with your domain. This file must be served with the content-type application/json and accessible over an HTTPS connection. Verify that your browser can access this file by typing the complete URL in the address bar.

原生应用验证

¥Native app verification

在 Android 设备上安装应用以触发 安卓应用验证 进程。

¥Install the app on an Android device to trigger the Android app verification process.

打开应用后,请参阅 处理进入你应用的链接 以获取有关如何处理入站链接并向用户显示他们请求的内容的更多信息。

¥Once you have your app opened, see Handle links into your app for more information on how to handle inbound links and show the user the content they requested.

调试

¥Debugging

Expo CLI 使你无需部署网站即可测试 Android App Links。利用 --tunnel 功能,你可以将开发服务器转发到公开可用的 HTTPS URL。

¥The Expo CLI enables you to test Android App Links without deploying a website. Utilizing the --tunnel functionality, you can forward your dev server to a publicly available HTTPS URL.

1

设置环境变量 EXPO_TUNNEL_SUBDOMAIN=my-custom-domain,其中 my-custom-domain 是你在开发过程中使用的唯一字符串。这可确保你的隧道 URL 在开发服务器重启时保持一致。

¥Set the environment variable EXPO_TUNNEL_SUBDOMAIN=my-custom-domain where my-custom-domain is a unique string that you use during development. This ensures that your tunnel URL is consistent across dev server restarts.

2

intentFilters 作为 如上所述 添加到你的应用配置中。用 Ngrok URL 替换 host 值:my-custom-domain.ngrok.io

¥Add intentFilters to your app config as described above. Replace the host value with a Ngrok URL: my-custom-domain.ngrok.io.

3

使用 --tunnel 标志启动你的开发服务器:

¥Start your dev server with the --tunnel flag:

Terminal
npx expo start --tunnel

4

在你的设备上编译开发版本:

¥Compile the development build on your device:

Terminal
npx expo run:android

5

使用以下 adb 命令启动意图活动并在你的应用上打开链接或在设备的 Web 浏览器中键入自定义域链接。

¥Use the following adb command to start the intent activity and open the link on your app or type the custom domain link in your device's web browser.

Terminal
adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "https://my-custom-domain.ngrok.io/" <your-package-name>

故障排除

¥Troubleshooting

以下是一些在实现 Android 应用链接时帮助你排除故障的常见提示:

¥Here are some common tips to help you troubleshoot when implementing Android App Links:

  • 确保你的网站通过 HTTPS 提供服务,并且内容类型为 application/json

    ¥Ensure your website is served over HTTPS and with the content-type application/json

  • 验证 Android 应用链接

    ¥Verify Android app links

  • Android 验证可能需要 20 秒或更长时间才能生效,因此请务必等到验证完成。

    ¥Android verification may take 20 seconds or longer to take effect, so be sure to wait until it is completed.

  • 如果你更新了 Web 文件,请重建原生应用以在供应商端(Google)触发服务器更新

    ¥If you update your web files, rebuild the native app to trigger a server update on the vendor side (Google)