title: Android dex class loader - class not found
body: <p>I want to use a dex class loader to dynamically load code into my Android application. I have followed this tutorial: <a href="http://www.marioalmeida.eu/2015/01/27/how-to-easy-way-load-apk-classes-using-dexclassloader/" rel="nofollow noreferrer">http://www.marioalmeida.eu/2015/01/27/how-to-easy-way-load-apk-classes-using-dexclassloader/</a>. However I get the following error stack trace:</p>
  
  <pre><code>01-08 12:25:38.007 15147-15147/com.ph.dexloader W/System.err: java.lang.ClassNotFoundException: Didn't find class "com.ph.injection.Injection" on path: DexPathList[[zip file "/storage/emulated/0/testjars/injector.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at com.ph.dexloader.MainActivity.test3(MainActivity.java:153)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at com.ph.dexloader.MainActivity$1.onClick(MainActivity.java:34)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at android.view.View.performClick(View.java:5198)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at android.view.View$PerformClick.run(View.java:21147)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at android.os.Looper.loop(Looper.java:148)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:   Suppressed: java.io.IOException: No original dex files found for dex location /storage/emulated/0/testjars/injector.apk
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.DexFile.openDexFileNative(Native Method)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.DexFile.openDexFile(DexFile.java:295)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.DexFile.&lt;init&gt;(DexFile.java:111)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.DexFile.loadDex(DexFile.java:151)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.DexPathList.loadDexFile(DexPathList.java:282)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.DexPathList.makePathElements(DexPathList.java:248)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.DexPathList.&lt;init&gt;(DexPathList.java:120)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.BaseDexClassLoader.&lt;init&gt;(BaseDexClassLoader.java:48)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.DexClassLoader.&lt;init&gt;(DexClassLoader.java:57)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at com.ph.dexloader.MainActivity.test3(MainActivity.java:146)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:       ... 10 more
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:   Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.ph.injection.Injection" on path: DexPathList[[zip file "/data/app/com.ph.dexloader-2/base.apk"],nativeLibraryDirectories=[/data/app/com.ph.dexloader-2/lib/x86, /vendor/lib, /system/lib]]
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:       ... 12 more
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:       Suppressed: java.lang.ClassNotFoundException: Didn't find class "com.ph.injection.Injection" on path: DexPathList[[dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-support-annotations-24.1.0_ae5436f3e75e322599cc28c891c7bbfb3749511e-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-internal_impl-24.1.0_3cdb966acce2459aa95bcd6df4240d298e8a6aa0-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-com.android.support-support-vector-drawable-24.1.0_68483e445bfd7fe2615d6637e23aa54f7eca71ad-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-com.android.support-support-v4-24.1.0_0b01882602c790d27bb53e6d99c19f41bfa0f42f-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-com.android.support-appcompat-v7-24.1.0_ffbd2666f53be3072a55297d806cbb863283a31e-classes.dex", dex file "/data/data/com.ph.dexloader/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-24.1.0_fa7ca9f78df22fc1c453fed6517df1a2457fbf73-classes.dex"],nativeLibraryDirectories=[/data/app/com.ph.dexloader-2/lib/x86, /vendor/lib, /system/lib]]
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at com.android.tools.fd.runtime.IncrementalClassLoader.findClass(IncrementalClassLoader.java:62)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:           ... 13 more
  01-08 12:25:38.008 15147-15147/com.ph.dexloader W/System.err:           Suppressed: java.lang.ClassNotFoundException: com.ph.injection.Injection
  01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err:     at java.lang.Class.classForName(Native Method)
  01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err:     at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
  01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err:     at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
  01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err:     at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
  01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err:               ... 14 more
  01-08 12:25:38.009 15147-15147/com.ph.dexloader W/System.err:           Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
  </code></pre>
  
  <p>I created an Android project with no Activity and the following class that I want to load:</p>
  
  <pre><code>package com.ph.injection;
  
  import android.util.Log;
  
  public class Injection {
  
      public void inject() {
          /* this code is getting injected */
          Log.d("INJECTED", "This is injected code!");
      }
  }
  </code></pre>
  
  <p>I generated the APK file with gradle assemble and put it into /storage/emulated/0/testjars/injector.apk on the Emulator.</p>
  
  <p>This is the code I use to load the DEX file:</p>
  
  <pre><code>    String dexFile = "/testjars/injector.apk";
  
      // Get the path to the SD card
      File f = new File(Environment.getExternalStorageDirectory().toString() + dexFile);
      final File optimizedDexOutputPath = getDir("outdex", 0);
      DexClassLoader classLoader = new DexClassLoader(f.getAbsolutePath(), optimizedDexOutputPath.getAbsolutePath(),null, getClassLoader());
  
      // The classpath is created for the new class
      String completeClassName = "com.ph.injection.Injection";
      String methodToInvoke = "inject";
  
      try {
          Class&lt;?&gt; myClass = classLoader.loadClass(completeClassName);
          Object obj = (Object) myClass.newInstance();
          Method m = myClass.getMethod(methodToInvoke);
          m.invoke(obj);
      }
      catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  
  <p>I have no idea why the DEX file is not loaded correctly / the class is not found. Could anybody help me? I use a Genymotion Android 6.0.0 emulator if that matters.</p>
  
  <p>Thanks!</p>