$ show linkedin/dexmaker
Dexmaker is a set of libraries that enable runtime bytecode generation and mocking on Android, primarily to support Mockito test frameworks. It provides two mocking modes: a proxy-based approach (dexmaker-mockito) that generates subclasses via the Dalvik bytecode generation API, and an inline approach (dexmaker-mockito-inline) that uses a JVMTI agent to transform class bytecode at runtime, allowing mocking of final and static methods. The core module (dexmaker) offers a low-level, fluent API for constructing DEX files programmatically, including types, methods, fields, and bytecode instructions. This is complemented by a C++ library (slicer) that reads, writes, and instruments DEX files, used by the inline mock maker to inject hooks into methods. The project is deeply integrated with Android internals, using hidden APIs and native agents, and is actively maintained to keep up with Android platform changes. It serves as the primary mocking solution for Android instrumentation tests where Java bytecode-based mocks (like ByteBuddy) are not available.
The LLM identified 4 files worth highlighting for reuse:
Central class for generating complete DEX files at runtime. Provides a declarative API to declare types, methods, fields, and annotations, then compiles them into a .dex file suitable for loading by a custom class loader. Its design separates declaration from code generation, making it reusable for any Android runtime code generation scenario beyond mocking.
Implements a domain-specific language for defining Dalvik bytecode instructions. Provides classes like Local, Label, and methods for all opcodes (invokes, arithmetic, branches, etc.). The API is type-safe and ensures correct operand ordering. This component is self-contained within the dexmaker module and demonstrates high craftsmanship in designing a fluent builder for low-level bytecode.
Creates dynamic proxy classes for concrete classes (not just interfaces) by generating a subclass dynamically. It uses DexMaker to produce the proxy and caches generated classes to avoid regeneration. Supports constructor argument injection and custom invocation handlers. This is a well-isolated utility that can be extracted to provide proxy functionality in other Android projects without the full mocking layer.
C++ implementation of method instrumentation for DEX files, providing EntryHook, ExitHook, and DetourHook capabilities. It modifies method bytecode to insert calls to a Java dispatcher before/after the original code. This component is a self-contained part of the slicer library, handling register allocation, stack manipulation, and instruction rewriting. It is reusable for any Android bytecode instrumentation tool that needs to add hooks to existing methods.