Skip to content

Use jadx as a library

Skylot edited this page Apr 17, 2024 · 7 revisions

From version 1.3.1 jadx artifacts available on maven central repository (list).

Include in project

  1. Add main jadx-core dependency (io.github.skylot:jadx-core).
    Latest release version: Maven Central
    To use the latest unstable build, see Using the latest unstable build
  2. Add google() repository to load aapt dependency (will be fixed in the future)
  3. Add one or several input plugins:
    • jadx-dex-input - allows reading dex files and all wrappers (apk, etc.)
    • jadx-java-input - support java bytecode loading
    • jadx-java-convert - support java bytecode through conversion using dx/d8 tools (conflicts with jadx-java-input)
    • jadx-smali-input - Smali input support
    • jadx-raung-input - Raung input support
  4. (Optional) As soon as jadx uses slf4j-api (manual), you will need to add and configure an implementation library as ch.qos.logback:logback-classic (manual)

Code

Complete example of simple code dump from dex input:

import java.io.File;
import jadx.api.JadxArgs;
import jadx.api.JadxDecompiler;

public class App {
    public static void main(String[] args) {
        JadxArgs jadxArgs = new JadxArgs();
        jadxArgs.setInputFile(new File("classes.dex"));
        jadxArgs.setOutDir(new File("output"));
        try (JadxDecompiler jadx = new JadxDecompiler(jadxArgs)) {
            jadx.load();
            jadx.save();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Instead jadx.save() you can iterate over classes to access necessary info:

for (JavaClass cls : jadx.getClasses()) {
    for (JavaMethod mth : cls.getMethods()) {
        System.out.println(mth.getName());
    }
}

Possible optimizations

  1. If code attributes are not needed, it is possible to switch to a simple code writer:
    jadxArgs.setCodeWriterProvider(SimpleCodeWriter::new);
  2. To reduce memory usage if simple dump is used (class info accessed only once):
    jadxArgs.setCodeCache(new NoOpCodeCache());

Use on Android

Please note that jadx library is not suited for use on Android, and it may not work on Android API < 26, check discussion.

Using the latest unstable build

To use the latest unstable build, as requested in issue #2106, add the following dependencies:

// build.gradle.kts

repositories {
    maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}

dependencies {
    implementation("io.github.skylot:jadx-core:1.5.0-SNAPSHOT") {
        isChanging = true
    }
    implementation("io.github.skylot:jadx-dex-input:1.5.0-SNAPSHOT") {
        isChanging = true
    }
    implementation("io.github.skylot:jadx-java-input:1.5.0-SNAPSHOT") {
        isChanging = true
    }
    implementation("io.github.skylot:jadx-java-convert:1.5.0-SNAPSHOT") {
        isChanging = true
    }
    implementation("io.github.skylot:jadx-smali-input:1.5.0-SNAPSHOT") {
        isChanging = true
    }
    implementation("io.github.skylot:jadx-raung-input:1.5.0-SNAPSHOT") {
        isChanging = true
    }
}

Additional information

Now jadx API allows access to internal nodes like: ClassNode, MethodNode, FieldNode, if possible don't use these classes as they can be changed without notice.

If you really need access to internal data, check issue #1482 for a possible workaround.