集成指南

设置可信 Web activity 不需要开发者编写 Java 代码,但需要使用 Android Studio。本指南使用 Android Studio 3.3 创建。请参阅有关如何安装该插件的文档

创建 Trusted Web Activity 项目

使用可信 Web 活动时,项目必须以 API 16 或更高级别为目标平台。

打开 Android Studio,然后点击 Start a new Android Studio project

Android Studio 会提示您选择 activity 类型。由于可信 Web 活动使用支持库提供的 activity,因此请选择 Add No Activity,然后点击 Next

下一步,向导会提示您为项目配置。以下是对每个字段的简要说明:

  • 名称Android 启动器中将用于应用的名称。
  • 软件包名称:Play 商店和 Android 设备上 Android 应用的唯一标识符。如需详细了解为 Android 应用创建软件包名称的要求和最佳实践,请参阅文档
  • 保存位置:Android Studio 将在文件系统中创建项目的位置。
  • 语言:此项目无需编写任何 Java 或 Kotlin 代码。选择 Java 作为默认选项。
  • 最低 API 级别:支持库至少需要 API 级别 16。选择 API 16 或更高版本。

由于我们不会使用 Instant Apps 或 AndroidX 工件,因此请勿选中其余复选框,然后点击完成

获取 Trusted Web Activity 支持库

如需在项目中设置可信 Web 活动库,您需要修改应用 build 文件。在 Project Navigator 中,查找 Gradle Scripts 部分。 有两个名为 build.gradle 的文件,这可能会让人感到困惑,括号中的说明有助于识别正确的文件。

我们要找的文件是名称旁边带有模块 Module 的文件。

Trusted Web Activities 库使用 Java 8 功能,第一个更改会启用 Java 8。将 compileOptions 部分添加到 android 部分底部,如下所示:

android {
        ...
    compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
    }
}

下一步是将 Trusted Web Activity 支持库添加到项目中。将新依赖项添加到 dependencies 部分:

dependencies {
    implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.2.0'
}

Android Studio 会显示提示,要求您再次同步项目。点击 Sync Now(立即同步)链接,然后进行同步。

启动 Trusted Web Activity

如需设置 Trusted Web Activity,请修改 Android 应用清单

Project Navigator 中,展开 app 部分,然后展开 manifests,并双击 AndroidManifest.xml 以打开该文件。

由于我们在创建项目时要求 Android Studio 不要向项目添加任何 activity,因此清单是空的,只包含 application 标记。

activity 标记插入 application 标记中,以添加可信 Web 活动:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.twa.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity
            android:name="com.google.androidbrowserhelper.trusted.LauncherActivity">

           <!-- Edit android:value to change the url opened by the Trusted Web Activity -->
           <meta-data
               android:name="android.support.customtabs.trusted.DEFAULT_URL"
               android:value="https://airhorner.com" />

           <!-- This intent-filter adds the Trusted Web Activity to the Android Launcher -->
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>

           <!--
             This intent-filter allows the Trusted Web Activity to handle Intents to open
             airhorner.com.
           -->
           <intent-filter>
               <action android:name="android.intent.action.VIEW"/>
               <category android:name="android.intent.category.DEFAULT" />
               <category android:name="android.intent.category.BROWSABLE"/>

               <!-- Edit android:host to handle links to the target URL-->
               <data
                 android:scheme="https"
                 android:host="airhorner.com"/>
           </intent-filter>
        </activity>
    </application>
</manifest>

添加到 XML 中的标记是标准的 Android 应用清单。与 Trusted Web Activity 上下文相关的信息有两项:

  1. meta-data 标记会告知 Trusted Web Activity 应打开哪个网址。将 android:value 属性更改为要打开的 PWA 的网址。在此示例中,该网址为 https://airhorner.com
  2. 通过第二个 intent-filter 标记,Trusted Web Activity 可以拦截用于打开 https://airhorner.com 的 Android intent。data 标记内的 android:host 属性必须指向 Trusted Web Activity 打开的网域。

下一部分将介绍如何设置 Digital AssetLinks 以验证网站与应用之间的关系,以及如何移除网址栏。

移除网址栏

可信 Web activity 需要在 Android 应用和网站之间建立关联,才能移除网址栏。

此关联是通过 Digital Asset Links 创建的,并且必须通过两种方式建立关联,即从应用到网站从网站到应用

您可以将应用设置为网站验证,并将 Chrome 设置为跳过网站到应用验证,以便进行调试。

打开字符串资源文件 app > res > values > strings.xml,然后在下方添加 Digital AssetLinks 语句:

<resources>
    <string name="app_name">AirHorner Trusted Web Activity</string>
    <string name="asset_statements">
        [{
            \"relation\": [\"delegate_permission/common.handle_all_urls\"],
            \"target\": {
                \"namespace\": \"web\",
                \"site\": \"https://airhorner.com\"}
        }]
    </string>
</resources>

更改 site 属性的内容,使其与 Trusted Web Activity 打开的架构和网域相匹配。

返回 Android 应用清单文件 AndroidManifest.xml,通过添加新的 meta-data 标记(这次作为