bug(splash-screen): SplashScreen.hide() removes the pre-draw listener off the main thread and can crash with IndexOutOfBoundsException on Android

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
android, java
领域
mobile

调研方向

阅读 SplashScreenPlugin.java 和 SplashScreen.java,重点关注 hide 中 Android 12 启动画面的分支,以及 show 和 hideDialog 如何将工作分派到 UI 线程。复现从非主线程移除监听器的情况,或跟踪其执行过程。然后验证隐藏路径会在主线程移除 pre-draw 监听器,并在之后解析调用。

由索引模型根据 Issue 内容生成。

描述

platform: android

Bug Report

Plugin(s)

@capacitor/splash-screen 8.0.2 (also present in 8.0.3-nightly-20261127T150834.0 and 9.0.0-alpha.2)

Capacitor Version
@capacitor/core: 8.6.0
@capacitor/android: 8.6.0
@capacitor/splash-screen: 8.0.2
Platform(s)

Android

Current Behavior

Calling SplashScreen.hide() from JavaScript while the launch splash is shown with the Android 12 API (launchAutoHide: true, no dialog) can crash the app:

java.lang.IndexOutOfBoundsException: Index 1 out of bounds for length 1
  at java.util.ArrayList.get (ArrayList.java:435)
  at android.view.ViewTreeObserver$CopyOnWriteArray$Access.get (ViewTreeObserver.java:1367)
  at android.view.ViewTreeObserver.dispatchOnPreDraw (ViewTreeObserver.java:1175)
  at android.view.ViewRootImpl.performTraversals (ViewRootImpl.java:4206)
  at android.view.ViewRootImpl.doTraversal (ViewRootImpl.java:2758)
  at android.view.Choreographer.doFrame (Choreographer.java:994)

SplashScreenPlugin.hide(PluginCall) calls splashScreen.hide(...) directly. Plugin methods run on the CapacitorPlugins handler thread (Bridge.callPluginMethod posts to taskHandler), so SplashScreen.hide(int, boolean) reaches content.getViewTreeObserver().removeOnPreDrawListener(this.onPreDrawListener) off the main thread. The pre-draw listener returns false on every frame while the splash is visible, so the main thread keeps dispatching pre-draw listeners. A removal from the plugin thread between CopyOnWriteArray.start() and get(1) produces the exception above. The same removal in the launch auto-hide path runs on the main thread (new Handler(context.getMainLooper()).postDelayed(...)) and is safe.

hideDialog, show and the non-launch hide path already post to the UI thread (activity.runOnUiThread / mainHandler.post); only the Android 12 launch-splash branch of hide does not.

Expected Behavior

SplashScreen.hide() removes the pre-draw listener on the main thread and never races the view traversal.

Code Reproduction

The crash is a timing race, so a sample app reproduces it only intermittently: an app with launchAutoHide: true and a long launchShowDuration that calls SplashScreen.hide() as soon as its first page paints. We saw it in production on 6 users over 28 days (Samsung devices, Android 14 to 17). The thread analysis above is from reading 8.0.2's SplashScreenPlugin.java and SplashScreen.java.

Other Technical Details

The fix we ship as a patch:

 @PluginMethod
 public void hide(PluginCall call) {
-    if (config.isUsingDialog()) {
-        splashScreen.hideDialog(getActivity());
-    } else {
-        splashScreen.hide(getSettings(call));
-    }
-    call.resolve();
+    getActivity()
+        .runOnUiThread(() -> {
+            if (config.isUsingDialog()) {
+                splashScreen.hideDialog(getActivity());
+            } else {
+                splashScreen.hide(getSettings(call));
+            }
+            call.resolve();
+        });
 }
Additional Context

Happy to open a pull request against main with this change if you agree with the approach.

This report was prepared with AI assistance (Claude) on behalf of the app team.

主要语言
Java
星标
688
派生
690
平均合并
16 小时 15 分钟
30 天内合并 PR
7

环境准备

  • 没有 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 摘要。