Is it possible to write tasks in Kotlin in buildSr...
# gradle
g
Is it possible to write tasks in Kotlin in buildSrc, without migrating to DSL?
👌 3
m
Yes, it should work fine.
g
hm. It's highlighted, but it stops working after I convert it to Kotlin. Ex: Java
Copy code
package com.foo;

public class Fun {
  public static void sayHello() {
    System.out.println("Hello");
  }
}
Usage:
Copy code
task hello {
    doLast {
        com.foo.Fun.sayHello()
    }
}
That works. Once I convert
Fun
to kotlin it can't import it anymore.
m
Did you configure kotlin in
buildSrc/build.gradle
?
g
yes, like this:
Copy code
plugins {
    kotlin("jvm") version '1.3.31'
}

dependencies {
    implementation gradleApi()
    compile(kotlin("stdlib-jdk8"))
}
g
You don't need Kotlin Plugin, it already bundled to Gradle, but you may use kotlin-dsl plugin which configures gradleApi, stdlib and uses bundled versions of it:
Copy code
plugins {
   id "org.gradle.kotlin.kotlin-dsl" version "1.2.10"
}
Check that your import is correct (maybe you need something like com.too.FunKt or Companion, depends on how your final code looks like), check that this file in buildSrc/src/kotlin, not in src/groovy
k
Also, might be possible that you’re missing
@JvmStatic
on that method, so the Groovy buildscript doesn’t see it as a static
Fun.sayHello()
anymore
g
yes, it depends what is final kotlin code
g
@gildor you mean to mirgate buildSrc to kotlin-dsl? Or just applying the dsl plugin in
build.gradle
? Because the later gives me
NoClassDefFoundError: org/gradle/kotlin/dsl/support/KotlinDslPluginsKt
g
Just apply Kotlin DSL plugin to buildSrc project
Anyway, it's not requirement, just simplifies configuration and provide a lot of Kotlin DSL helpers for work with Gradle API from kotlin
Looks that this version of Kotlin DSL not compatible with your gardle version, unfortunately there is no easy way to consume this plugin to match Gradle, you can check requires Kotlin DSL version in your Gradle distribution sources, but don't remember how to do that exactly. Because if you use kotlin-dsl for build.gradle, you have convinient accessor for this plugin. I created an issue about it, to simplify usage of kotlin-dsl plugin from Groovy code, but it's not fixed, as I remember
g
well, didn't work, as I mentioned the error. Even without going dsl path, let's take a simple case: /buildSrc/src/main/kotlin/Dependencies.kt
Copy code
object deps {
    val multidex = "androidx.multidex:multidex:2.0.1"
}
I expect this to be visible in any of my
build.gradle
files as
implementation deps.multidex
. But it's not...
l
@ghedeon Which Android Studio version are you using? FYI, 3.3 and 3.4 broke buildSrc referencing from groovy based
build.gradle
scripts, but it is fixed in 3.5 now in RC. (That bug kicked-off my migration to Gradle Kotlin DSL)
g
AS 3.4.2 😬
l
Then, install 3.5 RC3 aside, and open the project with it (no need to update AGP)
g
just did it. Still doesn't work but at least I've got imports recognized by IDE. So, clicking on
deps.foo
actually brings you to your kotlin file in buildSrc. I'm wondering what people that answered "YES" do...
l
They replied regarding theory, but at least two of them are using Gradle Kotlin DSL and dropped dynamic syntax.
m
@ghedeon an example of using a kotlin buildSrc with build.gradle files: https://github.com/HearthSim/Arcane-Tracker/tree/bdf01d728d5eddefba68f76e5da155620f480d00/buildSrc
g
I expect this to be visible in any of my
build.gradle
files as
implementation deps.multidex
. But it's not...
Why do you expect that it will work? Groovy is not Kotlin, you have to use Java syntax, something like:
deps.INSTANCE.getMultidex()
, or use JvmStatic instead
m
Or make
multidex
const ?
I have this,
Copy code
object Libs {
    const val supportLibVersion = "28.0.0"
}
Kotlin bytecode shows:
Copy code
public final class Libs {


  // access flags 0x19
  public final static Ljava/lang/String; supportLibVersion = "28.0.0"
  @Lorg/jetbrains/annotations/NotNull;() // invisible
g
yeah, you right! It will work with object
👍 1
g
@gildor Irrelevant. Groovy recognizes that, even without const
g
recognize Kotlin Object?
g
yes
g
what doesn’t work than in your case?
g
well, you won't belive me... but apparently lower case name of the class
motherfucking flying bag of dicks. 2 days.
object deps
-->
object Deps
g
maybe just some name conflict?
What kind error did you have?
g
same for Libs and libs
g
what abou SomethingRandom and somethingrandom? %)
g
that
deps
does't exists
g
Groovy 🤷‍♂️
g
same for somethingrandom. They are for real
if it's not ridiculous enough, in my side project with kotlin dsl lower case is not a problem. So, it's just this weird combination of groovy and kotlin
was nice knowing you guys, for all these years. I think I give up on android. 👋
g
Not really related on Android tho 😬
☝️ 2
g
disagree. It's an essential part of the android development. They brought gradle to android in 2013-2014? 5 years later terrible support in IDE, constant breaking changes, incompatibilities, performance issues, etc. It's an everyday struggle.
l
If you use gradle the default simple way, with no third-party plugins, it just works.
1
You're putting code into the build script itself. Understandable Google folks didn't expect this at first. Kotlin wasn't still in early, early stages, mostly unknown.
g
It’s an essential part of the android development
Yeah, same as huge amount of other technologies, and at least Gradle can be avoided if you really know what are you doing