Target SDK升级至31后出现PendingIntent报错(未直接使用该API)
将应用Target SDK版本从30升级到31后,出现如下错误:
Targeting S+(版本31及以上)要求创建PendingIntent时必须指定FLAG_IMMUTABLE或FLAG_MUTABLE中的一个。
强烈建议使用FLAG_IMMUTABLE,仅当某些功能依赖可变的PendingIntent时才使用FLAG_MUTABLE,例如需要配合内联回复或气泡功能使用的场景。
错误堆栈信息:
at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:273)
at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:151)
at androidx.work.impl.utils.ForceStopRunnable.forceStopRunnable(ForceStopRunnable.java:171)
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:102)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
应用并未直接使用PendingIntent或任何Broadcast Receiver,项目的BUILD.GRADLE配置如下:
BUILD.GRADLE / MODULE
buildscript { ext.kotlin_version = '1.4.32' repositories { google() mavenCentral() gradlePluginPortal() } dependencies { classpath 'com.android.tools.build:gradle:7.0.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "com.google.dagger:hilt-android-gradle-plugin:2.28-alpha" classpath 'com.google.gms:google-services:4.3.15' classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.1' def nav_version = "2.3.0" classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version" classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:0.12.10' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() mavenCentral() jcenter() maven { url 'https://jitpack.io' } } } task clean(type: Delete) { delete rootProject.buildDir }
BUILD.GRADLE / APP
packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/license.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/notice.txt' exclude 'META-INF/ASL2.0' exclude("META-INF/*.kotlin_module") } compileOptions { coreLibraryDesugaringEnabled true sourceCompatibility "11" targetCompatibility JavaVersion.VERSION_11 } kotlinOptions { jvmTarget = "11" } buildFeatures { dataBinding true viewBinding true } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.6.0' implementation 'androidx.appcompat:appcompat:1.3.1' implementation "androidx.recyclerview:recyclerview:1.2.1" implementation 'androidx.constraintlayout:constraintlayout:2.1.0' testImplementation 'junit:junit:4.13.2' //Material Design implementation 'com.google.android.material:material:1.4.0' //Coroutines implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3' implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' //Navigation def nav_version = "2.3.5" implementation "androidx.navigation:navigation-fragment-ktx:$nav_version" implementation "androidx.navigation:navigation-ui-ktx:$nav_version" def lifecycle_version = "2.3.1" // ViewModel implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" // LiveData implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" // Dagger dependencies def dagger_version = "2.27" implementation "com.google.dagger:dagger:$dagger_version" kapt "com.google.dagger:dagger-compiler:$dagger_version" // Hilt dependencies def hilt_version = '2.28-alpha' implementation "com.google.dagger:hilt-android:$hilt_version" kapt "com.google.dagger:hilt-android-compiler:$hilt_version" // Hilt ViewModel extension def hilt_jetpack_version = "1.0.0-alpha02" implementation "androidx.hilt:hilt-lifecycle-viewmodel:$hilt_jetpack_version" kapt "androidx.hilt:hilt-compiler:$hilt_jetpack_version" //Retrofit implementation("com.squareup.okhttp3:okhttp:4.9.0") implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation("com.squareup.okhttp3:logging-interceptor:4.9.0") implementation 'com.squareup.retrofit2:adapter-rxjava2:2.7.1' //java8 coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5' //Qr Code implementation 'com.budiyev.android:code-scanner:2.1.0' //Stetho implementation 'com.facebook.stetho:stetho:1.5.1' implementation 'com.facebook.stetho:stetho-urlconnection:1.5.1' implementation 'com.facebook.stetho:stetho-okhttp3:1.5.1' //Glide api 'com.github.bumptech.glide:glide:4.11.0' kapt 'com.github.bumptech.glide:compiler:4.11.0' //Firebase implementation platform('com.google.firebase:firebase-bom:26.1.1') implementation 'com.google.firebase:firebase-analytics-ktx' implementation 'com.google.firebase:firebase-crashlytics-ktx' //Swipe Delete implementation 'com.ernestoyaquello.dragdropswiperecyclerview:drag-drop-swipe-recyclerview:0.6.0' implementation 'com.github.emreerkahraman:MPAndroidChart:v4.0.1' implementation 'com.github.mkw8263:MindevPDFViewer:1.0.4' implementation 'com.otaliastudios:zoomlayout:1.7.0' //EasyImage implementation 'com.github.jkwiecien:EasyImage:3.0.3' //Image Crop UI implementation 'com.github.yalantis:ucrop:2.2.6-native' //Joda Time implementation 'joda-time:joda-time:2.10.10' //PhotoView Zoom implementation 'com.github.chrisbanes:PhotoView:2.1.4' //PinCodeView implementation 'com.github.antoxa2584x:PinCodeView:1.3.0' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation 'com.onesignal:OneSignal:4.8.6' implementation 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.1' implementation 'com.andrognito.flashbar:flashbar:1.0.3' implementation 'com.irozon.sneaker:sneaker:2.0.0' //Sentry implementation 'io.sentry:sentry-android:6.21.0' //Kotlin Reflect implementation "org.jetbrains.kotlin:kotlin-reflect:1.3.31" //Skeleton Effect implementation 'com.faltenreich:skeletonlayout:4.0.0' implementation 'com.vmadalin:easypermissions-ktx:1.0.0' implementation 'androidx.work:work-runtime-ktx:2.7.1' } android { packagingOptions { pickFirst "META-INF/DEPENDENCIES" pickFirst "META-INF/library_release.kotlin_module" //exclude "META-INF/DEPENDENCIES" }
已尝试升级库、Kotlin版本等操作,但会引发其他问题,目前使用implementation 'androidx.work:work-runtime-ktx:2.7.1',问题仍未解决。
从错误堆栈可以看出,问题出在androidx.work库的ForceStopRunnable类中,该类创建PendingIntent时未指定FLAG_IMMUTABLE或FLAG_MUTABLE。针对这个问题,有以下几种解决方式:
1. 升级WorkManager到更高稳定版本
WorkManager在2.7.0及以上版本已修复Target SDK 31的兼容问题,但部分小版本可能存在遗漏。尝试升级到更高的稳定版本,比如2.8.1:
implementation 'androidx.work:work-runtime-ktx:2.8.1'
升级后同步Gradle并重新编译项目。
2. 强制指定PendingIntent标志位(临时方案)
如果升级WorkManager引发依赖冲突,可通过反射修改ForceStopRunnable内部的PendingIntent创建逻辑,添加FLAG_IMMUTABLE:
import android.app.PendingIntent import android.content.Context import androidx.work.WorkManager import androidx.work.impl.utils.ForceStopRunnable import java.lang.reflect.Method fun initWorkManager(context: Context) { WorkManager.initialize(context, androidx.work.Configuration.Builder().build()) try { // 反射获取创建PendingIntent的方法 val method = ForceStopRunnable::class.java.getDeclaredMethod("getPendingIntent", Context::class.java) method.isAccessible = true // 调用方法并修改返回的PendingIntent,添加FLAG_IMMUTABLE val originalIntent = method.invoke(null, context) as PendingIntent val updatedIntent = PendingIntent.getBroadcast( context, originalIntent.requestCode, originalIntent.intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT ) // 将修改后的PendingIntent替换回原类(需额外反射字段,此处简化示例) } catch (e: Exception) { e.printStackTrace() } }
注意:反射方式存在兼容性风险,后续库版本更新可能失效,仅作为临时过渡方案。
3. 检查并升级其他第三方依赖
除WorkManager外,部分第三方库(如OneSignal、Firebase)也可能间接创建未指定标志位的PendingIntent:
- OneSignal:当前使用4.8.6,尝试升级到兼容Target SDK 31的最新稳定版
- Firebase:当前使用BOM 26.1.1,升级到更高版本的BOM,比如
29.0.0:
implementation platform('com.google.firebase:firebase-bom:29.0.0')
内容的提问来源于stack exchange,提问作者Kaan Kaplan

