TWA(Trusted Web Activity) で BuildTypes によって表示するページを変更する


環境

macOS High Sierra - 10.13.6
Android Studio - 3.4

変更する方法

build.gradle に TWA の設定を debug、release それぞれ記述します

build.gradle
android {
    ...
    buildTypes {
        debug {
            ...
            manifestPlaceholders = [
                    hostName       : "*****",
                    defaultUrl     : "debug用のURL",
                    assetStatements: '[{ "relation": ["delegate_permission/common.handle_all_urls"], ' +
                            '"target": {"namespace": "web", "site": "debug用のURL"}}]'
            ]
        }
        release {
            ...
            manifestPlaceholders = [
                    hostName       : "*****",
                    defaultUrl     : "release用のURL",
                    assetStatements: '[{ "relation": ["delegate_permission/common.handle_all_urls"], ' +
                            '"target": {"namespace": "web", "site": "release用のURL"}}]'
            ]
        }
    }
}

AndroidManifest.xml の android.support.customtabs.trusted.DEFAULT_URL を build.gradle から取得するようにします

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

    ...

    <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>

debug、release を切り替えて実行してみて、表示されるページが変わっていたら対応完了です!