博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android Studio2.0的逆天功能Instant Run(告别编译运行)
阅读量:4068 次
发布时间:2019-05-25

本文共 2388 字,大约阅读时间需要 7 分钟。



作为一个Android开发者,很多的时候我们需要花大量的时间在bulid,运行到真机(虚拟机)上,对于ios上的Playground羡慕不已,这种情况将在Android Studio 2.0有了很大改善,使用instant run,在第一次运行之后,就可以快速的在真机中看见修改后的结果,不仅仅是UI可以直接显示,还包括代码逻辑。不用再苦苦等build了,节约生命呀!

 

即时运行:更快的构建和部署


 

终于现在Android Studio 2 Preview推出了,其中一个革命性的功能就是Instant Run(即时运行)!新的即时运行功能可以让开发者像写html网页一样写Android原生代码,能做到一边修改代码,一边在模拟器或者实际设备上看到修改代码后的结果。

下面是几个平台上的下载地址,下载后直接解压进入bin文件夹就可以运行(建议保留之前Android Studio1.4或者1.5的版本不要删除),同时它会自动import老版本的项目和设置信息。

  • Windows:  (320 MB)

  • Mac:  (319 MB)

  • Linux:  (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。它是一个拥有自定义属性的自定义控件,布局代码片段为:

 

首先我们需要先跑一下这个项目,然后先点击界面直到上述的界面为止停住不动,这个时候我们再修改上述代码(这一步是必须的,不然的Instant Run功能使用时会出现问题,导致重新运行)

这个时候我们让模拟器保持在这个页面上,同时修改布局代码成:

 

然后点击带闪电的运行:

 

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

 

最后说明


 

需要说明的是,我在使用过程中发现,改Instant Run仅仅适用于布局的修改。即我们可以把一次修改然后到运行看效果看作一个“周期”,在这个周期里面你仅仅修改了xml布局文件,或者说和逻辑代码不相关的文件,那么你点击运行的时候才会触发Instant Run,否则的话,Android Studio还是依然会重新编译运行。

其实想想也是合理的,比如若你修改了代码,而该代码恰好是当前界面的“逻辑前提”,那么你怎么仅仅刷当前界面就能得到正确结果呢?

对于到底目前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.

转载地址:http://hgxji.baihongyu.com/

你可能感兴趣的文章
Unix + OS IBM Aix FTP / wu-ftp / proftp
查看>>
framework apache commons
查看>>
my read work
查看>>
blancerServer IBM WebSphere Edge Server 6.1
查看>>
db db2 base / instance database tablespace container
查看>>
my read _job
查看>>
hd disk / disk raid / disk io / iops / iostat / iowait / iotop / iometer
查看>>
project ASP.NET
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
OS + Unix Aix telnet
查看>>
IBM Lotus
查看>>
Linux +Win LAMPP Tools XAMPP 1.7.3 / 5.6.3
查看>>
my read_university
查看>>
network manager
查看>>
searchServer IBM OminiFind / WebSphere Commerce SOLR
查看>>
OS + Linux Disk disk lvm / disk partition / disk mount / disk io
查看>>
my read_Country
查看>>
RedHat + OS CPU、MEM、DISK
查看>>
project bbs_discuz
查看>>
net TCP/IP / TIME_WAIT / tcpip / iperf / cain
查看>>