目标 S + (版本10000及以上)要求在创建 PendingInant 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

正在尝试将我的应用程序更新到 Android S,遇到了一些题目/错误所说的问题。我知道错了

以 S + (版本10000及以上)为目标,须符合下列条件之一: 时指定 FLAG _ IMMUTABLE 或 FLAG _ MUTABLE 未决意图。 强烈考虑使用 FLAG _ IMMUTABLE,如果某些功能依赖于 PendingInent 是可变的,则仅使用 FLAG _ MUTABLE,例如,如果 它需要与内联答复或气泡一起使用。

在我的通知代码中,我只有1个挂起意图,并且我添加了标志

PendingIntent.getActivity(
mContext,
0 /* Request code */,
intentOptional.get(),
PendingIntent.FLAG_IMMUTABLE
)

阅读谷歌的文档这应该是所有我需要的安全更新在 Android S。我在这里发现一个几个月前的帖子,要求类似的东西,有人说,添加工作管理器 https://stackoverflow.com/a/67181567/4219444到项目,即使你不使用它。所以我加了一句

def work_version = "2.7.0-alpha04"
implementation "androidx.work:work-runtime-ktx:$work_version"

这没有任何帮助,因为我仍然收到错误。有没有人知道这是 Android S 升级的常见问题,还是也会检查库?因为应用程序一直崩溃,不知道该怎么办。

我已经创建了一个应用程序,没有使用任何库,并使用了相同的 PendingInent,并且能够运行一个基本的 hello world 应用程序。我从试图编译的项目中得到的完整错误是:

目标 S + (版本10000及以上)要求在创建 PendingInant 时指定 FLAG _ IMMUTABLE 或 FLAG _ MUTABLE 之一。 强烈考虑使用 FLAG _ IMMUTABLE,只有当某些功能依赖于 PendingInent 是可变的时候才使用 FLAG _ MUTABLE,例如,如果它需要与内联应答或气泡一起使用。 在 android.app. PendingInent.checkFlags (PendingInent.java: 375) 在 android.app. PendingInent.getBroadcastAsUser (PendingInent.java: 645) 在 android.app. PendingInent.getBroadcast (PendingInent.java: 632) 在 com.google.android.gms.interal.gtm.zzbv.zzfe (未知来源: 52) 取消(未知来源: 54) 在 com.google.android.gms.interal.gtm.zzbv.zzaw (未知来源: 4) 在 com.google.android.gms.interal.gtm.zzan.zzag (未知来源: 7) 网址: com.google.android.gms.interal.gtm.zzap (未知来源: 67) 在 com.google.android.gms.interal.gtm.zzap.zzc (未知来源: 82) 在 com.google.android.gms.analytics. GoogleAnalytics.getInstance (未知来源: 15) 应用程序模块: 339) ApplicationModule _ ProvidesGoogleAnalyticsLogger $app _ developmentDebugFactory. provision esGoogleAnalyticsLogger $app _ developmentDebug (ApplicationModule _ ProvidesGoogleAnalyticsLogger $app _ developmentDebugFactory. java: 47) ApplicationModule _ ProvidesGoogleAnalyticsLogger $app _ developmentDebugFactory. get (ApplicationModule _ ProvidesGoogleAnalyticsLogger $app _ developmentDebugFactory. java: 36) ApplicationModule _ ProvidesGoogleAnalyticsLogger $app _ developmentDebugFactory. get (ApplicationModule _ ProvidesGoogleAnalyticsLogger $app _ developmentDebugFactory. java: 11) 在 dagger.Internal. DoubleCheck.get (DoubleCheck.java: 47) ApplicationModule _ ProvidesMultiAnalyticsLogger $app _ developmentDebugFactory. get (ApplicationModule _ ProvidesMultiAnalyticsLogger $app _ developmentDebugFactory. java: 35) ApplicationModule _ ProvidesMultiAnalyticsLogger $app _ developmentDebugFactory. get (ApplicationModule _ ProvidesMultiAnalyticsLogger $app _ developmentDebugFactory. java: 10) 在 dagger.Internal. DoubleCheck.get (DoubleCheck.java: 47) 应用程序(DaggerIProdApplicationComponent. java: 941) 2021-07-0211:18:17.61122561-22561/com.chrobinson.navispherecarrier.dev E/AndroidRuntime: at di.interal.Component 在 com.chrobinson.navispherecarrier.ChApplication.onCreate (ChApplication.kt: 90) 在 android.app. Instrumentation.callApplicationOnCreate (Instrumentation.java: 1211) ActivityThread.handleBindApplication (ActivityThread.java: 6682)

52393 次浏览

So to solve this issue with the Android S update, I was able to spend some time and removing Google Analytics completely and replacing it with Firebase Analytics and Crashlytics.

I am not fully sure if this is because Google hasn't/wont update Google Analytics for Android S or what.

It is solved by adding

implementation 'androidx.work:work-runtime:2.7.0-alpha05'

in my gradle. (You can add the latest release version of this library)

Seems like it is a bug in workmanager. Check the bug fix in Version 2.7.0-alpha02

https://developer.android.com/jetpack/androidx/releases/work#2.7.0-alpha02

According to documentation, "It is strongly recommended to use FLAG_IMMUTABLE when creating a PendingIntent. FLAG_MUTABLE should only be used when some functionality relies on modifying the underlying intent, e.g. any PendingIntent that needs to be used with inline reply or bubbles."

So use this PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT

Instead of just PendingIntent.FLAG_UPDATE_CURRENT

e.g PendingIntent.getActivity(context, yourRequestID, yourIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)

Because "FLAG_UPDATE_CURRENT still works even if FLAG_IMMUTABLE is set"

If the error still remains and your targetSdkVersion = 31 then the error must caused because one of your dependencies is internally using WorkManager or your are directly using old version of WorkManager.

To solve simply add this dependency

implementation 'androidx.work:work-runtime-ktx:2.7.0'

Well after reading the answers here I found out this in Android's website:

Note: WorkManager Version 2.7.0 is required for apps targeting Android 12 (S).

https://developer.android.com/jetpack/androidx/releases/work

So adding this dependency will make it work.

 def work_version = "2.7.0"


// (Java only)
implementation "androidx.work:work-runtime:$work_version"


// Kotlin + coroutines
implementation "androidx.work:work-runtime-ktx:$work_version"

I experience a SIMILAR PROBLEM on android 12.

PendingIntent.java – line 375 android.app.PendingIntent.checkFlags enter image description here

It was solved by updating Onesignal Dependency.

    implementation 'com.onesignal:OneSignal:4.6.3'

Upgrading Google Play Services and Firebase libraries to the latest version solved this problem for me.

Add your pending intent like this:

 PendingIntent.getActivity(getApplicationContext(), 0, intent,PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT)

I updated my work-runtime-ktx version to 2.7.1

After the above change i got into another error

java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type: ErrorScope{Error scope for class <ERROR CLASS> with arguments: org.jetbrains.kotlin.types.IndexedParametersSubstitution@14ac19e7}

Look how i solved the above error by updating kotlin-gradle-plugin version here.

In Android Studio go to:

File -> Project Structure -> Dependencies -> app,

check there if any underlined dependencies with suggestions, if so, let the studio implement that correctly.


THIS and implementation 'androidx.work:work-runtime-ktx:2.7.1' Solved the problem to me.

p.s. You can not see that kind suggestions in the gradle

If you're using the Google AdMob SDK (version 20.4.0), they recommend adding this constraint in the AdMob official release notes.

Link: https://developers.google.com/admob/android/rel-notes?hl=en

dependencies {
implementation 'com.google.android.gms:play-services-ads:20.4.0'


// For apps targeting Android 12, add WorkManager dependency.
constraints {
implementation('androidx.work:work-runtime:2.7.0') {
because '''androidx.work:work-runtime:2.1.0 pulled from
play-services-ads has a bug using PendingIntent without
FLAG_IMMUTABLE or FLAG_MUTABLE and will fail in Apps
targeting S+.'''
}
}
}

If you happen to use Google Analytics and already tried changing flags, just update the aforementioned lib to its latest version.

implementation "com.google.android.gms:play-services-analytics:18.0.1"

Furthermore, if you're also using Firebase Analytics, both could live at the same time:

implementation platform("com.google.firebase:firebase-bom:30.3.1")
implementation "com.google.firebase:firebase-analytics"
implementation "com.google.android.gms:play-services-analytics:18.0.1"

There are two steps

1- Add androidx.work which is shown in the Error message

dependencies {
implementation 'androidx.work:work-runtime-ktx:2.7.1'}

2- Fix PendingIntent by Chang or add FLAG_IMMUTABLE

PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
    

Updating the WorkManager library isn't probably gonna be enough. There might also be other libraries that are using PendingIntent without the mutability flag, especially when you are working on a huge project. To find out this, use android studio's code inspection tool that will list down all the places where the Pending intent is used without a mutability flag.

Inspection results -> Android -> Lint -> Security

I have resolved my issue by doing this

PendingIntent pendingIntent = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity
(this, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
}
else
{
pendingIntent = PendingIntent.getActivity
(this, 0, notificationIntent,0);
}

u Need this,

dependencies {
implementation 'androidx.work:work-runtime:2.7.1'
implementation 'com.google.firebase:firebase-messaging:21.0.1'
implementation('com.google.firebase:firebase-iid:21.1.0')
implementation platform('com.google.firebase:firebase-bom:31.1.0')
}