11

By default when I create a compose app from Android Studio, it will give me ext as below in my root project build.gradle.

buildscript {
    ext {
        compose_version = '1.0.0'
    }
    repositories {
        google()
        mavenCentral()
    }
}

As I change it to build.gradle.kts, it will complaint

Unresolved reference: ext
Unresolved reference: compose_version
Too many characters in a character literal ''1.0.0''

How can I resolve this issue?

Elye
  • 53,639
  • 54
  • 212
  • 474

2 Answers2

5

I use

buildscript {
    extra.apply{
        set("compose_version", "1.0.0")
    }
    repositories {
        google()
        mavenCentral()
    }

To access it from other gradle files, I need to explicitly set to a variable

val composeVersion = rootProject.extra.get("compose_version") as String

Then I can use it like

composeOptions {
    kotlinCompilerExtensionVersion = composeVersion
}

and

dependencies {
   implementation ("androidx.compose.ui:ui:$composeVersion")
}
Elye
  • 53,639
  • 54
  • 212
  • 474
2

Set it like this:

val composeVersion by extra("1.0.0")

Use it like this:

val composeVersion: String by rootProject.extra

Groovy allows you to use single quotes for strings, but Kotlin requires double quotes. Change all the strings in your kts files to use double quotes to get rid of the Too many characters in a character literal error.

Eva
  • 4,397
  • 5
  • 43
  • 65