升级到 Android 12 (SDK 31) 后无法 运行 可信网络 Activity

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

最近我们正在准备Android12,我们更新了我们的compileSdkVersion和targetSdkVersion到31 更新到31后,TWA无法启动。给出的错误是

    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)

更新后 androidx.browser:浏览器 implementation 'androidx.browser:browser:1.4.0-beta01' 我得到以下错误

    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$TwaLauncher(TwaLauncher.java:257)
        at com.google.androidbrowserhelper.trusted.-$$Lambda$TwaLauncherzo6viK_vVSs4bt2THXs48ruews.run(Unknown Source:6)
        at com.google.androidbrowserhelper.trusted.splashscreens.PwaWrapperSplashScreenStrategy.lambda$onSplashImageTransferred$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[=14=]$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.onPostExecute(SplashImageTransferTask.java:144)
        at com.google.androidbrowserhelper.trusted.splashscreens.SplashImageTransferTask.onPostExecute(SplashImageTransferTask.java:91)
        at android.os.AsyncTask.finish(AsyncTask.java:771)
        at android.os.AsyncTask.access0(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)

我试图找到 com.google.androidbrowserhelper 的更新版本,但找不到。我目前正在使用 implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.2.2'

有没有人遇到过这个并且能够提供帮助?

这是一个已知问题,需要对基础 androidx.browser 库进行更改。 1.4.0-rc1, which should go stable in the next weeks. Another change 中的更改在 android-browser-helper 中是必需的,一旦对 androidx.browser 的更改结束,我们将发布一个适用于 targetSdk 31 的更新。

您可以使用 android.browser 的候选发布版本并构建您自己的 android-browser-helper,但我建议等待生产版本升级到目标 SDK 31。