【问题标题】:Load DEX file dynamically on Android 5.0在 Android 5.0 上动态加载 DEX 文件
【发布时间】:2014-12-07 23:24:08
【问题描述】:

在 Android 5.0 之前,我能够使用 DexClassLoader 动态加载 DEX 文件并调用 loadClass() 方法,但在最新的 Android 版本中,我得到了 ClassNotFoundException

这是我正在做的事情:

  1. 生成 DEX 文件。

    ../android-sdk/android-sdk-linux_86/build-tools/21.1.1/dx --dex --output=bin/output.dex  bin/output.jar
    
  2. 创建一个 DexClassLoader。

    DexClassLoader cl = new DexClassLoader(
    dexFile.getAbsolutePath(),
    odexFile.getAbsolutePath(),
    null,
    mContext.getClassLoader());
    
  3. 致电cl.loadClass("myMethod");

我知道 ART 使用 dex2oat 生成一个由 ART 加载的 ELF 文件,但在第 2 步中,我正在生成一个 ODEX 文件,因此我不需要在 ART 中完成在运行时加载 DEX 文件的操作,谁能帮帮我?

【问题讨论】:

  • 为什么需要在运行时加载 DEX 文件? 5.0原生支持多个dex文件。
  • DEX文件有敏感信息,在assets目录下加密。当我需要使用它时,它被解密然后在运行时加载。
  • @garibay 你设法解决了这个问题吗?我遇到了同样的问题,这只适用于我的 Dalvik。
  • 这里有什么消息吗?我假设不再可能在较新的 Android 版本上加载动态代码(缺少 OpenDEXfile(byte[] ...)。有人知道不同的解决方案吗?
  • 在 android6 marshmallow 上自然是同样的问题。我认为注入代码运行时非常重要的概念,例如基于插件的应用程序,而不仅仅是与 multidex 问题有关。很遗憾在互联网上找不到任何解决方案

标签: android classloader dex android-runtime dex2oat


【解决方案1】:

更新

这适用于 Dalvik 和 ART:new DexClassLoader(jarredDex.getAbsolutePath(), context.getDir("outdex", Context.MODE_PRIVATE).getAbsolutePath(), null, context.getClassLoader()); 其中jarredDex 是带有classes.dex 的jar 文件。运行dx --dex --output=filename.jar your/classes/dir即可获取jar。


原答案

我从this article 获取了一个代码示例。但是 ART 使用 PathClassLoader 而不是 Dalvik 的 DexClassLoader。此代码在 Android 6 的模拟器和 Android 5.1 的小米上进行了测试,并且运行良好:

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

【讨论】:

【解决方案2】:

是制作dex文件的问题。如下更改命令,然后将适用于所有版本的android。

dx.bat --dex --output path/output.jar  path/input.jar

以上方法在 windows 中会生成一个 .jar 文件,该 jar 文件中包含 classes.dex 文件。 DexClassLoader 正在内部搜索 classes.dex 文件,这就是它抛出 ClassNotFoundException 的原因

【讨论】:

猜你喜欢
  • 2018-02-10
  • 2013-12-18
  • 1970-01-01
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 2010-09-23
相关资源
最近更新 更多