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

Abierto
#1,406 4 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
20/100
Tipo de issue
Nueva funcionalidad
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
android, java
Área
mobile

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

背景

我们有一个需求:宿主会传递一个 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
Lenguaje dominante
Java
Estrellas
7.8k
Forks
1.3k
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Tencent/Shadow

Todos los issues de Tencent/Shadow

Issues similares

Más issues de Java

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.