Maybe too basic but not able to get it to work. :s...
# gradle
a
Maybe too basic but not able to get it to work. 😅 I wanna reuse functions from one kts file to another. Eg. Create a
helper.gradle.kts
file with a fun that can be used in other
x.gradle.kts
files. What is the best way?
a
try taking a look at this Q&A https://stackoverflow.com/q/70670733/4161471
a
Is there not an easier way to do this eg. defining a function in one kts file and then using
import
or
apply
as with Groovy scripts?
a
probably not…
it’s just 3 steps 1. create
./buildSrc/build.gradle.kts
with
Copy code
plugins {
  `kotlin-dsl`
}
2. create
./buildSrc/settings.gradle.kts
with
Copy code
rootProject.name = "buildSrc"

pluginManagement {
  repositories {
    mavenCentral()
    gradlePluginPortal()
  }
}

@Suppress("UnstableApiUsage")
dependencyResolutionManagement {
    repositories {
      mavenCentral()
      gradlePluginPortal()
    }
}
3. create your file
Copy code
// ./buildSrc/src/main/kotlin/myExtensions.kt
import org.gradle.api.*

fun Project.myHelper() {
  println("I'm helping project ${project.displayName}!")
}
And then because there’s no
package
declaration in
myExtensions.kt
, you can use
myHelper()
in any subproject. If there was a
package my.buildsrc.helpers
, then you’d have to add
import my.buildsrc.helpers.myHelper
to any
build.gradle.kts
a
Idk, not working for me. 😞 Will have to try another time. Thanks for trying to help. 🙏