Can anybody help me with this question on SO? Appr...
# gradle
n
Can anybody help me with this question on SO? Appreciate your help, thanks. https://stackoverflow.com/questions/67582914/how-to-convert-a-common-gradle-script-to-gradle-kotlin-dsl
s
I saw custom plugin usage with .kts here, maybe will help https://github.com/mars885/gamedge
n
Thanks @solidogen! Seems this is a bit different and simplified version of what I am trying to do. However, will look more into that!
m
You can use something like
configure<BaseExtension>{}
instead of
android {}
. This is the non-generated-accessor equivalent
You'll need the Android Plugin in the buildscript classpath for that to work
n
Hi Martin, thank you. I tried that but didn’t work, either. Please see the screenshots
m
Do you know where the android plugin gets applied? You might want to try wrapping everything in
afterEvaluate {}
. That might or might not work
n
it is applied in `builSrc`’s
build.gradle.kts
m
That's adding the android plugin to the buildscript classpath but not applying it per se, i.e. not calling
Plugin.apply()
n
I see, how can I apply the
Gradle
plugin using
apply
? Sorry this’ been beyond my knowledge a bit 😕
m
The usual way is to add to the plugins block:
Copy code
plugins {
  id("com.android.library")
}
But looks like your setup is a bit different so it's hard to tell how to reproduce the exact same behaviour you had before
n
ahh yes but my concern is to avoid applying this as I want
android-defaults.gradle.kts
to be common for both
application
and
library
scripts. However, I can create another scripts which apply
android-defaults
as well as
com.android.library
or
com.android.application
like so; android-application.gradle.kts
plugin {
id("com.android.library")
id("android-defaults)
}
As far as I see, this doesn’t seem possible, right?
m
It is possible but you need to make sure to apply plugins in the right order
Or even better, "react" to plugins being applied. You can try something like:
Copy code
fun configureAndroidExtension() {
    configure<BaseExtension> {
    }
  }
  plugins.withId("com.android.library") {
      configureAndroidExtension()
  }
  plugins.withId("com.android.application") {
    configureAndroidExtension()
  }
n
I see. I don’t want to take your time further here so I’ll push a sample repo to Github, would you take a look whenever you are available?
m
Can't really commit to that, tonight is Google IO so it's a big night :)
You can also look into https://github.com/bernaferrari/GradleKotlinConverter. I found it useful for Groovy -> Kotlin conversions
👍 1
n
no worries take your time please, I am also gonna join I/O 🙂
Hi Martin, I fixed it. You were right, it was the order issue, now it is working. Thanks for your help! Only one more question 🙂 How can I apply another plugin in below script?
Copy code
apply<LibraryCommonPlugin>()

configure<BaseExtension> {

    defaultConfig {
        ...
    }
}
I tried adding
plugins
block inside
BaseExtension
and configuring
PluginDependencySpecs
but those didn't work