dart 目标U+(版本34及以上)不允许使用FLAG_MUTABLE创建或检索PendingIntent,FLAG_MUTABLE是一个隐式Intent,

kq0g1dla  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(564)

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.
这个错误在我更新到Android SDK 34后开始发生。
我的依赖项:

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'
}

字符串

ktca8awb

ktca8awb1#

该错误表示,从Android 14(API 34)开始,如果您在带FLAG_MUTABLE的PendingIntent对象中有隐式Intent,则您的应用将崩溃。
如果您不需要FLAG_MUTABLE标志,那么只需将其更改为FLAG_IMMUTABLE即可。但是,如果您的应用需要可变Intent,那么我建议您使用以下解决方案之一:

1-使用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-将隐式意图更改为显式意图

通过提供目标应用程序的包名称或完全限定的组件类名,明确您的意图。

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

参考资料:

  • https://developer.android.com/about/versions/14/behavior-changes-14#safer-intents
  • https://developer.android.com/guide/components/intents-filters#Types

相关问题