Performance best practices for Jetpack Compose

Android Developers · 104K views · 2022 · 21:17

Open on YouTube

Software performance & optimizationSoftware architecture & designWeb developmenttalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering Jetpack Compose questions about recomposition, lazy list lag and animation, since it shows how to use lambda modifiers, derivedStateOf and phase-aware code to avoid needless work.

Recommended by 4 people across 4 different months, 2022 to 2023.

What it is

A Google I/O 2022 session from the Android Developers channel in which Chuck Jazdzewski and Ben Trengrove explain how to get good performance from Jetpack Compose. It covers the composition, layout and draw phases, common mistakes and how to avoid unnecessary recomposition.

What people said

Recommends Роман Присяжнюк · 2022-05 · Stack Overflow · on “LazyColumn doing infinite number of recompositions (Jetpack Compose)”

Due to this video - [this video] , i did smthn like this, and it works perfectly: @Composable fun ListOfReceivers( receivers: List<ReceivedPingItem>, ) { if (receivers.isNotEmpty()) { val listState = rememberLazyListState() val derivedListState by remember { derivedStateOf { listState.isScrolledToTheEnd() } } if (derivedListState) { viewModel.onRecyclerViewScrolledToLast() } // LazyColumn -> equivalent of the RecyclerView LazyColumn( state = listState, contentPadding = PaddingValues(horizontal = 15.dp, vertical = 15.dp), verticalArrangement = Arrangement.spacedBy(8.dp), ) { items(receivers) { receiverItem -> Log.d("*****", "ADDED $receiverItem") ReceiversListItem( receiver = receiverItem, modifie

Recommends Phil Dukhov · 2022-06 · Stack Overflow · on “Offset a wide image for horizontal parallax effect in Android Compose”

Image is too smart and doesn't draw anything beyond the bounds. translationX doesn't change the bound but only moves the view. Here's how you can draw it manually: val painter = painterResource(id = R.drawable.my_image_1) Canvas( modifier = Modifier .fillMaxWidth() .height(BG_IMAGE_HEIGHT) ) { translate( left = -parallaxOffset, ) { with(painter) { draw(Size(width = painter.intrinsicSize.aspectRatio * size.height, height = size.height)) } } } I don't see your code that calculates parallaxOffset , but just in case, I suggest you watch this video [this video] to get the best performance.

Recommends Phil Dukhov · 2022-06 · Stack Overflow · on “Itemdecoration in Jetpack compose”

This response will have rather poor performance, it can be improved so that `ListBg` is not recomposed at all. Please see this video [this video] and update it

Recommends Vahid Garousi · 2022-11 · Stack Overflow · on “Set Background color to ConstraintLayout in Compose”

In Compose, you can use modifiers to apply color to the background. what you want; This is not possible at the moment But you can achieve your desire in the following ways: 1- Using the background modifier ConstraintLayout( modifier = Modifier .fillMaxSize() .background(Color.Yellow) ) { } 2 - Using the drawBehind modifier ConstraintLayout( modifier = Modifier .fillMaxSize() .drawBehind { drawRect(Color.Yellow) } ) { } But according to the explanations in this video, it is better to use lambda modifier to achieve better performance [this video]

Recommends c-an · 2023-03 · Stack Overflow · on “How can I create a typing animation without compose state(draw state) in jetpack compose?”

At the moment, this recomposes whenever the caretIndex changes. But, I found out this can lead UI freezing. Text( text = buildAnnotatedString { withStyle(style = SpanStyle(color = color)) { append(text.substring(0, caretIndex)) } withStyle(style = SpanStyle(color = Color.Transparent)) { append(text.substring(caretIndex, text.length)) } }, style = style, modifier = modifier ) And I found out there are three steps, compose-layout-drawing from this video [LINK] and this best practice video [this video] So, I am trying to fix this within draw state. But, drawBehind seems like it only draws behind the actual UI. And, I found graphicsLayer and I think it works in draw state. But this doesn't give any f

Cites Mattia Ferigutti · 2022-01 · Stack Overflow · on “Laggy Lazy Column Android Compose”

Things will speed up your Lazy List If you're in debug mode, that's normal . Don't worry if your app is laggy in debugging. It's completely fine. Just create the APK in release mode (Build -> Generated Signed Bundle/APK), which might solve your problem. This happens because when in debugging, Compose translates bytecode in runtime using JIT. Make sure you're also using the R8 compiler in the release build. This is extremely important to improve general performance, mostly for lambda expressions which are heavily used in compose. Set a key for your item . Initialize your Lazy list like this. LazyColumn() { items( count = cartItems.size, key = { cartItems[it].cartItem.id }, itemContent = { ind

Cites Jose Gonzalez · 2022-08 · Stack Overflow · on “Multiple re-executions of Jetpack Compose functions on read”

I've been checking out the Performance best practices for Jetpack Compose [this video] Google I/O, in there it's stated that this code should only re-execute the Text() function, since only this function reads a value that changes. private class NameHolder(var name: String) @Composable private fun LittleText(nameHolder: NameHolder) { Box { Text(text = "Nombre: ${nameHolder.name}") println("compose 2") } println("compose 1") } however when I run it I can see that for every change both prints execute as well. I also tested with something like this: @Composable private fun LittleText(name: String) { Box { Text(text = "Nombre: $name") println("compose 2") } println("compose 1") } With the same result