Android targetSdkVersion改成33遇到的坑

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 的适配。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值