1. 程式人生 > >Android Studio 2.1中的instant run功能 介紹

Android Studio 2.1中的instant run功能 介紹

Studio 2.0 之後就有了instant run 功能

說說 instant run  功能:字面意思就能明白 即時執行

這個功能在2.1是預設開啟的 而它的相關設定介面呢如下:


就是右邊的是個設定了 

如何判斷是否可以呢 通過 看到了 執行案例左邊的⚡️ 了嗎? 那就是可以了

先給 大家貼上 官方介紹文件

這個功能就是當你修改了程式碼或者佈局的時候 點選執行 它不會將整改專案都進行重新打包,而是能直接在你的測試機上體現。

比如我們以前調整了佈局後 需要看到效果就得重新打包運行了。

而現在呢就不用了,當然了使用方法是一樣的 。但是呢速度要快很多。幾秒內就能完成。

而如果是整體打包的話那速度就慢了。以前1.4版本的時候一分鐘就能打包完了。現在得3分鐘。

Code Change Instant Run Behavior
  • Change implementation code of an existing instance method or static method(改變現有的例項方法或靜態方法的實現程式碼。前提是這個實現方法和static方法是存在的,如果不存在 那就是新增了,新增請參考第三條)
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.
如果是左邊的這種情況,那就是熱插拔模式了,修改後能馬上看到效果(熱插拔大家都瞭解,就是現在的U盤的那種模式,插上就能用)
  • Change or remove an existing resource(你修改了佈局中的資源資訊,其實主要是layout檔案中的修改,比如這個佈局不要了, 我進行刪除啊,這個view 文字不正確 我就想修改等。如果是新增的話 那就是第三行的情況了)
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.
(如果是這種情況的話,那速度也挺快的 但是沒有上面的快,如果你要修改的這個頁面是正在顯示的頁面 它就會閃一下,然後你的修改就能看到你。 這種情況 你的app 不會重新啟動的。成功後 你的裝置中就會彈出一個Toash)
Structural code changes, such as:
(程式碼結構進行了變化)
  • Add, remove, or change: (比如下面的這些情況下的 新增,刪除和修改)
    • an annotation(註釋)
    • an instance field(例項欄位)
    • a static field(靜態欄位)
    • a static method signature(就是static方法被修改了,比如改名啦,改變傳參啦。)
    • an instance method signature(就是例項的簽名被修改的情況,比如類包名修改了,類名被修改了,例項名稱被修改了)
  • Change which parent class the current class inherits from(比如修改了被繼承的父類)
  • Change the list of implemented interfaces(修改列表實現的介面啦)
  • Change a class static initializer(改變類的static 初始化方法啦)
  • Reorder layout elements that utilize dynamic resource IDs(利用動態生成的id 來調整佈局元素 等)

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.

就是說 如果發生了左邊說的那種情況,而剛好你的api登記是在21或以上,那就會將修改結構程式碼更改推到目標裝置執行。這個時候你的app會重新啟動。但是耗時不會很長。

但是如果你的版本是20版本或以下,那就沒什麼好說的你。 重新生成構建整個apk了。那速度 可真是相當慢了。

  • Change the app manifest (如果你修改了androidmanifest檔案)
  • Change resources reference by the app manifest(如果你修改了manifes檔案引用的資原始檔)
  • Change an Android widget UI element (requires a Clean and Rerun)(如果你改變了一個UI控制元件的元素 比如說自定義控制元件 你給它新增加了個屬性 )

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.

這邊就告訴你說,這種情況下 instant run 就不起作用了。得重新打包生成整個專案了