接入Android 12 Splash Screen API启动崩溃如何解决?
崩溃根因定位
你遇到的资源找不到异常由两个问题共同导致:
- 你使用的
androidx.core:core-splashscreen:1.0.0-alpha02是早期测试版本,存在大量兼容层bug,不支持渐变Drawable作为启动页背景,会强制把背景资源解析为ColorStateList,解析失败就抛出NotFound异常。 - 你直接将渐变drawable
@drawable/background_gradient赋值给了windowSplashScreenBackground属性,低版本系统下兼容层无法识别该类型资源。
修复步骤
- 升级SplashScreen依赖到稳定版
替换Gradle中的依赖配置为最新稳定版:
- 升级SplashScreen依赖到稳定版
implementation 'androidx.core:core-splashscreen:1.0.1'
- 分版本适配启动页背景
- 新建
values-v31/styles.xml(适配Android12及以上系统),保留你原有配置,原生系统支持渐变背景:
<style name="Theme.App.Starting" parent="Theme.SplashScreen"> <item name="windowSplashScreenBackground">@drawable/background_gradient</item> <item name="windowSplashScreenAnimatedIcon">@drawable/ic_logo_svg</item> <item name="postSplashScreenTheme">@style/Theme.AppNative</item> </style>- 在默认
values/styles.xml中,将windowSplashScreenBackground改为纯色值,兼容低版本系统:
<style name="Theme.App.Starting" parent="Theme.SplashScreen"> <item name="windowSplashScreenBackground">@color/你的渐变主色值</item> <item name="windowSplashScreenAnimatedIcon">@drawable/ic_logo_svg</item> <item name="postSplashScreenTheme">@style/Theme.AppNative</item> </style>低版本的渐变效果可以在进入Activity后通过布局对齐实现,不影响视觉体验。
- 修正API调用顺序(可选,符合官方规范)
将installSplashScreen()调用移到super.onCreate()之前,避免时序问题:
- 修正API调用顺序(可选,符合官方规范)
override fun onCreate(savedInstanceState: Bundle?) { installSplashScreen() super.onCreate(savedInstanceState) // 其余原有逻辑不变 }
- 清理缓存重打包
执行Android StudioFile → Invalidate Caches → Invalidate and Restart,清理旧构建缓存后重新编译,避免残留资源ID导致异常。
- 清理缓存重打包
内容的提问来源于stack exchange,提问作者user2836797
相关产品推荐
相关产品推荐

