androidtrusted-web-activitydigital-assets-links

Trusted Web Activity: Not able to remove URL header


I am exploring the Trusted Web Activity concepts and trying to launch it like this -

final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
final CustomTabsIntent customTabsIntent = builder.build();    
customTabsIntent.intent.putExtra(TrustedWebUtils.EXTRA_LAUNCH_AS_TRUSTED_WEB_ACTIVITY, true);
customTabsIntent.launchUrl(activity, Uri.parse(wrappedTargetUrl));

I used google's asset link generator here and tested it for the domain. It shows successful linking. Also, I am using the correct app signing key from Playstore. Still, it is not hiding the URL bar for me. I have looked and tried all the available options on StackOverflow and still no luck.


Solution

  • To remove the Url bar of a Trusted Web Activity it requires an association between the Android application and the website. The association must be established in both ways, linking from the Application to the Website and from the Website to the Application.

    Establish an association from Application to the Website

    1.Add asset_statements String app>res>values>strings.xml like below and change the contents for the site attribute to match the schema and domain opened by the Trusted Web Activity:

    <resources>
        <string name="asset_statements">
            [{
                \"relation\": [\"delegate_permission/common.handle_all_urls\"],
                \"target\": {
                    \"namespace\": \"web\",
                    \"site\": \"https://my.site.com\"}
            }]
        </string>
    </resources>
    

    2.Add the above asset_statements into your AndroidManifest.xml as meta-data under application tag like below and set the correct data scheme and host in your Launcher Activity:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.my.application">
    
        <application>
    
            <meta-data
                android:name="asset_statements"
                android:resource="@string/asset_statements" />
    
            <activity android:name=".LauncherActivity">
               <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 the app
               -->
              <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"/>
    
                <!-- To handle links to the target URL-->
                <data
                    android:scheme="https"
                    android:host="my.site.com"/>
              </intent-filter>
           </activity>
    
        </application>
    </manifest>
    

    Establish an association from Website to the Application

    You have to upload an assetlinks.json file into your website under the path: https://my.site.com/.well-known/assetlinks.json where assetlinks.json should look like this:

    [{
      "relation": ["delegate_permission/common.handle_all_urls"],
      "target": {
        "namespace": "android_app",
        "package_name": "com.example.my.application",
        "sha256_cert_fingerprints":
        ["5B:49:F7:CB:BL:C8:6K:CJ:5F:D8:33:2A:DA:FA:71:18:88:N9:49:9C:D3:66:98:35:B4:A2:2C:EC:B9:65:6C:65"]
      }
    }]
    

    From the above file you have to set the correct Package Name and the correct SHA-256 Fingerprint (Keystore signature Hash).

    There is an easy way to generate the above assetlinks.json file and the above hash from Android Studio. You have to go to Android Studio Tools > App Links Assistant and click the Open Digital Asset Links File Generator and add your site domain, the application package name and select your release keystore file like below and click Generate Digital Asset Links file Button to generate the assetlinks.json which is ready to be uploaded to the site. There is also a very helpful link which describes the association Associate your app with your website

    digital_asset_link

    To verify that the link is correct there is a Link and Verify Button to verify that the association from Website to Application is correct like in the below image sample:

    link verification

    After the above steps if the Sha256 which was uploaded to the site was for Release keystore you have to generate a release apk file which now if opened you should see that the Header Url Bar was removed successfully. I have tested the above steps and it works as expected.

    Note: In case you have opted into having Google Play sign your releases with a key they generated, you simply need to copy the App signing key certificate SHA-256 fingerprint under the Google Play Console -> Setup -> App Integrity and put it in the assetlinks.json file.