https://kotlinlang.org logo
Title
m

Matej Drobnič

10/15/2019, 7:41 AM
Is there a way to suppress
Kotlin plugin should be enabled before 'kotlin-kapt'
error message? I have gradle script like this:
plugins {
    kotlin("kapt") version Versions.kotlin
}

subprojects {
    kapt {
        useBuildCache = true
    }
}
Basically, I want to automatically enable build cache on all projects. But since some projects are android and some are regular kotlin, I cannot apply any plugin in this root gradle script, I only need the kapt.
g

gildor

10/15/2019, 7:45 AM
I would apply kapt plugin explicitly on every module where you need it
m

Matej Drobnič

10/15/2019, 7:45 AM
I do that already
but then I would have to copy paste this useBuildCache block to every submodule gradle file
g

gildor

10/15/2019, 7:46 AM
Ah, I got what you want
m

Matej Drobnič

10/15/2019, 7:46 AM
which would clutter the scripts
g

gildor

10/15/2019, 7:46 AM
you need another approach
project.plugins.withId(“kotlin-kapt”) { kapt { useBuildCache = true } }
m

Matej Drobnič

10/15/2019, 7:47 AM
but script still wouldn't compile, right?
g

gildor

10/15/2019, 7:47 AM
or even better use it by type
m

Matej Drobnič

10/15/2019, 7:47 AM
since plugin would not be declared
g

gildor

10/15/2019, 7:47 AM
You have to specify type
project.plugins.withType<KaptPlugin>
(not sure about class of KaptPlugin tho
But in general, you don’t need all this stuff
just set
kapt.useBuildCache=true
to your gradle.properties
😲 1
m

Matej Drobnič

10/15/2019, 7:56 AM
It's
Kapt3GradleSubplugin
thanks for both workarounds
t

trevjones

10/15/2019, 6:48 PM
another good way to do it that is more general is with
project.pluginManager.withPlugin("com.android.application") {
      apply from: "${rootProject.projectDir}/gradle/defaultConfig.androidApp.gradle"
    }
that will detect the plugin having been applied and once the plugin.apply returns it will run your config block
I do this is our project so that all the library build files end up as just:
plugins {
  `android-library`
}
dependencies {
  ...
}
same for
kotlin("jvm")
etc…
t

Thiago Nerys

10/16/2019, 12:18 PM
plugins {
    kotlin("kapt") version Versions.kotlin apply false
}
g

gildor

10/16/2019, 12:31 PM
apply false would disable Kotlin DSL type safe accessors and snippet above will be broken
t

Thiago Nerys

10/16/2019, 12:41 PM
Yeah... I got your point.