跪拜 Guibai
← All articles
Android · Reverse Engineering · Security

Decompilation Leaves Only Two Classes: A Self-Built Android Shell's Engineering Retrospective

By 王二蛋与他的张大花 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Componentized Android apps can now hide all business code from static decompilers without commercial hardening, but only if they replicate the same three solutions: a three-path ClassLoader swap with verification, FileLock-based multi-process decryption, and ARouter cache prewriting. The approach deliberately sets the bar at forcing an attacker into dynamic debugging, not blocking professional reverse engineers.

Summary

The post presents a whole-payload encryption approach for Android hardening, contrasting it with method-body stripping (dpt-shell). It details how the APK retains only two shell classes (an Application stub and an AppComponentFactory), a stripped native library, and encrypted payloads for DEX, SO, and H5. The encryption is a simple XOR stream, designed to hide business code from static decompilers, not to resist skilled reverse engineers.

The startup pipeline runs seven steps: attachBaseContext, signature and integrity checks, native decryption to private storage with FileLock, ClassLoader replacement with three reflection paths plus an official AppComponentFactory fallback, ARouter cache prewiring, and reflective creation of the real Application. Key decisions include avoiding method stripping to preserve runtime DEX scanning for ARouter, using a marker file and atomic rename for multi-process decryption, and generating integrity checks before the APK is finalized.

Validation used an adversarial matrix: modified signatures, resource tampering, Frida/injection detection (TracerPid, maps, ports), and static decompilation. Three-process cold starts on Android 10 and 16 showed zero crashes, with a benign hidden API warning on Android 16. The author also distinguishes real security findings from false positives from third-party scanners, and refuses to impersonate commercial shell vendors for compliance. The boundaries are explicit: root can dump decrypted payloads, and the shell is not commercial-grade (no VMP, no strong anti-debug).

Takeaways
— The hardened APK contains only two shell classes (Application stub and AppComponentFactory) in classes.dex, with all business DEX, SO, and H5 encrypted into payload files in assets.
— Whole-DEX encryption preserves runtime scanning by frameworks like ARouter, whereas method-body stripping (dpt-shell) breaks route registration and does not encrypt SO.
— The native library performs decryption, signature verification, anti-debug, and hidden API bypass; it is stripped but keeps Java_* exports.
— Startup runs seven steps; any failure kills the process, never continues in a half-restored state.
— ClassLoader replacement tries three reflection paths, then verifies by loading a class that only exists in the real DEX; it also uses AppComponentFactory.instantiateClassLoader as an official hook and a ShellComponentFactory as a final safety net.
— Multi-process decryption uses FileLock, a marker file, and atomic rename to avoid concurrent truncate causing SIGBUS/SIGSEGV.
— Integrity checks must be generated after APK finalization but before signing, and must skip META-INF and AndroidManifest.xml to avoid false failures.
— ARouter scans all DEX on first init and writes an empty route cache if it runs before decryption; the shell pre-writes the route SharedPreferences to avoid this.
— The debug switch class/field names after obfuscation are specific to the target project; a generic prewarm step is not zero-config.
— The native build must link -llog and -pthread, and use --strip-unneeded (not --strip-all) to keep JNI dynamic symbols.
— The shell is meant to raise the bar from "can use apktool" to "needs dynamic debugging"; root can dump decrypted payload, and it does not include VMP, strong anti-debug, or other commercial features.
Conclusions

Method-body stripping is often perceived as stronger hardening, but it breaks any framework that reflectively scans DEX at runtime. The article's decision to use whole encryption treats the existing engineering architecture as a non-negotiable constraint, which is a pragmatic and often overlooked priority.

The design degrades to plaintext execution if all fallback paths fail, a deliberate trade-off to keep the app alive. That philosophy makes sense for a self-built shell but means the protection is only as good as the fallback reliability.

The ARouter cache issue is a classic example of a tool caching a result obtained too early in the boot chain. Any hardening approach that delays the real DEX must either prevent an early scan or pre-warm the cache; this is a general lesson beyond Android.

The article's explicit refusal to fake commercial shell markers for compliance is a notable integrity boundary, especially in a field where passing third-party security scans often becomes the primary goal.

Concepts & terms
Method-body stripping
A hardening technique that empties each method's implementation in the DEX at build time and restores it at runtime. It can break frameworks that use reflection or annotation scanning over DEX contents.
DEX shell
A wrapper APK that starts with only stub classes and decrypts/restores the original DEX at runtime, often using native code to protect the encryption key.
AppComponentFactory
An Android system hook that allows an application to override classloader instantiation and component creation; here it provides an official fallback path for ClassLoader replacement.
ARouter route cache
ARouter builds a route table by scanning DEX files on first initialization and caches the result; if the scan happens before the real DEX is present, the empty table becomes permanent.
TracerPid
A field in /proc/self/status that identifies a ptrace-attached debugger; reading it is a common anti-debug technique.
From the discussion
Featured comments
细语棉柔

Can REA crack it?

王二蛋与他的张大花

Good question. REA is a scout, not a lockpicker—officially it doesn't do automated unpacking; after packing, it only sees the 2 shell classes. Frida dynamic instrumentation gets blocked by anti-injection detection. Dumping under root is theoretically still possible; that's the boundary of this practical shell, as made clear in section 7. It can stop automated dismantling, but against professional reverse engineering you'd still need commercial hardening.

See top comments, translated →
Source: juejin.cn ↗ Google Translate ↗ Backup ↗