如何正确传递ShadowActivity上下文给插件中的第三方SDK?

未关闭
#1,369 4 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
android, java
领域
backend, mobile

调研方向

Start by reading ShadowActivityDelegate.kt around line 159 and PluginContainerActivity.java around line 84, then trace the third-party SDK call from KtSuwellActivity.kt around line 129. Determine which context the SDK can use to start an activity inside a plugin, and document a supported pattern or the required framework change. The issue provides no test or acceptance criteria, so confirm the expected behavior with maintainers.

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

描述

问题描述:

我们在使用Shadow框架时遇到类转换异常:

Caused by:java.lang.RuntimeException: java.lang.ClassCastException: com.tencent.shadow.test.plugin.general_cases.lib.gallery.TestApplication cannot be cast to com.tencent.shadow.core.runtime.ShadowActivity
com.tencent.shadow.core.loader.delegates.ShadowActivityDelegate.onCreate(ShadowActivityDelegate.kt:159)
com.tencent.shadow.core.runtime.container.PluginContainerActivity.onCreate(PluginContainerActivity.java:84)
android.app.Activity.performCreate(Activity.java:9221)
android.app.Activity.performCreate(Activity.java:9177)
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1538)
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4392)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4631)
android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:112)
android.app.servertransaction.TransactionExecutor.executeNonLifecycleItem(TransactionExecutor.java:186)
android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:112)
android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:84)
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2873)
android.os.Handler.dispatchMessage(Handler.java:107)
android.os.Looper.loopOnce(Looper.java:249)
android.os.Looper.loop(Looper.java:337)
android.app.ActivityThread.main(ActivityThread.java:9580)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:593)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
Caused by:java.lang.ClassCastException: com.tencent.shadow.test.plugin.general_cases.lib.gallery.TestApplication cannot be cast to com.tencent.shadow.core.runtime.ShadowActivity
com.box_suwell.kt.KtSuwellActivity$Companion.startDocAct(KtSuwellActivity.kt:129)

背景:

  1. 插件化之前,原宿主中通过jsFuncApi.run()直接调用第三方SDK的接口实现(在宿主中正常)
  2. 现在将该接口实现迁移到插件中,context参数传递宿主Activity或插件Application均失败
  3. 第三方SDK内部会使用传入的context.startActivity()
  4. 第三方SDK的代码我们无法修改

关键代码:

// 宿主调用插件接口
jsFuncApi.run(
    context, // 宿主Activity
    parseParamsData,
    getDefaultExtFuncCallBack(callbackId)
)

// 插件中尝试启动Activity
context.startActivity(intent) // 这里触发异常

错误分析:
根据框架原理,Shadow通过动态代理机制将插件Activity包装成ShadowActivity。当使用宿主Activity作为context启动插件组件时:

  1. 宿主Activity无法识别插件中的类加载器
  2. 插件组件需要通过Shadow的代理机制与宿主交互

尝试过的方案:

  1. 传入宿主Activity -> 无法startActivity
  2. 传入插件Application -> 类转换异常

请求建议:
根据框架设计文档,能否指导如何:

  1. 在插件中正确获取有效的ShadowActivity上下文?
  2. 是否需要通过PluginContainerActivity等专用容器传递上下文?
  3. 是否有推荐的上下文传递模式来兼容第三方SDK的startActivity调用?

@shifujun

主要语言
Java
星标
7.8k
派生
1.3k
PR 合并指标
30 天内没有已合并 PR

环境准备

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

从这里开始

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

Tencent/Shadow 的其他 Issue

查看 Tencent/Shadow 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

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