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
d

Daniele B

02/28/2023, 6:29 PM
In previous official samples, there was always the gradle plugin specified as classpath, in the project’s gradle file, as a dependency of the buildscript
buildscript {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
    }
    dependencies {
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin.version}")
    }
}
But in the latest multiplatform samples, the buildscript section is not included anymore. Does buildscript belong to an old way of configuring gradle, or is it still relevant today? Previously, the documentation of popular libraries such as Kotlin Serialization or SqlDelight required to have a classpath specified as a dependency of buildscript, but now not anymore.
v

Vampire

02/28/2023, 7:11 PM
Adding something to the buildscript classpath if you need its classes in the build script is fine. But adding a plugin artifact to the buildscript classpath and applying it using
apply...
is legacy and not recommended since years. Instead the
plugins { ... }
block is the proper way to apply a plugin.
d

Daniele B

02/28/2023, 7:27 PM
thanks for the clarification! at the moment, this is what I am doing: I have this in settings.gradle.kts:
pluginManagement {
    repositories {
        google()
        gradlePluginPortal()
        mavenCentral()
        maven("<https://maven.pkg.jetbrains.space/public/p/compose/dev>")
    }

    plugins {
        kotlin("multiplatform").version(extra["kotlin.version"] as String)
        id("org.jetbrains.compose").version(extra["compose.version"] as String)
        ...
    }
}
this in the *project*’s build.gradle.kts:
plugins {
    kotlin("multiplatform") apply false
    id("org.jetbrains.compose") apply false
    ...
}
and then recalling the plugins in all target-specific build.gradle.kts (e.g. shared, jsApp, wasmApp, desktopApp):
plugins {
    kotlin("multiplatform")
    id("org.jetbrains.compose")
    ...
}
There seems to be quite a lot of duplication. Should all plugins be defined in the project’s build.gradle.kts too? and what’s the meaning of
apply false
?
m

mbonnin

02/28/2023, 10:20 PM
Welcome to classloader hell 🤘 😃
apply false
is used to put the plugin jar in the root project classpath (so that I think they are not loaded several times by different projects, but don't quote me on this...) My rule of thumb is: put all your plugin jars in your
buildSrc
/`build-logic` implementation configuration then use ids in individual projects. I'm not sure about
pluginManagement.plugins
. Looks like they're used to define versions but you could as well define them in your root project
plugins {}
block and I think that'd work too
If you don't have
buildSrc
/`build-logic` , I guess
root project, apply false
+
ids
in subprojects would work too
But
buildSrc
/`build-logic` allow you to factor your build logic with convention plugins and once you have a lot of modules, this becomes handy
d

Daniele B

02/28/2023, 10:49 PM
I just tried to define the plugins in the root project build.gradle.kts (with version and
apply false
), but it gives me some gradle errors, so I think they need to be defined first in the
pluginManagement.plugins
. When I have more time, I might try to investigate the
buildSrc
/`build-logic` way you suggest. Many thanks!
m

mbonnin

02/28/2023, 10:51 PM
it gives me some gradle errors
Interesting. Can you share them? I'm curious what
pluginManagement.plugins
does now 🙂
The javadoc isn't super chatty
d

Daniele B

02/28/2023, 10:58 PM
The error is related to the
com.android.application
plugin, not sure why:
Build file '/Users/daniele/Dev/KMP/PSF_CMS/build.gradle.kts' line: 13

Plugin [id: 'com.android.application', version: '7.3.0', apply: false] was not found in any of the following sources:

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'com.android.application', version: '7.3.0', apply: false] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.android.application:com.android.application.gradle.plugin:7.3.0')
  Searched in the following repositories:
    Gradle Central Plugin Repository
    at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.resolveToFoundResult(DefaultPluginRequestApplicator.java:238)
    at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.lambda$resolvePluginRequests$3(DefaultPluginRequestApplicator.java:168)
    at org.gradle.util.internal.CollectionUtils.collect(CollectionUtils.java:212)
    at org.gradle.util.internal.CollectionUtils.collect(CollectionUtils.java:206)
    at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.resolvePluginRequests(DefaultPluginRequestApplicator.java:166)
    at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.applyPlugins(DefaultPluginRequestApplicator.java:101)
    at org.gradle.kotlin.dsl.provider.PluginRequestsHandler.handle(PluginRequestsHandler.kt:48)
    at org.gradle.kotlin.dsl.provider.StandardKotlinScriptEvaluator$InterpreterHost.applyPluginsTo(KotlinScriptEvaluator.kt:195)
Screenshot 2023-02-28 alle 23.59.48.png
m

mbonnin

02/28/2023, 11:00 PM
Searched in the following repositories:
    Gradle Central Plugin Repository
Weird, looks like it's not searching in the Google maven repo...
But you have it in your
pluginManagement.repositories {}
d

Daniele B

02/28/2023, 11:02 PM
Ah, wait, I had removed the
pluginManagement
section. I will now, put it back, without the
plugin
subsection
m

mbonnin

02/28/2023, 11:03 PM
Ah yes, that'd explain it. By default only
gradlePluginPortal()
is there
d

Daniele B

02/28/2023, 11:05 PM
great! it actually works! 😄
thanks!
@mbonnin the annoying thing is now that it doesn’t read the gradle.properties
m

mbonnin

02/28/2023, 11:24 PM
Aww yea,
plugins {}
is "special"
It's a very constrained Kotlin syntax, you can't use much
The new way to do this would be to put your versions in a version catalog
Now in android has what is considered a "modern" setup: https://github.com/android/nowinandroid/blob/main/build.gradle.kts#L29
d

Daniele B

02/28/2023, 11:58 PM
I just had a look, but it looks so messy, with many files involved
m

mbonnin

02/28/2023, 11:59 PM
😅
"Don't fix it if it ain't broke" I guess
The version catalog is good though, it put everything in a central place that tools can process and it's nice to be able to quickly locate dependencies when coming in in a new project
(it's there in the now in android project)
At the end of the day, do whatever works for you!
d

Daniele B

03/01/2023, 12:08 AM
the version catalog is a good concept, but the implementation of it is too messy i am sure it will get better in the next gradle versions
v

Vampire

03/01/2023, 12:08 AM
The plugins block in plugin management defines default versions for plugins that are used if no version is used when using a plugin in a plugins block. It's syntax is more relaxed than the syntax of the other plugins blocks, so that you can calculate versions for example. But yeah, use a version catalog. :-)
Why do you say it is messy? What do you mean?
It's already stable, so there will probably not be too deep changes in the upcoming versions
d

Daniele B

03/01/2023, 12:13 AM
I like the concept of having a unique file where to list all versions and plugins, but I don’t see why there have to be some many other files involved. Maybe there could be a simpler example
v

Vampire

03/01/2023, 12:14 AM
Which other files? confused
You put the versions, libs, and plugins in there and then you can use them already, no other files involved
d

Daniele B

03/01/2023, 12:15 AM
mmm, ok I will try
m

mbonnin

03/01/2023, 12:17 AM
I guess it's the convention plugins that add some files/build logic.
v

Vampire

03/01/2023, 12:17 AM
In that example there is one other file actually,
build-logic/settings.gradle.kts
as it references the same version catalog for as the parent build. Separate build by default have separate files unless you configure it differently.
d

Daniele B

03/01/2023, 12:23 AM
To start with, “libs” doesn’t get recognized
v

Vampire

03/01/2023, 12:24 AM
Is that your root builds settings script?
Then don't try to define the default file there, it will fail and is unnecessary
d

Daniele B

03/01/2023, 12:25 AM
yes, I placed it in “settings.gradle.kts”
v

Vampire

03/01/2023, 12:26 AM
If it is not, but some build logic project, don't try to use Groovy Syntax in a Kotlin file
Yeah, then as I said, don't redeclare the conventional file, that will fail and is totally unnecessary
d

Daniele B

03/01/2023, 12:27 AM
where should I place it?
v

Vampire

03/01/2023, 12:27 AM
Again, you need to have nothing in the settings script if you name the file like the convention. Just put the versions libs and plugins in there and you are ready to use them
d

Daniele B

03/01/2023, 12:30 AM
sorry, I can’t really follow what you mean. that gradle code is supposed to define where the toml file is, and I thought I had to place it in settings.gradle.kts, where else?
v

Vampire

03/01/2023, 12:31 AM
NOWHERE if you put the file in the default place, what's so hard to understand with that? I'm really lost in how else I should express it.
Just create the file, put the versions, libs and plugins in there and you are ready to use them as I said multiple times
You only need that code if you want to put the file into a non-standard place / name, or if you want to have multiple files with different name-spaces
d

Daniele B

03/01/2023, 12:40 AM
what you say makes a lot of sense and it’s very desirable, but I still can’t get it… I created a file
libs.versions.toml
in the root folder, with this content:
[versions]
kotlin = "1.8.0"
agp = "7.4.0-beta02"
compose = "1.4.0-alpha01-dev940"
ktor = "2.2.2"

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
but I can’t see how I can access this data
what should I type?
v

Vampire

03/01/2023, 12:41 AM
Not in the root folder, in the gradle folder
d

Daniele B

03/01/2023, 12:41 AM
ah ok
ok, it’s in the gradle folder now but if I type
libs.
in any gradle file, nothing comes up
v

Vampire

03/01/2023, 12:49 AM
Did you sync?
d

Daniele B

03/01/2023, 12:59 AM
I did, but with
libs.
nothing comes up, and now it complains about the existing plugins:
Build file '/Users/daniele/Dev/KMP/PSF_CMS/androidApp/build.gradle.kts' line: 1

Plugin [id: 'org.jetbrains.compose'] was not found in any of the following sources:

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'org.jetbrains.compose'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
	at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.resolveToFoundResult(DefaultPluginRequestApplicator.java:238)
	at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.lambda$resolvePluginRequests$3(DefaultPluginRequestApplicator.java:168)
	at org.gradle.util.internal.CollectionUtils.collect(CollectionUtils.java:212)
	at org.gradle.util.internal.CollectionUtils.collect(CollectionUtils.java:206)
	at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.resolvePluginRequests(DefaultPluginRequestApplicator.java:166)
	at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.applyPlugins(DefaultPluginRequestApplicator.java:101)
	at org.gradle.kotlin.dsl.provider.PluginRequestsHandler.handle(PluginRequestsHandler.kt:48)
	at org.gradle.kotlin.dsl.provider.StandardKotlinScriptEvaluator$InterpreterHost.applyPluginsTo(KotlinScriptEvaluator.kt:195)
here:
plugins {
    id("org.jetbrains.compose")
    id("com.android.application")
    kotlin("android")
}
v

Vampire

03/01/2023, 1:01 AM
You did not, at least not successfully if it also does not build currently with that error. ;-)
You still need the repositories in the plugin management block if you need something else than the gradle plugin portal
Or rather, you probably removed the plugins block that defined the versions
And now it cannot find the plugin without version of course as you do not use the version catalog yet but still have the plugins via I'd in the build script
As it is new to you, don't change to much at once. Take your working build as base, add the file, sync, then use the version catalog and remove the non version catalog usages
d

Daniele B

03/01/2023, 1:08 AM
yes, trying now to re-establish how it was
wow! finally!!!
now I get all these values with
libs.
!!!
it actually wasn’t hard 😄 but if you read the documentation and see the androidnow sample, it looks confusing
many thanks for your support 🤗🤗🤗
v

Vampire

03/01/2023, 1:16 AM
The Android now example also had nothing in settings script, only in the one of the included
build-logic
build as I mentioned previously. Actually, I don't think the docs are confusing there, but if your think so, you should open an improvement issue about it.
d

Daniele B

03/01/2023, 1:28 AM
I will play around to find the ideal/simplest configuration and then I will see if I come up with some documentation improvements, many thanks!