Android Lint for Jetpack Compose:
Fixing UI Performance Warnings the Smart Way
What is Android Lint (And Why Should You Care)?
- Bad practices (e.g., hardcoded text)
- Layout issues (e.g., missing constraints)
- Resource misuse (e.g., unused IDs)
- Now: Compose-specific problems
1. "Modifier order matters"
Modifier chain should be ordered for better performance and clarity
Example Fix:
// π« Inefficient
Modifier
.padding(16.dp)
.fillMaxWidth()
.background(Color.Red)
// ✅ Better
Modifier
.fillMaxWidth()
.background(Color.Red)
.padding(16.dp)Bad
@Composable
fun UserList() {
val users = listOf("A", "B", "C")
LazyColumn {
items(users) {
Text(it)
}
}
}Good
@Composable
fun UserList() {
val users = remember { listOf("A", "B", "C") }
LazyColumn {
items(users) {
Text(it)
}
}
}3. "Remember call inside composition is missing"
π« This will break:
scrollState = ScrollState(0)This is correct:
>val scrollState = rememberScrollState()4. "Composables not marked as @Composable"
// Missing @Composable annotation = crash
fun ShowText() {
Text("Hello")
}How to Fix These Warnings the Smart Way
Step 1: Run Inspect Code
- Go to Analyze > Inspect Code
- Choose your module or whole project
- Select Jetpack Compose > Compose Lint Checks
Step 2: Fix with Auto-Correction (When Safe)
Step 3: Turn On Lint in CI/CD
android {
lintOptions {
abortOnError true
warningsAsErrors true
checkReleaseBuilds true
}
}Step 4: Create Your Own Custom Lint Rules (Optional)
- Enforce all text to come from string.xml
- Disallow Modifier.clickable without semantics
Common Compose Lint Mistakes That Lead to Bugs
- Creating state inside loops
- Forgetting to use remember for ScrollState, CoroutineScope, etc.
- Modifying lists without using mutableStateListOf()
- Passing mutable parameters directly into Composables
Real Developer Advice: Don’t Fight Lint, Learn From It
Summary: Compose Lint Survival Kit
| Problem | How to Fix |
|---|---|
| Wrong modifier order | Use fillMaxWidth() before padding |
| Unstable collections passed in | Wrap them with remember { } |
| State objects not remembered | Use remember, rememberSaveable |
| Missing @Composable annotation | Mark your UI function with @Composable |
| Performance warnings in Lazy lists | Use key param and avoid reallocation |
