Android Studio中的Instant Run

Android Studio 2.3版本过后提供了一种Instant Run(立即运行)运行机制,大大提高了应用程序从编译,到运行的速度, 它能在不重启应用程序的情况下,把代码修改直接运行,有时候甚至不用重启Activity, 下面来谈谈如何使用这个功能。
首先使用这个功能的前提是:
1,targetSdkVersion必须 >= 21
2,android plugin for Gradle >= 2.3.0
3,minSdkVersion >= 15
4,必须运行在5.0+的设备上
5,Android Studio版本 >= 2.3

如果你的工程满足以上条件,恭喜你,可以进行Instant Run开发.

红色剪头指导的这个按钮就是Instant Run,当代码修改过后,直接点击Instant Run按钮,一般会很快的运行起来(不用像以前点击运行,需要重新编译,app重启运行等待这么久了).
其实具体来说跑起来花多久时间,还是根据具体情况来定,一般有三种模式。
hot swap:这种方式非常快,app保持运行,有时候连Activity都不需要重启. 如果修改一个方法里面的代码,是采用该模式。
warm swap:这种方式也很快,app保持运行,但是要重启Activity. 如果修改移除存在的资源,是采用该模式
cold swap:这种方式会比上面两种方式要慢点,需要重启应用.如果增加移除修改注解,实例区域,静态区域,一个实例的方法结构,重新加载新的资源id是采用该模式。
另外,manifest里面的修改和系统控件的修改(就像widget和通知),这种情况是需要完全重新编译安装的。
有时候这个Instant Run是置灰状态,不能够点击

因为它还是disable状态,我们需要enable状态。通过以下方式打开它
1,打开Settings或者Preferences对话框
2,选中Build, Execution, Deployment > Instant Run然后选中Update Project

如果没有显示Update Project说明已经是最新,我们只需保证Enable Instant Run被选中就可以.
另外值得注意的是debug variant才能使用,记得切换到debug variant运行.

完整构建和部署

首先我们来看下没有使用Instant Run,代码改变之后的完整构建和部署的流程

完整构建和部署流程.png

Android Build Process流程图.png

我们可以看到,在没有Instant Run时,代码变更之后运行,需要构建Application,你的Mainfest文件被合并,和资源文件一起被打包到APK里面,类似的,Java文件被编译成字节码,然后转换成DEX文件,最后部署App,重新启动App,重新启动Activity,最终变更的代码才能变执行。之间需要花费大量的时间,工程小还好,一旦大了就需要花费几分钟甚至几十分钟时间,严重影响了我们开发的效率和心情(最重要的是心情!),还能不能愉快地编程了!Google真是急人之所急,我们的救世主呀,在Android Studio 2.0之后就推出了Instant Run功能,看字面意思就知道是即时运行的意思,本着工程师探索原理的精神,让我们一探究竟。

Instant Run分类

Instant Run .png

我们可以看到,和完整构建、停止、重新安装和加载App相比,Instant Run 只增量构建和部署代码改变的部分,在很大程度上缩短构建和部署的时间。Instant Run有3种交换类型,分别是Hot Swap、Warm Swap和Cold Swap,Instant Run会根据改变代码的类型,自动决定使用哪种类型。

Instant Run如何工作

Instant Run构建

Instant Run构建流程.png

根据上图,我们可以看到Instant Run构建流程,首先增加字节码到你的Class文件,用来让我们之后进行替换,然后增加一个新的App Server Class运行在之后你的App里面,用来传输之后在运行期变化的代码,Gradle也会将修改Mainfest进行合并,让我们运行实时代码的改变。

App Server运行

App Server运行流程.png

当你在Android Studio点击Run按钮时,首先会检测在你的App里是否有一个开启Socket端口的Server在运行,来确定Instant Run是否可用,在发送变化的代码之前它也会检测App构建的ID来确定是期望的版本,当你在开发过程中,Android Studio会监控哪些文件已经被改变,运行一个自定义的Gradle Task为那些改变的class文件创建一个DEX文件,这个新的DEX文件会被Android Studio部署到我们运行的App Server里面,App Server接收到之后就会加载和部署被更新的classes,使用之前注入到我们原始类,委托方法来调用我们刚才加载覆盖的新classes里的方法。

Hot Swap

Hot Swap.png

 

Hot Swap主要用于改变现有方法的实现代码导致的代码改变,这是最快的一种交换方式,不需要重启App或者Activity,在你下一次调用方法时,新的实现就能被调用。

Warm Swap

Warm Swap.png

Warm Swap主要用于修改或者删除已经存在的资源文件,这个交换仍然非常快,但需要重启Activity,因为加载这些被影响的资源,重启是必须的。虽然改变的所有资源都需要被重新打包,但我们是增量开发,可以只打包和部署已经改变的资源,不过和Mainfest相关的资源改变或者Mainfest自身的改变,Warm Swap不会有任何作用,那是因为从Mainfest里面读取值,在APK被安装的时候就已经被确定下来,所以那些改变就需要重新安装才能看到。从这里我们就能看出来,Instant Run并不支持改变定义App名称的字符串或者App图标。

Cold Swap

Cold Swap.png

Cold Swap支持新增、移除、修改注释、实例或者静态字段,实例或者静态方法签名、改变父类classes或者类静态初始化。这种交换会慢一点,虽然不需要完整构建一个新的APK,但需要重启Application。原理就是发送有效代码的DEX文件到指定设备,通过App加载多个DEX来实现代码的改变,这就需要ART,所以Cold Swap只在Android 5.0及以上的设备才有效,那些Android 5.0以下的设备就需要部署一个完整的APK。

总结

到目前为止,我想大家已经理解了这3种交换类型的原理,也能了解其中的局限性,做到知其然,知其所以然。最后祝大家利用好Instant Run,享受即时编程。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 3.2 Development Essentials – Kotlin Edition 版本: Developing Android 9 Apps Using Android Studio 3.2, Kotlin and Android Jetpack By 作者: Neil Smyth ISBN-10 书号: 0960010939 ISBN-13 书号: 9780960010936 出版日期: 2018-12-06 pages 页数: (1037) Assuming you already have some programming experience, are ready to download Android Studio and the Android SDK, have access to a Windows, Mac or Linux system and ideas for some apps to develop, you are ready to get started. 1. Introduction 2. Setting up an Android Studio Development Environment 3. Creating an Example Android App in Android Studio 4reating an sngdroid vurtual bevice tsvb in Android tudlo 6. ATouroftheAndroid studio user terface 7. Testing Android Studio Apps on a Physical Android Device 8. The Basicsofthe Android Studio Code Editor 10. The Anatomy of an Android Application 11. An Introduction to Kotlin 12. Kotin Data Types, VariablesandNullablty 1A katHnHwCaLCd 15. An Overview of Kotin Functions andLambdas 16. The Basics of Object Oriented Programming in Kotlin 18. Understanding Android Application and Activity Lifecycles 19. Handling Android Activity state Changes 20. Android Activity State Changes by Example SJOOVImgOgheStoung nenEian Anurou agtuy. 23. AGuide to the Android Studio Layout Editor Tool 24.A Guide to the Android ConstraintLayout 26. Wartinowith constaimtLayout chansand Ratiosin AndroidStudi. Z7. An Android Studio Layout Edtor ConstraintLayout Tutorial 28. ManualXML Layout Designin Android Studio 42-Managing constraintsysing constraint sets 31. AGuide to using Instant Run in Android Studio 32. An Overview and Example of Android Event Handling AnMCCCCcMaTMCLEOLDY CDaCorc. 35. Implementing Custom Gesture and Pinch Recognition on Android 36. An Introduction to Android Fragments 37. Using Fragmentsin Android Studio-An Example 39. An Android letpack viewModel Tutorial 40. An Android Jetpack LiveData Tutorial 41. An Overview of Android Jetpack Data binding 43. Working with Android Lifecycle-Aware Components 44. An Android JetpackLifecycle Awareness Tutorial 45. An Overview of the Navigation Architecture Component 47. CreatingandManaaing overflow Menus. on Android 48. Animating User Interfaceswith the Android Transitions Framework 49. An Android Transition Tutorial using beginDelayedTransition sI wWatinawthbthe Fbatino A-tionBaittan andsnactbar 52. Creating a Tabbed Interface using the TabLayout Component 53. Working with the ReyclerView and CardViewWidgets A ANAOCTOLLIHECYCIETYISNODCOrCYew ItOrial 56. Working with the AppBar and Collapsing Toolbar Layouts 57. Implementing an Android Navigation Drawer 2 AnAndrOid StUWIO MaSteruOetalFlow Iutorial 60. Android Explicit Intents-A Worked Example 61. AndroidlmplicitIntents-A Worked Example O4AngTOIO brOadCast Intents and broaacast ihecewers 64. AnOverview of Android Startedand Bound Services 65. Implementing an Android Started Service -A WorkedExample bC. Ancdroidtocalbound Services -A WorkedExample 68. An Android Notificatons Tutorial 69. An Android Direct Reply Notification Tutorial . AnhTrcdwto. to AntowMtutiindewpi . cbom ritoal Z. AnoenwewofAndrad solteoatdbases L 73. The Android Room Persistence Library 74. An Android Tablelayoutand TableRow Tutorial 76. Acesing Cloud Storage using the Android Storage Acces Framework 7. An Android Storage Aces Framework Example 78. Implementing VideoPlaybackon Androidusing the VideoViewand Mediacontroller Clases 80. An AndroidPicture in-Picture Titorial 81. Video Recording and Image Capture on Android using Camera lntents 82. Making Runtime Permision Requestsin Andraid 6. Ancdtonubuctirfecordingoandthaybackwinghediarbyerangdhecdatecorder 85. Printingwith the Android Printing Framework 86. An Android HTML and Web Content Printing Example RAC-CC. CCACGCLXCCH-CIN9 89. An Android Studio App Links Tutorial 90. An Introduction to AndroidInstantApps 2. AMbspwtngwn Andfoasudoporectornstants. Apps 93. AGuicde tothe Android studioProfler 94. An Android Biometric Authentication Tutorial 5. Creating, TestinganduploacinganAncroidAppBunde

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值