I have seen in many multiplatform projects, global...
# multiplatform
d
I have seen in many multiplatform projects, global variables are defined in a Kotlin file inside a manually created
buildSrc
folder. Is there any reason to use that instead of just define them in the global gradle file
buildscript
section?
j
I think this is more of a gradle question, but if you use buildSrc you can actually write normal code, create functions etc. which you can call from your build.gradle. Also I can never find the ext.whatever declarations from my projects build.gradle by simply `ctrl`+`clicking` on it, something you can do with code from buildSrc. If you have a multi module project, you can simply write:
Copy code
dependencies {
    implementBaseDependencies()
}
and then
implementBaseDependencies()
would be a function in your buildSrc that implements all your base dependencies. (I mean this is just a stupid example but you get the gist). A copy paste from my project:
Copy code
object Serialization {

        private const val Version = "1.1.0"

        private fun serialization(dependency: String) = group(GroupKotlinX, dependency, Version)

        private const val Core = "kotlinx-serialization-core"

        private const val Json = "kotlinx-serialization-json"

        fun DependencyHandlerScope.implementSerialization(flavor: String = "") {
            implementation(serialization(Core), flavor)
            implementation(serialization(Json), flavor)
        }
    }
In this case I can simply call
implementSerialization()
from my
build.gradle
and I have it, it wouldn't look as nice using
buildScript
j
You should avoid buildSrc to declare dependencies because it breaks multiple cache systems like configuration cache. Take a look to Gradle 7.0 Version Catalog feature.
e
code in buildSrc! = code in buildscript
j
@Javier can you point me to that version catalog feature?
sounds interesting but googling Gradle version catalog points me to a lot of articles about gradle versions 😛