[@capacitor/splash-screen] Android: hide() removes the launch pre-draw listener off the UI thread, crashing cold start

オープン 初心者向け
#2,627 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
80/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
android, java
領域
mobile

調査の方向性

android/src/main/java/com/capacitorjs/plugins/splashscreen/SplashScreen.javaを読み、hide(int, boolean)のisLaunchSplash分岐と、hideDialog(...)のAndroid 12 API分岐に注目してください。両方の呼び出し箇所でpre-drawリスナーをどのように解放しているかを確認し、提案されているreleaseLaunchSplashの方法と比較してください。両方の経路でUIスレッド上にて可視性を更新し、リスナーを削除してそのフィールドを同時にクリアし、コールドスタート時にクラッシュしなくなれば完了です。

索引モデルが issue の本文から書いたものです。

説明

triage

Plugin: @capacitor/splash-screen 8.0.2 (Android)

Describe the bug

SplashScreen.hide() runs on Capacitor's plugin thread. On the Android 12 splash-screen API path it calls
content.getViewTreeObserver().removeOnPreDrawListener(this.onPreDrawListener) directly on that thread, but a
ViewTreeObserver may only be touched from the UI thread.

With launchAutoHide: false the launch listener returns false, so it holds drawing and the UI thread runs
ViewTreeObserver.dispatchOnPreDraw() on every frame until the web layer calls hide(). The off-thread removal
then races that dispatch: removing an entry shifts the array under the dispatch loop and leaves a null slot, and
the app dies with

java.lang.NullPointerException: Attempt to invoke interface method
'boolean android.view.ViewTreeObserver$OnPreDrawListener.onPreDraw()' on a null object reference
    at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1116)

roughly 10 ms after the hide() call. The same block also sets isVisible = false before the removal, so a
dispatch landing between the two can set it back and leave the splash pinned up.

Both affected call sites are in android/src/main/java/com/capacitorjs/plugins/splashscreen/SplashScreen.java:
the isLaunchSplash early-return inside hide(int, boolean), and the "Hide with Android 12 API" branch of
hideDialog(...).

To Reproduce
  1. A Capacitor 8 Android app targeting the Android 12 splash API, with launchAutoHide: false in
    capacitor.config, so the launch splash is held until the web layer hides it.
  2. Call SplashScreen.hide() from JavaScript as soon as the web app has painted (the usual pattern).
  3. Cold-start the app repeatedly on a device or emulator (Android 12+; seen on Android 14).

It is a race, so it is intermittent — observed twice in one night across an automated relaunch suite that
cold-starts the app many times.

Expected behavior

Hiding the launch splash should never crash the app, and the pre-draw listener should be removed on the UI thread.

Suggested fix

Post the whole release — clear isVisible, remove the listener, clear the field — to the main looper as a single
runnable, from both call sites. Doing all three together on the UI thread also closes the second half of the bug,
since no dispatch can run between clearing the flag and removing the listener:

private void releaseLaunchSplash(final OnPreDrawListener listener) {
    new Handler(context.getMainLooper()).post(() -> {
        isVisible = false;
        if (null != content) {
            content.getViewTreeObserver().removeOnPreDrawListener(listener);
        }
        if (onPreDrawListener == listener) {
            onPreDrawListener = null;
        }
    });
}

We are running exactly this as a pinned patch against 8.0.2 and the cold-start crash has not recurred. Happy to
open a PR if that is useful.

Additional context
  • Capacitor: 8.4.2
  • @capacitor/splash-screen: 8.0.2 (current npm latest at time of writing)
  • Platform: Android 12+ (reproduced on Android 14)
主要言語
Java
スター
688
フォーク
691
平均マージ
1日 15時間
マージ済み PR(30日)
3

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ionic-team/capacitor-plugins のほかの issue

ionic-team/capacitor-plugins の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。