Instant Run detailed

Source: Internet
Author: User

Instantrun detailed

Before writing Androidstudio to improve the build speed of this article wrote, want to quickly, use Instant Run . Recently, a friend sent an e-mail to discuss its principles, the recent project is not busy, simply to learn under the system.

Android Studio2.0 has been introduced Instant Run , it is mainly in Run Debug the and when you can reduce the time to update the application. While the first time may take a Build little longer to complete, Instant Run you can push the update to the device without having to re- build make a new one so that it apk quickly lets us see the change.

Instant RunOnly the build.gralde version that is configured in the file Gradle is 2.0.0 or above and more than minSdkVersion 15 is allowed. In order to be able to use better, please minSdkVrsion set to more than 21.

After the app is deployed, Run a small yellow lightning bolt appears on the (or Debug) icon, which means Instant Run you're ready to push the update the next time you click the button. It does not need to rebuild a new one APK , it pushes only those new changes, and in some cases app does not require a reboot to directly show the effect of the new change.

Configure your app to use Instant Run

Android StuidoProjects using Gralde 2.0.0 and above will be used by default Instant Run .
To update the project to the latest version:

    1. Click Settings the button in the Preferences .
    2. Locate Build,Execution,Deployment Instant Run and then click on Update Project , if the Update Project section is not shown, that means you are currently in the latest version.
Repair type

Instant RunPush changed code and resources to your device or simulator with hot fixes, warm fixes, or cold fixes. It automatically selects the corresponding repair type based on what you have changed.

    • Hot FIX: The changed content does not need to restart the app and does not even need to restart the current to activity allow the display to change, for most of the changes in the way content changes can be modified in this way.
    • Warm FIX: A restart is required for activity the change to take effect, which is the way to change resources.
    • Cold FIX: The app needs to be restarted (not reinstalled). Structural changes such as the structure and implementation of some method methods need to be done in this way.

How does the specific change and repair method correspond?

  • Change the way it exists now
    This will be done with a hot fix , which is the quickest way to fix it. Your app will use the newly implemented method directly the next time it is called, while it is running.
    Hot fix does not require reinitialization of objects. You may need to restart the current or app before you see the specific changes activity . By default, the current is restarted Android Studio automatically after a hot fix is performed activity . If you don't want it to restart automatically activity you can also disable it in Settings.

  • Change or remove a resource that already exists
    This will be done through a warm fix : This is also very fast, but you Instant Run must restart the current when you push these updates activity . Your app will still continue to run, but the activity screen may blink when it restarts-it's normal.

  • Structural changes such as:

    • Additions or deletions:

      • an annotation
      • a variable
      • a static variable
      • A method structure
      • A static method structure
    • Change the parent class of this class integration

    • Change the implementation list of an interface
    • change the static modifier for a class
    • li> using dynamic resource ID re-layout

    These changes will be through cold fix (API 21 or more support) To execute. The cold fix will be a little bit slower because although you don't need to build a new APK , you have to restart the entire app to push these structural code changes. For versions API 20 and below, Android Studio will redeploy APK .

  • Change the manifest file or change the manifest resource referenced in the file or change Android widget the implementation of the desktop UI (requires clean and rerun)
    When you change the resources referenced in a manifest file or in a manifest file, the Android Studio references are automatically redeployed to make changes to the content. This is because, for example, app names, icons, and intent filters these things are determined by the manifest file when the app is installed on the device.
    Android UI widgetWhen you update, you need to execute Clean and Rerun to see what's changed because it Instant Run clean takes a long time to execute when it's used, so you UI widget can disable it when you update Instant Run .

Using rerun

If you modify some of the content that will affect the initialization, such as modifying the method of the app onCreate() , you need to restart your app to make the changes take effect, and you can click on the Rerun icon. It will stop the app from running and clean then redeploy a new one to your device after performing the Operation APK .

Implementation principle

Under normal circumstances, we modify the content and want it to take effect, that requires the following steps:

Instant RunThe goal is also very simple:

Remove as much of the above steps as possible to keep the rest of the parts as fast as possible.

This means that:

    • Only the parts of the new changes are built and deployed.
    • Do not reinstall the app.
    • Do not restart the app.
    • Not even to reboot activity .

Under normal circumstances Run , when you click or Debug button, the following actions are performed:

The manifest file will be consolidated and then packaged with the resource restart of your application APK . The same .java source code will be compiled into binary, then converted to a .dex file, and they will be included in the APK .

In Instant Run the case of use, the first click Run and Debug Gradle will perform some additional actions:

instrumentationThe associated binary content is added to the .class file, and a new App Server class is injected into the application.
It also adds a new Application class that injects some customizations class loader and can be started App Server . So the minifest file will be modified to guarantee the use of the newly added Application class (if you have already created your own Application class, Instant Run the version will be expanded with the proxy extension you defined)

After the above operation Instant Run began to execute, if you change the Code section, in the re-click Run or Debug , Instant Run will be hot, warm, cold repair to minimize the above construction process.

Instant Run Android Studio Whether your app version is supported and running on a port that is useful to you before you change the content Instant Run App Server (socket is used internally). This will determine whether your app is running in the foreground and find the Studio build you need ID .

To summarize, it is in fact that each class file is internally injected with instant run the relevant code, and then customize an application internally specified custom classLoader (that is, do not use the default ClassLoader, as long as the use of instant Run will need to use its custom classloader), then open a server in the application, Studio send the modified code to the server, and then through the custom classLoader loading code will ask the server to determine whether the code is updated, If there is an update, the new updated code is loaded through the delegate mechanism and then injected into the application, which completes the replacement operation.

Heat Repair Process

Android Studiolistens to which file has changed during development, and Gralde task class generates the corresponding file only for the modified file through a custom. .dex
These new .dex files will be Studio published to the application running through delivery App Server .

Because these files are already in the running application at the beginning class -they need to be updated to ensure Gralde they can overwrite files that existed before. The conversion of these update files is App Server done by customizing class loader .

From now on, each time a method is called (regardless of where it is in the app), it is injected into the original class file to instrumentation App Server see if they are updated. If there is an update, the execution will be assigned to the new file that is filled in class , so that the newly modified method is executed.

If you set a breakpoint, you will find the override method in the class of the name you are calling.

This way of re-specifying the method is very effective when changing the method implementation, but what about the change that needs to be Activity loaded at startup?

Warm Repair

Warm repair will restart Activity . Resource files are Activity loaded at startup, so modifying them requires activity a reboot to load.

Now, changing any resource will cause all resources to be re-packaged and transferred to the app-but will later support the ability to package and deploy only those newly modified resources.

Note that warm fixes are manifest not valid when changing the contents of a piece or manifest file, because manifest the contents of the file need to be apk read at installation time. Changing Manifest the file (or what it refers to) needs to be fully built and deployed.

Unfortunately, rebooting is activity ineffective in making some structural changes. Cold fixes are required for these changes.

Cold Repair

At deployment time, your app and his sub-projects will be assigned to 10 different sections, each in their own separate dex files. Classes in different parts are assigned through the package name. When using cold repair, the modified class requires all other related files to be repackaged class before being deployed to the device.
This relies on the Android Runtime ability to support loading multiple .dex files, which is a ART new feature in, and it is only Android 5.0(API 21) supported on the devices above.
For API 20 the device, they are still using the Dalvik virtual machine and Android Studio need to deploy the whole APK .

Sometimes the code is changed by a hot fix, but it will be affected by the initialization of the app's first run, and you'll need restart your app to make it work (the shortcut is COMMAND + Ctrl + R).

Tips and tricks for using Instant run

Instant Runis Android Studio controlled by, so you can only pass IDE to start/restart your debug instance, not directly in the device of start/restart your application, otherwise the disorder will occur.

Instant Run throttling conditions
    • Deploy to multiple devices

      Instant Run different API level for devices use different technologies for hot, warm, cold repair. Therefore, if you deploy one application to multiple devices at the same time, Studio temporarily turns off the Instant Run feature.

    • Multidex

      If your project supports the traditional Multidex -that is, the build.gradle is configured multidexenabled true and minsdkversion and below-when you deploy a device that is applied to 4.4 , the Android Studio will disable Instant Run .

      If minsdkversion is set to 21 or higher, Instant Run is automatically configured to support Multidex , because Instant Run only supports the debug version, there is a need to configure your build variable to support when releasing release edition. Multidex .

    • Use third-party plugins
      Android Studio temporarily disable Java code Coverage when using Instant Run Library and Proguard . Because Instant Run only supports the debug version, it does not affect the build of the release version.

    • Can only run in the main process
      The current hot fix can only be done in the main process, and if hot, warm repair will not be available in other processes, it can only be replaced with a cold fix.

Reference:

    • Website
    • Instant run:how Does It work?!

For more articles please refer to Androidnote

Instant Run detailed

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.