Skip to main content

Module runtime

Module runtime 

Source
Expand description

Turning a generated DEX into a live class.

The Apple backend synthesises a delegate with objc_allocateClassPair and installs Rust functions with class_addMethod. Android’s equivalent is three steps rather than two, because the class arrives as a file:

  1. crate::dex emits a DEX defining the subclass, its overrides native.
  2. InMemoryDexClassLoader loads it — from a ByteBuffer over memory we own, so nothing is ever written to disk.
  3. RegisterNatives binds each native method to a Rust function.

The result is a class Android cannot distinguish from a compiled one. It has a real superclass, a real constructor, and real methods; they simply happen to be implemented in Rust.

§Two things that will bite

  • InMemoryDexClassLoader is API 26+. Below that the DEX has to reach a file, and DexClassLoader needs a writable optimised-output directory. There is no fallback here; the crate targets 26 and up.
  • The DEX bytes must outlive the loader. NewDirectByteBuffer does not copy, so the buffer is leaked deliberately — one allocation per class, for the life of the process.

Structs§

Runtime
The pieces of the Android runtime this crate needs, resolved once.

Enums§

Error
Why the runtime could not start.