[Android Pro] 告別編譯運行 ---- Android Studio 2.0 Preview發布Instant Run功能


reference to : http://www.cnblogs.com/soaringEveryday/p/4991563.html

以往的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老版本的項目和設置信息。

 

實際項目評測


 

這里我將用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.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM