zoukankan      html  css  js  c++  java
  • [Android] android studio 2.0即时运行功能探秘

    即时运行instant Run是android studio 2中,开发人员最关心的特性之一
    在google发布studio 2.0之后,马上更新体验了一把,然而发现,并没快多少,说好的即时运行呢?
    于是去看了下官方文档:

    Introduced in Android Studio 2.0, Instant Run is a behavior for the Run  and Debug  commands that significantly reduces the time between updates to your app. Although your first build may take longer to complete, Instant Run pushes subsequent updates to your app without building a new APK, so changes are visible much more quickly.

    Instant Run is supported only when you deploy the debug build variant, use Android Plugin for Gradle version 2.0.0 or higher, and set minSdkVersion to 15 or higher in your app's module-level build.gradle file. For the best performance, set minSdkVersion to 21 or higher.

    After deploying an app, a small, yellow thunderbolt icon appears within the Run  button (or Debug  button), indicating that Instant Run is ready to push updates the next time you click the button. Instead of building a new APK, it pushes just those new changes and, in some cases, the app doesn't even need to restart but immediately shows the effect of those code changes.

    Instant Run pushes updated code and resources to your connected device or emulator by performing a hot swapwarm swap, or cold swap. It automatically determines the type of swap to perform based on the type of change you made. The following table describes how Instant Run behaves when you push certain code changes to a target device.

     
    即时运行时Run和Debug两个指令的行为,可以明显的减少更新你的APP的时间,虽然首次build可能会更耗时,但即时运行在你的app中加入了你的更改,而不是重新构建一个新的APK,使得我们可以更快地看到app的变化。
     
     
    重点来了,即时运行只在以下情况才能被支持:
      1,你必须配置debug build variant.   如何配置:IDE的左下角,有个小标签build variant,里面可以配置你的应用跑起来时是debug还是release,默认都是debug,所以不需要特别去配置;
      2,你必须使用 Android Plugin for Gradle version 2.0.0  或者更高的版本,这个在应用迁移到android studio 2.0的时候会提示你,你只要点击确定就好啦~或者在工程根目录的build.gradle中修改也行;
      3.,应用的最小支持版本要在15或以上,google推荐21及以上的版本以获得最好的性能~
     
      配置完一个应用后,Run和Debug的按钮旁边就会多出一个闪电~表明即时运行已经准备好了~你只需点击按钮,就能快速看到新的变化哦~在一些情况下,你的应用甚至不需要去重启,就可以直接看到代码更改带来的变化!!!
    (实际测试的话,配置完是需要先跑一次的,此时做出更改的话,才会显示闪电符号)
     
      即时运行更新代码或资源到你的测试设备是通过3种方式的:热交换,暖交换,冷交换(后面两个交换没听过,自己瞎翻译的,故名思议这三个是三种不同的级别)
    它会通过你造成的改变,自动决定交换的类型。下面的表描述了一些改变对应的交换类型情况:
    代码的改变类型
    即时运行的行为
    改变一个已经存在的方法的实现
    支持热交换,这是最快的类型,可以快速地看到代码变化,你的应用会保持运行,下次你调用到那个更改的方法时,会执行新的代码。
    热交换并没有重新实例化你app中的对象,在看到更新前,你可能需要重启当前的activity或者应用。默认情况下,studio会自动重启当前activity。也可以自己设置不自动重启。
    更改或者移除一个存在的资源
    支持暖交换,这种交换还是很快。即时运行必须重启你当前的activity来完成资源的更新,应用会一直运行,不过重启activity时屏幕可能会闪烁,这是正常情况。
    结构代码的改变,例如:
    增加,移除,更改:
    1.注释
    2.实例域
    3.静态域
    4静态方法名?
    5.实例方法名?
    6父类的改变
    7继承的接口发生变化
    8类的构造方法
    9重新布局了使用动态资源ID的空间
    在api21及以上的版本支持冷交换,该交换会慢一些,因为需要重新启动应用,才能将更改提交到应用中。
    在21一下的版本中,studio会构建一个完整的apk
    更改了androidmanifest文件
    更改了androidmanifest文件中引用到的资源
    更改了android插件的UI元素(需要clean和rerun)
    修改了manifest文件或其引用的资源时,studio 会自动构建一个新的apk来更新该改变,因为应用在安装到设备上时的一些固定信息是由manifest文件中决定的。

  • 相关阅读:
    [LeetCode] 137. Single Number II
    [LeetCode] 136. Single Number
    [LeetCode] 678. Valid Parenthesis String
    [LeetCode] 605. Can Place Flowers
    [LeetCode] 419. Battleships in a Board
    [LeetCode] 1002. Find Common Characters
    [LeetCode] 912. Sort an Array
    [LeetCode] 350. Intersection of Two Arrays II
    [LeetCode] 349. Intersection of Two Arrays
    [LeetCode] 820. Short Encoding of Words
  • 原文地址:https://www.cnblogs.com/androidkay/p/5420463.html
Copyright © 2011-2022 走看看