Instant Run Your Android App | Android Studio

0 159

Get real time updates directly on you device, subscribe now.

3 min read

Android Studio 2.3 and above has a feature called Instant Run which significantly reduces the time taken to update your app with code and resource changes. Once the app is deployed to a target device which is running Android 5.0 (API level 21) or higher, all you have to do is click on the button which says Apply Changes . This will push certain code and resource changes to your running app without building a new APK—and, in some cases, without even restarting the current activity.

Whenever, you want to push the changes and force your app to restart, the Run  and Debug  buttons are always there for you.

However, you may find that using the Apply Changes  button provides a faster workflow for most incremental changes to your app.

Tip: You can also press Control + F10 (or Control + Command + R on Mac) to apply changes with Instant Run.

The Apply Changes  action is available only when you meet the following conditions:

  • Build your app using a debug build variant.
  • Use Android plugin for Gradle version 2.3.0 or higher.
  • Set minSdkVersion to 15 or higher in your app’s module-level build.gradle file.
  • Deploy your app to a target device running Android 5.0 (API level 21) and higher.

Instant Run pushes your changes by performing a hot swap, warm swap, or cold swap—the type of swap it performs depends on the type of change you made. Instant Run automatically determines the best option to push your changes to the target device, when you click Apply Changes  after making a code or resource change to your running app.

HOT SWAP 
Code Change: Implementation of the code change for existing methods.
Apply Change Behavior: The fastest type of swap and makes changes visible much more quickly. Your application keeps running and a stub method with the new implementation is used the next time the method is called. Using apply change behavior type of swap the objects are not re-initialized in the running app. To see certain updates, we may need to restart the current activity, either by selecting Run > Restart Activity, or restart your app, by clicking Run  (or Debug ). Android Studio default behavior is to restart the current activity after performing a hot swap. If we do not want this behavior, we can disable automatic activity restarts.

WARM SWAP
Code Change:
Change or Remove an existing resource.
Apply Change Behavior: This swap is still very fast, but Instant Run must restart the current activity when it pushes the changed resources to your app. Your app keeps running, but a small flicker may appear on the screen as the activity restarts—this is normal.

COLD SWAP
Code Change:
Structural code changes, such as:

  • Add, remove, or change:
    • an annotation
    • an instance field
    • a static field
    • a static method signature
    • an instance method signature
  • Change which parent class the current class inherits from
  • Change the list of implemented interfaces
  • Change a class’s static initializer
  • Reorder layout elements that use dynamic resource IDs

Apply Change Behavior: This swap is a bit slower because, although a new APK is not required, Instant Run must restart the whole app when it pushes structural code changes.

Disable automatic activity restart

When performing a hot swap, your app keeps running but Android Studio automatically restarts the current activity. To disable this default setting:

  1. Open the Settings or Preferences dialog:
    • On Windows or Linux, select File > Settings from the menu bar.
    • On Mac OSX, select Android Studio > Preferences from the menu bar.
  2. Navigate to Build, Execution, Deployment > Instant Run.
  3. Uncheck the box next to Restart activity on code changes.

If automatic activity restart is disabled, you can manually restart the current activity from the menu bar by selecting Run > Restart Activity.