Hey im having issues with the `subprojects` sectio...
# gradle
v
Hey im having issues with the
subprojects
section in the root gradle file. I cant seem to be able to use the
android
block inside the
subprojects
block when I use kts gradle files
g
Yes, it will not work, because you have type-safe accessors only in module which applied plugin using plugins DSL Check official guide, it covered very well there https://docs.gradle.org/current/userguide/kotlin_dsl.html#type-safe-accessors
v
yes, Ive actually tried applying the android library plugin
still wont recognize the android {} block
Copy code
subprojects {
         apply(plugin = "com.android.library")

        android {}
}
this does not work
g
You probably didn't get documentation which I sent above Plugin application is not enough to get type-safe accessors, you must use
plugins{}
block only
Please check, it covers such cases.
Essentially you cannot use this approach directly in KTS, there are a few other options, such as use dynamic syntax (using
configure
block) in this case, or apply this shared config as function on every module
v
How do you use the configure block? I read the documentation btw I just didnt see anything that helped with what I needed
So far its looking like kotlin dsl for gradle is not at all ideal for multi-module android projects as you cant re-use anything within the
android {}
block leading to a lot of boilerplate.
for me it seems this kind of outweighs the type safety of kotlin
g
This is not something Android specific, Kotlin DSL requires different, type safe, approaches to share configuration, it also covered in official doc and there is special migration guide that covers different migration strategies: https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/#migration_strategies
In general, there are 2 different ways: use dynamic syntax with configure<SomeExtension>() or use plugins and buildSrc to share common configs
But I don't want to say that you are wrong, not at all, if you not very familiar with Gradle or just don't see advantages to migrate to new type safe approach, for sure it easier to use Groovy Migration to Kotlin DSL requires changing a way how most of projects are configured in Groovy (in general it's the good thing and recommended practices even for Groovy), but it has own learning curve
v
ok thank you for these resources! that actually helps a lot. I like the kts scripts but looks like I need to refactor some of it
@gildor I tried using
configure<com.android.build.gradle.LibraryExtension> {
in its own file to try to separate some of the duplicated module build scripts, however im running into the same issue. It says
Unresolved Reference: Android
g
Unresolved reference
Android
, like that capitalized? Are you sure that it complains on this configure block?
v
sorry its not capitalized
and yes it points to the configure line
however I found something interesting
it does work correctly if I place that configure block inside the
subprojects
block in the root build file
I just cant seem to get it working when it is in its own file for some reason... I wanted to have a
LibraryConfig.gradle.kts
that I could then use in each module build file using
apply(from = "LibraryConfig.gradle.kts")