7

I started migrate our code to Compose and have problem with @Preview. For example, when I use modifier.fillMaxWidth() without default param "fraction = 1f" I have render problem

java.lang.NoSuchMethodError: 'androidx.compose.ui.Modifier androidx.compose.foundation.layout.SizeKt.fillMaxWidth$default(androidx.compose.ui.Modifier, java.lang.Float, int, java.lang.Object)'

or use padding without bottom param

.padding(
    top = dimensionResource(id = R.dimen._4sdp),
    start = dimensionResource(id = R.dimen._16sdp),
    end = dimensionResource(id = R.dimen._16sdp)
)

java.lang.NoSuchMethodError: 'androidx.compose.ui.Modifier androidx.compose.foundation.layout.PaddingKt.padding-qDBjuR0$default(androidx.compose.ui.Modifier, androidx.compose.ui.unit.Dp, androidx.compose.ui.unit.Dp, androidx.compose.ui.unit.Dp, androidx.compose.ui.unit.Dp, int, java.lang.Object)'

here the code I use

@Composable
fun Info(
    modifier: Modifier = Modifier,
    step: String,
    stepInfo: String,
) {
    Column(modifier = modifier.fillMaxWidth(fraction = 1f)) {
        Text(
            text = step,
            modifier = Modifier
                .background(
                    color = colorResource(id = R.color.color_on_background_variant3),
                    shape = RoundedCornerShape(90)
                )
                .size(dimensionResource(id = R.dimen._28sdp))
                .padding(dimensionResource(id = R.dimen._6sdp))
                .align(Alignment.CenterHorizontally),
            textAlign = TextAlign.Center
        )
        Text(
            text = stepInfo,
            textAlign = TextAlign.Center,
            modifier = Modifier
                .padding(
                    top = dimensionResource(id = R.dimen._4sdp),
                    start = dimensionResource(id = R.dimen._16sdp),
                    end = dimensionResource(id = R.dimen._16sdp),
                    bottom = 0.dp
                )
                .align(Alignment.CenterHorizontally),
        )
    }
}

@Preview
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES, name = "InfoPreviewNight")
@Composable
fun InfoPreview() {
    MdcTheme {
        Info(
            step = "1",
            stepInfo = ".............."
        )
    }
}

buidld.gradle (:app)

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-parcelize'
    apply plugin: 'kotlin-kapt'
    apply plugin: 'com.google.firebase.appdistribution'
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.3.1'
    }
    dependencies {
        implementation platform('androidx.compose:compose-bom:2022.10.00')
        implementation "androidx.compose.runtime:runtime"
        implementation "androidx.compose.ui:ui"
        implementation "androidx.compose.foundation:foundation"
        implementation "androidx.compose.foundation:foundation-layout"
        implementation "androidx.compose.material:material"
        implementation "androidx.compose.runtime:runtime-livedata"
        implementation "androidx.compose.ui:ui-tooling-preview"
        implementation "com.google.accompanist:accompanist-themeadapter-material:0.30.1"
        debugImplementation "androidx.compose.ui:ui-tooling"
        androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00')
    }

Gradle Plugin Version ------ 7.2.2 Gradle Version ------- 7.3.3

I tried change kotlinCompilerExtensionVersion but I got no result and stackoverflow question didn't helped.

6
  • Are you tried with latest compose bom version developer.android.com/jetpack/compose/bom/bom-mapping Commented Jul 11, 2023 at 15:53
  • @Srimani789 thx, but didn't helped Commented Jul 11, 2023 at 19:33
  • i executed the above code and got the preview without any issue. compose bom 2023.06.01, compose compiler 1.4.1, kotlin version 1.8.0 and gradle 8.0 Commented Jul 12, 2023 at 10:47
  • Also having this same error message. I've tried reproducing this in a new project but have not had any luck yet. I was thinking this was a conflicting import issue, but after importing all of our imports into a new project the issue did not appear. @Srimani789 Using all of the same versions you mentioned, all except gradle which were on 7.5.1. but the issue remains. Commented Jul 14, 2023 at 20:31
  • 2
    Also worth noting that I've been able to side step this issue by rebuilding my project and my previews build just fine. Its only when it tried to recompile the preview without rebuilding the project that the preview fails to build, such as updating a displayed text value. Commented Jul 14, 2023 at 20:38

2 Answers 2

0

I faced the same issue, I upgraded the Compose compiler to latest version and now the preview works properly again.

in build.gradle

composeOptions {
    //find latest compiler version here: https://developer.android.com/jetpack/androidx/releases/compose-compiler
    kotlinCompilerExtensionVersion "1.5.3"
    kotlinCompilerVersion kotlin_version
}
Sign up to request clarification or add additional context in comments.

1 Comment

good idea, but for me helped updated android studio, and i dont want try others solutions while it works :)
0

After updating Android Studio to Giraffe | 2022.3.1 Patch 1 version, the broblem has gone

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.