error in build.gradle.kts script using extra property - kotlin

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.

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"]}")
}
}

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()
}
}

Related

build.gradle file could not compile

This error appears when I run my project:
Could not compile build file 'H:\StartActivity\build.gradle'.
startup failed:
build file 'H:\StartActivity\build.gradle': 28: only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.0-release-764"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.5.0-release-764'
}
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
buildscript {
ext.kotlin_version = "1.5.0"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Changing ext.kotlin_version to 1.5.0 is working :)
You should move plugins{...} before allprojects{...}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.0-release-764"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.5.0-release-764'
}
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
....

Android Studio can't resolve androix.ui dependency although repository `google()` has been added

I am kind of frustated. I want to use the androidx.ui dependency but gradle denies to download it.
Here is my build.gradle.kts file:
import org.jetbrains.compose.compose
plugins {
kotlin("jvm") version "1.4.0"
id("org.jetbrains.compose") version "0.1.0-m1-build62"
}
buildscript {
repositories {
jcenter()
google()
mavenCentral()
maven { setUrl("https://maven.google.com") }
}
}
allprojects {
repositories {
jcenter()
google()
mavenCentral()
maven { setUrl("https://maven.google.com") }
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
}
dependencies {
implementation(compose.desktop.currentOs)
implementation("androidx.ui:ui-tooling:1.0.0-alpha07")
}
compose.desktop {
application {
mainClass = "MainKt"
}
}
Error:
Could not resolve androidx.ui:ui-tooling:1.0.0-alpha07.
Possible solution:
Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html

kotlin-compiler-embeddable missing within gradle build

I am trying to setup a multiproject gradle/kotlin build and I am getting following error:
Could not determine the dependencies of task ':compileKotlin'.
> Could not resolve all files for configuration ':kotlinCompilerClasspath'.
> Cannot resolve external dependency org.jetbrains.kotlin:kotlin-compiler-embeddable:1.4.10 because no repositories are defined.
Required by:
project :
Strange thing is the empty project :.
My simplifiged build.gradle.kts looks like this:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
base
kotlin("jvm") version "1.4.10"
}
buildscript {
extra["kotlin_version"] = "1.4.10"
repositories {
jcenter()
mavenCentral()
}
}
subprojects {
apply(plugin = "kotlin")
repositories {
jcenter()
mavenCentral()
}
tasks.compileKotlin {
kotlinOptions.jvmTarget = "11"
}
tasks.compileTestKotlin {
kotlinOptions.jvmTarget = "11"
}
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:${rootProject.extra["kotlin_version"]}")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${rootProject.extra["kotlin_version"]}")
}
}
Do I need to duplicate the repositories within the buildscript section?
What is that is causing the error above?
It seems like your repositories { ... } configuration done in the subproject section is sufficient, but it is only applied to the supbrojects but not the root project itself (which has the Kotlin plugin applied, too, and whose task :compileKotlin is failing).
There are two ways to fix this.
First, you could move the repositories { ... } section from subprojects { ... } to a new block allprojects { ... } (thus applied to the root project as well).
Or, if you don't actually need the Kotlin plugin in the root project (i.e. you don't have Kotlin code there), you can add .apply(false) to your plugin declaration:
plugins {
kotlin("jvm").version("1.4.10").apply(false)
}

maven-publish missing artifact to repository

II'm trying to publish my lib but i'm having some problems related to its publication.
I'm using this library https://github.com/vanniktech/gradle-maven-publish-plugin to publish my project on maven central (oss), but, this for some reason only -sources.jar, -javadoc.jar and .module are being pushed to the repository, however file-butler-0.1.4-SNAPSHOT.jar is missing :(
Does someone have any idea?
build.gradle(module)
plugins {
id 'java-library'
id 'kotlin'
id 'kotlin-kapt'
}
dependencies {
def gson = "com.google.code.gson:gson:2.8.6"
def moshi = "com.squareup.moshi:moshi:1.11.0"
def moshCodeGen = "com.squareup.moshi:moshi-kotlin-codegen:1.11.0"
implementation(Deps.core.kotlin)
implementation(gson)
implementation(moshi)
kapt(moshCodeGen)
testImplementation(Deps.testing.truth)
testImplementation(Deps.testing.jUnit)
kaptTest(moshCodeGen)
}
sourceSets {
main.kotlin.srcDirs += 'src/main/kotlin'
main.java.srcDirs += 'src/main/java'
}
apply plugin: "com.vanniktech.maven.publish"
build.gradle.kts(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.1.0")
classpath(kotlin("gradle-plugin", "1.4.10"))
classpath("org.jetbrains.dokka:dokka-gradle-plugin:1.4.10")
classpath("com.vanniktech:gradle-maven-publish-plugin:0.13.0")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenCentral()
google()
jcenter()
}
}
task<Delete>("clean") {
delete(rootProject.buildDir)
}
ps: publishToMavenLocal publish the expected jar to my local repository.

Gradle-Android: Where is the classpath() coming from?

Here is the root build.gradle file of an android app. I want to ask where does the classpath() method is declared. I guess in Project but I cant find it in the reference.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
From the Interface ScriptHandler, classpath is the configuration's value of public static final String CLASSPATH_CONFIGURATION "classpath".
You can call it via buildscript.CLASSPATH_CONFIGURATION as buildscript returns a ScriptHandler.