Search code examples
androidandroid-jetpackandroid-12

Android 12 Spash API crashes on launch


Gradle

implementation 'androidx.core:core-splashscreen:1.0.0-alpha02'

Then the styles.xml. Note that the icons and background gradient render just fine in the Android Studio side gutter.

<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>

In my AndroidManifest.xml, I set the starting theme

<application
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/APP_NAME"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.App.Starting">

    <activity
        android:name="com.myapp.android.feature.FeatureActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Then in the Activity, I install the splash screen

@ExperimentalAnimationApi
@ExperimentalMaterialApi
@AndroidEntryPoint
class FeatureActivity : ComponentActivity() {

    val viewModel: FeatureViewModel by viewModels()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        installSplashScreen()
        AppCenter.hydrate(application)
        setContent {
            CustomTheme {
                CompositionLocalProvider(
                    LocalCoilImageLoader provides viewModel.imageLoader)
                {
                    FeatureScreen()
                }
            }
        }
    }
}

App crashes with

Caused by: android.content.res.Resources$NotFoundException: Drawable com.myapp.android.debug:drawable/compat_splash_screen_no_icon_background with resource ID #0x7f070064 Caused by: android.content.res.Resources$NotFoundException: Can't find ColorStateList from drawable resource ID #0x7f0700af


Solution

  •  <item name="windowSplashScreenBackground">@drawable/background_gradient</item>
    

    This line is creating the error. The background must be a color value, drawables are not supported (at least for now). You can review the example in the docs

    working example:

    <item name="windowSplashScreenBackground">@color/colorPrimary</item>