Setting up a Trusted Web Activity doesn't require developers to author Java code, but Android Studio is required. This guide was created using Android Studio 3.3. Check the docs on how to install it.
Create a Trusted Web Activity Project
When using Trusted Web Activities, the project must target API 16 or higher.
Open Android Studio and click on Start a new Android Studio project.
Android Studio will prompt to choose an Activity type. Since Trusted Web Activities use an Activity provided by support library, choose Add No Activity and click Next.
Next step, the wizard will prompt for configurations for the project. Here's a short description of each field:
- Name: The name that will be used for your application on the Android Launcher.
- Package Name: An unique identifier for Android Applications on the Play Store and on Android devices. Check the documentation for more information on requirements and best practices for creating package names for Android apps.
- Save location: Where Android Studio will create the project in the file system.
- Language: The project doesn't require writing any Java or Kotlin code. Select Java, as the default.
- Minimum API Level: The Support Library requires at least API Level 16. Select API 16 any version above.
Leave the remaining checkboxes unchecked, as we will not be using Instant Apps or AndroidX artifacts, and click Finish.
Get the Trusted Web Activity Support Library
To setup the Trusted Web Activity library in the project you will need to edit the Application
build file. Look for the Gradle Scripts section in the Project Navigator.
There are two files called build.gradle, which may be a bit confusing and the
descriptions in parenthesis help identifying the correct one.
The file we are are looking for is the one with module Module next to its name.
The Trusted Web Activities library uses
Java 8 features
and the first change enables Java 8. Add a compileOptions section to the
bottom of the android section, as below:
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
The next step will add the Trusted Web Activity Support Library to the project. Add a new
dependency to the dependencies section:
dependencies {
implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.2.0'
}
Android Studio will show prompt asking to synchronize the project once more. Click on the Sync Now link and synchronize it.
Launch the Trusted Web Activity
Setting up the Trusted Web Activity is achieved by editing the Android App Manifest.
On the Project Navigator, expand the app section, followed by the
manifests and double click on AndroidManifest.xml to open the file.
Since we asked Android Studio not to add any Activity to our project when creating it, the manifest is empty and contains only the application tag.
Add the Trusted Web Activity by inserting an activity tag into the application tag:
<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>
The tags added to the XML are standard Android App Manifest. There are two relevant pieces of information for the context of Trusted Web Activities:
- The
meta-datatag tells the Trusted Web Activity which URL it should open. Change theandroid:valueattribute with the URL of the PWA you want to open. In this example, it ishttps://airhorner.com. - The second
intent-filtertag allows the Trusted Web Activity to intercept Android Intents that openhttps://airhorner.com. The