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. Although your first build may take longer to complete, Instant Run pushes subsequent updates to your app without building a new APK, so changes are visible much more quickly.
Instant Run is supported only when you deploy the debug build variant, use Android Plugin for Gradle version 2.0.0 or higher, and set minSdkVersion
to 15 or higher in your app's module-level build.gradle
file. For the best performance, set minSdkVersion
to 21 or 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.
Instant Run pushes updated code and resources to your connected device or emulator by performing a hot swap, warm 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 you push certain code changes to a target device.
代码的改变类型
|
即时运行的行为
|
改变一个已经存在的方法的实现
|
支持热交换,这是最快的类型,可以快速地看到代码变化,你的应用会保持运行,下次你调用到那个更改的方法时,会执行新的代码。
热交换并没有重新实例化你app中的对象,在看到更新前,你可能需要重启当前的activity或者应用。默认情况下,studio会自动重启当前activity。也可以自己设置不自动重启。
|
更改或者移除一个存在的资源
|
支持暖交换,这种交换还是很快。即时运行必须重启你当前的activity来完成资源的更新,应用会一直运行,不过重启activity时屏幕可能会闪烁,这是正常情况。
|
结构代码的改变,例如: 增加,移除,更改: 1.注释 2.实例域 3.静态域 4静态方法名? 5.实例方法名? 6父类的改变 7继承的接口发生变化 8类的构造方法 9重新布局了使用动态资源ID的空间 |
在api21及以上的版本支持冷交换,该交换会慢一些,因为需要重新启动应用,才能将更改提交到应用中。
在21一下的版本中,studio会构建一个完整的apk
|
更改了androidmanifest文件
更改了androidmanifest文件中引用到的资源
更改了android插件的UI元素(需要clean和rerun)
|
修改了manifest文件或其引用的资源时,studio 会自动构建一个新的apk来更新该改变,因为应用在安装到设备上时的一些固定信息是由manifest文件中决定的。 |