Track Compose Recomposition Part-2

Ankit Kumar
2 min readJan 18, 2023

--

Track Recomposition Part-1 : Navigate
Track Recomposition Part-2 : You are here
Check out sample project : Github

Avoid using value classes in composable

Value classes are considered unstable even if we annotate with @Stable πŸ‘‡πŸ»

value class compose

If you see in above image, TaskyDate is marked stable by compose compiler, but TaskyTime is unstable, the only difference b/w them is TaskyDate is data class and TaskyTime is value class

Optimisation in multi modular project

Let’s suppose we have a class in a non-compose module containing just a integer value, So ideally it should be stable πŸ‘‡πŸ»

class in non-compose module

Wondering Why? -

  • When analysing any non-compose module, compiler can’t check the stability of a class as compose compiler is not enabled for that module, so it will mark the class unstable

Think of a fix -

  • 1st solution β€” You can enable compose and compose compiler for all modules, so now compose can check for stability of your class
buildFeatures.compose = true
composeOptions {
kotlinCompilerExtensionVersion = Versions.Compose.Compiler
}
compose report when enabled compose
  • 2nd solution β€” mark all the classes as @Stable, basically you guarantee for the stability of the class and compose will mark it stable
// non-compose module 
@Stable
data class ModelFromCore(val value: Int)

To use @Stable annotation add the dependency
| androidx.compose.runtime:runtime:1.1.0 |
  • 3rd solution β€” create a wrapper for the classes using from any non-compose module
// class from non-compose module
data class ModelFromCore(val value: Int)

// when using in your composable
@Stable
fun StableModelFromCore(val value: ModelFromCore)

Or can create a generic wrapper πŸ‘‡πŸ»

@Stable
class StableHolder<T>(val item: T)

@Immutable
class ImmutableHolder<T>(val item: T)

usage:
fun nonComposeModuleClassUsage(model: StableHolder<ModelFromCore>) {
val value = model.item
}

Track Recomposition Part-1 : Navigate
Check out sample project : Github

--

--