我正在开发一个 Android 应用程序,最近删除了 OneSignal 库。更改后,我遇到以下错误:
java.lang.IllegalArgumentException: com.app.myapp: 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.getBroadcastAsUser(PendingIntent.java:645)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:285)
at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:158)
at androidx.work.impl.utils.ForceStopRunnable.forceStopRunnable(ForceStopRunnable.java:185)
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:103)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
为了解决这个问题,我修改了代码以使用:
PendingIntent.getBroadcast(context, 123, alarmIntent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE
else PendingIntent.FLAG_UPDATE_CURRENT);
另外,我添加了依赖:
implementation 'androidx.work:work-runtime-ktx:2.9.1'
尽管进行了这些更改,问题仍然存在。我还能做什么来解决此错误?
我通过在 build.gradle 文件中使用以下配置找到了该问题的解决方案:
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.group == 'androidx.work') {
details.useVersion('2.9.1')
}
}
}
在所有配置中将 androidx.work 的版本设置为 2.9.1