A Guide to using Instant Run in Android Studio 2
Now that some of the basic concepts of Android development using Android Studio have been covered, now is a good time to introduce the Android Studio Instant Run feature. As all experienced developers know, every second spent waiting for an app to compile and run is time better spent writing and refining code. In recognition of this fact, Google has introduced Instant Run, arguably one of the most important features included in the Android Studio 2 release.
Introducing Instant Run
Prior to the introduction of Instant Run, each time a change to a project needed to be tested Android Studio would recompile the code, convert it to Dex format, generate the APK package file and install it on the device or emulator. Having performed these steps the app would finally be launched ready for testing. Even on a fast development system this is a process that takes a considerable amount of time to complete. It is not uncommon for it to take a minute or more for this process to complete for a large application.
Instant Run, in contrast, allows many code and resource changes within a project to be reflected nearly instantaneously within the app while it is already running on a device or emulator session.
Consider, for the purposes of an example, an app being developed in Android Studio which has already been launched on a device or emulator. If changes are made to resource settings or the code within a method, Instant Run will push the updated code and resources to the running app and dynamically “swap” the changes. The changes are then reflected in the running app without the need to build, deploy and relaunch the entire app. In many cases, this allows changes to be tested in a fraction of the time it would take without Instant Run.
Understanding Instant Run Swapping Levels
Not all project changes are fully supported by Instant Run and different types of project changes result in a different level of “swap” being performed. There are three levels of Instant Run support, referred to as hot, warm and cold swapping:
- Hot Swapping – Hot swapping occurs when the code within an existing method implementation is changed. The new method implementation is used next time it is called by the app. A hot swap occurs instantaneously and, if configured, is accompanied by a toast message on the device screen that reads “Applied code changes without activity restart”.
- Warm Swapping – When a change is made to a resource file of the project (for example a layout change or the modification of a string or color resource setting) an Instant Run warm swap is performed. A warm swap involves the restarting of the currently running activity. Typically the screen will flicker as the activity restarts. A warm swap is reported on the device screen by a toast message that reads “Applied changes, restarted activity”.
- Cold Swapping – Structural code changes such as the addition of a new method, a change to the signature of an existing method or a change to the class hierarchy of the project triggers a cold swap in which the entire app is restarted. Note that a cold swap does not result in the app being re-installed on the device.
Currently the only project change unsupported by any of the above Instant Run levels is a change to the manifest file. Such a change results in a complete re-build and re-installation of the app on the target device.
Enabling and Disabling Instant Run
Instant Run is enabled and disabled via the Android Studio Settings screen. To view the current settings begin by selecting the File -> Settings… menu option. Within the Settings dialog select the Build, Execution, Deployment entry in the left hand panel followed by Instant Run as shown in Figure 22-1:
[[Image:]]
Figure 22-1
The options provided in the panel apply only to the current project. Each new project will start with the default settings. The first option controls whether or not Instant Run is enabled by default each time the project is opened in Android Studio. The Restart activity on code changes option forces Instant Run to restart the current activity every time a change is made, regardless of whether a hot swap could have been performed. The final option controls whether or not a toast message is displayed within the app indicating the type of Instant Run level performed.
Using Instant Run
When a project has been loaded into Android Studio, but is not yet running on a device or emulator, it can be launched as usual using either the run (marked A in Figure 22-2) or debug (B) button located in the toolbar:
[[Image:]]
Figure 22-2
After the app has launched and is running, the availability of Instant Run will be indicated by the appearance of lightning bolt next to the button icon. Figure 22-3, for example, shows availability of Instant Run mode for the run button:
[[Image:]]
Figure 22-3
When the lightning bolt is displayed, clicking the button will use Instant Run to update the running app.
An Instant Run Tutorial
Begin by launching Android Studio and creating a new project. Within the New Project dialog, enter InstantRunDemo into the Application name field and ebookfrenzy.com as the Company Domain setting before clicking on the Next button.
On the form factors screen, enable the Phone and Tablet option and set the minimum SDK setting to API 23: Android 6.0 (Marshmallow). Continue to proceed through the screens, requesting the creation of a blank activity named InstantRunDemoActivity with a corresponding layout named activity_instant_run_demo.
Click on the Finish button to initiate the project creation process.
Triggering an Instant Run Hot Swap
Begin by clicking on the run button and selecting a suitable emulator or physical device as the run target. After clicking the run button track the amount of time before the example app appears on the device or emulator. Once running, click on the action button (the button displaying an envelope icon located in the lower right hand corner of the screen). Note that a Snackbar instance appears displaying text which reads “Replace with your own action” as shown in Figure 22-4:
[[Image:]]
Figure 22-4
Once the app is running the run button should contain the lightning bolt indicating that Instant Run will be used the next time that the button is clicked. To see this in action, edit the InstantRunDemoActivity.java file, locate the onCreate method and modify the action code so that a different message is displayed when the action button is selected:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Instant Run is Amazing!", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } });
With the code change implemented, click on the run button and note that the toast message appears within a few seconds indicating the app has been updated. Tap the action button and note that the new message is now displayed in the Snackbar. Instant Run has successfully performed a hot swap.
Triggering an Instant Run Warm Swap
Any resource change should result in Instant Run performing a warm swap. Within Android Studio select the app -> res -> layout -> content_instant_run_demo.xml layout file. With the Designer tool in design mode, select the RelativeLayout background view of the layout and, using the Properties tool window, locate the background property. Click in the background property text field so that the button displaying three dots appears. Click on the button, select a color from the Resources dialog and click on OK. With the background color of the activity content modified, run the app once again. This time a warm swap will be performed and the currently running activity should quickly restart to adopt the new background color setting.
Triggering an Instant Run Cold Swap
As previously described, a cold swap triggers a complete restart of the running app. To experience an Instant Run cold swap edit the InstantRunDemoActivity.java file and add a new method after the onCreate method as follows:
public void demoMethod() { }
Click on the run button and note that the app now has to terminate and restart to accommodate the addition of the new method.
Making a Manifest Change
As a final step in this tutorial, edit the app -> manifests -> AndroidManifest.xml file and add a new permission request as follows:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ebookfrenzy.instantrundemo" > <uses-permission android:name="android.permission.INTERNET" /> . . . </manifest>
Click on the run button and note the appearance of a notification popup indicating that a full build and install needs to be performed:
[[Image:]]
Figure 22-5
Note that without the benefit of Instant Run support the modified app takes a considerably longer period of time to launch.
Summary
Instant Run is a feature introduced with Android Studio 2 designed to significantly accelerate the code, build and run cycle. Using a swapping mechanism, Instant Run is able to push updates to the running application, in many cases without the need to re-install or even restart the app. Instant Run provides a number of different levels of support depending on the nature of the modification being applied to the project. These levels are referred to as hot, warm and cold swapping. This chapter has introduced the concepts of Instant Run and worked through some demonstrations of the different levels of swapping.