Android Weekly Notes #418
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的更新:
- Hilt.
- Paging 3.
- App Startup.
- Auto-fill IME.
- SeekableAnimatedVectorDrawable.
- Database Inspector in Android Studio 4.1.
- WindowManager -> 为了支持折叠屏.
- MotionLayout Editor.
还有一些现有库的更新.
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有受检异常. -> 编译器会提示, 所以不会忘记处理异常.
受检异常会有的问题:
- 可能有的异常根本不会发生.
- API设计的时候滥用受检异常, 导致开发者直接catch然后忽略.
- Java 8的lambda, stream和操作符等和受检异常很难一起工作.
所以受检异常已经穷途末路了.
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.
- Dual-use APIs.
怎么处理string.toInt()
可能由于非法输入而发生的异常?
你可能会想到加个try-catch, 因为在Java中这是常用做法, 但是在Kotlin中不要这样做, 应该使用扩展方法: String.toIntOrNull()
.
有很多getOrNull()
方法就是为了处理数组越界异常而准备的.
- API design.
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.
- Input/output.
Dedicate a single top-level place in your code to address all network errors uniformly.
- Exceptions, asynchronous programming, and coroutines.
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.
理由:
- 容易变成对实现细节的测试. 而不是外部状态.
- mock容易鼓励你测试how, 而不是what. -> 不要测试行为, 而要测试结果.
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
- https://compose.academy/ 学习Jetpack Compose好去处.
- Kotlin DSL有限状态机: https://github.com/Tinder/StateMachine
News
- 11 Weeks of Android
- Distribution data for Android Android机型分布. 有历年数据.
Videos
- All about Android 11 - Playlist
-
Android Panel Discussion Android大咖们(Jake Wharton, Sha Sha Chu, Donn Felker, and Alex Chiou)和你聊天.
(进度条上有问题提示, 还挺好的.)
Jake说Flutter其实代表着Google的失败...