
Generate Android DEX files programmatically: construct classes, methods and bytecode, manipulate registers, control-flow, instructions and limits, then emit executable DEX binaries for runtime loading.
Dexter is a small Kotlin Multiplatform library for writing Android DEX files with Kotlin.
dependencies {
implementation("io.github.charlietap.dexter:dexter:0.2.0")
}Build a class, write its bytecode, and get a DEX file back:
val greeter = ClassType("Lcom/example/Greeter;")
val greet = DexMethod(greeter, "greet", DexType.String, DexType.String)
val concat = DexMethod(DexType.String, "concat", DexType.String, DexType.String)
val file = Dexter().beginFile()
val code = file.addClass(greeter, DexType.Object).beginStaticMethod(greet, registerCount = 2)
// String greet(String name) { return "Hello, ".concat(name); }
val greeting = Reg(0)
code.constString(greeting, "Hello, ")
code.invokeVirtual(concat, greeting, code.parameter(0))
code.moveResult(greeting)
code.returnValue(greeting)
code.finish()
val dex: ByteArray = file.finish()On Android, load it and call it like any other class:
val loader = InMemoryDexClassLoader(ByteBuffer.wrap(dex), context.classLoader)
val greet = loader.loadClass("com.example.Greeter").getMethod("greet", String::class.java)
greet.invoke(null, "Dexter") // "Hello, Dexter"Dexter to bytesThis project is dual-licensed under both the MIT and Apache 2.0 licenses. You can choose which one you want to use the software under.
Dexter is a small Kotlin Multiplatform library for writing Android DEX files with Kotlin.
dependencies {
implementation("io.github.charlietap.dexter:dexter:0.2.0")
}Build a class, write its bytecode, and get a DEX file back:
val greeter = ClassType("Lcom/example/Greeter;")
val greet = DexMethod(greeter, "greet", DexType.String, DexType.String)
val concat = DexMethod(DexType.String, "concat", DexType.String, DexType.String)
val file = Dexter().beginFile()
val code = file.addClass(greeter, DexType.Object).beginStaticMethod(greet, registerCount = 2)
// String greet(String name) { return "Hello, ".concat(name); }
val greeting = Reg(0)
code.constString(greeting, "Hello, ")
code.invokeVirtual(concat, greeting, code.parameter(0))
code.moveResult(greeting)
code.returnValue(greeting)
code.finish()
val dex: ByteArray = file.finish()On Android, load it and call it like any other class:
val loader = InMemoryDexClassLoader(ByteBuffer.wrap(dex), context.classLoader)
val greet = loader.loadClass("com.example.Greeter").getMethod("greet", String::class.java)
greet.invoke(null, "Dexter") // "Hello, Dexter"Dexter to bytesThis project is dual-licensed under both the MIT and Apache 2.0 licenses. You can choose which one you want to use the software under.