Android开发成长

Android Weekly Notes #418

2020-06-17  本文已影响0人  圣骑士wind

Android Weekly Issue #418

Kotlin Symbol Processing: Early Thoughts

Kotlin Symbol Processing (KSP)
编译期插件, 想要比kapt更快.

作者在他的repo: https://github.com/zacsweers/moshi-sealed 中做实验.

PR at: https://github.com/ZacSweers/moshi-sealed/pull/24

What’s new in Jetpack

Jetpack的更新:

还有一些现有库的更新.

Dagger Hilt: Basics, Architecture, Concerns

关于Dagger Hilt的一些介绍和想法.

Network call interface in Kotlin

不应该用GlobalScope的原因: https://medium.com/@elizarov/the-reason-to-avoid-globalscope-835337445abc

suspend fun callService(): MyResult = withContext(Dispatchers.IO) {
    // Make your blocking network call here (e.g. with OkHttp)
}

viewModelScope.launch {
    val result = callService()
    // Do something with it
}

这样是串行执行的:

viewModelScope.launch {
    val result1 = callService1()
    val result2 = callService2()
    // Combine/process the 2 results
}

要改成并行的:

viewModelScope.launch {
    val result1 = async { callService1() }
    val result2 = async { callService2() }
    result1.await()
    result2.await()
    // Combine/process the 2 results
}

Kotlin and Exceptions

Kotlin和异常.

Java的受检异常

Kotlin的异常是从Java来的.
Java有受检异常. -> 编译器会提示, 所以不会忘记处理异常.

受检异常会有的问题:

所以受检异常已经穷途末路了.

Kotlin中的异常

Kotlin中的异常用来:

As a rule of thumb, you should not be catching exceptions in general Kotlin code. That’s a code smell. Exceptions should be handled by some top-level framework code of your application to alert developers of the bugs in the code and to restart your application or its affected operation. That’s the primary purpose of exceptions in Kotlin.

怎么处理string.toInt()可能由于非法输入而发生的异常?
你可能会想到加个try-catch, 因为在Java中这是常用做法, 但是在Kotlin中不要这样做, 应该使用扩展方法: String.toIntOrNull().

有很多getOrNull()方法就是为了处理数组越界异常而准备的.

You should design your own general-purpose Kotlin APIs in the same way: use exceptions for logic errors, type-safe results for everything else. Don’t use exceptions as a work-around to sneak a result value out of a function.
Dedicate a single top-level place in your code to address all network errors uniformly.
Thus, the same general advice applies to exceptions and coroutines: don’t use exceptions if you need local handling of certain failure scenarios in your code, don’t use exceptions to return a result value, avoid try/catch in general application code, implement centralized exception-handling logic, handle input/output errors uniformly at an appropriate boundary of your code.

Draggable bottom Navigation Drawer

一个可以拖拽的Button Drawer.

先历数了一下导航方式的发展.

Bottom Drawer解决Bottom Tab个数不够用的情况.

Replacing Mocks

把mocks换成fakes.

理由:

How to break your Android App with proguard / R8

-assumenosideeffects class android.util.Log { public * ; }

-assumenosideeffects*一起写会有问题, 具体表现是网络请求都超时了.

作者经过排查发现是这行rule的问题.

原因:

you have to be very careful with -assumenosideeffects The problem is that in order for -assumenosideeffects to have the effect of actually removing calls, it matches up the class hierarchy. Therefore, this rule says that anything public in android.util.Log and its superclasses has no side-effects. That include the synchronization methods defined on Object. So, please don't use * wildcards in connection with -assumenosideeffects. :(

最后, 移除Log的proguard应该写成这样:

# no logging in production
-assumenosideeffects class android.util.Log {
  v(...);
  d(...);
  i(...);
  w(...);
  e(...);
  println(...);
}

Fueled Reactive apps with Asynchronous Flow — Part 1 — Use case & Migration Strategy

Fueled Reactive apps with Asynchronous Flow — Part 2

Fueled Reactive apps with Asynchronous Flow — Part 3

RxJava到Kotlin协程, 项目改造三连发.

Code

News

Videos

Jake说Flutter其实代表着Google的失败...

上一篇下一篇

猜你喜欢

热点阅读