title: Android : load(getClass().getClassLoader(),classLoadingStrategy) throws java.lang.IllegalStateException
body: <p>Im trying to create byteBuddy object under method proxy(...) in Android and then call on the byteBuddy object some methods:</p>
  
  <pre><code>&lt;T&gt; T proxy(Class&lt;T&gt; clz, InvocationHandler invocationHandler) {
      ByteBuddy byteBuddy = null;
      try {
          byteBuddy = new ByteBuddy(ClassFileVersion.JAVA_V6);
      } catch (Throwable e) {
          //e.printStackTrace();
      }
      if(byteBuddy!=null) {
          Class&lt;?&gt; enhanced = byteBuddy
                  .subclass(clz, ConstructorStrategy.Default.IMITATE_SUPER_TYPE)
                  .method(ElementMatchers.not(ElementMatchers.isDeclaredBy(Object.class)))
                  .intercept(InvocationHandlerAdapter.of(invocationHandler))
                  .make().load(getClass().getClassLoader(), classLoadingStrategy)
                  .getLoaded();
  
          ...
  </code></pre>
  
  <p>But on line with:</p>
  
  <pre><code>load(getClass().getClassLoader(), classLoadingStrategy)
  </code></pre>
  
  <p>ByteBuddy throws exception :</p>
  
  <blockquote>
    <p>06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ java.lang.IllegalStateException: Cannot load class
    pdl.transport.overlay.fissione.FissioneTransport$FissioneHandler$ByteBuddy$vhLwLk79
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    net.bytebuddy.android.AndroidClassLoadingStrategy.load(AndroidClassLoadingStrategy.java:138)
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    net.bytebuddy.dynamic.DynamicType$Default$Unloaded.load(DynamicType.java:3380)
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at pdl.transport.Messenger.proxy(Messenger.java:320)
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at pdl.transport.Messenger.async(Messenger.java:382)
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at pdl.transport.Messenger.async(Messenger.java:373)
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    pdl.transport.overlay.fissione.FissioneTransport.join(FissioneTransport.java:221)
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    pdl.transport.overlay.fissione.FissioneTransport.open(FissioneTransport.java:202)
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at pdl.transport.overlay.util.DHT.(DHT.java:37)
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at pdl.transport.overlay.util.DHT.main(DHT.java:117)
    06-11 21:29:23.351  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    com.example.AndroidOverlay.MyActivity_newbie.onCreate(MyActivity_newbie.java:72)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    android.app.Activity.performCreate(Activity.java:6289) 06-11
    21:29:23.361  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    android.app.ActivityThread.access$900(ActivityThread.java:177) 06-11
    21:29:23.361  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at android.os.Looper.loop(Looper.java:145) 06-11
    21:29:23.361  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    android.app.ActivityThread.main(ActivityThread.java:5942) 06-11
    21:29:23.361  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    java.lang.reflect.Method.invoke(Native Method) 06-11 21:29:23.361 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    java.lang.reflect.Method.invoke(Method.java:372) 06-11 21:29:23.361 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 06-11
    21:29:23.361  12028-12028/com.example.AndroidOverlay W/System.err﹕
    Caused by: java.lang.ClassNotFoundException: Didn't find class
    "pdl.transport.overlay.fissione.FissioneTransport$FissioneHandler$ByteBuddy$vhLwLk79"
    on path: DexPathList[[zip file
    "/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar"],nativeLibraryDirectories=[/vendor/lib,
    /system/lib]] 06-11 21:29:23.361 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    net.bytebuddy.android.AndroidClassLoadingStrategy.load(AndroidClassLoadingStrategy.java:136)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ ... 22 more 06-11 21:29:23.361 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ Suppressed:
    java.io.IOException: Failed to open dex file
    '/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar' from
    memory: Unrecognized version number in
    /data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar: 0 3 6
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at dalvik.system.DexFile.openDexFileNative(Native
    Method) 06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at dalvik.system.DexFile.openDexFile(DexFile.java:295)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at dalvik.system.DexFile.(DexFile.java:111) 06-11
    21:29:23.361  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    dalvik.system.DexFile.loadDex(DexFile.java:151) 06-11 21:29:23.361 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    dalvik.system.DexPathList.loadDexFile(DexPathList.java:265) 06-11
    21:29:23.361  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    dalvik.system.DexPathList.makeDexElements(DexPathList.java:231) 06-11
    21:29:23.361  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    dalvik.system.DexPathList.(DexPathList.java:109) 06-11
    21:29:23.361  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    dalvik.system.BaseDexClassLoader.(BaseDexClassLoader.java:48)
    06-11 21:29:23.361  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    dalvik.system.DexClassLoader.(DexClassLoader.java:57) 06-11
    21:29:23.371  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    net.bytebuddy.android.AndroidClassLoadingStrategy$DexProcessor$ForSdkCompiler.makeClassLoader(AndroidClassLoadingStrategy.java:257)
    06-11 21:29:23.371  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at
    net.bytebuddy.android.AndroidClassLoadingStrategy.load(AndroidClassLoadingStrategy.java:132)
    06-11 21:29:23.371  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ ... 22 more 06-11 21:29:23.371 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ Caused by:
    java.io.IOException: Failed execv(/system/bin/dex2oat --runtime-arg
    -classpath --runtime-arg  --instruction-set=arm --instruction-set-features=div --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --dex-file=/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar
    --oat-fd=64 --art-fd=-1 --oat-location=/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.dex
    --runtime-arg -Xms64m --runtime-arg -Xmx512m) because non-0 exit status 06-11 21:29:23.371  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ ... 33 more 06-11 21:29:23.371 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ Caused by:
    java.io.IOException: Failed to find dex file
    '/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.jar' in
    oat location
    '/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.dex':
    Failed to find existing oat file at
    /data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.dex: File
    size of 0 bytes not large enough to contain ELF header of 52 bytes:
    '/data/data/com.example.AndroidOverlay/app_TnUR5LUb/1Gzh1FCI.dex'
    06-11 21:29:23.371  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ ... 33 more 06-11 21:29:23.371 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ Suppressed:
    java.lang.ClassNotFoundException: Didn't find class
    "pdl.transport.overlay.fissione.FissioneTransport$FissioneHandler$ByteBuddy$vhLwLk79"
    on path: DexPathList[[zip file
    "/data/app/com.example.AndroidOverlay-1/base.apk"],nativeLibraryDirectories=[/vendor/lib,
    /system/lib]] 06-11 21:29:23.371 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    06-11 21:29:23.371  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    06-11 21:29:23.371  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
    06-11 21:29:23.371  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ ... 24 more 06-11 21:29:23.371 
    12028-12028/com.example.AndroidOverlay W/System.err﹕ Suppressed:
    java.lang.ClassNotFoundException:
    pdl.transport.overlay.fissione.FissioneTransport$FissioneHandler$ByteBuddy$vhLwLk79
    06-11 21:29:23.371  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ at java.lang.Class.classForName(Native Method) 06-11
    21:29:23.371  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    java.lang.BootClassLoader.findClass(ClassLoader.java:781) 06-11
    21:29:23.371  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    java.lang.BootClassLoader.loadClass(ClassLoader.java:841) 06-11
    21:29:23.371  12028-12028/com.example.AndroidOverlay W/System.err﹕ at
    java.lang.ClassLoader.loadClass(ClassLoader.java:504) 06-11
    21:29:23.371  12028-12028/com.example.AndroidOverlay W/System.err﹕ ...
    25 more 06-11 21:29:23.371  12028-12028/com.example.AndroidOverlay
    W/System.err﹕ Caused by: java.lang.NoClassDefFoundError: Class not
    found using the boot class loader; no stack available</p>
  </blockquote>
  
  <p>Im using dx-1.7.jar from <a href="http://central.maven.org/maven2/com/google/android/tools/dx/1.7/dx-1.7.jar" rel="nofollow">http://central.maven.org/maven2/com/google/android/tools/dx/1.7/dx-1.7.jar</a>
  and byte-buddy-0.6.8.jar,byte-buddy-android-0.6.8.jar </p>
  
  <p>Am I missing something? I get also Could not run Dynamic type.(Failed resolution of:  Lorg/objectweb/asmType;) on yours android example application, it seems like Im missing something, some library or I dont know...
  Thank you for answers.</p>
  
  <p><strong>EDIT1:</strong>
  Im using Lollipop and this is how I setup AndroidClassLoadingStrategy:</p>
  
  <pre><code>final File dir = this.getDir("dexgen", Context.MODE_PRIVATE);
      Messenger.setClassLoadingStrategy(new AndroidClassLoadingStrategy(dir));
  </code></pre>
  
  <p>which sets to class Messenger where I have method proxy(...)</p>
  
  <pre><code>public static void setClassLoadingStrategy(ClassLoadingStrategy cls) {
      classLoadingStrategy = cls;
  }
  </code></pre>
  
  <p>and classLoadingStrategy is defined as</p>
  
  <pre><code>private static ClassLoadingStrategy classLoadingStrategy;
  </code></pre>
  
  <p><strong>Edit 2:</strong>
  After All I took ByteBuddy example application, tried lines for Android lollipop replacing</p>
  
  <pre><code>File file = TestActivity.this.getDir(RandomString.make(), Context.MODE_PRIVATE);
  </code></pre>
  
  <p>for one of theese which should be replacement, tried them all and all gives same exception, just from different class-activity</p>
  
  <pre><code>File file = getCodeCacheDir(); //NOT WORKING
  // File file = getApplicationContext().getCodeCacheDir(); //NOT WORKING
  // File file = getBaseContext().getCodeCacheDir(); //NOT WORKING
  // File file = TestActivity.this.getCodeCacheDir(); //NOT WORKING
  </code></pre>
  
  <p>Exception still:</p>
  
  <pre><code>06-12 23:18:57.916    1947-1947/net.bytebuddy.android.test W/net.bytebuddy﹕ java.lang.IllegalStateException: Cannot load class net.bytebuddy.renamed.java.lang.Object$ByteBuddy$uSYJ5787$auxiliary$MBywjCuh
  </code></pre>
  
  <p>I created and putted on my website sources for future possibility to reproduce error here:</p>
  
  <p><a href="http://bashism.com/shared/ByteBuddyTest.tar.gz" rel="nofollow">bashism.com/shared/ByteBuddyTest.tar.gz</a></p>
  
  <p><strong>Edit 3:</strong></p>
  
  <p>Android version: 5.0.1 Lollipop</p>
  
  <p>Device: Samsung galaxy S4</p>
  
  <p>IDE: IntelliJ Idea 14.1.3</p>
  
  <p><strong>Edit 4:</strong>
  After changing inside Main Activity:</p>
  
  <pre><code>File file = TestActivity.this.getDir(RandomString.make(), Context.MODE_PRIVATE);
  </code></pre>
  
  <p>and inside net.bytebuddy.android.AndroidClassLoadingStrategy</p>
  
  <pre><code>public ForSdkCompiler(DexOptions dexFileOptions, CfOptions dexCompilerOptions) {
  dexFileOptions.targetApiLevel = 13;
  this.dexFileOptions = dexFileOptions;
  this.dexCompilerOptions = dexCompilerOptions;
  </code></pre>
  
  <p>}</p>
  
  <p>Seems working/stable, further debugging soon</p>