当我将目标SDK更新到30+ (Android R或更高版本)时,我的PendingIntent上出现了一个lint警告Missing PendingIntent可变性标志。当我想定义PendingIntent时,FLAG_UPDATE_CURRENT标志。

我应该如何处理这个绒线没有对应用程序功能的影响?


当前回答

我将我的work-runtime-ktx版本更新到2.7.1

在上面的修改之后,我又遇到了另一个错误

java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type: ErrorScope{Error scope for class <ERROR CLASS> with arguments: org.jetbrains.kotlin.types.IndexedParametersSubstitution@14ac19e7}

看看我是如何通过更新kotlin-gradle-plugin版本来解决上述错误的。

其他回答

如果你的应用目标是Android 12 (targetSdkVersion = 31),并且直接使用旧版本的WorkManager或任何第三方库,那么你需要将其更新到最新版本来解决它。

dependencies {
    val work_version = "2.7.1"

    // (Java only)
    implementation("androidx.work:work-runtime:$work_version")

    // Kotlin + coroutines
    implementation("androidx.work:work-runtime-ktx:$work_version")

    // optional - RxJava2 support
    implementation("androidx.work:work-rxjava2:$work_version")        
}

如果您没有使用最新版本的WorkManager,就会看到这个问题。在2.7.0-alpha02版本中已修复:

使PendingIntent可变显式,以修复针对Android 12时的崩溃

请记住,2.7.0-alpha02只与Android 12开发者预览1 SDK兼容。所以你可能想要等到它进入beta版或RC版。

2021年4月21日更新-为任何谷歌搜索该问题的人添加这个答案,您可能遇到的错误可能是这样的:

java.lang.IllegalArgumentException: com.myapp.myapp: Targeting S+ (version 10000 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:386)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:657)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:644)
        at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:174)
        at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:108)
        at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:86)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)

你不需要在你的应用中直接使用WorkManager来查看这个崩溃。

如本文所述,解决方案是向构建中添加依赖项。Android 12版本的gradle文件:

 implementation 'androidx.work:work-runtime-ktx:2.7.0-alpha05'

注意,无论你使用的是Java、Kotlin +协程、RxJava2、GCMNetworkManager等等,这种依赖关系都是不同的。所以一定要检查上面的还原。

显然,用最新的版本号替换上面的版本号。如前所述,它不兼容android-13之前的版本。

当你升级你的项目和目标android版本12时,这个问题就来了。使用该解决方案,您可以更新所有待处理实习生

目标S+(版本31及以上)需要在创建PendingIntent时指定FLAG_IMMUTABLE或FLAG_MUTABLE之一

以下代码

 PendingIntent pendingIntent = null;
        if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.S){
             pendingIntent = stackBuilder.getPendingIntent(1, PendingIntent.FLAG_MUTABLE);

        }else {
             pendingIntent = stackBuilder.getPendingIntent(1, PendingIntent.FLAG_UPDATE_CURRENT);

        }

如果你在项目中使用接收器,也要实现这个依赖工作

//Work Manager dependency
implementation 'androidx.work:work-runtime:2.7.1'
final int flag =  Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT;
PendingIntent pendingIntent = PendingIntent.getActivity(context, PENDING_INTENT_REQUEST_CODE, notificationIntent, flag);

我创建了PendingIntentCompat。将PendingIntent逻辑抽象到一个单独的类中。

object PendingIntentCompat {

    @JvmStatic
    @JvmOverloads
    fun getActivity(
        context: Context,
        requestCode: Int,
        intent: Intent,
        flags: Int,
        isMutable: Boolean = false
    ): PendingIntent {
        return PendingIntent.getActivity(
            context,
            requestCode,
            intent,
            addMutabilityFlags(isMutable, flags)
        )
    }

    @JvmStatic
    @JvmOverloads
    fun getService(
        context: Context,
        requestCode: Int,
        intent: Intent,
        flags: Int,
        isMutable: Boolean = false
    ): PendingIntent {
        return PendingIntent.getService(
            context,
            requestCode,
            intent,
            addMutabilityFlags(isMutable, flags)
        )
    }

    @JvmStatic
    @JvmOverloads
    @RequiresApi(Build.VERSION_CODES.O)
    fun getForegroundService(
        context: Context,
        requestCode: Int,
        intent: Intent,
        flags: Int,
        isMutable: Boolean = false
    ): PendingIntent {
        return PendingIntent.getForegroundService(
            context,
            requestCode,
            intent,
            addMutabilityFlags(isMutable, flags)
        )
    }

    /**
     * https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability
     */
    private fun addMutabilityFlags(isMutable: Boolean, flags: Int): Int {
        var updatedFlags = flags

        if (isMutable) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                updatedFlags = flags or PendingIntent.FLAG_MUTABLE
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                updatedFlags = flags or PendingIntent.FLAG_IMMUTABLE
            }
        }
        return updatedFlags
    }
}