14

App crashed because of Nearby message API when targeting to android 12. Here is the crash log

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)

This exception happens even I added the flag PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_IMMUTABLE for the pendingIntent

    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
    )

This is a sample app that can reproduce this issue by clicking subscribe button in the app. I am using the version 18.0.0 of play-services-nearby

implementation 'com.google.android.gms:play-services-nearby:18.0.0'
Garry
  • 193
  • 1
  • 5
  • I'm facing the same issue despite of the most recent 17.6.0 basement version in the stack. I'm almost sure that basement code dated by Feb 21 is not Android S target ready yet. This is an internal pendingIntent, something related to onConnectionFailed branch. – apc Dec 09 '21 at 17:40

4 Answers4

9
  1. It sounds strange, but the fix is adding work manager dependency 2.7.0+ : implementation "androidx.work:work-runtime:2.7.0"

  2. You have to update dependencies that should support Android 12 braking changes (I had to update some third parties). Check that on github and documentation pages

  3. Also, some libraries are using permission <uses-permission android:name="com.google.android.gms.permission.AD_ID"/> that is required for Android 12. Please check the documentation for this permission

  4. Also, check google's issue tracker for google's library-specific issues related to Android 12

Maybe I missed something, but all this helped me to migrate. Good luck :)

Taxist Samael
  • 1,157
  • 1
  • 9
  • 23
  • 1
    Hi @Slava Sotone, Thanks for your comment. I tried your suggestion. Unfortunately, it doesn't work. – Garry Oct 29 '21 at 00:12
  • Doesn't work either, unfortunately :( – apc Dec 09 '21 at 17:41
  • 1
    @apc I've updated my answer. You could check it if it's still an issue for you – Taxist Samael Dec 10 '21 at 12:58
  • @Garry I've edited my answer. Do you know what else could help that is not listed in the answer – Taxist Samael Dec 10 '21 at 12:59
  • 1
    @TaxistSamael Thank you for the update, but in my case the stack trace is a bit different, it seems like there are some different internal PendingIntent problems inside Google Play Services library itself. This is a confirmed issue as for now: https://issuetracker.google.com/issues/206343771, also I perfectly understand why work-runtime fix works for one cases and doesn't work for another. Probably we have to wait for the fixes... (in my case, for the basement library) – apc Dec 10 '21 at 20:39
  • Thanks, @apc for your comment. I'll update my answer with the link to google's issue tracker to make it more informative for the community – Taxist Samael Dec 13 '21 at 08:51
  • @TaxistSamael Thank you for the update. I tried your update, unfortunately it doesn't work for my case. – Garry Dec 13 '21 at 23:54
  • 1
    @apc Thanks for your share too. I agree with you. I think we have to wait for the fixes – Garry Dec 13 '21 at 23:55
3

Installing Xamarin.AndroidX.work.runtime 2.7.0 resolved the issue for me. Please make sure you have the right version so you don't run into some problems as I did. Happy codding!!

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32324340) – MohanKumar Jul 29 '22 at 08:02
2

I was able to resolve this issue for a Xamarin project by adding the Xamarin.AndroidX.work.runtime NuGet package.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
loic
  • 51
  • 5
0

Updating the dependency alone should fix the issue. 18.1.0+ seems to fix this (looking at decompiled code) and 17.0.0 does not. (Not sure about the versions in between.)

implementation("com.google.android.gms:play-services-base:18.1.0")
Mygod
  • 2,077
  • 1
  • 19
  • 43