以往的Android開發有一個頭疼的且拖慢速度的問題,就是你每改一行代碼要想看到結果必須要編譯運作到手機或者模拟器上,而且需要從頭(可能是登入界面)一直點選到你修改的界面為止。開發一個完整的Android App你可能要經曆無數個重複編譯運作的過程,嚴重的拖慢了開發進度。
最近React Native for Android可謂是解決了這個問題,修改代碼可以直接在模拟其上重新整理出來目前修改的界面(畢竟是用web技術)。于是乎Google能看得下去讓FB占領自己的開發領域嗎?不可能!
即時運作:更快的建構和部署
終于現在Android Studio 2 Preview推出了,其中一個革命性的功能就是Instant Run(即時運作)!新的即時運作功能可以讓開發者像寫html網頁一樣寫Android原生代碼,能做到一邊修改代碼,一邊在模拟器或者實際裝置上看到修改代碼後的結果。
下面是幾個平台上的下載下傳位址,下載下傳後直接解壓進入bin檔案夾就可以運作(建議保留之前Android Studio1.4或者1.5的版本不要删除),同時它會自動import老版本的項目和設定資訊。
- Windows: https://dl.google.com/dl/android/studio/ide-zips/2.0.0.0/android-studio-ide-143.2443734-windows.zip (320 MB)
- Mac: https://dl.google.com/dl/android/studio/ide-zips/2.0.0.0/android-studio-ide-143.2443734-mac.zip (319 MB)
- Linux: https://dl.google.com/dl/android/studio/ide-zips/2.0.0.0/android-studio-ide-143.2443734-linux.zip (318 MB)
實際項目評測
這裡我将用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.