Hot Reload Runtime
The hot-reload-runtime library is the core component that enables true hot-reloading on Android devices by loading dynamic DEX patches without restarting the application.
Overview
When you make a change to a Kotlin file, the JetStart dev server compiles it into a DEX patch and sends it to your device. The hot-reload-runtime library handles:
- WebSocket Communication - Receiving DEX payloads from the dev server.
- DEX Loading - Using a custom
ClassLoaderto load the received bytecode into the running process. - State Management - Managing the
reloadVersionstate that triggers Jetpack Compose recomposition. - Method Swapping - Redirecting method calls from existing classes to the newly loaded
$Overrideclasses.
Installation
The library is included as a dependency in your app-level build.gradle:
dependencies {
// ...
implementation 'com.jetstart:hot-reload-runtime:1.0.0'
}
Key Components
HotReload Manager
The HotReload object is the primary API used in your MainActivity.kt:
HotReload.connect(context, url, sessionId)- Establishes a connection to the dev server.HotReload.reloadVersion- AStateFlowthat increments whenever a new hot-reload patch is applied.HotReload.disconnect()- Safely closes the connection.
HotReloadClassLoader
A specialized ClassLoader that implements a child-first loading strategy for hot-reload patches. This allows it to prioritize newly received DEX bytecode over the original classes bundled in the APK.
IncrementalChange
The interface used for method-level instrumentation, allowing the runtime to swap implementations at the method level (based on the InstantRun pattern).
Usage in App
The runtime is designed to be nearly invisible. Its main usage is in the app's entry point:
// Observe the reload version
val reloadVersion by HotReload.reloadVersion.collectAsState()
// Use reloadVersion as a key to force recomposition
key(reloadVersion) {
AppContent()
}
Performance
The runtime is optimized for speed, typically applying updates and triggering recomposition in under 20ms once the DEX payload is received.
Related Documentation
- Hot Reload System - Detailed explanation of the DEX pipeline
- Gradle Plugin - The plugin that configures this runtime
- Client App - The companion app that also uses hot-reload technology