我正在构建一个家庭自动化应用程序。我正在尝试添加一个插件系统。作为测试,我将测试类(Button 的子类)导出为 APK 文件,并将其放入我的应用程序的文件目录中。我能够创建此类的新实例并将其放入我的视图中使用DexClassLoader
and .loadClass
.
下一步是扫描该目录中的所有 APK 并获取其中的类名称。
我发现 DexFile 类可以做到这一点,但是它会抛出以下异常:
04-18 17:26:15.697: E/dalvikvm(726): Can't open dex cache '/data/dalvik-cache/data@[email protected] /cdn-cgi/l/email-protection@[email protected] /cdn-cgi/l/email-protection@classes.dex': No such file or directory
04-18 17:26:15.705: I/dalvikvm(726): Unable to open or create cache for /data/data/com.strutton.android.testplugin/files/testloadclass.apk (/data/dalvik-cache/data@[email protected] /cdn-cgi/l/email-protection@[email protected] /cdn-cgi/l/email-protection@classes.dex)
它似乎试图在系统缓存中找到优化的DexFile,但我没有缓存目录的权限。据我所知,这可能是设计使然,我对此没有任何问题。是否有另一种方法来解析 DEX 文件并从中获取类名?
我查看了一些 dex 反编译器项目的源代码。我必须推出自己的解决方案吗?
这是我的测试应用程序代码(来自我的 Activity OnCreate),以防万一我错过了某些内容:
try {
ArrayList<String> UIPlugins = new ArrayList<String>();
final File filesDir = this.getFilesDir();
final File tmpDir = getDir("dex", 0);
final DexClassLoader classloader = new DexClassLoader( filesDir.getAbsolutePath()+"/testloadclass.apk",
tmpDir.getAbsolutePath(),
null, this.getClass().getClassLoader());
final Class<Button> classToLoad =
(Class<Button>) classloader.loadClass("com.strutton.android.testloadclass.MyTestClass_IRDroidUIPlugIn");
Button mybutton = classToLoad.getDeclaredConstructor(Context.class).newInstance(this);
mybutton.setId(2);
mybutton.setOnClickListener(this);
main.addView(mybutton);
// Up to here everything works as expected
// This line throws the exceptions
DexFile mDexFile = new DexFile(filesDir.getAbsolutePath()+"/testloadclass.apk";);
Enumeration<String> classNames = mDexFile.entries();
while (classNames.hasMoreElements()){
String className = classNames.nextElement();
if (className.endsWith("IRDroidUIPlugIn")){
UIPlugins.add(className);
}
}
final Class<Button> classToLoad =
(Class<Button>) classloader.loadClass("com.strutton.android.testloadclass.MyTestClass_IRDroidUIPlugIn");
Button mybutton = classToLoad.getDeclaredConstructor(Context.class).newInstance(this);
mybutton.setId(2);
mybutton.setOnClickListener(this);
main.addView(mybutton);
btn.setText(tmpDir.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}