I have a `precompiled script` for child `Gradle` s...
# gradle
n
I have a
precompiled script
for child
Gradle
scripts. How can I add another plugin in such file?
Copy code
apply<LibraryCommonPlugin>()

configure<BaseExtension> {

    defaultConfig {
        ...
    }
}
What I’ve tried; • to add
plugins { kotlin("android") }
on top of file but didn’t work • to configure like
configure<PluginDependenciesSpecScope> { kotlin("android") }
on top of file but didn’t work What do I need to do to get it to working?
v
Can you provide an MCVE of what you have?
n
will try, but do you have any approach in your mind anyway?
v
approach? Just write a minmal build showing what you have. At least I am not sure what you have where and so could not give any advice for your problem
n
okay will create a sample repo then thanks
@Vampire sorry had to mention you
v
No you did not have to mention me, but sometimes even Vampires need to sleep or do their dayjob 😉
n
Yeah I thought now it could be a good time to poke you, sorry :)
v
And what error do you get? First version should be fine
I cannot really try as I'm not going to install an Android SDK
But at least
gradlew help
works fine with it commented in. Well, there is one point. To generate type-safe accessors for precompiled Kotlin build scripts, Gradle takes the
plugins
block, applies it to a dummy project, evaluates it and then checks which things were added for which it then generates the accessors.
kotlin("android")
requires that the
android
extension is present. You require this by the requirement to apply your precompiled script plugin after the
com.android.application
plugin. So the accessor generation fails how you do it, but the build still works, you just cannot use type-safe accessors in your precompiled script plugin and get an exception logged. If you do
Copy code
plugins {
    id("com.android.application")
    kotlin("android")
}
in your precompiled script plugin the
android
extension is present even during type-safe accessor generation, so the logged exception goes away and you can use type-safe accessors. You should then also be able to do
android {
instead of
configure<BaseExtension> {
as then the accessors for
com.android.application
are also generated.
👍 2
💯 1
n
Thanks for your answer Björn! Actually that script is common one for both
library
and
application
scripts that’s why I can’t apply
com.android.application
or
com.android.library
directly to it. But manually ,I can apply
kotlin("android")
in each child script instead like so; android-application.gradle.kts
plugins {
id("com.android.application")
kotlin("android")
id("android-defaults)
}
android-library.gradle.kts
plugins {
id("com.android.library")
kotlin("android")
id("android-defaults)
}
I just wanted to avoid repeating step here but seems it is not possible.
v
Well, as I said you have two possibilities, or maybe even three. 1. Apply it with the
plugins
block, but don't use type-safe accessors, as the generation of them will fail. Additionally you have to ignore the exception that will be logged during the accessor generation, it does not affect the build if you don't use the accessors. 2. Don't apply the plugin using the
plugins
block, but the legacy
apply
method. You still can't use accessors from that plugin, as those are only generated for plugins applied in the
plugins
block, but you also don't get the exception logged anymore. 3. Write another precompiled script plugin where you check with an
if
either whether one of the Android plugins is already applied, or whether you are within the dummy project for accessor generation, and if so, apply one of the Android plugins, just to be able to apply the Kotlin Android plugin, then apply that plugin in your existing plugin too.
Another thing you could do is to post a feature request to the android developers, so they lift that restriction and for example just do nothing if none of the android plugins is applied yet, or only do things that don't need the
android
extensions, so that for the rest accessors can be generated.
And another option of course would be to write a good old binary plugin instead of a precompiled script plugin. As you don't have typesafe accessors anyway that wouldn't make too much difference actually.