1 Jadx scripts guide
Skylot edited this page 2024-04-17 17:45:08 +01:00

Jadx Scripts guide

Jadx uses Kotlin to support scripting.

Simple jadx script: hello.jadx.kts

log.info { "Hello from jadx script!" }

// get jadx decompiler script instance
val jadx = getJadxInstance()

// adjust options if needed
jadx.args.isDeobfuscationOn = false

// rename example
jadx.rename.all { name ->
    when (name) {
        "HelloWorld" -> "HelloJadx"
        else -> null
    }
}

// run some code after loading is finished
jadx.afterLoad {
    log.info { "Loaded classes: ${jadx.classes.size}" }
    // print class code
    jadx.classes.firstOrNull()?.let { cls ->
        log.info { "Class: '${cls.name}'" }
        log.info { cls.code }
    }
}

jadx.gui.ifAvailable {
    // if a script is running in jadx-gui, 
    // we can add menu entry to run custom code
    addMenuAction("Decompile All") {
        jadx.decompile.allThreaded()
    }
}

Warning

Script file name should end with .jadx.kts

Examples

Check script examples in examples/scripts/

Script usage

In jadx-cli

Add script file as input:

jadx classes.dex hello.jadx.kts

In jadx-gui

  1. Add script file to the project (using Add files or right-click menu on Inputs/Scripts and select New script)
  2. Script will appear in Inputs/Scripts section
  3. After script change, you can run it using Run button in script editor toolbar or reload whole project (Reload button in toolbar or F5). Also, you can enable Live reload option in File menu to reload a project automatically on script change

Script development

Jadx-gui for now don't support code navigation and docs, so the best way for script editing is to open jadx project in IntelliJ IDEA and write your script in examples/scripts/ folder. Also, this allows debugging your scripts: for that you need to create run configuration for jadx-cli or jadx-gui, add breakpoints and next run it in debug mode (jadx-gui is preferred because of faster script reload).

Script logs and compilation errors will appear in Log viewer (change filter mode for only script related logs)

Execution time

Each part of jadx script is executed at different time or stages.
Script base code loaded and evaluated at the loading stage, at this point classes info is not yet loaded, but it is possible to change loading options.
If you need to process already loaded classes, code to do so should be placed in jadx.afterLoad block.
Also, to execute during decompilation you can use jadx.addPass or use utility methods in jadx.stages

Available methods

For simplify usage of jadx API in scripts, wrapper methods were added and divided into next sections:

  • rename - to rename any classes, methods, fields
  • replace - replace/change instructions in methods
  • stages - execute custom code at different decompilation stages
  • search - search class or method
  • gui - methods to access jadx-gui UI
  • events - receive or send jadx events (only UI events for now)
  • decompile - run classes decompilation
  • debug - utility methods used to view/access internal information

For more details, check the source code here.

Internal info

Now jadx.internalDecompiler can be used to access data in jadx decompiler (same as in Use jadx as a library.
This can be useful if this is no other way to access some jadx data, but it is suggested to not use this field.
Please feel free to open an issue if you still need to use jadx.internalDecompiler.