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文件中決定的。 |