I've successfully used DexClassLoader. It's important to provide a dexOutputDir
that is actually writeable by your app, so not /data/dalvik-cache
. Otherwise the log will show one or two lines about failing to write there, followed by ClassNotFoundException
.
cl = new DexClassLoader("/full/path/com.example.apk",
getFilesDir().getAbsolutePath(),// /data/data/foo/files
null, // native lib path, I haven't used this
MyClass.class.getClassLoader());
// This doesn't make Class.forName() work, instead I do this:
Class<?> foo = cl.loadClass("com.example.foo");
To make Class.forName()
work, you could try Thread.setContextClassLoader() (I haven't).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…