New features of Android Studio parsing, do you really know instant run?

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/guolin_blog/article/details/51271369
This article started out in my public number, and I synced this article to my blog because there are so few articles on the web that explain the instant run feature in Android Studio, so that more people can understand the technology. Friends who want to see more technical articles can sweep the QR code at the bottom of this article to follow my public number and have technical articles pushed every week.

First of all, I started self-taught Android in August 2010, and it's almost 6 years humorous. I bought a book for self-study, called Android Advanced Programming, and the author is Reto Meier, which is still on my bookshelf, a slightly stale picture I just took:


Then recently in learning Instant Run, read the official Android video, I accidentally found that the help me get started with the handsome guy has already joined Google.

Now I want to know when the author of the first line of code can join Google?

Pull the egg first to get here, we come to the point of today.

What is instant Run?

As we all know, Android Studio is very powerful and is better than eclipse in every aspect of functionality, except that it is worse at speed. The Android Studio team has also done a lot of optimizations for this, and in Android Studio 2.0, there has been a dramatic improvement in speed:


As you can see, the speed of compilation and deployment in Android Studio 2.0 has been greatly improved, but the installation speed has not been improved, it is normal, after all, the application is installed by our phone's hardware configuration to determine, and development tools have anything to do with it.

But the Android Studio team was not satisfied with this, and they wanted to get the speed up to the extreme, so they added the instant run feature to Android Studio 2.0. Of course, as long as your Android studio version is above 2.0, you can use instant Run without any learning, but if you understand the rationale behind it, you will be able to use it better.

Traditionally, we have modified the program to run again once the program needs to go through the process of re-booting such as code recompilation, stop program, reinstall, and instant run attempts to deploy only parts of the program changes to the phone, avoiding the need to reinstall or restart the program , which greatly improves the efficiency of the debug program.

When we run the program for the first time, the Run button in Android Studio will look like this:


As you can see, there is a lightning bolt symbol next to the running triangle, which means that instant run can now be used.

Instant Run is divided into three main types, hot swap, warm swap and cold Swap,android Studio will automatically choose which swap type to use depending on how the code is modified, so let's learn more about these three swap types.

Hot Swap

Hot swap is the most efficient of all swap modes, and the application does not need to be reinstalled or restarted to complete program changes. However, hot swap does not reinitialize objects in the program, meaning that it is possible to restart activity in some scenarios to see the specific changes. Android Studio for hot swap This is the default restart activity, of course, you can also go to the settings to change the default behavior, the specific path is Settings, Build, execution, Deployment, Instant Run, Restart activity on code changes.

There are fewer conditions for hot swap, and only one scenario will be considered a hot swap type by Android studio, modifying the code in an existing method, as shown in the following:


As you can see, I've only changed the internal code of an existing method, and it's very fast to compile and deploy after re-running, and most crucially, the application has not been reinstalled or restarted, or even the activity has not been restarted (as I've set it above), and then the modified code has been successfully replaced.

Warm Swap

Warm swap is also very fast, and this swap type does not need to reinstall or restart the program to complete the program changes, but warm swap requires that the activity be restarted. You will see the screen flash quickly on the interface, and the activity's life cycle will be re-executed.

The conditions for warm swap are also limited, and only one scenario will be considered by Android studio as the warm swap type, which is to modify or delete an existing resource file, as shown in the following:


As you can see, I've changed the contents of the layout file, it's still very fast after re-running, the application has not been reinstalled or restarted, only the activity has restarted because the modified layout content will be displayed on the interface.

Cold Swap

Cold swap is relatively slower, and Android studio automatically records every modification of our project and then sends the modified part to a Dex file, even though the swap type still doesn't need to install a brand new APK, However, in order to load the new Dex file, the entire application must be restarted. In addition, cold swap works based on the multidex mechanism, where only 5.0 and more devices support Multidex without the introduction of an external library, so if you use a device under 5.0, cold swap won't work. This situation will perform the most primitive full APK installation process.

Cold swap has a lot of conditions to apply, so here's a detailed list of things that will be considered cold swap types by Android Studio:

    • Add, delete, or modify an annotation
    • Add, remove, or modify a field
    • Add, remove, or modify a method
    • Add a Class
    • Modifying the inheritance structure of a class
    • Modifying an interface implementation of a class
    • Modify the static modifier of a class
    • Changes involving the ID of the resource file

So let's take a look at the demo, as shown here:


As you can see, here I add a new click event method to the second button, and add a method that satisfies the cold swap condition, so we can clearly see that the application is restarted, but the overall speed is still fast, and the whole process of re-running is done within 5 seconds, I have real-time speed and no accelerated playback.

Full APK

In addition to other program changes that meet these criteria, Instant Run is not currently supported, including the following:

    • Change the contents of a Androidmanifest.xml file
    • Change the resources referenced by the Androidmanifest.xml file, such as the app_name in String.xml
    • Changing the UI-related elements of a desktop widget

When the program changes are not supported by instant run, the full APK installation process is performed, and Android studio gives the following hints:


Since this is a long time to run, it is not a demonstration, as we used to do when we were developing with Android Studio under version 2.0.

Of course, this is only the current instant run rule, and the Android Studio team will always be optimized to increase hot swap and warm swap conditions, reduce cold swap and full apk conditions, and trust the future Android Studio will be more useful.

Rerun

Although instant run wants to be smarter as much as possible, it does not have the ability to turn back the clock. For example, hot swap or warm swap does not restart the program at all, and if you modify some code that is initialized only when the program starts, then instant run is powerless because the modified code is not executed at all.

In this case, Android Studio specifically provides a rerun button:

The middle of the button is the rerun button, use this button to rerun the program, the application will be forced to restart, so the initialization of some code can be executed. Android Studio doesn't know if the changed code was executed at the time the program was initialized, and we knew it, so make sure you understand the role of the rerun button and use it at the right time.

Add

Hot swap has some special problems due to the limitations of its working principle. Hot swap opens a server inside the application, and then the Android Studio automatically calculates the changes implemented within the method, sends the change code to the server, and the server then injects the new code implementation into the existing application using the ClassLoader and the delegation mechanism to complete the replacement work.

But the entire process, the new code implementation is not saved to the local, that is, once the device and Android Studio connection is broken (such as unplugging the data cable), we use Hot Swap code is also missing. When you open the program again, you'll see a hint like this:


It is not surprising to see this hint, which means that your hot swap code is out of service, and now the program still uses the old code. In this case, you just need to connect your phone to your computer and then run the program again in Android studio to fix it. In addition, this hint can only be seen in debug mode, which is not possible in release mode, so don't worry about the user's frustration.

Pay attention to my public number, the first time to get a blog update reminders, you can also contribute to the public number, to share their technical summary to other people.
Sweep the QR code below or search Guo Lin to focus on:

New features of Android Studio parsing, do you really know instant run?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.