Quick Building with Android Studio Apply Changes

Now that some of the basic concepts of Android development using Android Studio have been covered, this 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 better spent writing and refining code.

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 and ready for testing. Even on a fast development system, this process takes considerable 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 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. This often allows changes to be tested in a fraction of the time without Apply Changes.

Understanding Apply Changes Options

Android Studio provides three options for 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:

 

You are reading a sample chapter from Android Studio Hedgehog Essentials – Java Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 87 chapters and over 806 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

  • 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 restart the app. If, on the other hand, changes have been made to the project, Android Studio will rebuild and re-install 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 or method has been added. When selected, the changes will be applied to the running app without needing to restart 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 removing methods, changing a method signature, renaming classes, and other structural code changes. It is also impossible 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 re-installing 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. However, the same restrictions involving some structural code changes and manifest modifications apply.

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, a stop button (marked A in Figure 32-2) will appear, and the Apply Changes and Restart Activity (B) and Apply Code Changes (C) buttons will be enabled:

Figure 25-2

If the changes cannot be applied when one of the Apply Changes buttons is selected, Android Studio will display a message indicating the failure and 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 removal of a method is made:

 

You are reading a sample chapter from Android Studio Hedgehog Essentials – Java Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 87 chapters and over 806 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

Figure 25-4

In this case, the only option is to click on the Run App button to re-install and restart the app. As an alternative to manually selecting the correct option, Android Studio may be configured to automatically fall back to performing a Run App operation.

Configuring Apply Changes Fallback Settings

The Apply Changes fallback settings are located in the Android Studio Settings dialog. Within the Settings dialog, select the Build, Execution, Deployment entry in the left-hand panel, followed by Deployment, as shown in Figure 32-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 re-install and restart the app when necessary.

An Apply Changes Tutorial

Launch Android Studio, select the New Project option from the welcome screen, and choose the Basic Views Activity template within the resulting new project dialog before clicking the Next button.

Enter ApplyChanges into the Name field and specify com.ebookfrenzy.applychanges as the package name. Before clicking the Finish button, change the Minimum API level setting to API 26: Android 8.0 (Oreo) and the Language menu to Java.

 

You are reading a sample chapter from Android Studio Hedgehog Essentials – Java Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 87 chapters and over 806 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

Using Apply Code Changes

Begin by clicking the run button and selecting an emulator or physical device as the run target. After clicking the run button, track the time before the example app appears on the device or emulator.

Once running, click on the action button (the button displaying an envelope icon in the screen’s lower right-hand corner). 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 reinstalling and restarting 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:

binding.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();
    }
});Code language: Java (java)

With the code change implemented, click 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.

Using Apply Changes and Restart Activity

Any resource change will require the 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”.

 

You are reading a sample chapter from Android Studio Hedgehog Essentials – Java Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 87 chapters and over 806 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 

Ensure 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.

Using Run App

As previously described, removing 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() {
}Code language: Java (java)

Use the Apply Code Changes button and confirm that the changes are applied without re-installing the app.

Next, delete the new method and verify that 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 the Run App button.

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 can push updates to the running application, in many cases, without reinstalling or restarting the app. Apply Changes provides several different levels of support depending on the nature of the modification being applied to the project.

 

You are reading a sample chapter from Android Studio Hedgehog Essentials – Java Edition.

Buy the full book now in eBook (PDF) or Print format.

The full book contains 87 chapters and over 806 pages of in-depth information.

Learn more.

Preview  Buy eBook  Buy Print

 


Categories ,