Android targetSdkVersion改成33遇到的坑

发布于:2024-08-11 ⋅ 阅读:(136) ⋅ 点赞:(0)

targetSdkVersion 改成 33 ,遇到一些坑。

需要注意的地方:

  • 修改 targetSdkVersion 为 33。
  • AndroidManifest.xml 里添加 android:exported=“true”
  • 升级 Gradle 版本。
  • 升级第三方库。

修改 app 的 build.gradle

android {
    compileSdkVersion 33

    defaultConfig {
        targetSdkVersion 33
    }
}

修改 AndroidManifest.xml ,为 activity 、service 等四大组件配置 android:exported="true"

编译报错,提示

We recommend using a newer Android Gradle plugin to use compileSdk = 33

This Android Gradle plugin (7.1.1) was tested up to compileSdk = 32

This warning can be suppressed by adding
    android.suppressUnsupportedCompileSdk=33
to this project's gradle.properties

The build will continue, but you are strongly encouraged to update your project to
use a newer Android Gradle Plugin that has been tested with compileSdk = 33

需要升级 Gradle 版本,修改 project 的 build.gradle

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        //classpath "com.android.tools.build:gradle:7.1.1"
        classpath "com.android.tools.build:gradle:7.4.2"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

修改 gradle-wrapper.properties ,使用 gradle-7.5-bin.zip ,

#Thu Aug 08 18:55:44 CST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

编译OK,功能验证报错,

java.lang.IllegalArgumentException: com.xxx: 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:401)
	at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:671)
	at android.app.PendingIntent.getBroadcast(PendingIntent.java:658)
	at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:273)
	at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:151)
	at androidx.work.impl.utils.ForceStopRunnable.forceStopRunnable(ForceStopRunnable.java:171)
	at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:102)
	at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)

wtf ? 我都没有使用 PendingIntent 。

多次尝试后,最后发现是导入了第三方库导致的。

解决办法:更新第三方库。 旧版本的第三方库可能没有做 targetSdkVersion 33 的适配。


网站公告

今日签到

点亮在社区的每一天
去签到