Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to load a class from a byte array in android?
    primarykey
    data
    text
    <p>First off I have seen <a href="https://stackoverflow.com/questions/3174436/load-java-byte-code-at-runtime">Load Java-Byte-Code at Runtime</a> and it was helpful in so far as getting to me to the same spot I'm stuck at currently. </p> <p>I'm trying to load a class from a byte array to avoid storing a file on disk. For testing purpose in this example I am simply reading in a .class file into a byte array so obviously the file is still stored on disk, but it is just to see if the code can work.</p> <p>I take this byte array and then use a custom ClassLoader with the method loadClass to load a Class, but it doesn't work. </p> <pre><code> byte[] bytearray = null; try{ RandomAccessFile f = new RandomAccessFile("/sdcard/ClassToGet.dex", "r"); bytearray = new byte[(int) f.length()]; f.read(bytearray); MyClassLoader classloader = new MyClassLoader(); classloader.setBuffer(bytearray); classloader.loadClass("com.pack.ClassIWant"); } </code></pre> <p>Here is the ClassLoader implementation:</p> <pre><code>public class MyClassLoader extends DexClassLoader { private byte[] buffer; @Override public Class findClass(String className){ byte[] b = getBuffer(); return this.defineClass(className, b, 0, b.length); } public void setBuffer(byte[] b){ buffer = b; } public byte[] getBuffer(){ return buffer; } </code></pre> <p>And the error I am receiving is this: </p> <p>java.lang.UnsupportedOperationException: can't load this type of class file at java.lang.VMClassLoader.defineClass(Native Method)</p> <p>I have supplied it with .class files, .dex files, .apk, .jar, etc... I have no idea what "type of class file" it wants from me and the documentation on it is nonexistent. Any help would be great I've been trying to get this work for four days straight. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload