Skip to content

dex++

dex++ reads Android APK and DEX files and answers questions about them. It is a C++23 library with Python, C, Go and Java bindings. The Python package is called dexpp.

This site describes version 0.3.1. The library is in alpha: the API may still change between 0.x releases.

What it does

  • APK metadata. The manifest (package, SDK levels, permissions, components and whether other apps can reach them), string resources, and the signing schemes and certificates.
  • Code analysis. Classes, methods and fields; decoded bytecode; a control-flow graph per method; try blocks; annotations; initial values of static fields.
  • Whole-program analysis. A call graph that knows about virtual dispatch, the class hierarchy, and a cross-reference index: who calls a method, who reads or writes a field, who loads a string, who uses a type.
  • DEX editing. Read a DEX file into a model, change it, and write a valid DEX file again. From Python, Go and Java the model works on whole classes: copy, move, remove and rename classes.

It does not decompile to Java, and it does not verify signatures.

A first look

The same program in Python, Go and Java. Pick your language in the tabs: every other example on the site then switches to it too.

import dexpp

apk: dexpp.Apk = dexpp.Apk.open("tests/data/signed.apk")

# Package metadata comes from the manifest. It is None when the APK has none.
manifest: dexpp.Manifest | None = apk.manifest
assert manifest is not None
print("package:", manifest.package)
print("version:", manifest.version_name)
print("permissions:", manifest.permissions)
print("launcher:", manifest.launcher_activity)

# Who signed it.
signing: dexpp.SigningInfo = apk.signing
for certificate in signing.certificates:
    print("signed by:", certificate.subject, certificate.sha256_hex[:16] + "...")

# The code: every classes*.dex of the APK in one analysis context.
ctx: dexpp.AnalysisContext = apk.analysis()
classes: list[dexpp.Class] = ctx.classes()
for cls in classes:
    method_names: list[str] = [method.name for method in cls.methods]
    print(cls.descriptor, method_names)
package: com.example.dexpp
version: 1.2.3
permissions: ['android.permission.INTERNET', 'android.permission.CAMERA']
launcher: com.example.dexpp.MainActivity
signed by: C=MA, O=DexppOrg, CN=Dexpp Test 6c9647205d1ae141...
LTestDex; ['<init>', 'helloDex', 'main']
apk, err := dexpp.OpenAPK("../../../tests/data/signed.apk")
if err != nil {
    log.Fatal(err)
}
defer apk.Close()

// Package metadata comes from the manifest. ok is false when the APK has none.
manifest, ok := apk.Manifest()
if !ok {
    log.Fatal("no manifest")
}
fmt.Println("package:", manifest.Package)
if manifest.VersionName != nil {
    fmt.Println("version:", *manifest.VersionName)
}
fmt.Println("permissions:", manifest.Permissions)
if manifest.LauncherActivity != nil {
    fmt.Println("launcher:", *manifest.LauncherActivity)
}

// Who signed it.
for _, certificate := range apk.Signing().Certificates {
    fmt.Println("signed by:", certificate.Subject, certificate.SHA256Hex[:16]+"...")
}

// The code: every classes*.dex of the APK in one analysis context.
ctx, err := apk.Analysis()
if err != nil {
    log.Fatal(err)
}
defer ctx.Close()
for _, cls := range ctx.Classes() {
    var methodNames []string
    for _, method := range cls.Methods() {
        methodNames = append(methodNames, method.Name())
    }
    fmt.Println(cls.Descriptor(), methodNames)
}
package: com.example.dexpp
version: 1.2.3
permissions: [android.permission.INTERNET android.permission.CAMERA]
launcher: com.example.dexpp.MainActivity
signed by: C=MA, O=DexppOrg, CN=Dexpp Test 6c9647205d1ae141...
LTestDex; [<init> helloDex main]
try (Apk apk = Apk.open(Path.of("../../tests/data/signed.apk"))) {
    // Package metadata comes from the manifest. It is empty when the APK has none.
    Manifest manifest = apk.manifest().orElseThrow();
    System.out.println("package: " + manifest.packageName());
    manifest.versionName()
            .ifPresent((String version) -> System.out.println("version: " + version));
    System.out.println("permissions: " + manifest.permissions());
    manifest.launcherActivity()
            .ifPresent((String launcher) -> System.out.println("launcher: " + launcher));

    // Who signed it.
    for (Certificate certificate : apk.signing().certificates()) {
        String fingerprint = certificate.sha256Hex().substring(0, 16) + "...";
        System.out.println("signed by: " + certificate.subject() + " " + fingerprint);
    }

    // The code: every classes*.dex of the APK in one analysis context.
    try (AnalysisContext ctx = apk.analysis()) {
        for (DexClass cls : ctx.classes()) {
            List<String> methodNames = cls.methods().stream().map(DexMethod::name).toList();
            System.out.println(cls.descriptor() + " " + methodNames);
        }
    }
}
package: com.example.dexpp
version: 1.2.3
permissions: [android.permission.INTERNET, android.permission.CAMERA]
launcher: com.example.dexpp.MainActivity
signed by: C=MA, O=DexppOrg, CN=Dexpp Test 6c9647205d1ae141...
LTestDex; [<init>, helloDex, main]

Where to go next

You want to Read
Install the Python package, or set up Go or Java Install
See the main ideas in ten minutes Quick start
Understand descriptors, handles and laziness Concepts
Learn one area in depth, with examples in Python, Go and Java the guides, starting with APK metadata
Look up a Python class or method Python reference
Use the library from C++ C++ and the C++ reference
Build and use the C API or the Go package C and Go
Build and use the Java package Java

Speed and memory

dex++ does as little as possible when it loads a file. It maps the file into memory and parses only the tables it needs. Class data, bytecode, control-flow graphs, the call graph, the hierarchy and the cross-reference index are each built the first time you ask for them, and then kept. So loading is cheap, and you pay only for the analyses you use.