As 2.0 new function Instant Run

Source: Internet
Author: User

Instant Run as an Android developer, many times we need to spend a lot of time on Bulid, running to a real machine (virtual machine), for iOSPlaygroundEnvious, this situation will be greatly improved in Android Studio 2.0, using instant Run,after the first run, you can quickly see the modified results in the real machine, not just the UI directly, but also the code logic。 No longer wait for build, save life!
1, first to upgrade to the Android Studio 2.0+
2. Then you need to upgrade the Android Tools Buildinstant Run feature before you can use the 2.* version of Android tools build. You need to specify dependencies {classpath ' com.android.tools.build:gradle:2.* '} in Build.gradle in the project hierarchy
3, set Instant runpreferences, Build,execution,deployment, Instant Run

4, run instant run no longer the project, our run icon is the same as before the running of the project after the icon is such a change of code after the direct click on the Run button can be immediately on your device to see the effect

Instant Run's background knowledge Instant Run is a new operating mechanism for Android studio2.0, which significantly reduces the time you build and deploy your current application when you encode development, test, or Debug. When we first clicked the Run, the Debug button, it ran the same time as we always did. But in the next time, every time you modify the code after clicking on the Run, the Debug button, the corresponding changes will be quickly deployed to the program you are running, the legend is faster than you can focus on the phone screen, it has made the corresponding changes.
A typical build cycle flowchart
Build-and-Deploy installation->app login->activity Create the goal of instant run: Eliminate as many unnecessary steps as possible and then increase the speed of the necessary steps. This means thatin practice:
    • Build and deploy only part of the code change
    • Do not reinstall apps
    • Do not restart the app
    • Do not restart activity
    • Hot-swappable, warm-swappable, cold-swappable
Instant Run = Incremental build + hot/warm/cold drawn plug
    • Hot Plug: Code changes are applied, projected onto the app, without restarting the app, and no need to recreate the current activity. Scenarios: Simple changes for most (including modifications to some method implementations, or variable value modifications)
    • Wimberg: The activity needs to be restarted to see the desired changes. Scenario: A typical case is that code modifications involve resource files, or resources.
    • Cold plug: The app needs to be restarted (but it still doesn't need to be reinstalled). Scenario: Anything that involves structural changes, such as modifying inheritance rules, modifying method signatures, and so on.

Instant Run principle
Manifest files are merged, packaged, and AAPT merged into the APK with Res, and the same project code is compiled into bytecode and converted to. dex files, which are also incorporated into the APK.
First run Instant Run,gradle action APK build process


        in an environment with instant run: A new app server class is injected into the app to monitor code changes in collaboration with bytecode instrumentation.         There will also be a new application class, which injects a custom ClassLoader (class Loader), and the application class will launch the new injected app Server we need. As a result, manifest will be modified to ensure that our application can use this new application class. (There's no need to worry about inheriting the application class, the new application class Instant run will proxy our custom application Class)        at this point, Instant run has been able to run, and when we use it, it will make a reasonable use of cold and warm plug to help us reduce the time to build the program.         before instant run runs, Android studio checks to see if it can connect to the app server. And make sure that this app server is needed for Android studio. This also ensures that the application is in the foreground because multiple devices are currently not supported for simultaneous execution. Hot Plug
        Android Studio monitors: Running the Gradle task to generate an incremental. dex file (this Dex file is corresponding to the modified class in development) Android Studio will extract these. dex files to the app Server, Then deploy to the app.         because the original version of the class is loaded in the running program, Gradle will translate, update these. dex files, send to the app server, to the custom class loader to load the. dex file. Take a look at the schematic diagram below:
        The App server constantly listens for the need to rewrite the class file, and if necessary, the task is executed immediately. The new changes can be immediately responded to. We can find out by interrupting the point of debugging that it does.
Warm plug        Wimberg needs to restart the activity because the resource file is loaded at activity creation, so the activity must be restarted to reload the resource file .         For now , any modification to the resource file will result in repackaging and then sending it to the app. However, Google's development team is working on developing an incremental package that will only wrap the modified resource files and be deployed to the current app.
        Note: The temp plug involves the resource file modification, which is invalid on manifest (here the invalid is not to start instant Run), because the value of manifest is read when the APK is installed, so you want to manifest the resources under the change to take effect, There is also a need to trigger a complete application build and deployment. To sum up: If you modify the manifest related resource files, you still need to face the same turtle speed as before.         so Wimberg actually can only deal with a few cases, it can not cope with the application of the structure, structural changes. For example: Annotations,fields additions and deletions, changes to the parent file, static modified classes, methods, constants and so on can only rely on cold plug.
Cold Drawn Plug        when the application is deployed, the project is split into 10 parts, each with its own. dex file, and all classes are assigned to the appropriate. dex file based on the package name. When the cold plug is turned on, the. dex file for the modified class is reorganized to generate a new. dex file and then deployed to the device.         The reason you can do this is to rely on Android's ART mode, which allows you to load multiple. dex files. Art mode is added in android4.4, but Dalvik is still the first choice, so the Android5.0,art mode becomes the system default preference, so instant run only runs in API-21 and above, and as for the lower version, the entire application is rebuilt.
The Instant run technique Instant Run is controlled by Android studio. So we can only launch it through the IDE, and if you start the app through the device, the Instant run will have an exception.
    • If the minsdkversion of the app is less than 21, most of the instant run functions may be hung out. Can be used to debug by creating a new branch with a minsdkversion greater than 21.
    • Instant run can currently only run in the main process, if the application is multi-process, similar to the webview out of a single process, the hot, Wimberg will be downgraded to cold plug.
    • Under Windows, Windows Defender Real-time protection may cause instant run to be hung out and can be resolved by adding a whitelist list.
    • Jack Compiler,instrumentation Tests is not supported temporarily or deployed to multiple devices at the same time.
    • Instant run cannot be rolled back. Code changes can be quickly deployed by hot-swapping, but hot-swapping affects the initialization of the app, so we have to respond to those changes by restarting the app.



From for notes (Wiz)

As 2.0 new function Instant Run

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.