In previous official samples, there was always the...
# gradle
d
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
Copy code
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
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
thanks for the clarification! at the moment, this is what I am doing: I have this in settings.gradle.kts:
Copy code
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:
Copy code
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):
Copy code
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
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
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
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
The error is related to the
com.android.application
plugin, not sure why:
Copy code
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
Copy code
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
Ah, wait, I had removed the
pluginManagement
section. I will now, put it back, without the
plugin
subsection
m
Ah yes, that'd explain it. By default only
gradlePluginPortal()
is there
d
great! it actually works! 😄
thanks!
@mbonnin the annoying thing is now that it doesn’t read the gradle.properties
m
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
I just had a look, but it looks so messy, with many files involved
m
😅
"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
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
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
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
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
mmm, ok I will try
m
I guess it's the convention plugins that add some files/build logic.
v
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
To start with, “libs” doesn’t get recognized
v
Is that your root builds settings script?
Then don't try to define the default file there, it will fail and is unnecessary
d
yes, I placed it in “settings.gradle.kts”
v
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
where should I place it?
v
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
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
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
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:
Copy code
[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
Not in the root folder, in the gradle folder
d
ah ok
ok, it’s in the gradle folder now but if I type
libs.
in any gradle file, nothing comes up
v
Did you sync?
d
I did, but with
libs.
nothing comes up, and now it complains about the existing plugins:
Copy code
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:
Copy code
plugins {
    id("org.jetbrains.compose")
    id("com.android.application")
    kotlin("android")
}
v
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
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
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
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!
147 Views