升级Android SDK至33后PWA应用崩溃:PendingIntent标志缺失
Android SDK 33下PWA应用PendingIntent崩溃解决方案
错误详情
升级到Android SDK 33后应用崩溃,抛出如下异常:
FATAL EXCEPTION: main Process: xyz.appmaker.iiuiro, PID: 2838 java.lang.IllegalArgumentException: xyz.appmaker.iiuiro: 面向S+(版本31及以上)时,创建PendingIntent必须指定FLAG_IMMUTABLE或FLAG_MUTABLE其中一个标志。强烈建议使用FLAG_IMMUTABLE,仅当某些功能依赖PendingIntent可变时才使用FLAG_MUTABLE,例如需要用于内联回复或气泡功能时。 at android.app.PendingIntent.checkFlags(PendingIntent.java:401) at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:484) at android.app.PendingIntent.getActivity(PendingIntent.java:470) at android.app.PendingIntent.getActivity(PendingIntent.java:434) at androidx.browser.customtabs.CustomTabsClient.createSessionId(CustomTabsClient.java:185) at androidx.browser.customtabs.CustomTabsClient.newSession(CustomTabsClient.java:223) at com.google.androidbrowserhelper.trusted.TwaLauncher$TwaCustomTabsServiceConnection.onCustomTabsServiceConnected(TwaLauncher.java:301) at androidx.browser.customtabs.CustomTabsServiceConnection.onServiceConnected(CustomTabsServiceConnection.java:57) at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:2188) at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:2221) at android.os.Handler.handleCallback(Handler.java:942) 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:7872) 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:936)
问题原因
从栈跟踪可见,崩溃源于androidx.browser.customtabs.CustomTabsClient创建SessionId时未指定PendingIntent的标志。Android 12(API 31)及以上版本强制要求创建PendingIntent必须设置FLAG_IMMUTABLE或FLAG_MUTABLE,而当前使用的旧版本androidx.browser和androidbrowserhelper库未适配该规则。
解决步骤
直接升级相关依赖库到适配Android 12+的版本即可:
- 将
androidx.browser:browser升级至1.4.0或更高版本 - 将
com.google.androidbrowserhelper:androidbrowserhelper升级至2.2.0或更高版本
在Module级别的build.gradle中更新依赖:
dependencies { implementation 'androidx.browser:browser:1.6.0' implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.5.0' }
验证
更新依赖后重新编译安装应用,在Android 12+设备上测试,崩溃问题即可解决。
附原应用AndroidManifest.xml代码:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/appName" android:supportsRtl="true" android:theme="@style/Theme.LauncherActivity"> <meta-data android:name="asset_statements" android:resource="@string/assetStatements" /> <meta-data android:name="twa_generator" android:value="@string/generatorApp" /> <activity android:exported="true" android:name="com.google.androidbrowserhelper.trusted.LauncherActivity" android:label="@string/launcherName"> <meta-data android:name="android.support.customtabs.trusted.DEFAULT_URL" android:value="@string/launchUrl" /> <meta-data android:name="android.support.customtabs.trusted.STATUS_BAR_COLOR" android:resource="@color/colorPrimary" /> <meta-data android:name="android.support.customtabs.trusted.NAVIGATION_BAR_COLOR" android:resource="@color/navigationColor" /> <meta-data android:name="android.support.customtabs.trusted.SPLASH_IMAGE_DRAWABLE" android:resource="@drawable/splash"/> <meta-data android:name="android.support.customtabs.trusted.SPLASH_SCREEN_BACKGROUND_COLOR" android:resource="@color/backgroundColor"/> <meta-data android:name="android.support.customtabs.trusted.SPLASH_SCREEN_FADE_OUT_DURATION" android:value="@integer/splashScreenFadeOutDuration"/> <meta-data android:name="android.support.customtabs.trusted.FILE_PROVIDER_AUTHORITY" android:value="@string/providerAuthority"/> <meta-data android:name="android.app.shortcuts" android:resource="@xml/shortcuts" /> <meta-data android:name="android.support.customtabs.trusted.FALLBACK_STRATEGY" android:value="@string/fallbackType" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <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"/> <data android:scheme="https" android:host="@string/hostName"/> </intent-filter> </activity> <activity android:name="com.google.androidbrowserhelper.trusted.FocusActivity" /> <activity android:name="com.google.androidbrowserhelper.trusted.WebViewFallbackActivity" android:configChanges="orientation|screenSize" /> <provider android:name="androidx.core.content.FileProvider" android:authorities="@string/providerAuthority" android:grantUriPermissions="true" android:exported="false"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider> <service android:name="com.google.androidbrowserhelper.trusted.DelegationService" android:enabled="@bool/enableNotification" android:exported="@bool/enableNotification"> <intent-filter> <action android:name="android.support.customtabs.trusted.TRUSTED_WEB_ACTIVITY_SERVICE"/> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </service> </application>
内容的提问来源于stack exchange,提问作者Sam
相关产品推荐
相关产品推荐

