
Posted by Alan Leung, Employees Software program Engineer, Fabien Sanglard, Senior Software program Engineer, Juan Sebastian Oviedo, Senior Product Supervisor
What’s Stay Edit and the way can it assist me?
Stay Edit introduces a brand new approach to edit your app’s Jetpack Compose UI by immediately deploying code adjustments to the operating utility on a bodily system or emulator. This implies that you could make adjustments to your app’s UI and instantly see their impact on the operating utility, enabling you to iterate quicker and be extra productive in your improvement. Stay Edit was lately launched to the steady channel with Android Studio Giraffe and might be enabled within the Editor settings. Builders like Plex and Pocket Casts are already utilizing Stay Edit and it has accelerated their improvement course of for Compose UI. Additionally it is serving to them within the technique of migrating from XML views to Compose.

When ought to I exploit Stay Edit?
Stay Edit is a unique characteristic from Compose Preview and Apply Modifications. These options present worth in numerous methods:
How does it work?
At a excessive degree, Stay Edit does the next:
- Detects supply code adjustments.
- Compiles lessons that have been up to date.
- Pushes new lessons to the system.
- Provides a hook in every class methodology bytecode to redirect calls to the brand new bytecode.
- Edits the app classpath to make sure adjustments persist even when the app is restarted.
.png)
Keystroke detection
This step is dealt with by way of the Intellij IDEA Program Construction Interface (PSI) tree. Listeners enable LE to detect the second a developer makes a change within the Android Studio editor.
Compilation
Basically, Stay Edit nonetheless depends on the Kotlin compiler to generate code for every incremental change.
Our aim was to create a system the place there’s lower than 250ms latency between the final keystroke and the second the recomposition occurs on the system. Doing a typical incremental construct or invoking an exterior compiler in a standard sense wouldn’t obtain our efficiency requirement. As an alternative, Stay Edit leverages Android Studio’s tight integration with the Kotlin compiler.
On the very best degree, the Kotlin compiler’s compilation might be divided into two phases.
The evaluation carried out as step one will not be solely restricted to a construct course of. Actually, the identical step is often executed outdoors the construct system as a part of an IDE. From primary syntax checking to auto-complete options, the IDE is consistently performing the identical evaluation (Step 1 of Diagram 1) and caching the end result to offer Kotlin- and Compose-specific performance to the developer. Our experiment exhibits that almost all of the compilation time is spent within the evaluation stage throughout construct. Stay Edit makes use of that info to invoke the Compose compiler. This permits compilation to occur inside 200ms utilizing typical laptops utilized by builders. Stay Edit additional optimizes the code era course of and focuses solely on producing code that’s solely essential to replace the applying.
The result’s a plain .class file (not a .dex file) that’s handed to the subsequent step within the pipeline, desugaring.
Learn how to desugar
When Android app supply code is processed by the construct system, it’s often “desugared” after it’s compiled. This transformation step lets an app run on a set of Android variations devoid of syntactic sugar assist and up to date API options. This permits builders to make use of new APIs of their app whereas nonetheless making it out there to gadgets that run older variations of Android.
There are two sorts of desugaring, often called language desugaring and library desugaring. Each of those transformations are carried out by R8. To verify the injected bytecode will match what’s at the moment operating on the system, Stay Edit should be sure that every class file is desugared in a means that’s appropriate with the desugaring executed by the construct system.
Language desugaring:
Such a bytecode rewrite goals to offer newer language options for decrease focused API degree gadgets. The aim is to assist language options such because the default interface methodology, lambda expression, methodology reference, and so forth, permitting assist right down to the min API degree. This worth is extracted from the .apk file’s DEX recordsdata utilizing markers left in there by R8.
API desugaring:
Also referred to as library desugaring, this type of desugaring goals to assist JAVA SDK strategies and lessons. That is configured by a JSON file. Amongst different issues, methodology name websites are rewritten to focus on capabilities positioned within the desugar library (which can be embedded within the app, in a DEX file). To carry out this step, Gradle collaborates with Stay Edit by offering the JSON file used throughout library desugaring.
Operate trampoline
To facilitate a speedy “per-key-stroke” velocity replace to a operating utility, we determined to not consistently make the most of the JVMTI codeswap capacity of the Android Runtime (ART) for each single edit. As an alternative, JVMTI is just used as soon as to carry out a code swap that installs trampolines onto a subset of strategies throughout the soon-to-be modified lessons contained in the VMs. Using one thing we known as the “Primer” (Step 3 of Diagram 1), invocation of the strategies is redirected to a specialised interpreter. When the applying not sees updates for a time frame, Stay Edit will change the code with conventional DEX code for efficiency advantages of ART. This protects builders time by instantly updating the operating utility as code adjustments are made.
.png)
How code is interpreted
Stay Edit compiles code on the fly. The ensuing .class recordsdata are pushed, trampolined (as beforehand described), after which interpreted on the system. This interpretation is carried out by the LiveEditInterpreter. The interpreter will not be a full VM inside ART. It’s a Body interpreter constructed on prime of ASM Body. ASM Body handles the low degree logistics such because the stack/native variables’s push/load, nevertheless it wants an Interpreter to truly execute opcode. That is what the OpcodeInterpreter is for.
.png)
Stay Edit Interpreter is a straightforward loop which drives ASM/Interpreter opcodes interpretation.
Some JVM directions can’t be applied utilizing a pure Java interpreter (specifically invokeSpecial and monitorEnter/Exit are problematic). For these, Stay Edit makes use of JNI.
Coping with lambdas
Lambdas are dealt with in a unique method as a result of adjustments to lambda captures can lead to adjustments in VM lessons which are totally different in lots of methodology signatures. As an alternative, new lambda-related updates are despatched to the operating system and loaded as new lessons as a substitute of redefining any current loaded lessons as described within the earlier part.
How does recomposition work?
Builders wished a seamless and frictionless new strategy to program Android functions. A key a part of the Stay Edit expertise is the flexibility to see the applying up to date whereas the developer constantly writes code, with out having to explicitly set off a re-run with a button press. We would have liked a UI framework that has the flexibility to take heed to mannequin adjustments throughout the utility and carry out optimum redraws accordingly. Fortunately, Jetpack Compose matches this activity completely. With Stay Edit, we added an additional dimension to the reactive programming paradigm the place the framework additionally observes adjustments to the capabilities’ code.
To facilitate code modification monitoring, the Jetpack Compose compiler provides Android Studio with a mapping of operate components to a set of recomposition group keys. The hooked up JVMTI agent invalidates the Compose state of a modified operate in an asynchronous method and the Compose runtime performs recomposition on Composables which are invalidated.
How we deal with runtime errors throughout recomposition

Whereas the idea of a constantly updating utility is somewhat exhilarating, our area research confirmed that generally when builders are writing code, this system might be in an incomplete state the place updating and re-executing sure capabilities would result in undesirable outcomes. Apart from the automated mode the place updates are occurring virtually constantly, we now have launched two guide modes for the developer who needs a bit extra management on when the applying will get up to date after new code is detected.
Even with that in thoughts, we need to be sure that frequent points brought on by executing incomplete capabilities don’t trigger the applying to terminate prematurely. Instances the place a loop’s exit situation continues to be being written are detected by Stay Edit to keep away from an infinite loop throughout the program. Additionally, if a Stay Edit replace triggers recomposition and causes a runtime exception to be thrown, the Compose runtime will catch such an exception and recompose utilizing the final recognized good state.
Think about the next piece of code:
var x = y / 10
Suppose the developer wish to change 10 to 50 by deleting the character 1 and inserting character 5 after. Android Studio might doubtlessly replace the applying earlier than the 5 is inserted and thus create a division-by-zero ArithmeticException. Nonetheless, with the added error dealing with talked about, the applying would merely revert to “y / 10” till additional updates are executed within the editor.
What’s coming?
The Android Studio group believes Stay Edit will change how UI code is written in a optimistic means and we’re dedicated to constantly enhance the Stay Edit improvement expertise. We’re engaged on increasing the kinds of edits builders can carry out. Moreover, future variations of Stay Edit will remove the necessity to invalidate the entire utility throughout sure eventualities.
Moreover, PSI occasion detection comes with limitations corresponding to when the person edits import statements. To unravel this drawback, future variations of Stay Edit will depend on .class diffing to detect adjustments. Lastly, the complete persisting performance is not at the moment out there. Future variations of Stay Edit will enable the applying to be restarted outdoors of Android Studio and retain the Stay Edit adjustments.
Get began with Stay Edit
Stay Edit is prepared for use in manufacturing and we hope it may well tremendously enhance your expertise creating for Android, particularly for UI-heavy iterations. We might love to listen to extra about your fascinating use instances, greatest practices and bug reviews and options.
Java is a trademark or registered trademark of Oracle and/or its associates.