将 PluginContainerActivity 改为继承 FragmentActivity 时遇到的类加载问题

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

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
20/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
android, java
领域
mobile

调研方向

Start with ActivityCodeGenerator.kt and trace how GeneratedPluginContainerActivity is generated and loaded by the plugin runtime. Then inspect the runtime class loading and dependency handling for AndroidX Fragment and Lifecycle, including the FragmentActivity stubs and key event dispatch changes described in the issue. The issue is done when the inheritance change works in a real host and plugin setup without class resolution or Lifecycle access failures; it does not identify a specific test or provide a settled implementation route.

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

描述

背景

我们有一个需求:宿主会传递一个 WebView 给插件使用,WebView 有时需要启动 Dialog。Dialog 需要 FragmentActivity 或 AppCompatActivity 作为 Context,而当前 PluginContainerActivity 继承自普通 Activity,导致 Dialog 无法正常弹出。

因此我尝试将 PluginContainerActivity 改为继承 FragmentActivity。

已完成的修改

  1. 修改 ActivityCodeGenerator.kt,将 GeneratedPluginContainerActivity 的父类从 Activity 改为 FragmentActivity
  2. 添加 FragmentActivity stub 类用于编译
  3. 处理 ComponentActivity 的 final 方法(如 onRetainNonConfigurationInstance)
  4. 解决 KeyEventDispatcher 导致的无限递归问题(覆写 superDispatchKeyEvent)

遇到的问题

在 sample 项目中测试正常,但在实际项目中遇到以下问题:

问题 1:使用 compileOnly 依赖时类找不到

在 plugin-runtime 中使用 compileOnly 'androidx.fragment:fragment:1.4.1',加载插件时报错:

Failed resolution of: Lcom/tencent/shadow/core/runtime/container/PluginContainerActivity;
问题 2:使用 implementation 依赖时方法不可访问

改为 implementation 'androidx.fragment:fragment:1.4.1' 后,启动时崩溃:

Method 'androidx.lifecycle.LifecycleEventObserver androidx.lifecycle.Lifecycling.lifecycleEventObserver(java.lang.Object)' is inaccessible to class 'androidx.lifecycle.LifecycleRegistry$ObserverWithState' (declaration of 'androidx.lifecycle.LifecycleRegistry$ObserverWithState' appears in /data/app/~~ZsI_dEdXQCfBzCzBCwZ9fg==/com.netease.gl-T_n2e4Q6M7HhQCr2S2jokw==/base.apk)
    at androidx.lifecycle.LifecycleRegistry$ObserverWithState.<init>(LifecycleRegistry.java:353)
    at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:180)

这个错误看起来是宿主和插件的 androidx.lifecycle 类存在版本冲突或类加载器隔离问题。

问题

  1. 我的修改方向是否有问题?作者能给一个大概要修改的类的路线吗?
  2. 如果要让 PluginContainerActivity 继承 FragmentActivity,在 Shadow 框架中还有需要注意哪些地方?

环境信息

  • Shadow 版本:基于 2024-12-25 的 master 分支(commit 09e7e0a6)
  • androidx.fragment 版本:1.4.1
主要语言
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 摘要。