Home > Net >  Some issues were found when trying to render this preview - Android Studio Dolphin
Some issues were found when trying to render this preview - Android Studio Dolphin

Time:09-22

Today, I upgraded my Android Studio from Chipmunk to Dolphin, and most of my Composable Previews have stopped working and are giving the error Some issues were found when trying to render this preview.

On clicking the Show Exception button in the issues panel, I found the following error.

java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process null. Make sure to call FirebaseApp.initializeApp(Context) first.

Most of my composables use Firebase Analytics for event logging purposes and hence FirebaseAnalytics is one of the parameters in almost all the composable functions. Until Android Studio Chipmunk, I was able to show the preview for all the composables by following the below approach.

@Preview
@Composable
private fun DemoPreview() {
    MyAppTheme {
        Demo(firebaseAnalytics = Firebase.analytics, onClick = {})
    }
}

But the above method no longer works for Android Studio Dolphin.

Is there any other way to pass the FirebaseAnalytics object that makes the Composable Preview work again?

CodePudding user response:

When you have parameters to your composable I recommend to totally separate actual functionality and UI:

@Composable
private fun DemoContent(analyticsCallback: (input: String) -> Unit, onClick: () -> Unit) {
    // Your UI - call analyticsCallback when needed
}

@Composable
fun DemoScreen(firebaseAnalytics: FirebaseAnalytics, onClick: () -> Unit) {
    DenoContent(
        analyticsCallback = { input ->
            // Process input with firebase
        },
        onClick
    )
}

@Preview
@Composable
private fun DemoPreview() {
    MyAppTheme {
        DemoContent(
            analyticsCallback = { input ->
                // Log the input
            },
            onClick = { }
        )
    }
}

The input is just a placeholder - change it to whatever you need to use firebase.

CodePudding user response:

Starting with Android Studio Chipmunk, if you're seeing java.lang.NoSuchFieldError: view_tree_saved_state_registry_owner or java.lang.ClassNotFoundException: androidx.savedstate.R$id in the issues panel, make sure to include a debugImplementation dependency to androidx.lifecycle:lifecycle-viewmodel-savedstate in your module.

If you're seeing java.lang.NoSuchFieldError: view_tree_lifecycle_owner in the issues panel, make sure to include a debugImplementation dependency to androidx.lifecycle:lifecycle-runtime in your module.

If you're seeing java.lang.NoClassDefFoundError: Could not initialize class androidx.customview.poolingcontainer.PoolingContainer or java.lang.NoClassDefFoundError: androidx/customview/poolingcontainer/PoolingContainerListener in the issues panel, make sure to include a debugImplementation dependency to androidx.customview:customview-poolingcontainer in your module.

  • Related