• Android targetSdkVersion改成33遇到的坑


    targetSdkVersion 改成 33 ,遇到一些坑。

    需要注意的地方:

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

    修改 app 的 build.gradle , targetSdkVersion 改为 33 ,
    导入 androidx 的基本包,示例对应的是 AppCompatActivity 、ConstraintLayout。

    android {
        compileSdkVersion 33
    
        defaultConfig {
            targetSdkVersion 33
        }
    }
    
    dependencies {
        implementation 'androidx.appcompat:appcompat:1.5.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    }
    

    修改 AndroidManifest.xml ,为 activity 、service 等四大组件配置 android:exported="true"android:enabled="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
    

    support 包需要换成 androidx 的包,如

    //import android.support.annotation.NonNull;
    import androidx.annotation.NonNull;
    //import android.support.v7.app.AppCompatActivity;
    import androidx.appcompat.app.AppCompatActivity;
    

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

    多次尝试后,最后发现是导入了第三方库 work-runtime 导致的,升级库版本就好了。

    -implementation "androidx.work:work-runtime:2.5.0"
    +implementation "androidx.work:work-runtime:2.7.0"
    

    如果有使用 PendingIntent ,改为

    		PendingIntent pi;
    		if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    			pi = PendingIntent.getActivity(mContext, 0, startIntent, PendingIntent.FLAG_IMMUTABLE);
    		} else {
    			pi = PendingIntent.getActivity(mContext, 0, startIntent, 0);
    		}
    
  • 相关阅读:
    Ubuntu sh文件编写,开多终端,自动读取密码
    C++入门(二)
    4.1 继承性
    机器视觉3D项目评估的基本要素及测量案例分析
    Java编码规范--OOP规约
    Spring 源码(12)Spring Bean 的创建过程(3)
    【进阶版】机器学习之神经网络与深度学习基本知识和理论原理(07)
    Windows10神州网信版的USB故障处理(设备描述符请求失败)
    Java面试题第八天
    Android 开发RxJava2常用操作符
  • 原文地址:https://blog.csdn.net/weixin_44021334/article/details/141052466