gradle: kotlin("jvm") vs id("org.jetbrains.kotlin.jvm") [duplicate] - gradle

It looks like there are two ways of declaring some Kotlin plugins using plugins DSL: Using the id() method and the kotlin() method. For example, the android plugin can be added using either id("kotlin-android") or kotlin("android"). This is also the case for kapt but not for parcelize. Why can't this be kotlin("parcelize")? Is there a reason for this discrepancy? I tried to look up relevant documentation but that didn't get me very far.

TL;DR: Take the Gradle plugin ID for Parcelize and use everything after org.jetbrains.kotlin.
plugins {
kotlin("plugin.parcelize") version "1.6.10"
}
The kotlin(...) function is part of the Gradle Kotlin DSL. It is an extension function that extends
PluginDependenciesSpec, the plugins {} block
DependencyHandler, the dependencies {} block
I'm going to focus on the Plugin extension function. Some of this answer is applicable to the Dependency extension.
kotlin(...) source code
It's generated so it's difficult to see the source code. I dug through GitHub and found it in GenerateKotlinDependencyExtensions.kt
fun PluginDependenciesSpec.kotlin(module: String): PluginDependencySpec =
id("org.jetbrains.kotlin.$module")
(edited, to show the end result)
kotlin(...) = id("org.jetbrains.kotlin.$module")
So it's nothing special. It's a Kotlin-specific shortcut for id(...). So if you
take the plugin ID for Parcelize, org.jetbrains.kotlin.plugin.parcelize,
and remove the bits that the kotlin(...) function adds (org.jetbrains.kotlin.),
you're left with plugin.parcelize.
NOTE Because this is in the plugins {} block, it's working on the Gradle plugin ID (org.jetbrains.kotlin.plugin.parcelize), not the Maven coordinates (org.jetbrains.kotlin:kotlin-gradle-plugin).
plugins {
// these two are equivalent
// id("org.jetbrains.kotlin.plugin.parcelize")
kotlin("plugin.parcelize")
}
Oh wait... it doesn't work??
FAILURE: Build failed with an exception.
* Where:
Build file '/.../build.gradle.kts' line: 3
* What went wrong:
Plugin [id: 'org.jetbrains.kotlin.plugin.parcelize'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
"Build failed with an exception" - Plugin version
That's because unlike the DependencyHandler.kotlin(...) extension, the PluginDependenciesSpec.kotlin(...) doesn't include the version. It says that in the error message: "plugin dependency must include a version number"
So to resolve it, add a version number.
plugins {
kotlin("plugin.parcelize") version "1.6.10"
}
Other Kotlin plugins
The same goes for the other Kotlin plugins. For example...
plugins {
// https://kotlinlang.org/docs/all-open-plugin.html
kotlin("plugin.allopen") version "1.6.10"
// https://kotlinlang.org/docs/all-open-plugin.html#spring-support
kotlin("plugin.spring") version "1.6.10"
// https://kotlinlang.org/docs/no-arg-plugin.html
kotlin("plugin.noarg") version "1.6.10"
// I can't find a Gradle Plugin ID for
// https://kotlinlang.org/docs/sam-with-receiver-plugin.html
// so this won't work!
// kotlin("kotlin-sam-with-receiver") version "1.6.10"
}

Related

Cannot apply snapshot plugin for OpenAPI

I am trying to get a SNAPSHOT of a plugin that may fix our issue, but the I cannot get the library:
plugins {
kotlin("jvm") version "1.7.10"
id("maven-publish")
id("org.jetbrains.kotlin.plugin.serialization") version "1.7.10"
id("org.openapi.generator") version "6.1.0-SNAPSHOT"
}
I have already added all the necessary repositories, but still get this error:
Plugin [id: 'org.openapi.generator', version: '6.1.0-SNAPSHOT'] was not found in any of the following sources
(If I use the released 6.0.1 version, everything works fine)
Attempt 2: I have tried to get the plugin in another way.
buildscript {
repositories {
maven("https://companyserver.io/company-maven-snapshot")
// or, via Gradle Plugin Portal:
// url
}
dependencies {
classpath ("org.openapitools:openapi-generator-gradle-plugin:6.0.1")
}
}
apply(plugin="org.openapi.generator")
This seems to find the plugin, but I still get an error on the first line of:
openApiGenerate {
generatorName.set("kotlin")
inputSpec.set("$projectDir/mapi-open-api-spec.json")
modelPackage.set("mapi.generated.models")
apiPackage.set("mapi.generated.apis")
invokerPackage.set("mapi.generated.invokers")
configOptions.put("library", "multiplatform")
typeMappings.put("kotlin.Any", "kotlin.String") // change autogenerated data type of generated templates
kotlin.sourceSets["main"].kotlin.srcDir("$buildDir/generate-resources/main/src/main/kotlin")
}
The error:
.../build.gradle.kts:75:1: Unresolved reference: openApiGenerate
I am a bit stuck now, any ideas?

Kotlin Gradle DSL - Precompiled script in buildSrc not working

I am using Gradle version 4.10.3 (need Android support) and am trying to put a test precompiled script into my buildSrc folder for use with various modules within the project. Here is my basic code setup:
//maven-deploy.gradle.kts, a custom precompiled plugin under
// buildSrc\src\main\kotlin\maven-deploy.gradle.kts
plugins {
maven
signing
}
//a test task
tasks {
register("hello") {
doLast {
println("hello")
}
}
}
//buildSrc build.gradle.kts
plugins {
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
}
repositories {
jcenter()
}
//main project root's build.gradle.kts
plugins {
base
kotlin("jvm") version Vof.kotlin apply false
kotlin("android") version Vof.kotlin apply false
kotlin("android.extensions") version Vof.kotlin apply false
id("kotlinx-serialization") version Vof.kotlin apply false
id("maven-deploy")
}
and I'm getting this error:
error:Plugin [id: 'maven-deploy'] was not found in any of the following sources:
- Gradle Core Plugins (not a core plugin, please see https://docs.gradle.org/4.10.3/userguide/standard_plugins.html for available core plugins)
- Plugin Repositories (plugin dependency must include a version number for this source)
Am I doing this incorrectly? I am following this guide on the Gradle website. Is this outdated? Any help is appreciated!
EDIT: I got this working by adding java-gradle-plugin (with tildes) to the buildSrc's build.gradle.kts file under plugins. Not sure if that is the correct way to go about this, however.

Plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.2.71'] was not found in any of the following sources

I have a fresh install of IntelliJ, I created a new kotlin gradle project using the following settings:
This produces the following build.gradle.kts, (the exact same file works on my Windows machine):
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.2.71"
}
group = "com.test"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
compile(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
Which produces this error, when trying to do a gradle refresh:
Plugin [id: 'org.jetbrains.kotlin.jvm', version: '1.2.71'] was not
found in any of the following sources:
Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
Plugin Repositories (could not resolve plugin artifact 'org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.2.71')
Searched in the following repositories:
Gradle Central Plugin Repository
Check your Internet connection and make sure your Internet is not restricted.
I solved this problem by turning on proxy for all tunnels (not just HTTP) with a VPN app.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
// kotlin("jvm") version "1.2.71"
}
group = "com.test"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
compile(kotlin("stdlib-jdk8"))
}
//tasks.withType<KotlinCompile> {
// kotlinOptions.jvmTarget = "1.8"
//}
gradle sync by commenting the above lines. The gradle will be set up.
once the gradle is downloaded, uncomment those line and sync again.
if the dependencies are not downloaded properly, run 'gradle build' in the terminal and click on gradle sync.
This solved the issue for me.
(1) in my case (OpenJDK 11 on Ubuntu 18.04) the problem was Gradle not being able to download the POM file from gradle plugin-server. you can test it by entering this line into jshell:
new java.net.URL("https://plugins.gradle.org/m2/org/jetbrains/kotlin/jvm/org.jetbrains.kotlin.jvm.gradle.plugin/1.3.11/org.jetbrains.kotlin.jvm.gradle.plugin-1.3.11.pom").openStream()
(you can find your url by running gradle with --debug option)
So if you received an exception like this: InvalidAlgorithmParameterException: trustAnchors parameter must be non-empty then the trouble is CA-certs cache. which could be easily fixed by writing these lines into bash Ref:
sudo su
/usr/bin/printf '\xfe\xed\xfe\xed\x00\x00\x00\x02\x00\x00\x00\x00\xe2\x68\x6e\x45\xfb\x43\xdf\xa4\xd9\x92\xdd\x41\xce\xb6\xb2\x1c\x63\x30\xd7\x92' > /etc/ssl/certs/java/cacerts
/var/lib/dpkg/info/ca-certificates-java.postinst configure
By the way do not forget to restart gradle daemon before trying again. (gradle --stop)
(2) another reason could be your internet not having access to bintray.com (the internet of Iran or China) which you can test by putting this line on jshell :
new java.net.URL("https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin-api/1.3.11/kotlin-gradle-plugin-api-1.3.11.pom").openStream()
If you received a connection timeout, it confirms this theory. In this case you need to buy and have proxy/vpn connected in order to be able to download these dependencies.
Check your gradle and kotlin (or Java) versions.
I got the same error and my issue is solved by specifying the kotlin version in build.gradle:
Before:
plugins {
id 'org.jetbrains.kotlin.jvm'
}
After:
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.4.10"
}
In my case (Ubuntu 20.04), problem was with gradle 7.2, installed from snap.
I have removed gradle 7.2, installed from snap and install gradle 7.2 from sdkman. Works fine for me.
If you are using java like me .I got the issue fixed by adding the following:
Root gradle
dependencies {
ext.kotlin_version = '1.4.10'
classpath "com.android.tools.build:gradle:7.0.4"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.38.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
......
}
App gradle file
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
dependencies {
implementation "com.google.dagger:hilt-android:2.38.1"
kapt "com.google.dagger:hilt-compiler:2.38.1"
......
}
Ok, so the answer was very simple all along. For some reason I activated gradle's "Offline work" toggle and that was the cause of the problem.
To disable it simply go to Settings > Build, Execution, Deployment > Build Tools > Gradle and deselect the "Offline work" checkbox.
In my case the problem was because Charles Proxy. After closing Charles I could start working again
I updated my Kotlin version to 1.7.20 and fixed this problem.
id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
I recently had similar issue with an empty project autogenerated by Intellij Idea.
Solved this problem by combining Java and Gradle versions.
Initially I had Oracle Java 8 with Gradle 6.8.3.
After several attempts I found a working combination - AdoptOpenJDK 11 and Gradle 5.6.4
In my case I changes the Gradle JVM in Settings > Build, Execution, Deployment > Build Tools > Gradle and it worked.
Disconnect from your VPN (or make sure you have an open internet connection), then restart Android Studio.
If you don't restart it sometimes it continues with invalid proxy properties.
This for Ktor devs. If you are working on a ktor application with the web project generator there is a chance the generator sets invalid jvm plugin version. So make sure you are setting correct version for jvm plugin. You can find the latest jvm plugin version here. Here is the sample build.gradle.kts file.
//Plugin section
plugins {
kotlin("jvm") version "1.8.0"
id("io.ktor.plugin") version "2.2.2"
}
//Dependancy section
dependencies {
...
testImplementation("io.ktor:ktor-server-tests-jvm:1.8.0")
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:1.8.0")
...
}

gradle-bintray-plugin Plugin [id: 'com.jfrog.bintray', version: '1.+'] was not found

I'm trying to use the gradle plugin gradle-bintray-plugin.
Currently using Gradle 4.4
Following the tutorial in the github page I should add the plugin in this way:
plugins {
...
id "com.jfrog.bintray" version "1.+"
}
I'm receiving this error message and not being able to continue:
Plugin [id: 'com.jfrog.bintray', version: '1.+'] was not found in any
of the following sources:
Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
Plugin Repositories (dynamic plugin versions are not supported) Open File
Dynamic versions were maybe authorized in the past for the plugins block (as the tutorial gives it as an example) but now it's forbidden
if (versionSelectorScheme.parseSelector(markerVersion).isDynamic()) {
result.notFound(SOURCE_NAME, "dynamic plugin versions are not supported");
return;
}
But it's not the case for the old buildscript way and the below code is working fine with Gradle 4.10
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.+'
}
}
I just find out the solution. Looks like it just works specifying the exact version that you want to use.
So changing this:
plugins {
...
id "com.jfrog.bintray" version "1.+"
}
To this:
plugins {
...
id "com.jfrog.bintray" version "1.8.4" // exact version!
}
Now Works!

Gradle: how to use the latest version of a plugin automatically

I have developed an internal plugin. The plugin has its own version. I then use that plugin for a build process in a repository.
If I change the version of the plugin, I have to update the build.gradle to spell our the new version. I have about 100 of these repositories.
Is there a way to specify in my build.gradle to use the latest version of the plugin that can be found in that location?
I could ran a batch file before gradle that find the latest, updates build.gradle with that number and then runs the build process but this is a big work around to a functionality that should be available.
See code below where I call the plugin that I change quite often:
buildscript {
repositories {
maven {
url "c:/git/_TEST/plug-in"
}
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath group: 'com.myplugin.gradle', name: 'com.myplugin.mypluginbuild', version: '1.0'
}
apply plugin: 'com.myplugin.mypluginbuild'
}
if I don't specify the version, it returns an error. Any suggestions?
For those who use plugins{} block, gradle7+ supports dynamic version, which includes + and latest.release.
plugins {
id "your-plugin-id" version "1.0.+"
}
dynamic version doc
It's not possible this way. See the plugins {} block and plugins documentation.
For core plugins you must not provide a version.
For community plugins you have to provide a version.
Maybe script plugins are way to go:
apply from: 'my_script_plugin.gradle'
I have a solution to this question.
The + specified in the version field will do the trick. It will enable gradle to use the latest plug-in automatically.
I.e:
dependencies {
classpath group: 'com.myplugin.gradle', name: 'com.myplugin.mypluginbuild', version: '1.+'
}

Resources