·11 min read

Capacitor splash screen twice on Android 12

Android 12+ always draws an icon splash, and @capacitor/splash-screen holds it until hide(). How I got a Capacitor game down to one splash.

capacitorandroidgamedev

Contents

On Android, ASMR Logo Craft Block opened with two splash screens. First the app icon on a flat purple, for about two seconds. Then the game's own intro: the logo centred and tilted over a painted sky, which then glides up into its corner while the menu appears. Each one looked fine. Together they looked like the app had started twice.

iOS showed one. Same web code, same Capacitor config, same assets. So the question was a narrow one: why does a Capacitor app show its splash screen twice on Android 12 and later, and only there?

The setup: Capacitor 8 (@capacitor/android ^8.0.2, @capacitor/splash-screen ^8.0.0, androidx.core:core-splashscreen 1.2.0), with the splash plugin configured the usual way for a game that hides it itself:

"SplashScreen": {
  "launchAutoHide": false,
  "backgroundColor": "#6a3aa8",
  "androidSplashResourceName": "splash",
  "showSpinner": false
}

I checked it on two API 36 emulators, a phone and a tablet, and on the iOS simulator. I did not trust my eyes for any of it. Every launch was recorded (adb shell screenrecord, the emulator's own adb emu screenrecord once the tablet got too slow for that, and xcrun simctl io recordVideo on iOS) and cut into frames with ffmpeg at 10 or 20 fps. Most of what follows was found by reading single frames, and two of the bugs were exactly one frame long.

Where the second splash comes from

Android 12 made the launch splash mandatory. From the tap until the app draws its first frame, the system paints a screen built from the launcher activity's theme, and that screen can hold exactly two things: a background colour and an icon. No full-screen picture, no way to turn it off. This applies to every app on Android 12 and later, not to Capacitor in particular.

What the Capacitor plugin adds is the length. On API 31+, @capacitor/splash-screen does not draw its own splash at all. It installs the AndroidX splash API and keeps the system one up:

androidx.core.splashscreen.SplashScreen windowSplashScreen =
    androidx.core.splashscreen.SplashScreen.installSplashScreen(activity);
windowSplashScreen.setKeepOnScreenCondition(() -> isVisible || isHiding);

That is from the plugin's own SplashScreen.java. With launchAutoHide: false, isVisible stays true until JavaScript calls SplashScreen.hide(). So the icon-on-purple screen covered the whole load, the game called hide() once its saves were read, and then the WebView underneath started the intro. Two splashes, by construction. The splash drawable named in the config is only used if that Android 12 path throws, which it did not.

On iOS the same plugin reuses the launch storyboard, so the screen the OS shows and the screen the plugin holds are the same picture. One splash.

The template this game was built from had the same config, so it had the same two screens.

The screen you cannot remove

Since the system splash cannot go, the plan was to make it as short and as forgettable as possible, and to put the real splash, the intro's first frame, on top of the WebView from the very first frame the app draws.

No icon. The launch theme keeps the colour and drops the icon:

<!-- res/values/themes.xml (a simplified example) -->
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/splash_sky</item>
    <item name="windowSplashScreenAnimatedIcon">@android:color/transparent</item>
</style>

The launcher activity in AndroidManifest.xml uses that theme. Set the background colour explicitly. On an earlier build where windowSplashScreenBackground was missing, the system splash came up light grey and then cut to purple, which was its own small flash.

Gone at the first frame. Capacitor's BridgeActivity switches to AppTheme.NoActionBar in onCreate, so once nothing calls setKeepOnScreenCondition, the system splash lasts only until the activity draws. That meant taking @capacitor/splash-screen out of the Android build. My first pass did it in the game alone, with android.includePlugins in capacitor.config.json, which lists the plugins Android links (every one except the splash). That list is an allowlist: every plugin you add later has to be added there as well. The version that shipped replaces the plugin on both platforms instead.

The real splash, in the first frame. A Capacitor plugin's load() runs in onCreate, on the main thread, after the WebView exists. A view added there sits on top of the WebView in the very first frame. A simplified example of the idea:

@CapacitorPlugin(name = "GameSplash")
public class GameSplashPlugin extends Plugin {
    private View splash;

    @Override
    public void load() {
        splash = new SplashView(getActivity());   // sky, vignette, logo
        getActivity().addContentView(splash, new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @PluginMethod
    public void hide(PluginCall call) {
        getActivity().runOnUiThread(() -> {
            View view = splash;
            splash = null;
            if (view != null) view.animate().alpha(0f).setDuration(200)
                .withEndAction(() -> ((ViewGroup) view.getParent()).removeView(view));
            call.resolve();
        });
    }
}

There is no timer. hide() is the only thing that takes it down.

On iOS the launch storyboard draws the same three layers, and a small plugin keeps that storyboard's view over the WebView until hide(). Both are generated from one description in capacitor.config.json (a colour plus a stack of pictures), and CI fails when the native files drift from it. The pictures come from the game's own art: the sky, its vignette, and the logo with its −6° tilt and drop shadow baked in, because a launch storyboard cannot rotate a view. To place the logo exactly where the intro draws it, I read the live layout out of the running WebView over the Chrome DevTools protocol instead of guessing from the CSS.

Picking the colour of the flat screen

The system splash still shows for a moment on every cold start. It was purple, the game's page colour, so it still read as a separate screen before the sky.

So I matched it to the picture that follows. The whole sky averages to #888b94, a muddy grey that matches nothing on screen. The top third alone averages to #476599, the twilight blue your eye lands on first. With sharp, roughly:

// Average colour of the top third of the splash's first layer.
const { width, height } = await sharp('sky.jpg').metadata();
const top = await sharp('sky.jpg')
  .extract({ left: 0, top: 0, width, height: Math.round(height / 3) })
  .toBuffer();
const { channels } = await sharp(top).stats();
const hex = '#' + channels.slice(0, 3)
  .map((c) => Math.round(c.mean).toString(16).padStart(2, '0')).join('');

The build computes it from the art rather than storing it, so it follows the sky if the sky changes. Measured from the original, uncompressed sky it came out as #466599. Now the first second of a launch reads as the sky fading in.

Two flashes the recordings caught

The structure was right after the steps above. The hand-off was not, and both problems were too short to see at full speed.

Android: a washed-out logo mid-fade. Five frames across the hand-off, and the third one showed the logo pale, as if half there. The game called hide() when its state had loaded, but the first screen's pictures had not been decoded yet, because the page only asks for them once it renders. The splash faded over a page that was still missing its logo. The fix is to wait for the pictures before hiding:

// Wait until the first screen's pictures are decoded and painted.
async function waitForFirstPictures(urls, timeoutMs = 2000) {
  const decoded = Promise.all(urls.map((url) => {
    const img = new Image();
    img.src = url;
    return img.decode().catch(() => {});   // a broken picture must not hold the splash
  }));
  await Promise.race([decoded, new Promise((r) => setTimeout(r, timeoutMs))]);
  await new Promise((r) => requestAnimationFrame(r));
  await new Promise((r) => requestAnimationFrame(r));
}

await waitForFirstPictures([logoUrl, backdropUrl]);
await GameSplash.hide();

The backdrop URL depends on a player setting, so it is read at that moment, after settings load, rather than imported as a constant.

iOS: one frame of bare WebView. A per-frame colour sample of the logo area was flat across the whole launch except for one frame that dipped and bounced back. iOS swaps its launch screen for the app's first frame, and my plugin added the splash with DispatchQueue.main.async, one turn of the run loop later. That left one frame with nothing on top of the WebView. Adding the view synchronously in load() removed the dip in every recording after it. Before that fix I had also added a 150 ms wait after the pictures were drawn, on iOS. I never went back to test whether it still matters once the run-loop fix is in, so I cannot tell you it does.

iOS: the logo a status bar too high. With the dip gone, the next recording showed a steady shift in the logo area at the hand-off. The WebView is not in a window yet when plugins load, so the splash's safe area is zero, and nothing passes the real one down when the window arrives. A logo laid out below the safe area sat about one status bar too high, then jumped. The splash now borrows the window's safe area until it is attached, then attaches again and reads the real one. In the three launches I recorded after that, the logo area held steady through the hand-off.

What a launch looks like now

The last Android recording, on the tablet emulator: flat blue for about 1.1 s, then the native sky and logo, steady from frame 20, then a single clean change at frame 54 where the page takes over and the intro carries on. No icon screen, no flash, no jump.

The cold-start numbers are from adb shell am start -W, on an emulator that was badly overloaded (the Mac's load average read 15, and later 20, when I checked):

TotalTime: 2462
TotalTime: 1103
TotalTime: 1151

The first launch after a fresh install held the blue screen for about 7 s, and a later warm-up launch took 11.5 s. I did not work out how much of that was the app and how much was the machine, so treat those two as unexplained. The steady figure is about 1.1 s on that emulator. If your emulator is this slow, fix the emulator first; it made every recording here harder than it needed to be.

The debug APK also came out 1.7 MB smaller. The old splash PNGs, one per density, orientation and night mode across 26 drawable folders, are gone. The new sky is a JPEG at quality 90 (219 KB, where the PNG was 2.4 MB) and the logo is a palette PNG (265 KB, from 1.29 MB). Both are copied into the Android and the iOS project, so both apps carry them. Cutting a Capacitor Android build in half covers where the rest of an APK's size goes.

What capacitor-assets does to your icons

The same game had a second launch-screen bug from the day it was ported: the Android launcher icon read "…GO CRA… BLOC…", a cropped slice of the wordmark instead of the icon.

resources/ held both icon.svg and logo.svg. Run with both present, npx capacitor-assets generate (@capacitor/assets 3.0.5) built every Android launcher density, adaptive foreground included, from the logo. I moved the logo out of resources/, regenerated, and the icon came out right in the square, round and adaptive versions and in the emulator's app drawer. A test now fails if any logo.* file appears in resources/ again, because nothing else would notice.

When I later swapped in the final icon, generate did four more things nobody asked for:

  • It wrote a default splash.png into all 26 drawable folders, though nothing referenced them and the game no longer used a Capacitor splash.

  • It put the web app icons in a new top-level icons/ folder and rewrote public/manifest.webmanifest to point at them:

    -      "src": "icons/icon-48.webp",
    -      "type": "image/webp",
    +      "src": "../icons/icon-48.webp",
    +      "type": "image/png",
    

    Both are wrong: ../icons/ is outside public/, so Vite never copies it, and the files are WebP. The template this game came from has a test that catches exactly this pair of mistakes in the manifest. I suspect this is how they got there the first time, but I have not checked.

  • It failed on iOS with Unable to generate assets …/Splash.imageset/Default@1x~universal~anyany.png: unable to open for write, because I had deleted that imageset along with the old splash. Harmless here, and the Android and web files were written anyway.

  • It left the notification large icon (ic_launcher_large.png) alone. Ours was still the template's placeholder letter.

I reverted the manifest, deleted the folder and the splash files, and regenerated the large icon by hand. Read git status after every capacitor-assets generate.

The generated icons were also big. Reducing each PNG to a 256-colour palette with sharp (libimagequant underneath) gave:

File Before After
iOS app icon, 1024 2.26 MB 332 KB
Play listing icon, 512 612 KB 253 KB
resources/icon.svg (PNG inside) 2.9 MB 443 KB
Android xxxhdpi adaptive foreground 489 KB 74 KB

A 100% crop looked the same before and after. The iOS icon still has no alpha channel and the Play icon is still a 32-bit RGBA PNG, which is what each store asks for, and assembleDebug builds with them.

Takeaway

On Android 12 and later, count the screens in a frame-by-frame recording of a cold start, not by watching one. If you see the icon and then your own splash, the system splash is being held open, and setKeepOnScreenCondition is the line doing it. You cannot remove that screen. You can drop its icon, give it the colour of whatever comes next, and make sure it ends at the first frame, with your real splash already drawn on top.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.