How can I get my Android app to always open in Chrome or another browser that supports TWA?

1.3k Views Asked by At

I have a very basic Android app generated from https://appmaker.xyz/pwa-to-apk/. That app was actually modeled very closely off an example published by Google which I cannot find anymore.

The problem is that if you set the default browser on the device to one that does not support TWA, the app opens but shows the URL bar. If you want all the technical fun, here's a bug report that explains everything: https://bugs.chromium.org/p/chromium/issues/detail?id=942930

My Android development skills are limited to compiling the app in Android Studio and I have no clue what modification I can make to force my app to prefer a browser that supports TWA. Is there some modification I can make to that will do this?

Here's my AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.placeholder">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="${launcherName}"
        android:supportsRtl="true"
        android:theme="@style/Theme.TwaSplash">

        <meta-data
            android:name="asset_statements"
            android:value="${assetStatements}" />

        <activity android:name="android.support.customtabs.trusted.LauncherActivity"
            android:label="${launcherName}">
            <meta-data android:name="android.support.customtabs.trusted.DEFAULT_URL"
                android:value="${defaultUrl}" />

            <meta-data
                android:name="android.support.customtabs.trusted.STATUS_BAR_COLOR"
                android:resource="@color/colorPrimary" />

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <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="https"
                    android:host="${hostName}"/>
            </intent-filter>
        </activity>
    </application>
</manifest>
1

There are 1 best solutions below

0
On

Browser support for Trusted Web Activity is improving - Chrome, Edge, and a couple of others support it, with Firefox coming soon (Firefox Nightly already supports it).

The example was svgomg-twa, which is now deprecated.

The best way to generate a an app that uses Trusted Web Activity is using something like Bubblewrap - It's a Node.js CLI application.

It defaults to the Custom Tabs fallback behaviour (the URL bar) but has the option to enable a WebView fallback.

  1. Initialize the project with bubblewrap init --manifest=https://example.com/manifest.json
  2. This will generate the Android project and a filed called twa-manifest.json. Edit this file and change the fallbackType field from customtabs to webview.
  3. Update the project with bubblewrap update
  4. Lastly, build the APK with bubblewrap build

Alternative approach:

PWABuilder uses Bubblewrap as a library and can be used for the same goal:

  1. Navigate to https://www.pwabuilder.com/
  2. Enter the add address to the PWA on the input
  3. Click on Build my PWA
  4. Click on the arrow pointing down on the Android card
  5. Click on Options
  6. Change the Fallback type radio from "Custom Tabs" to "Web View"
  7. Click Done
  8. Click on Download