Android What’s new this week? #1

What a week! Many things have come: Kotlin 1.6.20-M1, Android 13 DP1 etc.

Lam Pham
4 min readFeb 13, 2022
Photo by Alexandr Bormotin on Unsplash

KOTLIN

Kotlin 1.6.20-M1: the first preview of Kotlin 1.6.20 release is out.

This version includes these noticeable changes:

Prototype of context receivers for Kotlin/JVM.

The idea of this feature is to have multiple context receivers for a function. Before v1.6.20-M1, a function can only be run within at max one context:

fun LoggingContext.startBusinessOperation() {
log.info("This operation is run within only one context")
}

interface LoggingContext {
val log : Logger
}

The change is quite understandable: make a function run in more than 1 contexts. The syntax for the declaration is:

context(LoggingContext1, LoggingContext2, LoggingContext3)
fun startBusinessOperation() {
log1.println("Operation 1 has started")
log2.println("Operation 2 has started")
log3.println("Operation 3 has started")
}

To invoke this function, we can use different Kotlin scope functions such as with , apply , run :

with(loggingContext1) {
with(loggingContext2) {
with(loggingContext3) {
startBusinessOperation()
}
}
}
//or
with(loggingContext1) {
loggingContext2.apply {
loggingContext3.run {
startBusinessOperation()
}
}
}

Note: Make sure you add kotlin compiler option -Xcontext-receivers to be able to use context(...)

You can also define 2 functions with the same name and same parameters but different set of context receivers:

context(LoggingContext1)
private fun startBusinessOperation() {
log1.println("Operation 1 has started")
}

context(LoggingContext1, LoggingContext2, LoggingContext3)
private fun startBusinessOperation() {
log1.println("Operation 1 has started")
log2.println("Operation 2 has started")
log3.println("Operation 3 has started")
}

Take a look at this class for the full codes.

As you can see, the more Contexts are passed to the function, the more scope functions you need to add, which means the uglier your codes become. To avoid it, I tried to reach something like this:

withs(loggingContext1, loggingContext2, loggingContext3) {
startBusinessOperation()
}

For this, I tried to declare this kind of extensions:

fun <R> withs(vararg receivers: Any, block: context(vararg Any).() -> R): R {
requireNotNull(receivers[0])
if (receivers.size == 1) return with(receivers[0], block::invoke)
return with(receivers[0]) {
withs(receivers.drop(1), block)
}
}

Of course this doesn’t work. The contextual function parameter is not supported yet.

Luckily, this problem has been noticed by Jetbrain team. Hope it will be improved in future releases.

Koltin context receivers

Support for parallel compilation of a single module in the JVM backend

Kotlin 1.6.20-M1 adds the experimental JVM IR backend mode to compile all the files in a module in parallel. Parallel compilation can reduce the total compilation time by up to 15%.

Note that this parallelizes the compilation for a single module; so, applying this to a multi-module project doesn’t help too much (it might even make things worse as switching contexts while building small modules really costs). The best use-cases for this are monolithic-module projects.

Some Kotlin/Native performance improvements:

This 1.6.20 Kotlin release brings some performance updates and bug fixes that affect the LLVM IR that Kotlin generates:

  • 15% reduction in execution time
  • 20% reduction in the code size of both release and debug binaries
  • 26% reduction in the compilation time of release binaries

Learn more.

ANDROID

Jetpack Compose 1.1 is now stable!

This release :

  • Brings some new features: Image vector caching, Touch target sizing.
  • Graduates experimental to stable APIs: some Animation related and Vector related APIs.
  • Brings new experimental APIs.

Note: Using Compose 1.1 requires using Kotlin 1.6.10. Check out the Compose to Kotlin Compatibility Map for more information.

Learn more.

The first developer preview (DP1) for Android 13 (Tiramisu)

Some highlights:

  • Photo picker API: This API provides a safe, built-in way for users to select media files, without needing to grant your app access to their entire media library. Learn more.
  • Nearby device permission for Wi-Fi: New Runtime Permission NEARBY_WIFI_DEVICES is introduced that allows scanning and connecting to nearby access points over Wi-Fi. This avoids requesting unnecessarily location permission. Learn more.
  • Quick Settings Placement API: provides a new dialog allowing users to add a tile to Quick Setting without leaving app.
Quick Settings dialog.
  • Per-app language preferences: adds a LocaleManager which allows users to set their preferred language for an application rather than using the device’s language.
  • OpenJDK 11 updates: Android team has started working on aligning Android libraries with OpenJDK 11 LTS. Many changes are already introduced, you can find more information here.
  • Compatibility framework changes: Compatibility framework tools are a set of developer tools which help testing and debugging your app by toggling breaking changes using Developer Options or adb commands. Android 13 adds more toggleable changes:
Compatibility framework changes in Android 13

Learn more.

  • Platform stability milestone:
Android 13 release roadmap.

See full detailed roadmap here.

Nobody: “Android 13 is coming, my Note 10+’s still stuck with Android 11 😐

** Become a Medium’s member to read my and other writers’ articles without limit.

— Lam PHAM

--

--