Instant Run 立即刷新功能

      原文地址:點擊打開鏈接

      以往的Android開發有一個頭疼的且拖慢速度的問題,就是你每改一行代碼要想看到結果必須要編譯運行到手機或者模擬器上,而且需要從頭(可能是登錄界面)一直點擊到你修改的界面爲止。開發一個完整的Android App你可能要經歷無數個重複編譯運行的過程,嚴重的拖慢了開發進度。

最近React Native for Android可謂是解決了這個問題,修改代碼可以直接在模擬其上刷新出來當前修改的界面(畢竟是用web技術)。於是乎Google能看得下去讓FB佔領自己的開發領域嗎?不可能!


即時運行:更快的構建和部署


      終於現在Android Studio 2 Preview推出了,其中一個革命性的功能就是Instant Run(即時運行)!新的即時運行功能可以讓開發者像寫html網頁一樣寫Android原生代碼,能做到一邊修改代碼,一邊在模擬器或者實際設備上看到修改代碼後的結果。


實際項目評測


      這裏我將用Android Studio 2.0 配合 Genymotion模擬器實際演示一個項目

進入Android Studio2.0打開項目後依次進入Setting->Build,Execution,Deployment->Instant Run查看即時運行的設置項目,你可能會發現勾選項目是灰色的,如圖


      這個是因爲你的project gradle是舊的,點擊下Update Project稍等片刻就好。

      更新我發現Project gragle的依賴:

dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
    }

被更新成了:

dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha1'
    }

 

      這個時候再次打開Instant Run的設置會發現已經可以勾選了,請保持如圖的勾選:

 

      此時我們觀察運行按鈕的左側多了一個類似於“閃電”的標誌:

 

      我們的項目中有這樣的一個頁面:

 

      準備把臨時拜訪換成別的字串比如“你好”,同時換掉左邊的Icon。它是一個擁有自定義屬性的自定義控件,佈局代碼片段爲:

複製代碼
   <com.qianmi.shine.widget.CommonLeftIconRightButtonRelativeLayout
        android:id="@+id/ll_sudden_visit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:CLIRBRIconId="@drawable/icon_temp"
        app:CLIRBRTitleName="@string/sudden_visit"
        app:CLIRBRActionIconId="@drawable/btn_go_nor"
        />
複製代碼

 

      首先我們需要先跑一下這個項目,然後先點擊界面直到上述的界面爲止停住不動,這個時候我們再修改上述代碼(這一步是必須的,不然的Instant Run功能使用時會出現問題,導致重新運行)

     這個時候我們讓模擬器保持在這個頁面上,同時修改佈局代碼成:

複製代碼
<com.qianmi.shine.widget.CommonLeftIconRightButtonRelativeLayout
        android:id="@+id/ll_sudden_visit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:CLIRBRIconId="@drawable/icon_resent"//修改1
        app:CLIRBRTitleName="你好"//修改2
        app:CLIRBRActionIconId="@drawable/btn_go_nor"
        />
複製代碼

 

      然後點擊帶閃電的運行:

 

      可以看到界面快速的刷新成了:

 

最後說明


       需要說明的是,我在使用過程中發現,改Instant Run僅僅適用於佈局的修改。即我們可以把一次修改然後到運行看效果看作一個“週期”,在這個週期裏面你僅僅修改了xml佈局文件,或者說和邏輯代碼不相關的文件,那麼你點擊運行的時候纔會觸發Instant Run,否則的話,Android Studio還是依然會重新編譯運行。

      其實想想也是合理的,比如若你修改了代碼,而該代碼恰好是當前界面的“邏輯前提”,那麼你怎麼僅僅刷當前界面就能得到正確結果呢?

對於到底目前Instant Run支持哪些形式的代碼修改,官方有一篇文章可供參考

https://sites.google.com/a/android.com/tools/tech-docs/instant-run

 

Not all code changes are supported by Instant Run currently. Here is the current list of supported code change scenarios.



Code Change

Instant Run Support

Change instance method implementation

Change static method implementation

Add or remove a class

Supported

Add, remove, or change a string resource

Supported but requires an Activity restart.

 

Here are some code changes that Instant Run does not currently support:

  • Add/remove/change annotations

  • Add/remove/change an instance field

  • Add/remove/change a static field

  • Add/remove a static method signature

  • Change a static method signature

  • Add/remove an instance method

  • Change an instance method signature

  • Changing which parent class the current class inherits from

  • Change the list of implemented interfaces

  • Changing static initializer of a class

 

Over the coming months, we plan to expand the Instant Run enable more change types, and continue to make your edit, build, run cycle faster.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章