#2964·jadx

Misleading and non-compilable output from replaceConsts, try/finally lowering, and switch reconstruction (JADX 1.5.6)

Author: zgyhimCreated Sep 25, 2026Updated Sep 25, 2026

Summary

On JADX 1.5.6 (Arch Linux pacman package), decompiling a plain Android APK produces source that is (a) misleading and (b) sometimes cannot be compiled. Two distinct mechanisms cause this, and a third produces duplicated/unreachable statements.

I first suspected a post-processing/external tool, but I verified against JADX's own GUI code cache and by re-running the CLI: the artifacts are produced by JADX itself, and they are present in the GUI output before any export. The "Export project -> Export as Gradle project" step is not involved.

Environment

  • JADX 1.5.6 (Arch pacman), CLI and GUI, Linux.
  • GUI config ~/.config/jadx/gui.json (relevant keys):
    • "replaceConsts": true
    • "extractFinally": true
    • "deobfuscationOn": true, "renameFlags": ["CASE","VALID","PRINTABLE"]
  • Input: a plain APK (~4 MB). No obfuscation mappings supplied.
  • Export path: File -> Export project -> Export as Gradle project -> template "Android APP" (not related to the issues below).

Reproduction:

javac-free CLI, default options:
  jadx -q -d out_default  --no-imports input.apk
disable const replacement:
  jadx -q -d out_noconsts --no-imports --no-replace-consts input.apk
disable finally extraction:
  jadx -q -d out_nofin    --no-imports --no-finally        input.apk

B#1. replaceConsts replaces literals with unrelated constant fields (sometimes non-public)

replaceConsts is on by default (and --no-replace-consts is documented as "don't replace constant value with matching constant field"). JADX replaces a numeric literal with a constant field of equal value picked from any class in the analyzed inputs, without checking that the field is related to the use site or even accessible.

Example (default output, com.google.zxing.pdf417.PDF417Common):

java
// actual field values kept, but names come from unrelated classes:
UnixStat.DEFAULT_DIR_PERM
TransportMediator.KEYCODE_MEDIA_PLAY
UnixStat.DEFAULT_LINK_PERM
UnixStat.DEFAULT_FILE_PERM
TransportMediator.KEYCODE_MEDIA_RECORD
TransportMediator.KEYCODE_MEDIA_PAUSE

Example (com.google.zxing.oned.UPCEReader):

java
case BZip2Constants.G_SIZE /* 50 */:   // field is package-private in an unrelated package

Impact:

  1. Readability / correctness of intent: the emitted name is unrelated to the code, so a reader cannot tell what the literal means. (The numeric value is preserved, so this is not value corruption.)
  2. Non-compilable output: when the chosen field is not accessible from the use site (e.g. a package-private interface BZip2Constants), JADX still emits the qualified reference, which does not compile without adding an otherwise unnecessary import / moving code.

Verification: with --no-replace-consts, BZip2Constants.G_SIZE and the UnixStat.* / TransportMediator.* names disappear from the output.

Suggestion: restrict candidate fields to constants that are public and compatible with the expression/constant pool, and/or prefer fields declared in the same class hierarchy / same package as the use site; otherwise emit the literal. Consider also making this clearly opt-in for library-heavy inputs.


B#2. try/finally lowering references an undeclared synthetic exception variable

JADX lowers try-with-resources / try-finally to nested catches using a synthetic Throwable variable (here th), but emits assignments to it without ever declaring it, and adds self-assignment and constant conditions.

Actual (default output, org.apache.commons.compress.compressors.pack200.Pack200Utils):

java
try {
    ...
    try {
        ...
        if (0 != 0) {          // always false
            j.close();
        }
        if (os2 != null) {
            os2.close();
        }
        f.delete();
    } catch (java.lang.Throwable th) {
        th = th;               // self-assignment
        os = os2;
        ...
        throw th;
    }
} catch (java.lang.Throwable th2) {
    th = th2;                  // `th` is never declared anywhere
    j = j2;
}
} catch (java.lang.Throwable th3) {
    th = th3;                  // nor here
}

There is no Throwable th; (nor = null) declaration anywhere in the method or file. This is a hard compile error (cannot find symbol: variable th).

Note: this is not caused by finally extraction. Running with --no-finally reproduces the identical output:

$ jadx ... --no-finally input.apk
} catch (java.lang.Throwable th2) {
    th = th2;      # still undeclared

Meanwhile if (0 != 0) { j.close(); } / if (1 == 0 && ... ) also appear, which is both noisy and can silently drop or fabricate cleanup.

Suggestion: declare the synthetic throwable in the enclosing scope (Throwable th = null;) when lowering, and normalize the constant conditions.


B#3. Duplicated / unreachable statements from obfuscated switch

Actual (default output, com.google.zxing.oned.UPCEANExtension5Support):

java
case '9':
    ...
    currency = "";
    break;
    break;          // duplicated, second is unreachable

JADX itself warns: JADX WARN: Can't fix incorrect switch cases order, some code will duplicate. The duplicated statement can be unreachable code (compile error) or alter behavior if the first statement is not a control-transfer.

Suggestion: de-duplicate directly adjacent identical statements generated by the switch reconstruction, or drop the unreachable one.


Impact

  • The output cannot be compiled without manual repair, and the repairs require first undoing JADX decisions (guessing which constant name was original, reconstructing the declared throwable, removing duplicated statements).
  • Readability is actively degraded: unrelated constant names and undeclared variables are presented as if they were original source.

Request

  1. replaceConsts: only use accessible/compatible constants, and prefer the use-site's own hierarchy; otherwise keep the literal.
  2. Exception lowering: always declare the synthetic throwable before assigning to it; clean up constant conditions.
  3. Switch reconstruction: de-duplicate adjacent identical statements.

I'm happy to split this into three separate issues or provide a minimal sample if that helps triage.


This report was drafted with AI assistance (deepseek-v4-flash) on 2026-09-25, based on JADX 1.5.6 GUI cache inspection and CLI re-runs; all snippets are copied from actual JADX output.