Modifying tasks in gradle plugin in buildSrc with kotlin dsl - kotlin

I have a Gradle plugin in buildSrc/src/main/kotlin/foo.bar.kts where I am trying to modify tasks, but those modification are failing. If I remove task modification parts, the rest, namely setting up repositories, works just fine.
I think I am missing the syntax how to modify tasks in the module that imports this plugin.
buildSrc/src/main/kotlin/foo.bar.kts:
repositories {
mavenCentral()
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
Using the plugin in my build.gradle.kts
plugins {
id("foo.bar")
}
The errors that ./gradlew clean build generates
> Task :buildSrc:compileKotlin FAILED
e: /some/path/buildSrc/src/main/kotlin/foo.bar.gradle.kts: (8, 7): Unresolved reference: test
e: /some/path/buildSrc/src/main/kotlin/foo.bar.gradle.kts: (9, 5): Unresolved reference: useJUnitPlatform
e: /some/path/buildSrc/src/main/kotlin/foo.bar.gradle.kts: (12, 16): Unresolved reference: KotlinCompile
e: /some/path/buildSrc/src/main/kotlin/foo.bar.gradle.kts: (12, 33): Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!> was expected
e: /some/path/buildSrc/src/main/kotlin/foo.bar.gradle.kts: (13, 5): Unresolved reference: kotlinOptions
e: /some/path/code/buildSrc/src/main/kotlin/foo.bar.gradle.kts: (13, 19): Variable expected

That's because foo.bar.kts from the buildSrc has to be compiled first, so it must be self-contained. It cannot foresee that it will be applied in other build scripts that will have the Kotlin plugin applied already (and that would actually be brittle).
This means 2 things:
at compile time, you cannot access types and functions from the plugin without a dependency on it (most likely declared in buildSrc/build.gradle.kts)
at runtime, you cannot assume the presence of tasks and extensions that would only be available when applying some plugin (here, the Kotlin plugin) - you need to make sure it has been applied.
To solve this, you can apply the Kotlin plugin in foo.bar.kts itself, or react to it with things like withPlugin() or plugins.withId():
plugins {
kotlin("jvm")
}
repositories {
mavenCentral()
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
And to do that, you may need a dependency on the Kotlin plugin in buildSrc/build.gradle.kts:
dependencies {
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.21")
}
Sometimes you don't want to forcefully apply the Kotlin plugin when yours is applied (because maybe the consuming projects might want Kotlin JVM or Kotlin Multiplatform, but not both).
The is probably not the case for you because when using convention plugins like this, you usually do want to be opinionated on the plugins, but it can still be useful when defining a convention that works both on JVM and multiplatform for instance. So for the sake of completeness let me elaborate on this case.
If you need this laziness and/or conditional configuration, you can register configuration that will only be applied when and if a given plugin is applied using plugins.withId(...). No need for afterEvaluate and the likes. For instance:
plugins.withId("org.jetbrains.kotlin.jvm") {
tasks.named<Test>("test") {
useJUnitPlatform()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
}
plugins.withId("org.jetbrains.kotlin.multiplatform") {
tasks.withType<Test>().configureEach {
useJUnitPlatform()
}
tasks.withType<KotlinCompileCommon>() {
// ..
}
}
Note that you will still need your buildSrc/build.gradle.kts to declare the dependency on the plugins if you need to use classes from them.

Related

Applying org.jetbrains.kotlin.jvm within custom Gradle plugin not working

I'm writing a custom Gradle plugin using Kotlin. The goal is to apply and configure certain plugins within this custom plugin. Consumers should only need to apply the custom plugin and be all set.
The problem:
I want to apply these two kotlin plugins (and other plugins) in the custom plugin (which can be applied like this normally in a Gradle build script):
kotlin("jvm") version "1.4.10"
kotlin("plugin.spring") version "1.4.10"
But when applying them from within my own plugin a couple of things goes wrong.
The jar task is not found. I get this complaint in the project consuming my plugin.
The contents of the dependencies section, e.g api, implementation etc are also causing errors, like if Gradle doesn't understand what they are.
I can, in the consuming project however, simply add the line below to the build script to get the jar task back, and make dependencies work again:
kotlin("jvm") version "1.4.10"
I also wrote a sort of debug function in the consuming Gradle build, just to see what plugins are applied. They all print true when I apply only my own plugin (or add kotlin-jvm to the script manually). Which seems to me like my own plugin is doing its job.
open class GreetingTask : DefaultTask() {
#TaskAction
fun greet() {
println("hello from GreetingTask")
println(project.pluginManager.hasPlugin("org.springframework.boot"))
println(project.pluginManager.hasPlugin("io.spring.dependency-management"))
println(project.pluginManager.hasPlugin("com.github.ben-manes.versions"))
println(project.pluginManager.hasPlugin("org.jetbrains.kotlin.jvm"))
println(project.pluginManager.hasPlugin("org.jetbrains.kotlin.plugin.spring"))
println(project.pluginManager.hasPlugin("java"))
}
}
My apply method so far in my plugin:
override fun apply(project: Project) {
configureRepositories(project)
configureTesting(project)
configureJava(project)
configureSpringBoot(project)
configureSpringDependencyManagement(project)
configureKotlin(project)
configureVersions(project)
configurePublishing(project)
}
And for setting up Kotlin:
private fun configureKotlin(project: Project) {
project.pluginManager.apply("org.jetbrains.kotlin.jvm")
project.pluginManager.apply("org.jetbrains.kotlin.plugin.spring")
project.tasks.withType(KotlinCompile::class.java).configureEach { task ->
task.kotlinOptions.freeCompilerArgs = listOf("-Xjsr305=strict")
task.kotlinOptions.jvmTarget = "11"
}
}
All the other configureXYZ seems to work out fine.
I solved it. The problem was how I applied the plugin.
This does NOT work:
build.gradle.kts
buildscript {
repositories {
mavenLocal()
gradlePluginPortal()
}
dependencies {
classpath("com.praqma:demo:1.0.0")
}
}
apply(plugin = "com.praqma.demo.DemoPlugin")
This does work:
settings.gradle.kts
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
}
}
build.gradle.kts
plugins {
id("gradle.demoPlugin") version "0.0.1-SNAPSHOT"
}

How to share Java code between Android and JVM targets (with Kotlin Multiplatform)?

I am trying to share Java code between Android and JVM targets using Kotlin Multiplatform feature (sample project: https://github.com/dmitrykolesnikovich/accessJavaCode-issue)
Simply saying, ":library1" and ":library2" both are Kotlin multiplatform libraries targeting JVM and Android. ":library2" depends on ":library1". They both uses Kotlin and Java. ":library2" is intended to be dependency of 1) Android application and 2) desktop (JavaFX) application. That's why 1) AAR artifact and 2) JAR artifact both are needed (?) - so I use 1) Android target and 2) JVM target for both ":library1" and ":library2".
The problem is that, when I have Java code in ":library1"
public class JavaCode {} // JavaCode.java
And Kotlin code in ":library2" that depends on ":library1"
class AccessJavaCode : JavaCode() // AccessJavaCode.kt
Android target is OK with recognizing Java but JVM target is not:
> Task :library2:compileKotlinJvm FAILED
e: AccessJavaCode.kt: (3, 38): Unresolved reference: JavaCode
In gradle config I define two plugins: kotlin-multiplatform and com.android.library:
apply plugin: "kotlin-multiplatform"
apply plugin: "com.android.library"
kotlin {
targets {
jvm()
android()
}
sourceSets {
jvmMain {
dependencies {
api kotlin("stdlib-common")
api kotlin("stdlib-jdk8")
}
}
androidMain {
dependsOn jvmMain
}
}
}
android {
compileSdkVersion 28
sourceSets {
main {
java.srcDirs += "src/jvmMain/kotlin" // Android target recognizes Java with this
manifest.srcFile "src/androidMain/AndroidManifest.xml"
}
}
}
I am pretty sure it’s something simple with my gradle file. Many thanks for your help guys.
~~~~ EDIT ~~~~
Another workaround the issue without losing the ability to generate an android archive .aar for the library1 would be to make a new version of this same library depending on precompiled artefacts of a splitted version of the original library1.
So you would end up with a multi-module gradle project, something like this:
library1-jvm with java plugin enabled
library1-android with android plugin enabled
library1 which will depends on prebuilt library1-jvm.jar and library1-android.aar
You could use whatever you prefer to publish those artefacts, but a local maven repo should work just fine!
That would mean replacing:
kotlin {
targets {
jvm()
android()
}
sourceSets {
jvmMain {
dependencies {
api kotlin("stdlib-common")
api kotlin("stdlib-jdk8")
}
}
androidMain {
dependsOn jvmMain
}
}
}
with:
kotlin {
targets {
jvm()
android()
}
sourceSets {
jvmMain {
dependencies {
api kotlin("stdlib-common")
api kotlin("stdlib-jdk8")
api "com.company:library-jvm:1.0.0"
}
}
androidMain {
dependsOn jvmMain
dependencies {
api "com.company:library-android:1.0.0"
}
}
}
}
That way, you don't need the java plugin at all in the final library1, because all the java code will already be built in a separate step.
Hence library1 could keep both the JVM and Android targets
~~~~~~~~~~~~~
In order to fix your issue, you need to:
Split your build.gradle configuration in order to have one config per library, this will be needed because you cannot enable the java plugin and the android at the same time for the same Gradle project, or you will end up with the following Error: The 'java' plugin has been applied, but it is not compatible with the Android plugins
Enable the java plugin in your library1 project if you want your JVM target to recognize your Java source files.
The Java source files need to be placed in the sibling directories java of the kotlin source roots.
More infos: Kotlin docs for java-support-in-jvm-targets
I also created a pull request solving your issue.
The downside of this approach is that you will not be able to generate an android archive .aar for the library1, but I guess using the java archive .jar in your android project should not be a problem at all.
library1/build.gradle:
apply plugin: "kotlin-multiplatform"
kotlin {
jvm {
withJava()
}
sourceSets {
jvmMain {
dependencies {
api kotlin("stdlib-common")
api kotlin("stdlib-jdk8")
}
}
}
}
library2/build.gradle:
apply plugin: "kotlin-multiplatform"
apply plugin: "com.android.library"
kotlin{
jvm()
android()
sourceSets {
jvmMain.dependencies {
api project(":library1")
}
androidMain {
dependsOn jvmMain
}
}
}
android {
compileSdkVersion 28
sourceSets {
main {
java.srcDirs += "src/jvmMain/kotlin"
manifest.srcFile "src/androidMain/AndroidManifest.xml"
}
}
}

launch is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2

I'm trying to run the simplest example with coroutines:
import kotlinx.coroutines.*
fun main() {
GlobalScope.launch {
delay(1000L)
println("${Thread.currentThread().name}: World")
}
println("${Thread.currentThread().name}: Hello")
Thread.sleep(2000L)
println("${Thread.currentThread().name}: Finish!")
}
And my build.gradle file looks like this:
buildscript {
// Consider moving these values to `gradle.properties`
ext.kotlin_version = '1.3.0-rc-146'
ext.kotlin_gradle_plugin_version = '1.3.0-rc-198'
ext.kotlinx_coroutines = '1.0.0-RC1'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.1.51"
}
apply plugin: 'idea'
apply plugin: 'application'
group 'by.kotlin'
version '1.0-SNAPSHOT'
mainClassName = 'MainKt'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
But when I run this example, I've got the following errors:
e: ...Main.kt: (6, 17): 'launch(CoroutineContext = ..., CoroutineStart = ..., [ERROR : Bad suspend function in metadata with constructor: Function2]<CoroutineScope, Continuation<Unit>, Any?>): Job' is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2
e: ...Main.kt: (7, 9): Suspend function 'delay' should be called only from a coroutine or another suspend function
e: ...Main.kt: (7, 9): 'delay(Long): Unit' is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2
> Task :compileKotlin FAILED
Why do these errors occur? I'm completely confused, because the first error says that launch "is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2", but I use Kotlin 1.3 in my build.gradle file (in particular, '1.3.0-rc-146')...
UPD
It seems that the reason of problem is in IntelliJ IDEA Settings:
But how to fix it, if the latest language version, which can be selected there, is 1.2, not 1.3?
Make sure you have updated Kotlin to 1.3. You can do this from Preference->Lanugage & Framework->Kotlin Updates
Then change the version of kotlin.jvm plugin to 1.3.0 in gradle. (https://plugins.gradle.org/plugin/org.jetbrains.kotlin.jvm)
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.3.0"
}
And for including coroutines
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.0.0'
}
It should be fine now.
You must change kotlin plugin version
Your current kotlin plugin version is 1.2.51
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
this is correct
buildscript {
ext.kotlin_version = '1.3.0'
ext.kotlin_gradle_plugin_version = '1.3.0'
ext.kotlinx_coroutines = '1.0.0'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
'org.jetbrains.kotlin:kotlin-gradle-plugin:'+kotlin_version
}
}
I solved this by manually updating the Kotlin-IntelliJ Plugin.
First, download the newer version of the Kotlin plugin that is compatible with your version of IntelliJ https://plugins.jetbrains.com/plugin/6954-kotlin/versions/stable
Then in IntelliJ's Settings -> Plugins, click on the settings/gear icon on the top-right side. From there choose Install Plugin from Disk..., choose the zip file you got from the intellij website. Then it will ask you to restart the IDE, and that's it :)
If it has started to occur only recently then chances are two links are generating same file with same name, in this case removing one of your recent file from build.gradle will help you resolving the issue.
In my case this was causing the issue
implementation 'com.miguelcatalan:materialsearchview:1.4.0'
implementation 'com.github.Ferfalk:SimpleSearchView:0.2.0'
After removing first one out of them the issue was resolved.

How to build kotlinx.coroutines in Kotlin/Native (test version 0.23.4-native-1)

This question is a continuation of this thread:
https://github.com/Kotlin/kotlinx.coroutines/issues/246#issuecomment-407023156
I am trying to use org.jetbrains.kotlinx:kotlinx-coroutines-core-native:0.23.4-native-1 in a Kotlin/Native project targeting iOS.
build.gradle:
buildscript {
repositories {
mavenCentral()
maven { url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" }
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-native-gradle-plugin:0.8'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
}
apply plugin: 'kotlin-platform-native'
repositories {
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
}
sourceSets {
main {
component {
target 'ios_arm32', 'ios_arm64', 'ios_x64'
outputKinds = [KLIBRARY]
}
}
}
dependencies {
expectedBy project(':common')
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:0.23.4-native-1"
}
The kotlinx:kotlinx-coroutines-core-native dependency doesn't seem to work, as the produces build errors like:
error: unresolved reference: coroutines
import kotlinx.coroutines.experimental.*
^
If I manually include the artifact dependencies such as org.jetbrains.kotlinx:kotlinx-coroutines-core-native_release_ios_x64:0.10.3-native, then I get a complier exception:
exception: java.lang.IllegalStateException: Could not find "atomicfu-native"
This error persists, even if I also add org.jetbrains.kotlinx:atomicfu-native:0.10.3-native dependency.
Here is a list of things to check for (I have been through this, and finally made it work) :
Enable Gradle metadata. It's required to retrieve the coroutines dependencies. To do so, add this line in your "settings.gradle" file, after all the "include" instructions :
enableFeaturePreview('GRADLE_METADATA')
use gradle 4.7 (newer version are incompatible with the meta data of the current coroutines library, they require something with 0.4 version and the current published one uses 0.3)
In the iOS module :
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:0.23.4-native-1"
In your common module :
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:0.23.4"
If you have a js module, it may fail due to the gradle metadata feature. You can fix it by adding this before each of your "repositories" blocks (https://github.com/srs/gradle-node-plugin/issues/301)
repositories.whenObjectAdded {
if (it instanceof IvyArtifactRepository) {
metadataSources {
artifact()
}
}
}
Hope this will be enough !

Unresolved references for Kotlin (JVM) Standard Library Elements

After adding gradle to an existing Kotlin project in IntelliJ, I started having issues with references to some standard library elements. For instance, Kotlin String type is recognized, but the mutableMapOf is giving me
Error:(11, 60) Kotlin: Unresolved reference: mutableMapOf
another is:
Error:(9, 78) Kotlin: Unresolved reference: Array
during compilation. They are marked red in the IDE, as well (not isolated to only compilation)
Another error that appears in IntelliJ is Kotlin not configured, with no options to configure Kotlin
Here is my gradle build file:
apply plugin: 'kotlin'
group = "com.serguei.myproject"
version = "1.0"
buildscript {
ext.kotlin_version = '1.2.10'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile 'com.google.code.gson:gson:2.8.2'
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
Thank you for the comments Ice100 and Preston Garno, they have helped greatly in finding a solution.
The issue was that my project was not structured correctly. I had:
MyProject -> src -> [Source Files]
-> build.gradle
however, after consulting Using Gradle more closely, it appears that the recommended project structure should be
MyProject -> src -> main -> kotlin -> [Source Files]
-> build.gradle
Once the project structure is adjusted, go into File -> Invalidate Caches / Restart and click the Invalidate and Restart. Once the indexer is rerun, the Kotlin standard library types, functions, etc. should now be available.
For any one coming to this page because of problem that IntelliJ suddenly does not find stuff in kotlin standard library: File -> Invalidate Caches / Restart helped me in a situation which didn't even involve gradle.
After migrating my Java app to Kotlin I find myself in this situation.
After checking gradle structure here
You can sync your project with gradle using sync button from menu bar.
this will download necessary files for Kotlin
Then you can just validate cache & Restart if necessary.