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

オープン
#1,406 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
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 はありません

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Tencent/Shadow のほかの issue

Tencent/Shadow の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。