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

Đang mở
#1,369 4 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
android, java
Lĩnh vực
backend, mobile

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

问题描述:

我们在使用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

Ngôn ngữ chính
Java
Star
7.8k
Fork
1.3k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Tencent/Shadow

Tất cả issue của Tencent/Shadow

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.