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

未关闭 适合新手
#2,627 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
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 小时
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ionic-team/capacitor-plugins 的其他 Issue

查看 ionic-team/capacitor-plugins 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。