Search code examples
androidandroid-jetpack-composeandroid-bottomnav

Too slow loading activity Jetpack Compose


My activity loads too slow with Jetpack Compose(about 5 seconds). I have 3 screens with 1 Lazy Vertical Grid and my custom bottom navigation. Does somebody know how to parallel composing this screens or make loading activity faster?

My code:

@ExperimentalAnimationApi
@ExperimentalFoundationApi
@Composable
fun ComposeNavigation() {
    val navController = rememberNavController()
    ConstraintLayout(modifier = Modifier.fillMaxSize()) {
        val (content, bottomSheet) = createRefs()
        //Bottom is my custom bottom navigation
        Bottom(navController, modifier = Modifier
            .constrainAs(bottomSheet) {
                start.linkTo(parent.start)
                end.linkTo(parent.end)
                bottom.linkTo(parent.bottom)
            }
            .fillMaxWidth())
        Box(modifier = Modifier
            .fillMaxWidth()
            .constrainAs(content) {
                start.linkTo(parent.start)
                end.linkTo(parent.end)
            }) {
            NavHost(
                navController = navController,
                startDestination = "first_screen",
            ) {
                composable("first_screen") {
                    FirstScreen(navController = navController)
                }
                composable("second_screen") {
                    SecondScreen(navController = navController)
                }
                composable("third_screen") {
                    ThirdScreen(navController = navController)
                }
            }
        }
    }
}

Solution

  • Look for rendering bottlenecks in your XScreen views.

    One of my mistake, that dramatically slowed rendering performance was the library i used to imitate neu shadows https://github.com/CuriousNikhil/neumorphic-compose. After removing all of this effects the App took off like a rocket.

    • Just run your app with Android Studio profiler https://developer.android.com/studio/profile/android-profiler (the only thing, that helped me to find bottleneck).

    • Other possible confuse - if you are investigating performance on Debug build variant. Try to run Release one. This is my Release config (pay attention to Proguard (actually R8) file ):

        buildTypes {
            release {
                isMinifyEnabled = true
                isShrinkResources = true
    
                proguardFiles(
                    getDefaultProguardFile("proguard-android.txt"),
                    "proguard-rules.pro"
                )
    
                signingConfig = signingConfigs.getByName("release")
            }
        }
    
    • The last one to notice is that ART improves the thing further when your App is installed from Play Store as a bundle.