25. A Guide to using Apply Changes in Android Studio
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 Apply Changes 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.
25.1 Introducing Apply Changes
In early versions of Android Studio, 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.
Apply Changes, 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, Apply Changes 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 Apply Changes.
25.2 Understanding Apply Changes Options
Android Studio provides three options in terms of applying changes to a running app in the form of Run App, Apply Changes and Restart Activity and Apply Code Changes. These options can be summarized as follows:
•Run App - Stops the currently running app and restarts it. If no changes have been made to the project since it was last launched, this option will simply restart the app. If, on the other hand, changes have been made to the project, Android Studio will rebuild and reinstall the app onto the device or emulator before launching it.
•Apply Code Changes - This option can be used when the only changes made to a project involve modifications to the body of existing methods or when a new class has been added. When selected, the changes will be applied to the running app without the need to restart either the app or the currently running activity. This mode cannot, however, be used when changes have been made to any project resources such as a layout file. Other restrictions include the addition or removal of methods, changes to a method signature, renaming of classes and other structural code changes. It is also not possible to use this option when changes have been made to the project manifest.
•Apply Changes and Restart Activity - When selected, this mode will dynamically apply any code or resource changes made within the project and restart the activity without reinstalling or restarting the app. Unlike the Apply Code changes option, this can be used when changes have been made to the code and resources of the project, though the same restrictions involving some structural code changes and manifest modifications apply.
25.3 Using Apply Changes
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 25-1) or debug (B) button located in the toolbar:
Figure 25-1
After the app has launched and is running, the icon on the run button will change to indicate that the app is running and the Apply Changes and Restart Activity and Apply Code Changes buttons will be enabled as indicated in Figure 25-2 below:
Figure 25-2
If the changes are unable to be applied when one of the Apply Changes buttons is selected, Android Studio will display a message indicating the failure together with an explanation. Figure 25-3, for example, shows the message displayed by Android Studio when the Apply Code Changes option is selected after a change has been made to a resource file:
Figure 25-3
In this situation, the solution is to use the Apply Changes and Restart Activity option (for which a link is provided). Similarly, the following message will appear when an attempt to apply changes that involve the addition or removal of a method is made:
Figure 25-4
In this case, the only option is to click on the Run App button to reinstall and restart the app. As an alternative to manually selecting the correct option in these situations, Android Studio may be configured to automatically fall back to performing a Run App operation.
25.4 Configuring Apply Changes Fallback Settings
The Apply Changes fallback settings are located in the Android Studio Preferences window which is displayed by selecting the File -> Settings menu option (Android Studio -> Preferences on macOS). Within the Preferences dialog, select the Build, Execution, Deployment entry in the left-hand panel followed by Deployment as shown in Figure 25-5:
Figure 25-5
Once the required options have been enabled, click on Apply followed by the OK button to commit the changes and dismiss the dialog. After these defaults have been enabled, Android Studio will automatically reinstall and restart the app when necessary.
25.5 An Apply Changes Tutorial
Launch Android Studio, select the Start a new Android Studio project quick start option from the welcome screen and, within the resulting new project dialog, choose the Basic Activity template before clicking on the Next button.
Enter ApplyChanges into the Name field and specify com.ebookfrenzy.applychanges as the package name. Before clicking on the Finish button, change the Minimum API level setting to API 26: Android 8.0 (Oreo) and the Language menu to Java.
25.6 Using Apply Code Changes
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 25-6:
Figure 25-6
Once the app is running, the Apply Changes buttons should have been enabled indicating that certain project changes can be applied without having to reinstall and restart the app. To see this in action, edit the MainActivity.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 = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Apply Changes is Amazing!",
Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
With the code change implemented, click on the Apply Code Changes button and note that a 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.
25.7 Using Apply Changes and Restart Activity
Any resource change will require use of the Apply Changes and Restart Activity option. Within Android Studio select the app -> res -> layout -> fragment_first.xml layout file. With the Layout Editor tool in Design mode, select the default TextView component and change the text property in the attributes tool window to “Hello Android”.
Make sure that the fallback options outlined in “Configuring Apply Changes Fallback Settings” above are turned off before clicking on the Apply Code Changes button. Note that the request fails because this change involves project resources. Click on the Apply Changes and Restart Activity button and verify that the activity restarts and displays the new text on the TextView widget.
25.8 Using Run App
As previously described, the addition or removal of a method requires the complete re-installation and restart of the running app. To experience this, edit the MainActivity.java file and add a new method after the onCreate method as follows:
public void demoMethod() {
}
Clicking on either of the two Apply Changes buttons will result in the request failing. The only way to run the app after such a change is to click on the Run App button.
25.9 Summary
Apply Changes is a feature of Android Studio designed to significantly accelerate the code, build and run cycle performed when developing an app. The Apply Changes feature is able to push updates to the running application, in many cases without the need to re-install or even restart the app. Apply Changes provides a number of different levels of support depending on the nature of the modification being applied to the project.