Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error in build.gradle.kts script using extra property

Tags:

kotlin

gradlew

below is a small (build.gradle.kts) script which gives at line 9 (the classpath line) the error : Cannot get property 'kotlinVersion' on extra properties extension as it does not exist

buildscript {
    extra["kotlinVersion"] = "1.2.70"

    repositories {
        jcenter()
    }

    dependencies {
      classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${extra["kotlinVersion"]}")
    }
}

I do not understand why this error occur.

like image 787
Emile Achadde Avatar asked Feb 13 '20 10:02

Emile Achadde


People also ask

What is Ext block in Gradle?

All enhanced objects in Gradle's domain model can hold extra user-defined properties. This includes, but is not limited to, projects, tasks, and source sets. Extra properties can be added, read and set via the owning object's ext property. Alternatively, an ext block can be used to add multiple properties at once.

What is DSL in Gradle?

Simply, it stands for 'Domain Specific Language'. IMO, in gradle context, DSL gives you a gradle specific way to form your build scripts. More precisely, it's a plugin-based build system that defines a way of setting up your build script using (mainly) building blocks defined in various plugins.

What is Gradle Kotlin?

Gradle is a build system that is very commonly used in the Java, Android, and other ecosystems. It is the default choice for Kotlin/Native and Multiplatform when it comes to build systems.


2 Answers

You must use "project.extra[...]" instead of "extra[...]"

    buildscript {
            extra["kotlin_version"] = "1.3.72"

            repositories {
                jcenter()
            }

            dependencies {
                classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${project.extra["kotlin_version"]}")
            }
        }
like image 70
Roman Savelev Avatar answered Nov 27 '22 03:11

Roman Savelev


This works for me:

buildscript {
    extra["kotlin_version"] = "1.3.61"

    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.5.3")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${extra["kotlin_version"]}")
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}
like image 23
Maksym V. Avatar answered Nov 27 '22 04:11

Maksym V.