Reader small image

You're reading from  Android Studio 4.1 Development Essentials – Java Edition

Product typeBook
Published inMay 2021
PublisherPackt
ISBN-139781801815161
Edition1st Edition
Right arrow
Author (1)
Neil Smyth
Neil Smyth
author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth

Right arrow

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

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 or method 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...

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

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 Create New 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...

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 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() {

}

Use the Apply Code Changes button and confirm that the changes are applied without the need to reinstall 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 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.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Studio 4.1 Development Essentials – Java Edition
Published in: May 2021Publisher: PacktISBN-13: 9781801815161
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor’s degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth