Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchFieldError: No field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion;

I'm using Jetpack Compose for the first time but I'm getting this error. I haven't figured out where the problem really is but I'm using single-activity architecture. If more information is needed, kindly inform me. According to the error, the problem seems to be coming from the Scaffold.

                val scaffoldState = rememberScaffoldState()

                Scaffold(
                    scaffoldState = scaffoldState,
                    snackbarHost = {
                        SnackbarHost(hostState = it)
                    }
                ) {
java.lang.NoSuchFieldError: No field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion; in class Landroidx/compose/foundation/layout/BoxScope; or its superclasses (declaration of 'androidx.compose.foundation.layout.BoxScope' appears in /data/app/com.octagon_technologies.scafe-0B8-dDpbnRqa6fydxFPekw==/base.apk)
        at androidx.compose.material.SurfaceKt$Surface$1.invoke(Surface.kt:149)
        at androidx.compose.material.SurfaceKt$Surface$1.invoke(Surface.kt:105)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
        at androidx.compose.material.SurfaceKt.Surface-F-jzlyU(Surface.kt:102)
        at androidx.compose.material.ScaffoldKt$Scaffold$child$1.invoke(Scaffold.kt:168)
        at androidx.compose.material.ScaffoldKt$Scaffold$child$1.invoke(Scaffold.kt:167)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:118)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.material.ScaffoldKt.Scaffold-J67Y1T8(Scaffold.kt:197)
        at com.octagon_technologies.scafe.presentation.MainActivity$onCreate$1$1.invoke(MainActivity.kt:54)
        at com.octagon_technologies.scafe.presentation.MainActivity$onCreate$1$1.invoke(MainActivity.kt:47)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
        at androidx.compose.material.TextKt.ProvideTextStyle(Text.kt:246)
        at androidx.compose.material.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:78)
        at androidx.compose.material.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:77)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
        at androidx.compose.material.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:69)
        at com.octagon_technologies.scafe.presentation.ui.theme.ThemeKt.ScafeTheme(Theme.kt:46)
        at com.octagon_technologies.scafe.presentation.MainActivity$onCreate$1.invoke(MainActivity.kt:47)
        at com.octagon_technologies.scafe.presentation.MainActivity$onCreate$1.invoke(MainActivity.kt:46)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:346)
        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:202)
        at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:201)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
like image 898
Andrew Chelix Avatar asked Apr 13 '21 11:04

Andrew Chelix


3 Answers

I had the same error and solved it by updating the compose version. The new project template generated by Android Studio comes with compose version 1.0.0-beta01

Open build.gradle file and update

compose_version = '1.0.0-beta05'

or newer if you prefer.

like image 140
adriandleon Avatar answered Oct 24 '22 12:10

adriandleon


I got this error after adding androidx.navigation:navigation-compose:1.0.0-alpha10 as a dependency.

What worked for me was to downgrade to androidx.navigation:navigation-compose:1.0.0-alpha09.

like image 44
Duncan Luk Avatar answered Oct 24 '22 13:10

Duncan Luk


I got the same error for this code

java.lang.NoSuchFieldError: 
No static field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion; 
in class Landroidx/compose/foundation/layout/BoxScope; 
or its superclasses (declaration of 'androidx.compose.foundation.layout.BoxScope' 
appears in /data/app/ink.iamt.demo-l1DoSMoQUFGcF5KYI5RU0w==/base.apk)
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            Card {
                Text("Hello, Compose")
            }
        }
    }
}

You know my code cannot be simpler. So I thought it should not be my fault. I checked my Android Studio version, it's 2020.3.1 Canary 14, which is the latest version. And checked the build.gradle for Jetpack compose, it's

    val compose_version by extra("1.0.0-beta01")

So the latest Android Studio creates a new Compose project with 1.0.0-beta01 compose version, while the latest compose version is 1.0.0-beta04. Then I try to update the version to beta04 and rerun my App. Then it works!

So please try to update your compose version to the latest and see if you have the luck.

like image 42
Tink Avatar answered Oct 24 '22 12:10

Tink