Overview
Compose Hot Reload enables you to see code changes instantly in your running Compose Multiplatform application without restarting. This page explains the architecture, components, and the flow of how code changes are detected and applied.Architecture
Compose Hot Reload consists of four main components that work together to enable hot reloading:1. Hot Reload Agent
The Hot Reload Agent is a Java agent that runs inside your application’s JVM process. It is loaded via the-javaagent JVM argument.
Key responsibilities:
- Class Redefinition: Uses the JVM’s enhanced class redefinition capabilities (DCEVM) to reload classes at runtime
- Instrumentation: Transforms classes to support hot reload features like static field reinitialization
- Orchestration Server/Client: Manages communication with other components
- Window Tracking: Instruments window creation to enable dev tools window snapping
- Compose Integration: Tracks Compose runtime state and triggers recomposition after reload
premain method and starts these subsystems:
hot-reload-agent/src/main/kotlin/org/jetbrains/compose/reload/agent/
2. Orchestration Protocol
The orchestration protocol is a lightweight broadcast communication system that allows all components to send and receive messages. Key features:- Connection Handshake: Uses magic number
24111602for protocol validation - Message Framing: Each message has a type, size, and binary payload
- Serialization: Uses Java serialization or custom encoders (protocol v1.4+)
- Acknowledgments: Server acknowledges each received message
- Server Mode: The agent hosts the orchestration server (default)
- Client Mode: The agent connects to an external orchestration server (e.g., when dev tools is available)
hot-reload-orchestration/
3. Dev Tools
Dev Tools is a separate process that provides the developer interface and manages the build system integration. Key responsibilities:- UI Overlay: Shows reload status, logs, and provides the “Reload UI” button
- Recompiler Management: Triggers Gradle builds in response to reload requests
- Error Reporting: Displays compilation errors and runtime issues
- Window Management: Optionally snaps to the application window
- Normal: Window attached to application window
- Detached (
-Dcompose.reload.devToolsDetached=true): Standalone window - Headless (
-Dcompose.reload.devToolsHeadless=true): No UI, background only - Disabled (
-Dcompose.reload.devToolsEnabled=false): No dev tools process
RecompileRequest messages:
hot-reload-devtools/src/main/kotlin/org/jetbrains/compose/devtools/
4. Gradle Plugin
The Gradle plugin integrates hot reload into your build configuration. Key responsibilities:- Task Creation: Registers
hotRunJvm,hotRunAsync, andreloadtasks - Classpath Configuration: Sets up agent and dev tools dependencies
- JBR Provisioning: Ensures application runs with JetBrains Runtime
- Argument Injection: Configures JVM arguments for hot reload
hot-reload-gradle-plugin/src/main/kotlin/org/jetbrains/compose/reload/gradle/
How Code Changes Are Applied
When you save changes to your code, here’s what happens:Step 1: Change Detection
The detection mechanism depends on the reload mode: Explicit Mode:- You manually trigger reload via IDE button or
./gradlew reload - Gradle file watching is not enabled
- Gradle’s continuous build (
--watch-fs) detects file changes - Automatically triggers recompilation
Step 2: Recompilation
- A
RecompileRequestmessage is sent via orchestration - Dev tools receives the request and invokes the Gradle recompiler
- Gradle incrementally compiles only changed files
- Compilation produces updated
.classfiles - Dev tools sends a
RecompileResultmessage back
Step 3: Class Analysis
The agent analyzes the changed class files:- Extract class metadata (name, package, loader)
- Find the
ClassLoaderthat loaded the original class - Apply transformations (e.g., static field reinitialization)
- Create
ClassDefinitionobjects for redefinition
Step 4: Class Redefinition
The agent uses the JVM’s enhanced class redefinition:This requires the
-XX:+AllowEnhancedClassRedefinition flag, which is only available in JetBrains Runtime (JBR). Standard JDK does not support this level of class redefinition.Step 5: State Invalidation
After classes are redefined:- Static Reinitialization: Static fields are reinitialized based on the configured mode
- Dirty Scope Resolution: Analyze which Compose functions are affected
- Compose Invalidation: Mark affected
@Composablefunctions as invalid - Resource Cache Clearing: Clear any resource caches if needed
Step 6: Recomposition
Finally, Compose is notified to recompose:- The agent tracks all active Compose runtimes
- For each runtime, it invalidates the dirty scopes
- Compose schedules recomposition on the UI thread
- Your UI updates with the new code
The Role of JetBrains Runtime
JetBrains Runtime (JBR) is required for Compose Hot Reload because it includes enhanced class redefinition capabilities: Standard JDK limitations:- Can only redefine method bodies
- Cannot add/remove methods or fields
- Cannot change class hierarchy
- Can add/remove methods and fields
- Can change method signatures
- Can modify class hierarchy (with limitations)
- Enables true hot code replacement
Learn more about obtaining and configuring JetBrains Runtime in the JetBrains Runtime guide.
Classpath and Dependencies
The hot reload system uses a special “hot classpath” to track which classes can be hot reloaded:- Are monitored for changes
- Can be hot reloaded
- Have their dependencies tracked
- Cannot be hot reloaded
- Require application restart if changed
Configuration Properties
The system is configured via system properties:| Property | Purpose |
|---|---|
compose.reload.isHotReloadActive | Indicates hot reload is active |
compose.reload.mainClass | The main class being executed |
compose.reload.pidFile | Process ID file for orchestration |
compose.reload.orchestration.port | Orchestration server port |
compose.reload.devToolsEnabled | Enable/disable dev tools |
compose.reload.buildSystem | Build system type (Gradle, Amper) |
compose.reload.virtualMethodResolveEnabled | Track interface implementations |
compose.reload.dirtyResolveDepthLimit | Dependency traversal depth |
Debugging Hot Reload
To debug the hot reload system: Enable debug logging:Summary
Compose Hot Reload is a sophisticated system that combines:- A Java agent for class redefinition and instrumentation
- An orchestration protocol for component communication
- A dev tools process for build management and UI
- A Gradle plugin for seamless integration
- JetBrains Runtime for enhanced class redefinition