0

I'm trying to parse a file dex, I have written the Java code to get information about:

  • List item
  • header
  • string_ids
  • type_ids
  • proto_ids
  • field_ids
  • method_ids
  • class_defs

simply with the byte shift based on the size of the individual fields.

Now I want to get the bytecode then the source code, the dex file in question. Maybe this information can be found in the structure "code_item"?
If so, at what point do I block of memory to be able to read it.

Thanks in advance!

invictus1306
  • 587
  • 1
  • 4
  • 19
  • The DEX file format is described in detail here: http://source.android.com/devices/tech/dalvik/dex-format.html . The dexdump tool includes a disassembler; looking through the sources for that may be enlightening: https://android.googlesource.com/platform/dalvik/+/kitkat-release/dexdump/DexDump.cpp . – fadden Nov 11 '13 at 18:34

1 Answers1

5

You might take a look at the dexlib2 library that's part of the smali/baksmali project. It provides an easy-to-use api for accessing the information in a dex file.

Example code:

DexFile dexFile = DexFileFactory.loadDexFile("blah.dex", 15);

for (ClassDef classDef: dexFile.getClasses()) {
  for (Method method: classDef.getMethods()) {
    MethodImplementation impl = method.getImplementation();
    if (impl != null) {
      for (Instruction instruction: impl.getInstructions()) {
        // process instruction as needed...
      }
    }
  }
}
JesusFreke
  • 19,784
  • 5
  • 65
  • 68
  • It is a good solution thanks. Thank you for the tools :) However, I am trying to get information at lower level. I would understand, how to get to print the bytecode file, and then know the location and length of the data elements contained in the file structure. – invictus1306 Nov 12 '13 at 08:18
  • As fadden mentioned, the dex-format.html document is the way to go. It has all the information you need to parse it. – JesusFreke Nov 12 '13 at 18:45