How to access classes.dex of an Android Application?

好久不见. 提交于 2020-01-23 01:08:08

问题


When an activity is launched, the classes.dex file is loaded by the system and start executing the instructions. I need to get readonly access to the classes.dex of the same application under which the current activity is executing.

After searching for hours on the net, I could only infer that the Android Security system does not allow access to the application sandbox.

However, i need readonly access to the classes.dex file in order to accomplish my task.

Does anyone have a insight on this?

Thanks in advance!


回答1:


Depends on what you are trying to do, but you can access the DexFile :

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

it gives you a http://developer.android.com/reference/dalvik/system/DexFile.html which you can enumerate, and load classes from.




回答2:


You may be able to obtain an InputStream for "classes.dex" in the following way:

  1. Retrieve the path to the apk container of your application.
  2. Retrieve the "classes.dex" entry inside your apk container thanks to JarFile class.
  3. Get an Input Stream for it.

Here is a snippet of code to exemplify:

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

Hope it helps!



来源:https://stackoverflow.com/questions/10122918/how-to-access-classes-dex-of-an-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!