Expand description
A DEX writer, for classes that do not exist until we make them.
Android’s BLE APIs are callback-driven, and every callback type —
BluetoothGattCallback, ScanCallback, BluetoothGattServerCallback,
AdvertiseCallback — is an abstract class, not an interface. So
java.lang.reflect.Proxy cannot help: it only proxies interfaces. Something
has to subclass them, and on a device there is no compiler to do it.
This is the same problem CoreBluetooth poses, and it gets the same answer:
build the class at run time. Where Apple has objc_allocateClassPair,
Android has a class file format — so the class is emitted as a DEX,
loaded with InMemoryDexClassLoader, and its methods bound to Rust
functions with RegisterNatives.
§Why this is far less work than it sounds
A method declared native has no bytecode — no code_item at all, just
a metadata entry. Since every override we need is native, this writer emits
constant tables and nothing else. The single exception is the constructor,
which must call super(), and that is four code units written by hand:
invoke-direct {v0}, Lsuper;-><init>()V
return-voidSo there is no compiler here, and no optimiser — a layout problem and a handful of sorted tables.
§The parts that must be exactly right
- Tables are sorted, and the verifier rejects the file if they are not:
strings by UTF-16 code point, types by descriptor, protos by return type
then parameters, methods by
(class, name, proto). encoded_methodstores index deltas, not indices, so the order of the method list and the order ofmethod_idsare coupled.- The header carries an Adler-32 and a SHA-1 over everything that follows them, written last.
- The
map_listmust describe every section, in offset order.
Structs§
- DexBuilder
- Builds a DEX containing one class: a subclass with native overrides.
- Native
Method - One method to declare
nativeon the generated class.