【问题标题】:How to access classes.dex of an Android Application?如何访问 Android 应用程序的 classes.dex?
【发布时间】:2012-04-24 18:14:50
【问题描述】:

启动活动时,系统会加载 classes.dex 文件并开始执行指令。我需要获得对执行当前活动的同一应用程序的 classes.dex 的只读访问权限。

在网上搜索了几个小时后,我只能推断 Android 安全系统不允许访问应用程序沙箱。

但是,我需要对 classes.dex 文件进行只读访问才能完成我的任务。

有人对此有见解吗?

提前致谢!

【问题讨论】:

  • 你能做到吗?我需要同样的东西,但找不到任何线索。如果您能指导我完成此过程,那将是非常有帮助的。

标签: android directory installation


【解决方案1】:

取决于你想要做什么,但你可以访问 DexFile :

String sourceDir = context.getApplicationInfo().sourceDir;
DexFile dexFile = new DexFile(sourceDir);

它为您提供了一个http://developer.android.com/reference/dalvik/system/DexFile.html,您可以从中枚举和加载类。

【讨论】:

  • 基本上,我正在尝试计算我需要访问文件的 InputStream 的 classes.dex 的 MD5 哈希。谢谢你的领导!!
  • 您可能仍然可以查看 ApplicationInfo.sourceDir。 Dex 文件可能在那里(如果是目录,则在目录中)。
  • 小问题,不过,你需要 md5 做什么?
【解决方案2】:

您可以通过以下方式获取“classes.dex”的 InputStream:

  1. 检索应用程序的 apk 容器的路径。
  2. 借助 JarFile 类,在您的 apk 容器中检索“classes.dex”条目。
  3. 为其获取输入流。

这里有一段代码来举例说明:

        // Get the path to the apk container.
        String apkPath = getApplicationInfo().sourceDir;
        JarFile containerJar = null;

        try {

            // Open the apk container as a jar..
            containerJar = new JarFile(apkPath);

            // Look for the "classes.dex" entry inside the container.
            ZipEntry ze = containerJar.getEntry("classes.dex");

            // If this entry is present in the jar container 
            if (ze != null) {

                 // Get an Input Stream for the "classes.dex" entry
                 InputStream in = containerJar.getInputStream(ze);

                 // Perform read operations on the stream like in.read();
                 // Notice that you reach this part of the code
                 // only if the InputStream was properly created;
                 // otherwise an IOException is raised
            }   

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (containerJar != null)
                try {
                    containerJar.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 2016-04-01
    相关资源
    最近更新 更多