我正在尝试构建一个插件系统,其中DexClassLoader正在从其他安装的包含片段的apks (我的插件)中获取代码,并在我的主机上显示它们。这工作很不错。
我也喜欢使插件热插拔,这意味着我可以改变代码从一个插件,安装它新的,主机会注意到,并将载入新的代码。如果我第一次改变代码的话,这也是有效的。(虽然我认为不应该这样做,但我似乎对这段代码有了错误的理解:
try {
requiredClass = Class.forName(fullName);
} catch(ClassNotFoundException e) {
isLoaded = false;
})
如果我第二次尝试使用相同的插件,主机就会在requiredClass = classLoader.loadClass(fullName);上关闭,比如
libc致命信号7 (SIGBUS)在0x596ed4d6 (code=2),线程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;
}事先非常感谢!
发布于 2014-12-15 15:42:47
这并不是说它真的很重要(因为实际上没有人看这个),或者我甚至了解发生了什么,但是在重新加载之前删除dexTemp.getAbsolutePath()中插件的相应文件就解决了这个问题。
PS:卷尾草-徽章,YAY!
https://stackoverflow.com/questions/27299476
复制相似问题