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
a

Alexandre Brown

03/28/2023, 7:27 PM
Hello, we have a Kotlin project with a lot of modules (see image). I am looking for recommendation on how to structure it following the latest gradle recommendation (did not look into gradle for a while). We are currently using Gradle Kotlin 7.1 and the following dsl to apply common dependencies to submodules: backend:build.gradle.kts
subprojects {
    apply(plugin = "org.jetbrains.kotlin.jvm")
	apply(plugin = "com.google.devtools.ksp")

	repositories {
		maven(url = "<https://repo.osgeo.org/repository/release/>")
		mavenCentral()
	}

    ...

    dependencies {
		implementation(project(":core:core-logging"))
		implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinxCoroutinesVersion")
        ...
}
I have heard that using this approach is not recommended and that one should use
buildSrc
instead. I also use
gradle.properties
to write the dependencies version : eg
kotlinxCoroutinesVersion=1.6.4
but I am not sure if this is the recommended approach. For Plugins I have the following in the root module (eg:
backend:build.gradle.kts
):
plugins {
	kotlin("jvm") version "1.7.21" apply false
	kotlin("plugin.serialization") version "1.7.21" apply false
	id("com.google.devtools.ksp") version "1.7.21-1.0.8" apply false
}
Then in the module that needs it I do the following : eg:
backend:backend-frameworks:build.gradle.kts
subprojects {

	apply(plugin = "org.jetbrains.kotlin.plugin.serialization")

	dependencies {
        ...
I would love some feedback and some pointers on how I should structure the dependencies using gradle recommended approach. Some bullet points could help : eg: To share dependencies use ABC, to version dependencies use XYZ. I'm pretty sure the way I set it up is not the recommended approach nowadays and I'm willing to change it.
v

Vampire

03/28/2023, 8:24 PM
I am looking for recommendation on how to structure it following the latest gradle recommendation (did not look into gradle for a while).
https://github.com/jjohannes/idiomatic-gradle
I have heard that using this approach is not recommended
Exact, practically any usage of
allprojects { ... }
or
subprojects { ... }
is bad and instead convention plugins should be used. Whether you write them in
buildSrc
or an included build or a standalone build that you publish doesn't matter and is totally up to you. I personally prefer an included build, or multiple.
I also use
gradle.properties
to write the dependencies version : eg
kotlinxCoroutinesVersion=1.6.4
but I am not sure if this is the recommended approach.
Nope, the recommended approach is using a version catalog: https://docs.gradle.org/current/userguide/platforms.html
Then in the module that needs it I do the following :
Well, again - and here even more as it is in an intermediate project -
subprojects { ... }
is bad as it introduces project coupling and so prevents more sophisticated features like configure-on-demand or parallel configuration, besides that it makes builds harder to understand and harder to maintain. And also requires to use the legacy way of applying plugins
apply(...)
instead of using the recommended
plugins {... }
block.
a

Alexandre Brown

03/28/2023, 8:26 PM
Thank you so much @Vampire, I will look at all the links you provided, that's exactly the kind of infromation I was looking for!
v

Vampire

03/28/2023, 8:31 PM
Also make sure to only use task-configuration avoidance compatible API. So for example no
task("...")
, no
tasks.withType<...> { ... }
, no
tasks.getByName(...)
, only very conservative
tasks....matching { ... }
if at all, no
tasks.create
, ... https://docs.gradle.org/current/userguide/task_configuration_avoidance.html
a

Alexandre Brown

03/28/2023, 8:32 PM
In some cases I have to use it (eg: third party flag to enable):
extensions.getByType<KspExtension>().apply {
		arg("mockative.stubsUnitByDefault", "true")
	}
But I will try to minimize such cases, thanks!
v

Vampire

03/28/2023, 10:14 PM
What is that against?
Besides that I would write it simply as
configure<KspExtension> {
    arg("mockative.stubsUnitByDefault", "true")
}
(yes, without
extensions.
)
If you confused that with
tasks.withType
,
withType
!=
getByType
. And also while you can follow the same rules for all containers (always using
bla.register
, not
bla.create
and so on, it is currently mostly relevant for task collections. As far as I know no other collection is really used lazily yet, so while more future proof, it is not tragic if you don't follow the rules on them. And besides that,
tasks.withType<...>().configureEach { ... }
is fine, that is lazy. But
tasks.withType<...> { ... }
is bad as it would always realize and configure all tasks of that type whether needed or not. (don't ask why, this is because the bad API was there before the task configuration avoidance approach, so it cannot change its meaning easily)