0

アプリケーションの一部としてメモリにインストールされていないコードを実行できるようにしたいと考えています。InMemoryDe​​xClassLoader はまさにこのために作成されたと想定したので、それを使用して、同じアプリケーション (同じクラスでも) でメモリからメソッドを実行しようとしました。そのために、APK 自体をバッファーにロードし、そのバッファーを InMemoryDe​​xClassLoader にフィードします。ただし、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();
    }
}

DexClassLoader でまったく同じことをするとうまくいきます! 誰が何が悪いのか理解できますか?

//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();
    }
}
4

1 に答える 1