https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
l

louiscad

02/16/2019, 12:53 AM
Hi, I'd like to reuse some code fro my
build.gradle.kts
files in a multi-modules project using
buildSrc
, but this code relies on some gradle plugins like Kotlin multiplatform and maven publish. How can I access those in the Kotlin sources of the
buildSrc
? Thanks for your help!
s

snowe

02/16/2019, 12:54 AM
you simply depend on them as dependencies in your
buildSrc/build.gradle.kts
and then you can access them in your code.
l

louiscad

02/16/2019, 12:55 AM
Using the
dependencies
block?
that file kinda shows how you can do it.
l

louiscad

02/16/2019, 1:01 AM
This file is groovy and has nothing to do with buildSrc as far as I can see… so "kinda" is accurate 😅
s

snowe

02/16/2019, 1:15 AM
buildSrc is just a plugin. You can implement it in groovy, kotlin, java, Scala, it doesn't matter.
If you were to copy that entire project into a buildSrc folder it would work.
l

louiscad

02/16/2019, 1:27 AM
Alright, adding the following into my `buildSrc`'s
build.gradle.kts
file helped:
repositories {
    google()
    jcenter()
    gradlePluginPortal()
}
dependencies {
    implementation("com.android.tools.build:gradle:3.3.1")
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21")
}
Also, I realized I do not need to use the plugin ids I'd use in the plugins DSL, but only what I usually put in the `builscript`'s dependencies using the
classpath
configuration (but using the
implementation
configuration here). Thanks for the hints, it was helpful!
g

gildor

02/16/2019, 3:55 AM
Yes, for now you have to use maven dependency id, but there is a feature request to allow use plugin id for dependencies of other plugins
buildSrc is just a plugin
To be morr correct "buildSrc" is not a plugin itself. It's just a JVM module that will be added to your project's gradle classpath. But because everything in buildSrc becomes part of build config classpath you also can add any dependencies to it, including third party or own plugins and they will be available to application to your own modules
s

snowe

02/16/2019, 4:46 AM
Hm. that distinction seems weird. Can you not just turn a
buildSrc
folder into a plugin directly? You can even reference it with a plugin id.
g

gildor

02/16/2019, 10:04 AM
No, code inside of buldSrc is not a Gradle plugin
Just a code that compiled and available for your build config
You cannot reference code in buildSrc by plugin id without explicit usage of binary plugin or precompilee plugin
l

louiscad

02/16/2019, 10:39 AM
Not sure how that applies to maven-publish though, the technique I mentioned above doesn't work because I don't know the dependency I should add nor the version
g

gildor

02/16/2019, 5:29 PM
Not sure what you mean and how this related to maven publish
You just can consume any Gradle plugin as regular dependency in your buildSrc
For now to do that you can use plugin published to any generic maven repo (including kotlin), or you can use workaround mentioned in this issue that allows consume any Gradle plugin published on Gradle plugins portal
l

louiscad

02/16/2019, 5:45 PM
I wanted to use the
publishing { … }
extension for
Project
with no luck, but it seems to only affect accessors, I can access the types from the
maven-publish
plugin.
g

gildor

02/16/2019, 5:48 PM
But why do you need publishing extension in buildSrc?
Just to provide common config?
maven-publish is core plugin, so you don't need any additional dependencies
Also you cannot use static accessors in this case, because static accessors work only when you apply plugin, but for some common configuration you have to listen when project applied some plugin and configure it, but you cannot use static accessors for it
But if apply
kotlin-dsl
plugin to your buildSrc, you will have access to some non-generated helper methods of kotlin-dsl
s

snowe

02/16/2019, 6:07 PM
@gildor you can use a plugin id, as referenced in the documentation. https://guides.gradle.org/writing-gradle-plugins/#declare_a_plugin_identifier
I would know because I wrote a buildSrc plugin, and converted it straight into a regular gradle plugin by just copying it into its own directory...
l

louiscad

02/16/2019, 9:15 PM
@gildor Yes it's to provide common config for a 40+ modules library. So it's because these generated static accessors are not buildSrc compatible. A bit weird to have this inconsistency, don't you think?
g

gildor

02/17/2019, 12:47 AM
@snowe But you wrote plugin! But not every code in buildSrc should be Gradle plugin, it may be any code, and buildSrc is not plugin itself. And as I mentioned above you can write plugin in buildSrc but only explicitly, buildSrc is just a JVM module
@louiscad it's exactly the same situation if you write a Gradle plugin, you don't have static accessors. Maybe it may be somehow improved in future, but even now you must apply plugin using plugins DSL if you need access to static accessors and this is special mechanism, not just adding plugin to classpath
s

snowe

02/17/2019, 1:10 AM
@gildor ah, I misunderstood. I don't think I've ever seen buildSrc without being a plugin. What other kind of code would you put in it?
g

gildor

02/17/2019, 1:28 AM
Most common use case is just some helper class or function (including a config for some plugin that may be applied manually, without plugin) , even custom task, also now many projects move versions and dependencies to object in buildStr Neither from mentioned use cases require plugin creation or plugin application, you just have access to all classes in all your modules
s

snowe

02/17/2019, 1:29 AM
why wouldn't you just use a gradle script then. you'd avoid all the trouble of buildSrc, and still have things separated.
g

gildor

02/17/2019, 1:31 AM
Because with Kotlin DSL usage would be really hard, you don't have auto complete, everything is dynamic Also you have to include script plugin, but build src code already available
Even in official Kotlin DSL guide usage of buildSrc is preferable over script plugins
s

snowe

02/17/2019, 1:33 AM
Hmm. I thought you had to include buildSrc as well, I'm guessing that's just for plugins then.
g

gildor

02/17/2019, 1:33 AM
As you can see there is no plugins
s

snowe

02/17/2019, 1:33 AM
and yeah I still write my build scripts in groovy.
g

gildor

02/17/2019, 1:34 AM
You can write build scripts in groovy, but nothing prevents you from using Kotlin in buildSrc
had to include buildSrc as well
buildScr included by default, but if you have plugin in buildSrc you have to apply it same as any other plugin
s

snowe

02/17/2019, 1:38 AM
yes, that's what I mean.
g

gildor

02/17/2019, 1:39 AM
What is your usual use case for plugin in buildSrc? To apply some configs?
s

snowe

02/17/2019, 7:35 PM
testing a plugin before extracting it out. I find almost no use for buildSrc otherwise.
g

gildor

02/17/2019, 11:29 PM
Maybe with groovy, when you don't have static checks and code completion this is like that