Search code examples
androidgoogle-play-servicesgoogle-nearby

App crashes because of pendingIntent when targeting to Android 12


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'

Solution

    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 :)