定位到 Android 12 时,应用因 pendingIntent 而崩溃

App crashes because of pendingIntent when targeting to Android 12

当定位到 android 12 时,由于 Nearby message API,应用程序崩溃了。 这是崩溃日志

2021-10-07 18:59:44.916 10343-10384/com.example.nearbymessagescanner E/AndroidRuntime: FATAL EXCEPTION: GoogleApiHandler
Process: com.example.nearbymessagescanner, PID: 10343
java.lang.IllegalArgumentException: com.example.nearbymessagescanner: 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.android.gms.common.api.GoogleApiActivity.zaa(com.google.android.gms:play-services-base@@17.5.0:4)
    at com.google.android.gms.common.GoogleApiAvailability.zaa(com.google.android.gms:play-services-base@@17.5.0:116)
    at com.google.android.gms.common.api.internal.GoogleApiManager.zaa(com.google.android.gms:play-services-base@@17.5.0:252)
    at com.google.android.gms.common.api.internal.GoogleApiManager$zaa.zaa(com.google.android.gms:play-services-base@@17.5.0:109)
    at com.google.android.gms.common.api.internal.GoogleApiManager$zaa.onConnectionFailed(com.google.android.gms:play-services-base@@17.5.0:75)
    at com.google.android.gms.common.internal.zai.onConnectionFailed(com.google.android.gms:play-services-base@@17.5.0:2)
    at com.google.android.gms.common.internal.BaseGmsClient$zzf.zza(com.google.android.gms:play-services-basement@@17.5.0:6)
    at com.google.android.gms.common.internal.BaseGmsClient$zza.zza(com.google.android.gms:play-services-basement@@17.5.0:21)
    at com.google.android.gms.common.internal.BaseGmsClient$zzc.zzc(com.google.android.gms:play-services-basement@@17.5.0:11)
    at com.google.android.gms.common.internal.BaseGmsClient$zzb.handleMessage(com.google.android.gms:play-services-basement@@17.5.0:49)
    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.os.HandlerThread.run(HandlerThread.java:67)

即使我为 pendingIntent

添加了标志 PendingIntent.FLAG_MUTABLEPendingIntent.FLAG_IMMUTABLE,也会发生此异常
    private fun backgroundSubscribe() {
    Log.d(TAG, "Subscribing for background updates.")
    val options = SubscribeOptions.Builder().setStrategy(Strategy.BLE_ONLY).build()
    messagesClient.subscribe(pendingIntent, options)
}

    private val pendingIntent: PendingIntent
    get() = PendingIntent.getBroadcast(
        this,
        0,
        Intent(this, BeaconMessageReceiver::class.java),
        PendingIntent.FLAG_MUTABLE
    )

这是一个 sample app,可以通过单击应用程序中的订阅按钮来重现此问题。 我使用的是18.0.0版本的play-services-nearby

implementation 'com.google.android.gms:play-services-nearby:18.0.0'
  1. 听起来很奇怪,但修复是添加工作管理器依赖项 2.7.0+:implementation "androidx.work:work-runtime:2.7.0"

  2. 您必须更新应该支持 Android 12 制动更改的依赖项(我必须更新一些第三方)。在 github 和文档页面

    上检查
  3. 此外,一些图书馆正在使用 Android 12 所需的权限 <uses-permission android:name="com.google.android.gms.permission.AD_ID"/>。请查看此权限的文档

  4. 此外,检查 google's issue tracker 是否存在与 Android 12

    有关的 google 库特定问题

也许我错过了什么,但所有这些都帮助我迁移了。祝你好运:)

我能够通过添加 Xamarin.AndroidX.work.runtime NuGet 包解决 Xamarin 项目的这个问题。