android - DexClassLoader,重新加载代码失败,信号 7

标签 android plugins classloader dexclassloader

我正在尝试构建一个插件系统,其中 DexClassLoader 从其他已安装的包含 fragment (我的插件)的 apk 中获取代码,并在我的主机中显示它们。这工作得很好。

我也喜欢让插件热插拔,这意味着我可以从插件更改代码,安装新的,主机会注意到并加载新代码。如果我是第一次更改代码,这也有效。 (虽然我认为不应该,但似乎我对这段代码的理解有误:

try {
 requiredClass = Class.forName(fullName);
 } catch(ClassNotFoundException e) {
 isLoaded = false;
 }

)

如果我再次尝试使用同一个插件,主机会在 requiredClass = classLoader.loadClass(fullName); 处关闭,类似

libc Fatal signal 7 (SIGBUS) at 0x596ed4d6 (code=2), thread 28814 (ctivityapp.host)

有没有人对 DexClassLoader 的功能有更深入的了解,并且可以告诉我,这里发生了什么?我完全坚持这一点。

加载外部代码的方法的完整代码如下:

     /**
     * takes the name of a package as String, and tries to load the code from the corresponding akp using DexclassLaoder. 
     * Checking if a package is a valid plugin must be done before calling this. 
     * The Plugin must contain a public class UI that extends Fragment and implements plugin as a starting point for loading
     * @param packageName The full name of the package, as String
     * @return the plugins object if loaded, null otherwise
     */
    private Plugin attachPluginToHost(String packageName) {
        try {
            Class<?> requiredClass = null;
            final ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,0);
            final String apkPath = info.sourceDir;
            final File dexTemp = context.getDir("temp_folder", 0);
            final String fullName = packageName + ".UI";
            boolean isLoaded = true;
            // Check if class loaded
            try {
                requiredClass = Class.forName(fullName);
            } catch(ClassNotFoundException e) {
                isLoaded = false;
            }
            if (!isLoaded) {
                final DexClassLoader classLoader = new DexClassLoader(apkPath, dexTemp.getAbsolutePath(), null, context.getApplicationContext().getClassLoader());
                requiredClass = classLoader.loadClass(fullName);
            }
            if (null != requiredClass) {
               // Try to cast to required interface to ensure that it's can be cast
                final Plugin plugin = Plugin.class.cast(requiredClass.newInstance());
                installedPlugins.put(plugin.getName(), plugin);
                return plugin;
            }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return null;
    }

非常感谢!

最佳答案

这并不重要(因为实际上没有人在看这个),或者我什至理解发生了什么,但是在重新加载之前删除 dexTemp.getAbsolutePath() 中插件的相应文件解决了问题。

PS:风滚草徽章,耶!

关于android - DexClassLoader,重新加载代码失败,信号 7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27299476/

相关文章:

java - URLClassLoader 无法在 Linux 上加载依赖项

android - 我在Android 10中收到IMEI编号错误

android - 在android中设置超时对话框?

java - 如何设置 hh 时间 :mm format in TimePicker Dialog.

reactjs - React Draft.js 工具栏插件未显示

file-io - run-app 和 test-app 之间的 Griffon 资源加载差异

android - 如何在 SwipeRefreshLayout 中设置增量

wordpress - 如何修复联系表单和多步骤中的此错误

ViM:minibufexpl 与 bufexplorer 插件

java - 什么会导致 Java 类被初始化?