美文网首页Android开发成长
Android Weekly Notes #423

Android Weekly Notes #423

作者: 圣骑士wind | 来源:发表于2020-07-24 12:32 被阅读0次

Android Weekly Issue #423

Android Vitals - What time is it?

Android的稳定性和性能监控.

Google的Android vitals: https://developer.android.com/topic/performance/vitals

各种获取时间的方法, 到底应该选择哪一个? SystemClock的官方文档中有解释:

  • System#currentTimeMillis: 可以被用户或者网络时间设置, 所以不适合用来计算时间间隔.
  • SystemClock#uptimeMillis: 当系统进入深睡的时候会停止, 所以只适合用来计算不跨越设备沉睡的时间间隔.
  • SystemClock#elapsedRealtime and SystemClock#elapsedRealtimeNanos, 这两个考虑到了深度睡眠, 建议被用作一般意义上的时间间隔测量.
  • System.nanoTime()uptimeMillis()更精确, 是纳秒单位. 但是在新的Android版本上才被标记为native实现(@CriticalNative). 实际比较了一下它测量的时间更长一些. 一般应用场景下毫秒级的单位应该够用了, 所以作者偏好于SystemClock.uptimeMillis().

The proper care and feeding of your Gradle build

介绍这个插件: autonomousapps/dependency-analysis-android-gradle-plugin

这个插件可以告诉你:

  • 哪个依赖/注解处理器没有被使用.
  • 依赖的声明是否正确: api, implementation, compileOnly等.
  • 是否有多余的插件.
  • 是否使用了没有声明的依赖. (Undeclared transitive dependencies).

顺便还搭车推荐了: https://github.com/runningcode/gradle-doctor

Refactoring Android Themes with Style

对Style和Theme做的lint check.

规定Style和Theme的命名, 然后扫描xml检查.

Kotlin Actors – No Drama Concurrency

Kotlin actor包含了协程, 协程内部封装的状态, 还有一个和其他协程通信的channel. 可以是方法也可以是类.

Kotlin Actors?

  • A Single Kotlin Coroutine
  • Processes incoming Messages
  • Backed by a Channel
  • Concurrent

Continuous Integration for Android

Android持续集成. Docker, Github Actions.

Repository Anti-Pattern in Android

这个文章的作者觉得一旦用了Repository, 程序就会变成面条程序.

However, in my opinion, if you follow this pattern, you’re guaranteed to end up with dirty spaghetti code in your project.

分析了Google Todo例子里面的Repository, 发现方法按职责可以分为三类: 为了观察而返回LiveData的方法; 访问数据的方法; 业务逻辑方法.

如果把domain相关的业务逻辑方法抽出来, 应该放在哪里呢? 通常是usecases或者interactors.

如果用了usecases, repository就显得有点多余.

为什么usecases比repository好呢: 以为前者更加具体, 后者很容易变成God Object.

所以结论是, 作者觉得Google推出的这个Repository实际上是一个Anti-pattern.

Improving app startup with I/O prefetching

Android 11, IORap, 预取技术, 加速app启动.

是在系统层的改动.

Support for newer Java language APIs

如果要使用更新版本的Java API. 用Android Gradle plugin 4.0.

具体是因为使用了java.time api, 在Android API level 26以下就会有问题.

Android实际上使用的是Open JDK.

解释了Desugaring, 是D8/R8来做的.
作用是为不支持Java 8的设备加入这些新的API.

build的时候检测到用了新的API, 首先把源代码编译成字节码, 转换成dex, 这时候为缺失Java 8的设备加入Java 8的runtime代码, 作为一个单独的dex library.

使用的时候:

android {
  defaultConfig {
    //Only required when setting minSdkVersion to 20 or lower
    multiDexEnabled true
  }

  compileOptions {
    // Flag to enable support for the new language APIs
    coreLibraryDesugaringEnabled true
    // Sets Java compatibility to Java 8
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

dependencies {
  // Dependency with the implementation code for the APIs
  coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.5'
}

具体涉及的新的API在这里列出: https://developer.android.com/studio/write/java8-support-table

LiveData with Coroutines and Flow — Part I: Reactive UIs

LiveData和协程, Flow.

Code

相关文章

网友评论

    本文标题:Android Weekly Notes #423

    本文链接:https://www.haomeiwen.com/subject/vcsjlktx.html