告别编译运行 ---- Android Studio 2.0 Preview发布Instant Run功能

以往的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.

 

posted @ 2015-11-24 14:39  soaringEveryday  阅读(10202)  评论(28编辑  收藏  举报