trusted-web-activityandroid-12

Unable to run Trusted Web Activity after upgrading to Android 12 (SDK 31)


Recently we are preparing for Android 12 and we updated our compileSdkVersion and targetSdkVersion to 31 After updating to 31, TWA is unable to be launched. Error given is

    java.lang.IllegalArgumentException: xxx.twa: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
        at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458)
        at android.app.PendingIntent.getActivity(PendingIntent.java:444)
        at android.app.PendingIntent.getActivity(PendingIntent.java:408)
        at androidx.browser.customtabs.CustomTabsClient.createSessionId(CustomTabsClient.java:231)
        at androidx.browser.customtabs.CustomTabsClient.newSession(CustomTabsClient.java:269)
        at com.google.androidbrowserhelper.trusted.TwaLauncher$TwaCustomTabsServiceConnection.onCustomTabsServiceConnected(TwaLauncher.java:324)
        at androidx.browser.customtabs.CustomTabsServiceConnection.onServiceConnected(CustomTabsServiceConnection.java:57)
        at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:2077)
        at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:2110)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

After updating androidx.browser:browser implementation 'androidx.browser:browser:1.4.0-beta01' i got the below error

    java.lang.IllegalArgumentException: xxx.twa: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
        at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:458)
        at android.app.PendingIntent.getActivity(PendingIntent.java:444)
        at android.app.PendingIntent.getActivity(PendingIntent.java:408)
        at com.google.androidbrowserhelper.trusted.FocusActivity.addToIntent(FocusActivity.java:43)
        at com.google.androidbrowserhelper.trusted.TwaLauncher.launchWhenSplashScreenReady(TwaLauncher.java:272)
        at com.google.androidbrowserhelper.trusted.TwaLauncher.lambda$launchWhenSessionEstablished$4$TwaLauncher(TwaLauncher.java:257)
        at com.google.androidbrowserhelper.trusted.-$$Lambda$TwaLauncher$6zo6viK_vVSs4bt2THXs48ruews.run(Unknown Source:6)
        at com.google.androidbrowserhelper.trusted.splashscreens.PwaWrapperSplashScreenStrategy.lambda$onSplashImageTransferred$1$PwaWrapperSplashScreenStrategy(PwaWrapperSplashScreenStrategy.java:208)
        at com.google.androidbrowserhelper.trusted.splashscreens.-$$Lambda$PwaWrapperSplashScreenStrategy$p-f_rUE4TsZEGw17K3MBDrUxlsQ.run(Unknown Source:4)
        at com.google.androidbrowserhelper.trusted.splashscreens.PwaWrapperSplashScreenStrategy.runWhenEnterAnimationComplete(PwaWrapperSplashScreenStrategy.java:215)
        at com.google.androidbrowserhelper.trusted.splashscreens.PwaWrapperSplashScreenStrategy.onSplashImageTransferred(PwaWrapperSplashScreenStrategy.java:207)
        at com.google.androidbrowserhelper.trusted.splashscreens.PwaWrapperSplashScreenStrategy.lambda$configureTwaBuilder$0$PwaWrapperSplashScreenStrategy(PwaWrapperSplashScreenStrategy.java:195)
        at com.google.androidbrowserhelper.trusted.splashscreens.-$$Lambda$PwaWrapperSplashScreenStrategy$AEuRt7oVnGOP2vf-Uvl2RaqkFW4.onFinished(Unknown Source:6)
        at com.google.androidbrowserhelper.trusted.splashscreens.SplashImageTransferTask$1.onPostExecute(SplashImageTransferTask.java:144)
        at com.google.androidbrowserhelper.trusted.splashscreens.SplashImageTransferTask$1.onPostExecute(SplashImageTransferTask.java:91)
        at android.os.AsyncTask.finish(AsyncTask.java:771)
        at android.os.AsyncTask.access$900(AsyncTask.java:199)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:788)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7842)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

I tried to find an updated version of com.google.androidbrowserhelper but is unable to find any. I am currently using implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.2.2'

Have anyone encounter this and is able to help?


Solution

  • This is a known issue and needs changes on the underlying androidx.browser library. The changes are in 1.4.0-rc1, which should go stable in the next weeks. Another change was required in android-browser-helper and once the change to androidx.browser is out, we'll release an update that will work well with targetSdk 31.

    You could use the release-candidate version of android.browser and build your own android-browser-helper, but I'd recommend waiting for the production versions to upgrade to target SDK 31.