Join David Gassner for an in-depth discussion in this video Update apps with Instant Run, part of Android Studio Essential Training.
- [Tutor] Instant Run is a feature that was first introduced in Android Studio 2.0. Prior to that, every time you made a change to your app whether in your Java code, your resources, or your layout files, you would have to completely rebuild and launch the entire app on the device, and that could be a little slow. With Instant Run, you can make incremental changes and those changes can be added to the app without completely shutting it down. This speeds up your development cycle. To get started with this feature, I'm going to create a brand new project.
From the welcome screen I'll start a new project and I'll call it Instant Run. I'll set the Minimum SDK at API 15; that's the oldest version of Android that Instant Run will work on, and I'll base my project on the Basic Activity template. Now before I make any changes at all, I'll run the app on a virtual device and I'm going to rearrange my windows a little bit, moving Android Studio over to the left and displaying my virtual device on the right, and I'll click the run button and choose the device, and then wait a moment until the app is deployed.
So in Android Studio 2.0, if an app was already running you would click the Run button. If the nature of the changes you have made allowed Instant Run to do its thing, then you would only see incremental changes on the device. But if something had changed that required a full deployment, that's what would happen. It was all very automated and a little bit difficult to control. Starting in Android Studio 2.3 there are two buttons on the toolbar. The Run button now always does a full deployment.
If you want to use Instant Run, you instead click the new Apply Changes button or press the associated keyboard shortcut, or select the new Apply Changes item from the menu. There are three basic options with Instant Run. I'm going to make a change to a bit of my Java code. Right now, when the user clicks on the Floating Action Button in the main activity, I'm displaying a Snackbar message; displaying some literal text, "Replace with your own action." Now before I demonstrate this I'm going to go to my settings.
I'll select File, and then Settings on Windows or Preferences on Mac, and under Build, Execution, and Deployment, I've selected Instant Run, and I'll deselect this option labeled, "Restart activity on code changes." I'll leave all the other settings at their defaults. Then I'll resize Android Studio again, and now I'll click on the Apply Changes button. And I see a message down at the bottom that says, "No changes to deploy." So now I'll select this bit of text, "Replace with your own action", and I'll replace it with some different literal text; "This is my message." And then I'll click Apply Changes again.
Because I deselected that option, it now allows me to do what's called a hot swap. A hot swap is possible when you change some of your Java code, but you don't add or remove any of your identifiers such as fields, methods, or class names. I'll test this by clicking on the Floating Action button and I see, "This is my message." Then I'll make another change; I'll say, "This is my revised message", and once again I'll Apply Changes, I see the message over in the device saying that the code changes were made without an activity restart, and I'll click the button and there's my revised message.
So that's a hot swap. A warm swap happens when you make a change that requires an activity to be reloaded, but it doesn't make it necessary to change the application context. I'll go to activity_main.xml and I'll look at this in text mode, and I'll go to my content_main layout, and once again I'll look at this in text mode, and I'll change this code a bit. I'll add a textsize attribute and I'll set it to 24sp.
Now once again, I'll resize everything so I can see my device, and then I'll click Apply Changes, and this time I see, "Applied changes and restarted activity." Anytime you make a change to an xml layout file, that requires a warm swap. And finally, if you add or remove any Java methods, or even give them different names, that will require a cold swap; a complete reload of the application. I'll add in override of the onPause method and then I'll use a little bit of logcat output; I'll set my tag as simply MainActivity.
Then once again I'll resize everything and I'll click Apply Changes, and this time the entire application reloads from scratch. So those are hot swaps, warm swaps, and cold swaps, and they all are possible when you click on that Apply Changes button. In Android Studio 2.3 though, if you click on the Run button you'll always do a complete redeployment, regardless of whether you've made any changes to the app or not. And there's an example.
I didn't make any changes but clicking Run restarted the app from scratch. The Instant Run feature has been refined over the different versions, starting with the Android Studio 2.0. And now in Android Studio 2.3 with the introduction of Apply Changes, you have much greater control over when it's used.
- Installing Android Studio on Mac and Windows
- Creating Android Studio projects
- Setting up the development environment, including HAXM and the new Android emulator
- Importing existing code into Android Studio projects
- Exploring the interface, including the editor and project windows
- Managing project builds and dependencies
- Creating new Java classes
- Refactoring code
- Using templates
- Using breakpoints and watch expressions
- Updating apps with Instant Run
- Using Git for version control
Skill Level Beginner
Q: This course was updated on 04/27/2017. What changed?
A: New videos were added that highlight the new features introduced in Android Studio 2.3. In addition, the following topic was updated: update apps with Instant Run.