Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Targeting U+ (version 34 and above) disallows creating or retrieving a PendingIntent with FLAG_MUTABLE, an implicit Intent within and

E/AndroidRuntime(15886): java.lang.RuntimeException: Unable to get provider androidx.startup.InitializationProvider: androidx.startup.StartupException: java.lang.IllegalArgumentException: br.com.cspautomacao.lazarus_comanda: Targeting U+ (version 34 and above) disallows creating or retrieving a PendingIntent with FLAG_MUTABLE, an implicit Intent within and without FLAG_NO_CREATE and FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT for security reasons. To retrieve an already existing PendingIntent, use FLAG_NO_CREATE, however, to create a new PendingIntent with an implicit Intent use FLAG_IMMUTABLE.

This error started occurring after I updated to Android SDK 34.

My dependencies:

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        
    // Dependências comuns
    implementation fileTree(dir: "libs/comun", include: ['*.aar'])
    implementation 'androidx.work:work-runtime-ktx:2.8.1'
    implementation 'androidx.appcompat:appcompat:1.6.1'    

    // Dependências "elgin"
    implementation fileTree(dir: 'libs/elgin', include: ['*.aar'])
    implementation 'org.apache.commons:commons-lang3:3.9'
    implementation 'com.google.code.gson:gson:2.8.6'
}
like image 567
Gustavo Schwarz Avatar asked Nov 30 '25 09:11

Gustavo Schwarz


1 Answers

The error says, starting with Android 14 (API 34), if you have an implicit intent within a PendingIntent object with FLAG_MUTABLE, your app will crash.

If you don't need the FLAG_MUTABLE flag, then just change it to FLAG_IMMUTABLE. However, if your app requires a mutable intent, then I suggest one of the following resolutions:

1- Use FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT

val implicitIntent = Intent("com.example.app.action.WHATEVER")
val implicitPendingIntent = if (Build.VERSION.SDK_INT >= 34) {
  PendingIntent.getBroadcast(this, 0, implicitIntent, PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT)
} else {
  PendingIntent.getBroadcast(this, 0, implicitIntent, PendingIntent.FLAG_MUTABLE)
}

2- Change your implicit intent to an explicit one

Make your intent explicit by supplying either the target app's package name or a fully-qualified component class name.

val explicitIntent = Intent("com.example.app.action.WHATEVER")
explicitIntent.setPackage(context.packageName)
// explicitIntent.component = ComponentName("com.example.app", "com.example.app.MainActivity")
val explicitPendingIntent = PendingIntent.getBroadcast(this, 0, explicitIntent, PendingIntent.FLAG_MUTABLE)

References:

  • https://developer.android.com/about/versions/14/behavior-changes-14#safer-intents
  • https://developer.android.com/guide/components/intents-filters#Types
like image 63
Darush Avatar answered Dec 01 '25 23:12

Darush