Skip to content

CompositionLocal LocalLifecycleOwner not present in Jetpack Compose

java
java.lang.IllegalStateException: CompositionLocal LocalLifecycleOwner not present
at androidx.lifecycle.compose.LocalLifecycleOwnerKt$LocalLifecycleOwner$1.invoke(LocalLifecycleOwner.kt:26)
at androidx.compose.runtime.LazyValueHolder.getValue(ValueHolders.kt:31)
at androidx.lifecycle.compose.FlowExtKt.collectAsStateWithLifecycle(FlowExt.kt:182)
at com.codejockie.wani.MainActivity$onCreate$1.invoke(MainActivity.kt:47)

Problem diagnosis

The IllegalStateException: CompositionLocal LocalLifecycleOwner not present typically occurs when using Jetpack Compose's collectAsStateWithLifecycle() or collectAsState() methods. This error specifically arises from:

  1. Lifecycle owner availability: A required LifecycleOwner isn't properly set in the Composition hierarchy
  2. Version incompatibility: A mismatch between Compose and AndroidX Lifecycle library versions
  3. Proguard/R8 issues: In release builds, where critical Composables might be stripped out

The issue commonly appears after updating dependencies (especially Lifecycle or Compose BOM) or switching development environments. The stack trace indicates the composition system fails to find the LocalLifecycleOwner needed for lifecycle-aware collection operations.

1. Update Lifecycle dependencies (preferred solution)

Upgrade to Lifecycle 2.8.3 or later, which fixes the compatibility issue:

kotlin
// Update these dependencies in your build.gradle.kts
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.3")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.3")
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.8.3")

No code changes required—the patch resolves the provider mapping internally.

2. Downgrade Lifecycle libraries (temporary fix)

If you can't update immediately, downgrade to Lifecycle 2.7.0:

kotlin
dependencies {
    implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
    implementation("androidx.lifecycle:lifecycle-runtime-compose:2.7.0")
}

3. Apply provider mapping workaround

If you must use Lifecycle 2.8.0-2.8.2, manually bridge the providers:

kotlin
// Wrap your main composable
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            CompositionLocalProvider(
                androidx.lifecycle.compose.LocalLifecycleOwner 
                    provides androidx.compose.ui.platform.LocalLifecycleOwner.current
            ) {
                // Your app content
                App()
            }
        }
    }
}

WARNING

If using Navigation Compose, apply this wrapper to each composable destination:

kotlin
composable("route") {
    CompositionLocalProvider(
        androidx.lifecycle.compose.LocalLifecycleOwner 
            provides androidx.compose.ui.platform.LocalLifecycleOwner.current
    ) {
        DestinationScreen()
    }
}

4. Fix Proguard rules for release builds

Add these rules if the error persists in release builds:

proguard
-keep class androidx.compose.ui.platform.AndroidCompositionLocals_androidKt {
    public static * getLocalLifecycleOwner*();
}

Best practices for dependency management

  1. Align BOM and library versions: When using Compose BOM, verify non-BOM libraries are compatible
  2. Check release notes: Consult compatibility tables for Lifecycle and Compose
  3. Use version catalogs: Centralize dependency versions for consistency:
toml
# gradle/libs.versions.toml
[versions]
lifecycle = "2.8.3"

[libraries]
androidx-lifecycle-runtime = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycle" }

Root cause explanation

This exception occurs due to changes in AndroidX packages between Compose 1.6 and Lifecycle 2.8:

  1. Lifecycle 2.8 moved LocalLifecycleOwner to androidx.lifecycle.compose
  2. Compose pre-1.7 expects it at androidx.compose.ui.platform
  3. Version gap: Lifecycle 2.8.0-2.8.2 didn't support older Compose versions
  4. Reflection issues: Proguard might strip required Composables in release builds

The resolution in Lifecycle 2.8.3 adds internal mapping logic that bridges both locations regardless of Compose version.

TIP

Always verify compatibility using the official Lifecycle release notes before updating.