最近发布Google 内侧版本app时老是会出现一个警告⚠️ “app的target sdk 版本必须不能低于31(Android 12)” 否则今年12月之后将影响上架。于是趁着空闲便搞下target sdk的升级。

期间还是踩了一点小坑的,这里就把我们项目中迁移遇到的记录下,希望大家碰到类似的能够轻松避开~

坑1:Activity的exported属性值要显式声明
/Users/xxx/xxx/xxx/app/src/main/AndroidManifest.xml Error:

	android:exported needs to be explicitly specified for <activity>. 
	
	Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` 
	when the corresponding component has an intent filter defined. 

    See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

这个看错误信息就ojbk了,含有intent-filter的activity就显示指明精确值就行。

坑2:LeakCanary2.2版本问题

改完Activity#exported问题后接着就遇到了LeakCanary2.2的坑。究其原因也是target sdk 的问题,这个版本sdk未兼容target sdk 31。

先看下当时的报错信息:

Installation failed due to: 'INSTALL_PARSE_FAILED_MANIFEST_MALFORMED: 

Failed parse during installPackageLI:/data/app/vmdl1585792192.tmp/base.apk (at Binary XML file line #526):
leakcanary.internal.activity.LeakLauncherActivity: Targeting S+ (version 31 and above) requires that an
explicit value for android:exported be defined when intent filters are present'

Retry
Failed to launch an application on all devices

其实这个问题在官网已经有issue了,因此我们直接升级2.7版本就解决了。

坑3:PendingIntent与IntentFlag使用问题

先看下错误log把:

Process: com.xxx.app.dev, PID: 8174
    java.lang.IllegalArgumentException: com.xxx.app.dev: 
    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.

大概意思是使用PendingIntent时 安卓12及其以上对intent的flag也有要求,至少指定FLAG_IMMUTABLE或者FLAG_MUTABLE。

这就很懵逼了,我们的项目中貌似还没有直接使用到这个PendingIntent,难道是其他推送sdk未兼容?起初还想去看看推送sdk
文档看看兼容安卓12没,结果细看log 竟然避坑了,少走了点弯路:

在这里插入图片描述
这个类有使用PendingIntent 全局搜索下~

在这里插入图片描述
看到这里定位到了,WorkManager的坑 😄 目前用的2.6.0 版本未兼容target sdk31 找个其他兼容版本即可(2.7.1)

坑4:compile sdk 过低

本来以为没啥问题了,结果还是始料未及,app工程的compile sdk 低于 work manager sdk minSdk:

在这里插入图片描述
很简单咯,把app的编译版本设置为31即可。

采坑完毕,终于跑起来了~

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐