http://tools.android.com/tech-docs/instant-run

N Developer Preview users: Instant Run is currently incompatible with the Jack toolchain. This new toolchain is required to use Java 8 language features supported by Android N. To learn more, read about the new Java 8 language features and Jack.

 

Introduced in Android Studio 2.0, Instant Run is a behavior for the Run  and Debug  commands that significantly reduces the time between updates to your app. Instant Run pushes changes to methods and existing app resources without building a new APK, so code changes are visible almost instantly.

Instant Run is supported only while deploying a debug build variant, using Android Plugin for Gradle version 2.0.0 or higher, and targeting devices running Android 4.0 (API level 15) and higher.

After deploying an app, a small, yellow thunderbolt icon appears within the Run  button (or Debug  button), indicating that Instant Run is ready to push updates the next time you click the button. Instead of building a new APK, it pushes just those new changes and, in some cases, the app doesn't even need to restart but immediately shows the effect of those code changes.

Note: Instant Run temporarily disables the Java Code Coverage Library (JaCoCo) and ProGuard. Because Instant Run only works with debug builds, this should not affect your release build.

Instant Run pushes updated code and resources to your connected device or emulator by performing a hot swapwarm swap, or cold swap. It automatically determines the type of swap to perform based on the type of change you made. The following table describes how Instant Run behaves when certain code changes are pushed to a target device.

Code Change Instant Run Behavior
  • Change implementation code of an existing instance method or static method
Supported with hot swap: This is the fastest type of swap and makes changes visible almost instantly. Your application keeps running and a stub method with the new implementation is used the next time the method is called.
  • Change or remove an existing resource
Supported with warm swap: This swap is still very fast, but requires an automatic activity restart when Instant Run pushes the changed resources to the target device. Your app keeps running, but a small flicker may appear on the screen as the activity restarts—this is normal.
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 static initializer
  • Reorder layout elements that utilize dynamic resource IDs

Supported with cold swap (API level 21 or higher): Instant Run pushes the structural code changes to the target device and restarts the whole app.

For target devices running API level 20 or lower, Android Studio deploys a full build of the APK.

  • Change the app manifest
  • Change resources reference by the app manifest
  • Change an Android widget UI element (requires a Clean and Rerun)

When making changes to the app manifest or resources referenced by the manifest, Android Studio automatically deploys a new build in order to push changes to your target device. This is because certain information about the app, such as its name, app icon resources, and intent filters, are determined from the manifest when the APK is installed on the device.

If your build process automatically updates any part of the app manifest, such as automatically iterating versionCode orversionName, you will not be able to benefit from the full performance of Instant Run. We recommend that you disable automatic updates to any part in the app manifest in your debug build variants.

By default, Android Studio automatically restarts the current activity after performing a hot swap, while the app keeps running. To disable this setting:

  1. Open the Settings or Preferences dialog.
  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.

Using Rerun

When pushing certain code changes, such as changes to an app's onCreate() method, you will need to restart your running app for the changes to take effect. You can click Rerun  to stop the app, perform an incremental build, and restart the app.

If you need to deploy a clean build, select Run > Clean and Rerun 'app'  from the main menu, or hold down the Shift key while clicking Rerun . This action stops the running app, performs a full clean build, and deploys the new APK to your target device.

Setting up your project for Instant Run

Android Studio enables Instant Run by default for projects built using Android Plugin for Gradle 2.0.0 and higher.

To update an existing project with the latest version of the plugin:

  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 and click Update Project, as shown in figure 1.

    If the option to update the project does not appear, it’s already up-to-date with the latest Android Plugin for Gradle.

    Figure 1. Updating the Android Plugin for Gradle for an existing project.

Things you should know

Instant Run is designed to work in most situations and to speed up the build and deploy process on a best-effort basis. However, there are some aspects to using Instant Run that you should be aware of.

Legacy Multidex with Android 5.0 or higher

When using Instant Run with a project configured for Legacy Multidex—that is, when build.gradle is configured withmultiDexEnabled true and minSdkVersion 20 or lower—build performance may decrease when deploying a clean build to target devices running Android 5.0 (API level 21) or higher.

After the initial clean build, incremental builds are significantly faster, and Instant Run deploys code and resource changes as normal. To improve clean build performance during development, consider creating a product flavor withminSdkVersion 21.

Exceeding the 64K DEX limit

In order to push updates to a running app, Instant Run needs to add additional methods to every debug APK–approximately 140 plus three times the number of classes and their local dependencies. This also increases the total number of methods referenced by the app’s DEX file.

If an app was previously approaching the 64K reference limit, enabling Instant Run may push it over this limit and cause a build error. Learn how to overcome this issue by configuring apps for Multidex.

Deploying to multiple devices

Instant Run uses different techniques to perform hot, warm, and cold swaps that are specific to the API level of the target device. For this reason, while deploying an app to multiple devices at once, Android Studio temporarily turns off Instant Run.

Instrumentation tests

Instrumentation tests load both the debug APK and a test APK into the same process on a test device, allowing control methods to override the normal lifecycle of the app and perform tests. While running or debugging instrumentation tests, Android Studio does not inject the additional methods required for Instant Run and turns the feature off.

While profiling an app, you should disable Instant Run. There is a small performance impact when using Instant Run and a slightly larger impact when overriding methods with a hot swap. This performance impact could interfere with information provided by performance profiling tools. Additionally, the stub methods generated with each hot swap can complicate stack traces.

Disabling Instant Run

To disable Instant Run:

  1. Open the Settings or Preferences dialog.
  2. Navigate to Build, Execution, Deployment > Instant Run.
  3. Uncheck the box next to Enable Instant Run.

We Want Your Feedback!

Instant Run is currently available only in Android Studio 2.0, which is still in the Beta Channel (and Android Studio 2.1 in Canary.) Over the next little while, we will continue to improve the feature, stabilize it as we move the release to the Stable channel.

Here are also some known issues that we are working on:

  • Apps that are using legacy multi-dex (with minSdkVersion < 21), with a main dex file near the 65K method limit may not build. This would require reworking the app to make room in the main dex list by reducing the number of classes required in the main dex file.

  • We have tested Instant Run on many devices, but it’s impossible to try it on every single device model. Please let us know if Instant Run doesn’t work on your device.

  • We have seen some intermittent issues where the IDE loses connection with the app which will trigger a full rebuild.

  • We have not yet tested this feature with 3rd party Gradle plugins, especially those that have not been updated to use the new transforms API. If you run into issues, please let us know.

  • Data-binding is broken in this build; we will restore this shortly.

If you run into any additional issues, please help by logging bugs in our public Issues Tracker. You can create an issue viathis link or by clicking on Help → Submit Feedback directly in Android Studio.

Instant Run的更多相关文章

  1. Error running app: This version of Android Studio is incompatible with the Gradle Plugin used. Try disabling Instant Run.

    转自:http://blog.csdn.net/qq_15807167/article/details/51984920 参考:http://stackoverflow.com/questions/3 ...

  2. This version of android studio is incompatible with the gradle version used.Try disabling the instant run解决办法

    今天打开android studio又碰到一个奇怪的问题:This version of android studio is incompatible with the gradle version ...

  3. 告别编译运行 ---- Android Studio 2.0 Preview发布Instant Run功能

    以往的Android开发有一个头疼的且拖慢速度的问题,就是你每改一行代码要想看到结果必须要编译运行到手机或者模拟器上,而且需要从头(可能是登录界面)一直点击到你修改的界面为止.开发一个完整的Andro ...

  4. [Android Pro] 告别编译运行 ---- Android Studio 2.0 Preview发布Instant Run功能

    reference to : http://www.cnblogs.com/soaringEveryday/p/4991563.html 以往的Android开发有一个头疼的且拖慢速度的问题,就是你每 ...

  5. [原创]Android Studio的Instant Run(即时安装)原理分析和源码浅析

    Android Studio升级到2.0之后,新增了Instant Run功能,该功能可以热替换apk中的部分代码,大幅提高测试安装的效率. 但是,由于我的项目中自定义了一些ClassLoader,当 ...

  6. AS 2.0新功能 Instant Run

    Instant Run上手 作为一个Android开发者,很多的时候我们需要花大量的时间在bulid,运行到真机(虚拟机)上,对于ios上的Playground羡慕不已,这种情况将在Android S ...

  7. Android新特性Instant Run详解

    关于 Instant Run Android Studio 2.0 中引入的 Instant Run 是 Run 和 Debug 命令的行为,可以大幅缩短应用更新的时间.尽管首次构建可能需要花费较长的 ...

  8. android studio2.0出现的gradle问题,instant Run即时运行不了.

    android studio 2.0出现的gradle问题: instant Run即时运行不了.经历了几乎9个preView版本的AS2.0,终于迎来了正式版,然而晴天我的霹雳,好不容易装好的2.0 ...

  9. Android Studio 2.3 instant run与miui冲突问题的解决

    Android Studio最近发布的2.3版本,由于这个版本改进后的Instant Run功能和很多国内ROM存在兼容问题,所以导致不得不做一些妥协策略,具体在小米Rom上,就是把小米rom的调试定 ...

随机推荐

  1. pascal矩阵 分类: 数学 2015-07-31 23:01 3人阅读 评论(0) 收藏

    帕斯卡矩阵 1.定义       帕斯卡矩阵:由杨辉三角形表组成的矩阵称为帕斯卡(Pascal)矩阵. 杨辉三角形表是二次项 (x+y)^n 展开后的系数随自然数 n 的增大组成的一个三角形表. 如4 ...

  2. PHP实例开发(1)PHP站内搜索

    PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...

  3. 关于获取目录的N种方法 的汇总

    前段时间在Global.asax.cs中的Session_End中使用Server.MapPath() 出现"服务器操作在此上下文中不可用"异常. 网络上给出的解决方案:Syste ...

  4. codeForce-19D Points (点更新+离散化)

    题目大意:在二维坐标系的x正半轴,y正半轴和第一象限内,有三种操作: 1.add x,y (添加点<x,y>): 2.remove x,y(移除点<x,y>): 3.find ...

  5. Linux驱动设计—— 内核模块(一)

    Linux内核理论基础 组成Linux内核的5个子系统:进程调度(SCHED)/内存管理(MM)/虚拟文件系统(VFS)/网络接口(NET)/进程间通信(IPC). 进程调度(SCHED) 在设备驱动 ...

  6. GNU C 扩展(转)

    GNU CC 是一个功能非常强大的跨平台 C 编译器,它对 C 语言提供了很多扩展,这些扩展对优化.目标代码布局.更安全的检查等方面提供了很强的支持.这里对支持支持 GNU 扩展的 C 语言成为 GN ...

  7. 采用PHP函数uniqid生成一个唯一的ID

    http://www.daimajiayuan.com/sitejs-17815-1.html

  8. SQL Debugging

    C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn>“C:\Program Files\Debugging ...

  9. JSBinding + SharpKit / 需要注意及不支持的列表

    1) 序列化不支持 public List<T>,其余都支持(JSBinding+Bridge无此功能) 2015年11月5日 补充:序列化只处理 Field.目前发现 Animation ...

  10. QQ登入(5)获取空间相册,新建相册,上传图片到空间相册

    ///////////////////////////////////////////////////////////////////// 获取相册列表:必须先授权登入 1.1.  String mA ...