2
votes

I am trying to build simple app using Jetpack Compose. I followed this documentation, downloaded repository and created my own module.

Code is pretty simple:

import android.app.Activity
import android.os.Bundle
import androidx.compose.Composable
import androidx.ui.core.Text
import androidx.ui.core.setContent
import androidx.ui.material.surface.Card
import androidx.ui.graphics.Color

class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyApp()
        }
    }

    @Composable
    fun MyApp() {
        Card(color = Color.Cyan) {
            Text("test")
        }
    }
}

But I noticed that some composable widgets doesnt work and I have following error:

Exception while analyzing expression at (23,9) in /path/Projects/androidx-master-dev/frameworks/support/ui/compose/src/main/java/app/myown/MainActivity.kt

Where (23,9) references to Card widget

By the way other widgets work, for example I dont have problems with

@Composable
fun MyApp() {
    Padding(10.dp) {
        Text("test")
    }
}

It compiles and runs perfectly.

I got following problem with:

  • Card
  • Column
  • Row
  • Center
  • FlexColumn

and I guess many others widgets

1
Add import androidx.compose.composer to your list of imports and see if that clears it up. If you are using the experimental Android Studio build that is part of the current Jetpack Compose environment, this import will show up as not being used. However, it is necessary for many composables to actually work. - CommonsWare
@CommonsWare thank you! That helped. Can you write it as answer please? And yes I`m using the experimental AS from Jetpack - Andrey Danilov

1 Answers

1
votes

I ran into this problem earlier.

There is an implicit need to have import androidx.compose.composer in every Kotlin source file that has @Composable functions. I say "implicit" because Android Studio thinks that it is unnecessary and has a tendency to remove that line (e.g., you ask it to optimize imports). Some @Composable functions can survive without this import, but others cannot.

As I understand it, this is one of those things that will get better as the libraries and tooling evolve, but at the moment, just keep an eye out for that import and add it if it is missing and you are getting weirder-than-normal results.