What’s New in Android Studio 2.0

Android Studio 2.0 was just released – take a quick tour of what’s new! By Huyen Tue Dao.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 3 of this article. Click here to view the first page.

Faster Build Speeds

Reporting speed-ups of 2-2.5x for full build times, the Tools Team has made improvements in several areas of the build system. To fully discuss them would be a pretty deep dive, but generally speaking they include:

  • Improvements to the dx merger. dx is the build tool that converts Java classes to Dalvik executable format (.dex) files.
  • A shrinker for debug mode.
  • Builds that specifically target connected devices.

For information on builds and the build system in general, check out the docs on android.developer.com.

For more on these build system improvements in particular, check out this session presented at the Android Dev Summit.

Faster ADB

The Android Debug Bridge (ADB) on the emulator has received a push/pull protocol update. When deploying APKs to the new emulator, you will see up to a 5x speed increase.

From the Android Dev Summit keynote presentation: https://youtu.be/xdItHEVfQ4U?t=13m17s

ADB Push Speeds

Instant Run

Wonderfully, Android Studio 2.0 is all about the need for speed. While the Android Tools Team has sped up builds and deployments, they have also tried to reduce the number of times you have to deploy.

To this end, they have introduced Instant Run, which provides pushing of updates—code and resources—to a running instance of your app on a device or emulator without requiring a full reinstall. Obviously, this is a huge time-saver.

Instant Run works differently for different kinds of changes. Currently, there are three ways Instant Run implements updates:

  1. A hot swap: your app continues to run and uses the changes next time the relevant method is called. This applies to method changes.
  2. A warm swap: your app continutes to run but restarts the current activity. This applies to resource changes.
  3. A cold swap: your app restarts but does not reinstall. This applies to structural code changes.

You can try out Instant Run right now in the companion app.

To enable Instant Run, select Preferences \ Build, Execution, Deployment \ Instant Run. Ensure that Enable Instant Run to hot swap code/resource changes on deploy is checked and that Restart activity on code changes is unchecked.

instant-run

If the companion app is not yet running, launch it by clicking the Run button, and wait for it to launch.

Initial app run

When the app is running, you should see a lightning bolt next to the Run button. The lightning bolt indicates that Instant Run is available.

Instant Run Ready

To test it out, open colors.xml and make the following changes to the colors:

<resources>
  <color name="colorPrimary">#455A64</color>
  <color name="colorPrimaryDark">#263238</color>
  <color name="colorAccent">#2196F3</color>
</resources>

This simply changes the values of colorPrimary and colorPrimaryDark, which determine the colors of the status bar and action bar.

Hit the Run button. The screen will flicker, and you’ll see a toast notifying you that the activity restarted. You will also see that the app already shows the colors changes without a full build and install.

Instant Run: Warm Swap

Because of the activity restart, you can probably guess that this was a warm swap.

Now open CardViewHolder.java and make the following changes to the onClick method:

@Override
public void onClick(View v) {
  if (mListener == null) {
    return;
  }

  Snackbar.make(itemView, R.string.instant_run, Snackbar.LENGTH_SHORT).show();

  mListener.onToggleLocationUpdates();
}

This added code requests that a snackbar be shown whenever someone clicks the START/STOP button on the top card of the companion app.

Hit the Run button. A toast will appear to notify you that Instant Run “Applied code changes without activity restart.”
Instant Run: Hot swap

Can you guess what kind of swap that was?

If you guessed hot swap, give yourself a cookie! :] Instant Run was able to push the changes without even restarting the activity. And if you click the START/STOP button, you can verify that the changes were successfully pushed:

Instant Run!

Instant Run can dramatically increase productivity, and dramatically decrease the time you spend browsing the Internet as you wait for your builds to finish and deploy (you’ll have to catch up on funny dog pics later). Beyond that, Instant Run will make your layout and styling more efficient, since you can immediately and directly see how code changes result in UI changes.

Where to Go From Here

There certainly is a lot to learn and try out in Android Studio 2.0 and Android Emulator 2.0! Much of the information about the two comes from the Android Dev Summit presentations, so check out those and other great sessions via the recordings posted on YouTube.

Many of the cool new things in Android Studio 2.0 are works in progress. A great way to keep up with updates, bug fixes and new features is to regularly check the Recent Changes page over at the Android Tools Project Site.

The Android Tools Project Site also contains technical docs on many of the Android Studio features.

If you have any questions or comments, feel free to post your comments below or in the forum!

Huyen Tue Dao

Contributors

Huyen Tue Dao

Author

Over 300 content creators. Join our team.