title: How to load a Java class dynamically on android/dalvik?
body: <p>I'm wondering if and how one can load dex or class files dynamically 
  in dalvik, some quick'n'dirty test function I wrote was this: </p>
  
  <pre><code>    public void testLoader() { 
              InputStream in; 
              int len; 
              byte[] data = new byte[2048]; 
              try { 
                      in = context.getAssets().open("f.dex"); 
                      len = in.read(data); 
                      in.close(); 
                      DexFile d; 
                      Class c = defineClass("net.webvm.FooImpl", data, 0, len); 
                      Foo foo = (Foo)c.newInstance(); 
              } catch (IOException e1) { 
                      // TODO Auto-generated catch block 
                      e1.printStackTrace(); 
              } catch (IllegalAccessException e) { 
                      // TODO Auto-generated catch block 
                      e.printStackTrace(); 
              } catch (InstantiationException e) { 
                      // TODO Auto-generated catch block 
                      e.printStackTrace(); 
              } 
      } 
  </code></pre>
  
  <p>whereas the Foo interface is this </p>
  
  <pre><code>    public interface Foo { 
              int get42(); 
      } 
  </code></pre>
  
  <p>and f.dex contains some dx'ed implementation of that interface: </p>
  
  <pre><code>    public class FooImpl implements Foo { 
              public int get42() { 
                      return 42; 
              } 
      } 
  </code></pre>
  
  <p>The above test driver throws at defineClass() and it doesn't 
  work and I investigated the dalvik code and found this: </p>
  
  <p><a href="http://www.google.com/codesearch/p?hl=en#atE6BTe41-M/vm/Jni.c&amp;q=Jni.c" rel="noreferrer">http://www.google.com/codesearch/p?hl=en#atE6BTe41-M/vm/Jni.c&amp;q=Jni.c</a>... </p>
  
  <p>So I'm wondering if anyone can enlighten me if this is possible in 
  some other way or not supposed to be possible. If it is not possible, 
  can anyone provide reasons why this is not possible?</p>