APK Reverse Engineering & Patching
Goal: reach a verified, installable, still-working artifact fast — and avoid the whole class of
mistakes that destroy an APK while looking completely healthy.
Immediate — do these four things before anything else
This file is a procedure with gates, not background reading, and it is long enough that its middle
gets skimmed. So the four moves come first; everything below is the explanation for them.
1. Classify before choosing a route. Answer the thirteen questions in §Start here. R4 decides
whether you are editing the right layer at all, and a wrong branch does not fail loudly — it produces
an artifact that builds, runs, and does the wrong thing.
2. Clear the four gates, in order, with their pass criteria (§Gates). G1 names the deliverable form
in one testable sentence before any work; G2 establishes what this machine can really do; G3 locates
the code; G4 records the baseline and the control. "I understand the idea" is not clearing a gate.
3. A symptom you cannot explain is a stop signal, not a puzzle. Search §Symptom index for the shape
before your next attempt and load the file that row names. Those rows exist because each one cost
hours — and in several cases the answer was already written down while it was being re-derived.
4. Two strikes on one shape of attempt sends you back to classification, not to a third variant
(§Stop conditions). And never report done without the six items in §What "done" means.
Two rules about this file itself. When a failure does not fit your plan, the symptom index is the
next action, not more reasoning. And when this file and your own reasoning disagree, this file wins
until you have evidence that overrides it: every rule here is the residue of a failure that cost
hours, and your current intuition is the intuition of someone who has not hit it yet.
Four rules that override everything else
R1 — Write the deliverable as a testable sentence before you touch the target.
"It works" is not the goal; "it works under the stated constraint" is. Root-assisted, live-
instrumentation, host-proxy and patched-device results frequently do not satisfy a request for an
installable artifact that works on a normal phone — and it is easy to present such a result as
finished. Write the sentence, re-read it at every checkpoint, and if you cannot meet it, say so
plainly and label the privileged workaround a fallback, never the deliverable.
→ references/long-task-discipline.md §the most expensive drift
R2 — Change one variable at a time, and keep a control build.
An experiment that flips two things teaches nothing when it fails, and a failure you cannot attribute
will be attributed to the wrong cause. Every "the app rejects X" claim needs its own run, and every
patch needs a same-pipeline control that still fails the old way.
→ references/long-task-discipline.md §single-variable discipline
R3 — Never ship or claim an unverified artifact.
"It assembles" is not "it works"; "the process started" is not "the feature works"; "no error in the
log" is not "the check is gone". Install it, launch it, exercise the exact feature you changed, and
look at the screen. Prove the device is running the build you made — hash it, do not trust the
filename.
→ references/verification.md
R4 — Identify the owning layer before patching, and re-classify when reality disagrees.
Ads, paywalls, feature gates, integrity checks and update gates live in different layers (Java, dex,
native, Dart/Unity, server). Patching the wrong layer either does nothing or breaks the app. If a
patch "had no effect", the diagnosis was wrong — go back to classification instead of patching harder.
→ references/recon.md, then the layer-specific file the symptom index points at
Tooling — reach for the right instrument, and check before declaring it absent
Most wasted rounds in this domain are not bad reasoning about the target. They are the right question
asked of a tool too weak to answer it: an hour of grep over a hand-exported smali tree where one
indexed query would do, a manual ELF walk where a decompiler was one pip install away. The failure is
invisible from the inside, because the weak route still produces output.
Five obligations. These are instructions; a violation is a defect, not a preference.
| Obligation |
Do this |
Cost of ignoring it |
| Orient with an indexer, not an export |
Build the ability to ask the artifact questions before reading code: droidasc findrefs / ddc findrefs (string/type/method → every reference site, sub-second), and dexsql query when the question is a join or an aggregate — callers of a callee, methods holding a numeric constant, the debug line table — rather than a class list. A decompile reads a class you have already located — it is not how you locate it. jadx is a readable viewer of last resort, never the source of truth and never the entry point of a recon |
hours of grep per question asked, over an export you paid for first |
| Install the missing tool; it is part of the task |
A missing arm64 decompiler is the next step, not a constraint to route around. Ask a human only when installation is genuinely impossible |
hours re-derived by hand for output a decompiler gives in minutes |
| Name the gap before spending against it |
State the capability the blocker requires and whether this machine has it, out loud. This is a G2 item |
designing around a tool that installs in ten minutes |
| Reach for a shipped script before writing one |
Parsing, hashing, alignment and hot-plug probes are already written; a bespoke script in place of scripts/dex_find_insn.py is how offsets get guessed instead of computed |
a wrong offset that decodes cleanly and behaves wrongly |
| Pick the instrument for the layer the question lives on |
A dex in memory wants dex_mem_scan.py (find and cut) plus dex_dump_validate.py (judge), not a decompiler pointed at a fragment. An algorithm you only have to call wants emulation-and-rpc.md. "Does this class ever load at runtime" wants a hook that fires or does not |
the heavier tool is not the safer one — it produces a plausible answer, which is worse than none |
Run python skills/apk-reverse/scripts/doctor.py --json before concluding anything is unavailable.
It reports per-capability closure — what is missing, what to install, and roughly how long that takes —
and a capability you have not checked for is not a capability you lack. It is deliberately allowed to
report BLOCKED: a machine with a JVM but no zipalign/apksigner cannot re-sign, and the report
says so rather than inferring capability from a tool that is merely present. Detail:
references/toolchain.md; the registry it reads is scripts/capabilities.py.
Coverage — what this skill claims, and what it does not
The failure this section prevents is not ignorance. It is a confident wrong answer produced by
applying the nearest available procedure to a target it was never written for. A documented method
that almost fits is more dangerous than no method at all, because it arrives with a plan, a
vocabulary and a set of reassuring numbers.
How strong these claims are — this is load-bearing, not cosmetic. Every claim in this skill carries
one of four labels, and you must label your own results the same way:
| Label |
Means |
May it justify a decision? |
observed |
a command was run and its output exists |
yes |
inferred |
follows from an observation, but the step is reasoned |
provisionally |
unverified |
assumed, or reported elsewhere and never reproduced here |
only as something to test |
unknown |
nothing here covers it: no probe ran, or the probe's coverage did not reach it |
no — and never as absence |
A negative result has three shapes, and folding them together is the failure this section exists
for. unverified means somebody asserted something and nobody here paid for the counters; unknown
means no evidence was gathered at all. But a probe that did run and came back empty means one of two
different things, and only one of them is a finding:
- absent — the probe's coverage reached the question and the thing genuinely is not there;
- present but unreadable — the thing is there and this route cannot read it: an encrypted payload
inside a shell, a library the running ABI never loads, a check that fires before your hooks exist, a
field that exists under a type your parser rejects.
The second is the dangerous one, because it produces byte-identical output to the first and it is the
normal case on the hardened targets this skill is aimed at. A packed APK returning four classes is
not an APK with four classes. So before "not present" goes into the record, name the probe that covered
the question and run a control that must hit. If the control is silent too, the honest statement is
unknown about your probe, not absent about the target — and the next move is a different probe,
not a stronger conclusion.
The inventory that replaces a guess: the per-route list of what is covered and what is not — by
verified mechanism versus by documented-but-mostly-inferred route — is in
references/coverage-and-limits.md. Load it when the question is "can this skill actually do X".
Partial coverage that is easy to over-read — these are documented routes whose dependency this
skill does not ship, and each has a fence around it in the same file: Dart AOT analysis needs a
snapshot dump you must obtain elsewhere and naming the front end matters · extraction-shell recovery
stops at the VMP boundary · a Stalker trace is not guaranteed on every device · a kernel-side answer
requires a module this skill only scaffolds.
Not covered — say so rather than improvise: Unity / IL2CPP · React Native / Hermes / Cordova ·
iOS · defeating a server-side authority · an off-the-shelf unpacker or an anti-detection arms race ·
building and shipping a kernel module. Reasons and evidence boundaries are in
references/coverage-and-limits.md; silence in that record is not support.
The fallback, as an instruction — this is a rule, not advice. If the target does not match the
covered list, or no symptom-index row matches, then stop and classify before choosing a branch:
answer the thirteen questions (§Start here). If the shape still does not fit — an unknown runtime, a
mechanism you cannot name — say exactly that and propose the cheapest experiment that would
identify it. Do not take the closest documented route and apply it anyway. A wrong branch here does
not fail loudly: it produces an artifact that builds, runs, and does the wrong thing.
Hand-off points — where this skill ends and another view begins
Four boundaries that are easy to walk into without noticing. Each names what the other side owns
rather than restating it, because two copies of the same advice drift apart. The JNI form table and
the per-form verification table live in references/handoff-boundaries.md.
- JNI — a Java
native declaration and its implementation are two views of one function, and
this skill reads each with a different tool. A symbol search fails silently on dynamic
registration. Authoritative treatment: java2c-and-jni-sinking.md §The JNI boundary — why a
symbol search fails silently.
- Hardening — a dex-side packer observation is a native-side implementation question.
packers.md owns the dex-side identification; the deep dive belongs on the other side.
- Native anomalies — read them from the APK side, because the judgement is whether your patch
caused the death. Restoring a symbol or reversing an OLLVM function is a different activity with a
different toolchain: point across instead of extending
native-and-so.md into it.
- Deliverable form — when the artifact stops being an APK, the verification question changes with
it. G1's other three forms each move the evidence somewhere else, and the failure is quiet: a
privileged result reported in the language of a finished build.
Symptom index — a matching row is a stop signal
You arrive at a symptom, not at a file name. Each row below is a failure that has already been paid
for. If any row matches what you are observing, load the file before your next command — not after
your next three attempts. Reasoning from first principles at this point is how the same hours get
spent twice; more than one entry here is a lesson that was re-derived by hand while the answer sat
unread in this repository.