4

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.

eng
  • 443
  • 1
  • 4
  • 10
Ihor
  • 41
  • 5
  • Are you tried with latest compose bom version https://developer.android.com/jetpack/compose/bom/bom-mapping – Sri mani789 Jul 11 '23 at 15:53
  • @Srimani789 thx, but didn't helped – Ihor Jul 11 '23 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` – Sri mani789 Jul 12 '23 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. – Adam Sousa Jul 14 '23 at 20:31
  • @Ihor Some of the compose imports you have in your project are unnecessary, many are embedded in `androidx.compose.material:material`. Check out this compose setup guide for more info: https://developer.android.com/jetpack/compose/setup – Adam Sousa Jul 14 '23 at 20:35
  • 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. – Adam Sousa Jul 14 '23 at 20:38

0 Answers0