dexmaker
A utility for doing compile or runtime code generation targeting Android's Dalvik VM
Top Related Projects
Most popular Mocking framework for unit tests written in Java
PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.
cglib - Byte Code Generation Library is high level API to generate and transform Java byte code. It is used by AOP, testing, data access frameworks to generate dynamic proxy objects and intercept field access.
An image loading and caching library for Android focused on smooth scrolling
Quick Overview
Dexmaker is a Java library for runtime code generation on Android. It allows developers to create and modify classes at runtime, which is particularly useful for mocking and stubbing in unit tests. Dexmaker is maintained by LinkedIn and is designed to work with Android's Dalvik and ART runtimes.
Pros
- Enables dynamic code generation on Android, which is not natively supported
- Useful for creating mocks and stubs in unit tests
- Compatible with both Dalvik and ART runtimes
- Integrates well with popular testing frameworks like Mockito
Cons
- Limited documentation and examples
- Not actively maintained (last commit was in 2019)
- May have compatibility issues with newer Android versions
- Performance overhead when generating code at runtime
Code Examples
- Creating a simple proxy class:
TypeId<?> proxyType = TypeId.get("LMyProxy;");
DexMaker dexMaker = new DexMaker();
dexMaker.declare(proxyType, "MyProxy.generated", Modifier.PUBLIC, TypeId.OBJECT);
// Generate the proxy class
ClassLoader loader = dexMaker.generateAndLoad(
MyProxy.class.getClassLoader(), getDataDirectory());
Class<?> proxyClass = loader.loadClass("MyProxy");
- Adding a method to a generated class:
MethodId<?, Void> methodId = proxyType.getMethod(TypeId.VOID, "myMethod");
Code code = dexMaker.declare(methodId, Modifier.PUBLIC);
Local<String> local = code.newLocal(TypeId.STRING);
code.loadConstant(local, "Hello, Dexmaker!");
code.returnVoid();
- Creating a mock object:
DexMaker dexMaker = new DexMaker();
ProxyBuilder.setDexMaker(dexMaker);
Class<?> proxyClass = ProxyBuilder.forClass(MyInterface.class)
.handler(new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Handle method invocations
return null;
}
}).buildProxyClass();
MyInterface mockObject = (MyInterface) proxyClass.newInstance();
Getting Started
To use Dexmaker in your Android project, add the following dependency to your build.gradle
file:
dependencies {
testImplementation 'com.linkedin.dexmaker:dexmaker:2.28.1'
testImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.28.1'
}
Then, you can start using Dexmaker in your test classes:
import com.linkedin.dexmaker.mockito.DexmakerMockitoMockMaker;
import org.mockito.MockitoAnnotations;
public class MyTest {
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testSomething() {
// Use Dexmaker with Mockito for mocking
MyClass mock = Mockito.mock(MyClass.class);
// ... continue with your test
}
}
Competitor Comparisons
Most popular Mocking framework for unit tests written in Java
Pros of Mockito
- More extensive feature set for mocking and stubbing
- Better documentation and community support
- Supports both Java and Kotlin out of the box
Cons of Mockito
- Larger library size, potentially increasing app size
- Steeper learning curve for beginners
Code Comparison
Mockito:
import static org.mockito.Mockito.*;
List mockedList = mock(List.class);
when(mockedList.get(0)).thenReturn("first");
System.out.println(mockedList.get(0)); // Outputs "first"
Dexmaker:
import static com.google.dexmaker.mockito.DexmakerMockito.*;
List mockedList = mock(List.class);
when(mockedList.get(0)).thenReturn("first");
System.out.println(mockedList.get(0)); // Outputs "first"
The code examples show that both libraries use similar syntax for basic mocking operations. However, Mockito offers a wider range of advanced features and matchers for more complex scenarios.
Dexmaker is specifically designed for Android development and integrates well with the Dalvik VM, making it a good choice for Android-specific projects. On the other hand, Mockito's broader applicability and richer feature set make it a more versatile option for general Java and Kotlin development across different platforms.
PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.
Pros of PowerMock
- More comprehensive mocking capabilities, including static methods and final classes
- Integrates well with other testing frameworks like JUnit and TestNG
- Supports both EasyMock and Mockito APIs
Cons of PowerMock
- Can be complex to set up and use, especially for beginners
- May lead to brittle tests if overused
- Performance overhead due to bytecode manipulation
Code Comparison
PowerMock:
@RunWith(PowerMockRunner.class)
@PrepareForTest(StaticClass.class)
public class TestClass {
@Test
public void testStaticMethod() {
PowerMockito.mockStatic(StaticClass.class);
when(StaticClass.staticMethod()).thenReturn("mocked");
}
}
Dexmaker:
public class TestClass {
@Test
public void testMethod() {
MyClass mock = mock(MyClass.class);
when(mock.someMethod()).thenReturn("mocked");
}
}
PowerMock offers more advanced mocking capabilities, allowing for static method mocking, which Dexmaker doesn't support out of the box. However, Dexmaker is simpler to use and doesn't require special test runners or annotations. PowerMock's syntax is more complex, while Dexmaker follows a straightforward Mockito-style approach. PowerMock is better suited for complex scenarios, while Dexmaker is ideal for simpler mocking needs in Android development.
cglib - Byte Code Generation Library is high level API to generate and transform Java byte code. It is used by AOP, testing, data access frameworks to generate dynamic proxy objects and intercept field access.
Pros of cglib
- More mature and widely used in enterprise Java applications
- Supports a broader range of bytecode generation and manipulation techniques
- Better documentation and community support
Cons of cglib
- Not designed for Android development, may require additional setup
- Larger library size, which can be a concern for mobile applications
- Slower runtime performance compared to Dexmaker in Android environments
Code Comparison
cglib:
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(MyClass.class);
enhancer.setCallback(new MethodInterceptor() {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
// Intercept method calls here
}
});
Dexmaker:
ProxyBuilder.forClass(MyClass.class)
.handler(new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// Handle method invocations here
}
})
.build();
Both libraries provide ways to create dynamic proxies, but cglib offers more advanced features for bytecode manipulation, while Dexmaker is optimized for Android development and has a simpler API for basic proxy creation.
An image loading and caching library for Android focused on smooth scrolling
Pros of Glide
- Specialized for image loading and caching in Android apps
- Extensive documentation and community support
- Offers advanced features like image transformations and placeholders
Cons of Glide
- Larger library size compared to Dexmaker
- May be overkill for projects not focused on image handling
Code Comparison
Glide (image loading):
Glide.with(context)
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.into(imageView);
Dexmaker (mocking):
MockMaker mockMaker = new InvocationHandlerAdapter(new InvocationHandler() {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return "mocked result";
}
});
Key Differences
Glide is primarily an image loading and caching library for Android, while Dexmaker is a runtime code generation library used for mocking in Android unit tests. They serve different purposes and are not direct competitors.
Glide excels in image handling tasks, offering a rich set of features for image loading, caching, and manipulation. It's widely used in Android app development for efficient image processing.
Dexmaker, on the other hand, is focused on generating mock objects at runtime for testing purposes. It's particularly useful for unit testing Android applications where creating mock objects can be challenging due to the Android runtime environment.
The choice between these libraries depends on the specific needs of your project. If you're dealing with image-heavy applications, Glide would be more beneficial. For unit testing and mocking in Android development, Dexmaker would be the appropriate choice.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Dexmaker
A Java-language API for doing compile time or runtime code generation targeting the Dalvik VM. Unlike
cglib or ASM, this library creates Dalvik .dex
files instead of Java .class
files.
It has a small, close-to-the-metal API. This API mirrors the
Dalvik bytecode specification giving you tight
control over the bytecode emitted. Code is generated instruction-by-instruction; you bring your own abstract
syntax tree if you need one. And since it uses Dalvik's dx
tool as a backend, you get efficient register
allocation and regular/wide instruction selection for free.
What does it do?
Mockito Mocks
Dexmaker lets you use the Mockito mocking library in your
Android projects by generating Dalvik bytecode class proxies. Just add an
androidTestImplementation
dependency on dexmaker-mockito
and you can use Mockito in your Android Instrumentation tests.
The version of Mockito that Dexmaker targets can be found in dexmaker-mockito
's build.gradle file. The general rule is that the major and minor version of Dexmaker will match the underlying major and minor version of Mockito.
Mocking Final Classes & Methods
Starting in Android "P", it is possible to mock final classes and methods using the dexmaker-mockito-inline
library. If you execute your tests on a device or emulator running Android P or above, you can add an androidTestImplementation
dependency on dexmaker-mockito-inline
(instead of dexmaker-mockito
; don't add both) and you can use the normal Mockito APIs to mock final classes and methods in your Android Instrumentation tests.
NOTE: This functionality requires OS APIs which were introduced in Android P and cannot work on older versions of Android.
Class Proxies
Dexmaker includes a stock code generator for class proxies. If you just want to do AOP or class mocking, you don't need to mess around with bytecodes.
Runtime Code Generation
This example generates a class and a method. It then loads that class into the current process and invokes its method.
public final class HelloWorldMaker {
public static void main(String[] args) throws Exception {
DexMaker dexMaker = new DexMaker();
// Generate a HelloWorld class.
TypeId<?> helloWorld = TypeId.get("LHelloWorld;");
dexMaker.declare(helloWorld, "HelloWorld.generated", Modifier.PUBLIC, TypeId.OBJECT);
generateHelloMethod(dexMaker, helloWorld);
// Create the dex file and load it.
File outputDir = new File(".");
ClassLoader loader = dexMaker.generateAndLoad(HelloWorldMaker.class.getClassLoader(),
outputDir, outputDir);
Class<?> helloWorldClass = loader.loadClass("HelloWorld");
// Execute our newly-generated code in-process.
helloWorldClass.getMethod("hello").invoke(null);
}
/**
* Generates Dalvik bytecode equivalent to the following method.
* public static void hello() {
* int a = 0xabcd;
* int b = 0xaaaa;
* int c = a - b;
* String s = Integer.toHexString(c);
* System.out.println(s);
* return;
* }
*/
private static void generateHelloMethod(DexMaker dexMaker, TypeId<?> declaringType) {
// Lookup some types we'll need along the way.
TypeId<System> systemType = TypeId.get(System.class);
TypeId<PrintStream> printStreamType = TypeId.get(PrintStream.class);
// Identify the 'hello()' method on declaringType.
MethodId hello = declaringType.getMethod(TypeId.VOID, "hello");
// Declare that method on the dexMaker. Use the returned Code instance
// as a builder that we can append instructions to.
Code code = dexMaker.declare(hello, Modifier.STATIC | Modifier.PUBLIC);
// Declare all the locals we'll need up front. The API requires this.
Local<Integer> a = code.newLocal(TypeId.INT);
Local<Integer> b = code.newLocal(TypeId.INT);
Local<Integer> c = code.newLocal(TypeId.INT);
Local<String> s = code.newLocal(TypeId.STRING);
Local<PrintStream> localSystemOut = code.newLocal(printStreamType);
// int a = 0xabcd;
code.loadConstant(a, 0xabcd);
// int b = 0xaaaa;
code.loadConstant(b, 0xaaaa);
// int c = a - b;
code.op(BinaryOp.SUBTRACT, c, a, b);
// String s = Integer.toHexString(c);
MethodId<Integer, String> toHexString
= TypeId.get(Integer.class).getMethod(TypeId.STRING, "toHexString", TypeId.INT);
code.invokeStatic(toHexString, s, c);
// System.out.println(s);
FieldId<System, PrintStream> systemOutField = systemType.getField(printStreamType, "out");
code.sget(systemOutField, localSystemOut);
MethodId<PrintStream, Void> printlnMethod = printStreamType.getMethod(
TypeId.VOID, "println", TypeId.STRING);
code.invokeVirtual(printlnMethod, null, localSystemOut, s);
// return;
code.returnVoid();
}
}
Download
For Mockito support, download the latest .jar via Maven:
<dependency>
<groupId>com.linkedin.dexmaker</groupId>
<artifactId>dexmaker-mockito</artifactId>
<version>2.28.4</version>
<type>pom</type>
</dependency>
or Gradle:
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.28.4'
Note: The dependency on Mockito will be transitively included, so there's no need to specify both Mockito AND dexmaker-mockito
Snapshots
You can use snapshot builds to test the latest unreleased changes. A new snapshot is published after every merge to the main branch by the Deploy Snapshot Github Action workflow.
Just add the Sonatype snapshot repository to your Gradle scripts:
repositories {
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
You can find the latest snapshot version to use in the gradle.properties file.
Top Related Projects
Most popular Mocking framework for unit tests written in Java
PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.
cglib - Byte Code Generation Library is high level API to generate and transform Java byte code. It is used by AOP, testing, data access frameworks to generate dynamic proxy objects and intercept field access.
An image loading and caching library for Android focused on smooth scrolling
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot