I am creating a home automation application. I am trying to add a system plugin. As a test, I exported a test class (this Button subclasses) as an APK file and placed it in the application file directory. I was able to create a new instance of this class and put it in my view using DexClassLoaderand .loadClass.
The next step is to scan the entire APK in this directory and get the class names in them.
I found the DexFile class that does just that, however it throws the following exceptions:
04-18 17:26:15.697: E/dalvikvm(726): Can't open dex cache '/data/dalvik-cache/data@data@com.strutton.android.testplugin@files@testloadclass.apk@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@data@com.strutton.android.testplugin@files@testloadclass.apk@classes.dex)
It would seem that he is trying to find optimized DexFile in the system cache, but I do not have rights to the cache directory. From what I see, this is probably by design, and I have no problem with that. Is there any other way to parse a DEX file and get class names from it?
I reviewed the source of some dex decompiler projects. Do I need to minimize my own solution?
Here is my application test code (from my Activity OnCreate) just in case I missed something:
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);
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();
}
source
share