dex2jar

repository·2.x·Indexed 12 days ago

https://github.com/pxb1988/dex2jar

A collection of tools for handling Android Dalvik Executable (.dex) files, primarily used to convert .dex files into Java .class files packaged as JARs for reverse engineering. The suite includes d2j-dex2jar for conversion, smali/baksmali for disassembly and assembly, a dex-reader/writer API, and d2j-decrypt-string for string decryption.

Tokens
1K
Snippets
3
Records
5
Agent score
46%

What's inside dex2jar

  1. Overview of dex2jar tools

    2.x

    dex2jar is a suite of tools designed to work with Android .dex and Java .class files. The project provides several specialized utilities:

    • dex-reader/writer: A lightweight API (similar to ASM) for reading and writing Dalvik Executable (.dex) files.
    • d2j-dex2jar: Converts .dex files into .class files, which are then bundled into a .jar file.
    • smali/baksmali: Disassembles .dex files into smali files and assembles them back. This implementation supports escaping in type descriptions (e.g., Lcom/dex2jar\t\u1234;).
    • d2j-decrypt-string: A utility for decrypting strings.
  2. Build and install dex2jar

    2.x

    To build the tools from source and prepare them for use, follow these steps:

    1. Run the Gradle distribution task from the project root to create a zip file.
    2. Navigate to the build distributions directory.
    3. Unzip the generated .zip file (e.g., dex-tools-2.1-SNAPSHOT.zip).
    4. Use the scripts located within the unzipped directory.
    ./gradlew distZip
    cd dex-tools/build/distributions
    unzip dex-tools-2.1-SNAPSHOT.zip
  3. Convert an APK to a JAR using d2j-dex2jar.sh

    2.x

    Use the d2j-dex2jar.sh script to decompile an Android APK into a JAR file containing .class files.

    Pass the path to your APK using the -f flag. The resulting output file will follow the naming convention: [original_filename]-dex2jar.jar.

    sh d2j-dex2jar.sh -f ~/path/to/apk_to_decompile.apk
  4. Populate annotation arrays with EncodedArrayAnnWriter

    2.x

    When an annotation contains an array, AnnotationWriter.visitArray(String name) returns an EncodedArrayAnnWriter. This specialized visitor is used to populate the EncodedArray with various value types:

    • Strings: Handled via cp.uniqString((String) value).
    • DexTypes: Handled via cp.uniqType(((DexType) value).desc).
    • Nested Annotations: Handled by calling visitAnnotation, which returns a new AnnotationWriter for the nested structure.
    • Nested Arrays: Handled by calling visitArray, which returns another EncodedArrayAnnWriter.
    • Enums: Handled via visitEnum using cp.uniqField.
  5. Write Dalvik annotations using AnnotationWriter

    2.x

    The AnnotationWriter class is used to write Dalvik annotations into a DEX file. It extends DexAnnotationVisitor and manages the creation of AnnotationElement entries within a provided list of elements. It relies on a ConstPool to handle unique string, type, and field indexing.

    Key methods for building annotations:

    • newAnnotationElement(String name): Creates a new annotation element with the specified name and adds it to the current list.
    • visit(String name, Object value): Handles primitive or object values. If the value is an Object[], it treats it as an array and delegates to visitArray.
    • visitAnnotation(String name, String desc): Creates a nested annotation. It returns a new DexAnnotationVisitor (an instance of AnnotationWriter) configured to write the elements of the nested annotation.
    • visitArray(String name): Creates an array value for an annotation element and returns an EncodedArrayAnnWriter to populate the array elements.
    • visitEnum(String name, String fower, String fname): Writes an enum value using the provided field information.
    // Note: AnnotationWriter is package-private (class AnnotationWriter), 
    // so it is intended for use within the com.googlecode.d2j.dex.writer package.
    
    AnnotationWriter writer = new AnnotationWriter(elementsList, constPool);
    // Example usage pattern via visitor methods:
    visitor.visit("myAnnotation", value);
    visitor.visitAnnotation("nestedAnnotation", "Lcom/example/Type;;");