在Android 5.0上动态加载DEX文件 [英] Load DEX file dynamically on Android 5.0
问题描述
在Android 5.0之前,我能够使用DexClassLoader动态加载DEX文件并调用 loadClass()
方法,但是在最新的Android版本中,我得到了 ClassNotFoundException
。
Prior to Android 5.0 I was able to load DEX files dynamically using DexClassLoader and calling loadClass()
method but with the latest Android version I get a ClassNotFoundException
.
这是我在做什么:
-
生成DEX文件。
Generate DEX file.
../android-sdk/android-sdk-linux_86/build-tools/21.1.1/dx --dex --output=bin/output.dex bin/output.jar
创建一个DexClassLoader。
Create a DexClassLoader.
DexClassLoader cl = new DexClassLoader(
dexFile.getAbsolutePath(),
odexFile.getAbsolutePath(),
null,
mContext.getClassLoader());
调用 cl.loadClass( myMethod);
我知道ART使用dex2oat生成由ART加载的ELF文件但在第2步中,我正在生成ODEX文件,因此我不需要在ART中完成在运行时加载DEX文件的操作,有人可以帮助我吗?
I am aware that ART uses dex2oat to generate an ELF file that is the loaded by ART but in step 2 I am generating an ODEX file so I am not what needs to be done in ART to load a DEX file at runtime, can anyone help me ?
推荐答案
更新
这对Dalvik和ART均有效: new DexClassLoader(jarredDex.getAbsolutePath(),上下文。 getDir( outdex,Context.MODE_PRIVATE).getAbsolutePath(),null,context.getClassLoader());
其中, jarredDex
是一个jar- classes.dex
的文件。可以通过运行 dx --dex --output = filename.jar your / classes / dir
来获得Jar。
Update
This works both on Dalvik and ART: new DexClassLoader(jarredDex.getAbsolutePath(), context.getDir("outdex", Context.MODE_PRIVATE).getAbsolutePath(), null, context.getClassLoader());
where jarredDex
is a jar-file with classes.dex
. Jar can be obtained by running dx --dex --output=filename.jar your/classes/dir
.
我已经从本文。但是ART使用 PathClassLoader
代替了Dalvik的 DexClassLoader
。这段代码已经在Android 6的模拟器上以及在Android 5.1的小米上进行了测试,并且可以正常工作:
I've took a code sample from this article. But ART uses PathClassLoader
instead of Dalvik's DexClassLoader
. This code is tested on emulator with Android 6 and on Xiaomi with Android 5.1 and works fine:
// Before the secondary dex file can be processed by the DexClassLoader,
// it has to be first copied from asset resource to a storage location.
File dexInternalStoragePath = new File(getDir("dex", Context.MODE_PRIVATE), SECONDARY_DEX_NAME);
try (BufferedInputStream bis = new BufferedInputStream(getAssets().open(SECONDARY_DEX_NAME));
OutputStream dexWriter = new BufferedOutputStream(new FileOutputStream(dexInternalStoragePath))) {
byte[] buf = new byte[BUF_SIZE];
int len;
while((len = bis.read(buf, 0, BUF_SIZE)) > 0) {
dexWriter.write(buf, 0, len);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
PathClassLoader loader = new PathClassLoader(dexInternalStoragePath.getAbsolutePath(), getClassLoader());
Class<?> toasterClass = loader.loadClass("my.package.ToasterImpl");
Toaster toaster = (Toaster) toasterClass.newInstance();
toaster.show(this, "Success!");
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
这篇关于在Android 5.0上动态加载DEX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!