I'd like to be able to run code not installed as part of an application in memory. I assumed InMemoryDexClassLoader was created precisely for this, so I tried using it to execute a method in the same application (same class, even), but from memory. To do so, I load the APK itself into a buffer, and feed that buffer to InMemoryDexClassLoader. However, I get a ClassNotFoundException.
public class Test {
public void loadSelf(Context c) {
try {
FileInputStream fis = new FileInputStream(c.getApplicationInfo().publicSourceDir);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
byte[] buffer = new byte[1024];
while ((bytesRead = fis.read(buffer, 0, buffer.length)) != -1) {
baos.write(buffer, 0, bytesRead);
}
baos.flush();
byte[] dex = baos.toByteArray();
ByteBuffer bb = ByteBuffer.allocate(dex.length);
bb.put(dex);
bb.position(0);
ClassLoader loader = new InMemoryDexClassLoader(bb, null);
Class thisClass = loader.loadClass(this.getClass().getName()); //ClassNotFoundException
Method method = thisClass.getMethod("sayHi", Context.class);
method.invoke(thisClass.newInstance(), c);
bb.clear();
return;
} catch (Exception e) {
e.printStackTrace();
}
}
public void sayHi(Context c) {
Toast.makeText(c, "Hi!", Toast.LENGTH_LONG).show();
}
}
Doing the exact same thing with DexClassLoader works fine! Can anyone understand what's wrong?
//This works fine and shows the Toast
public class Test {
public void loadSelf(Context c) {
try {
ClassLoader loader = new DexClassLoader(c.getApplicationInfo().publicSourceDir, null, null, null);
Class thisClass = loader.loadClass(this.getClass().getName());
Method method = thisClass.getMethod("sayHi", Context.class);
method.invoke(thisClass.newInstance(), c);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
public void sayHi(Context c) {
Toast.makeText(c, "Hi!", Toast.LENGTH_LONG).show();
}
}