Site icon Android Tutorial Online

A simple Compose library to print the reason for recomposition in your Logcat window

šŸž Rebugger

Being a ā€œcompose devā€ our enemy number one is unnecessary recompositions šŸ¤•. Often times we use tools likeĀ recompositionHighligher,Ā LogComposition, and layout inspector to count the recomposition, but thereā€™s no direct way to understand ā€œwhyā€ the recomposition has happened.

Rebugger is a simple compose utility function that can track the change in the given arguments. Itā€™ll print the reason for recomposition in your Logcat window.

āŒØļø Demo

Usage

1. Add dependencies

repositories {
  maven { url 'https://jitpack.io' } // Add jitpack
}

dependencies {
  implementation 'com.github.theapache64:rebugger:<latest.version>'
}

2. AddĀ RebuggerĀ call

CallĀ RebuggerĀ with the states or args you want to track

@Composable
fun VehicleUi(
    car: Car,
    bike: Bike,
) {
    var human by remember { mutableStateOf(Human("John")) }

    // Call Rebugger and pass the states you want to track. 
    // It could be a function arg or a state
    Rebugger(
        trackMap = mapOf(
            "car" to car,
            "bike" to bike,
            "human" to human
        ),
    )
    
    //...

3. SeeĀ LogCat

Search forĀ Rebugger

šŸ–„ Sample Outputs

šŸŸ  Limitation

Auto Name Picking

When Rebugger is placed deep inside the composable, it may not be able to pick the correct composable name. For example, if I place the Rebugger somewhere inside the Button lambda like this

@Composable
fun VehicleUi(
  car: Car,
  bike: Bike,
) {
// ...

    Column {
        // ...

        Button(
            onClick = {
                //...
            }
        ) {

            // šŸŸ  Inside Button's content lambda
            Rebugger(
                trackMap = mapOf(
                    "car" to car,
                    "bike" to bike,
                    "human" to human
                ),
            )
            
            // ...
        }
    }
}

Itā€™ll print something like this

Fix

To fix this, you can pass composableName argument to override the automatic name picking behaviour

Rebugger(
    composableName = "Button's body",
    trackMap = mapOf(
        "car" to car,
        "bike" to bike,
        "human" to human
    ),
)

šŸŒ‡ TODO

GitHub

View Github

Exit mobile version