Jetpack compose:如何检测导致可组合项重组的原因

问题描述 投票:0回答:5

我开始注意到我的应用程序中出现一些卡顿,我相信原因是可组合项在不应该的情况下被重新组合。

我检测到一些用户交互触发了不必要的重新组合,但我无法确定是什么导致了重新组合。我在修改状态的每个地方都添加了断点,并且仍然可以找到触发重组的内容。

AS有提供调试这种事情的方法吗?

android android-jetpack android-jetpack-compose
5个回答
4
投票

2023答案

现在您可以通过正常的调试过程来定义重组触发器。

为此,您需要使用最新版本的

Android Studio Hedgehog

这是有关此新功能的更多信息

有了它,您只需在

@Composable
函数中放置断点即可获取已更改和未更改参数的列表


2
投票

AFAIK,目前还没有 IDE 支持。但您可以使用下面的自定义修饰符来跟踪重组。

演示

重组荧光笔.kt


import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawWithCache
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.drawscope.Fill
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.lerp
import androidx.compose.ui.platform.debugInspectorInfo
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.lerp
import kotlin.math.min
import kotlinx.coroutines.delay

/**
 * A [Modifier] that draws a border around elements that are recomposing. The border increases in
 * size and interpolates from red to green as more recompositions occur before a timeout.
 */
@Stable
fun Modifier.recomposeHighlighter(): Modifier = this.then(recomposeModifier)

// Use a single instance + @Stable to ensure that recompositions can enable skipping optimizations
// Modifier.composed will still remember unique data per call site.
private val recomposeModifier =
    Modifier.composed(inspectorInfo = debugInspectorInfo { name = "recomposeHighlighter" }) {
        // The total number of compositions that have occurred. We're not using a State<> here be
        // able to read/write the value without invalidating (which would cause infinite
        // recomposition).
        val totalCompositions = remember { arrayOf(0L) }
        totalCompositions[0]++

        // The value of totalCompositions at the last timeout.
        val totalCompositionsAtLastTimeout = remember { mutableStateOf(0L) }

        // Start the timeout, and reset everytime there's a recomposition. (Using totalCompositions
        // as the key is really just to cause the timer to restart every composition).
        LaunchedEffect(totalCompositions[0]) {
            delay(3000)
            totalCompositionsAtLastTimeout.value = totalCompositions[0]
        }

        Modifier.drawWithCache {
            onDrawWithContent {
                // Draw actual content.
                drawContent()

                // Below is to draw the highlight, if necessary. A lot of the logic is copied from
                // Modifier.border
                val numCompositionsSinceTimeout =
                    totalCompositions[0] - totalCompositionsAtLastTimeout.value

                val hasValidBorderParams = size.minDimension > 0f
                if (!hasValidBorderParams || numCompositionsSinceTimeout <= 0) {
                    return@onDrawWithContent
                }

                val (color, strokeWidthPx) =
                    when (numCompositionsSinceTimeout) {
                        // We need at least one composition to draw, so draw the smallest border 
                        // color in blue.
                        1L -> Color.Blue to 1f
                        // 2 compositions is _probably_ okay.
                        2L -> Color.Green to 2.dp.toPx()
                        // 3 or more compositions before timeout may indicate an issue. lerp the
                        // color from yellow to red, and continually increase the border size.
                        else -> {
                            lerp(
                                Color.Yellow.copy(alpha = 0.8f),
                                Color.Red.copy(alpha = 0.5f),
                                min(1f, (numCompositionsSinceTimeout - 1).toFloat() / 100f)
                            ) to numCompositionsSinceTimeout.toInt().dp.toPx()
                        }
                    }

                val halfStroke = strokeWidthPx / 2
                val topLeft = Offset(halfStroke, halfStroke)
                val borderSize = Size(size.width - strokeWidthPx, size.height - strokeWidthPx)

                val fillArea = (strokeWidthPx * 2) > size.minDimension
                val rectTopLeft = if (fillArea) Offset.Zero else topLeft
                val size = if (fillArea) size else borderSize
                val style = if (fillArea) Fill else Stroke(strokeWidthPx)

                drawRect(
                    brush = SolidColor(color),
                    topLeft = rectTopLeft,
                    size = size,
                    style = style
                )
            }
        }
    }

来源:https://android-developers.googleblog.com/2022/03/play-time-with-jetpack-compose.html


1
投票

为了调试重组,您需要 Android Studio Electric Eel,并在其后使用重组计数器。

示例:

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApplicationTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Column {
                        A()
                        B()
                    }
                }
            }
        }
    }
}

@Composable
fun A() {
    var count by remember { mutableStateOf(0) }

    // Causes recomposition on click
    Button(onClick = { count++ }, Modifier.wrapContentSize()) {
        Text("Recompose")
    }

    Text("$count")
    count++ // Backwards write, writing to state after it has been read
}

@Composable
fun B() {
    Text("B")
}

在 Column 中,我们有 A() 和 B() Compose 函数。 当用户按下 A() 中的按钮时,我们会遇到“向后问题”并无限重组。 为了查找问题,我们使用布局检查器。 您可以在下面看到用于调试的屏幕录制。

https://www.veed.io/view/2fda4463-a23d-4567-b853-e7f766e9342a?panel=share


0
投票
mutableState

mutableStateList
会触发重组。 如果你想控制这个,可以看看
Side-Effects
https://developer.android.com/jetpack/compose/side-effects
特别是这个:

https://developer.android.com/jetpack/compose/side-effects#snapshotFlow


0
投票

在右下角你可以找到布局检查器选项
  1. 布局检查器 -> 查看选项组件树 -> 显示重组计数
  2. 通过启用此功能,您可以轻松查看重组计数
© www.soinside.com 2019 - 2024. All rights reserved.