1

My App crashed when targeting to Android 12

Here is the crash log:

java.lang.RuntimeException: Unable to create service com.kdr.snipping.CheckRecentRun: java.lang.IllegalArgumentException: com.kdr.snipping: 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.ActivityThread.handleCreateService(ActivityThread.java:4500)
        at android.app.ActivityThread.access$1700(ActivityThread.java:247)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2072)
        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.app.ActivityThread.main(ActivityThread.java:7839)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
     Caused by: java.lang.IllegalArgumentException: com.kdr.snipping: 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.buildServicePendingIntent(PendingIntent.java:724)
        at android.app.PendingIntent.getService(PendingIntent.java:686)
        at com.kdr.snipping.CheckRecentRun.setAlarm(CheckRecentRun.java:44)
        at com.kdr.snipping.CheckRecentRun.onCreate(CheckRecentRun.java:35)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4487)
            ... 9 more

I use following implementation in my Gradle

implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'com.google.android.gms:play-services-ads-lite:20.6.0'
    implementation 'com.google.android.gms:play-services-ads:20.6.0'
    implementation 'androidx.work:work-runtime:2.7.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

Hope someone can Help me Thx

Zubayr Shah
  • 21
  • 1
  • 4

1 Answers1

5

With the introduction of Android 12, you now have to set either the flag PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_MUTABLE.

PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE)

or

PendingIntent.getActivity(context, 1337, openAppIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT)

See more here

Ashish
  • 196
  • 2
  • 10