Is there a way to ignore certain errors in Kotlin ...
# gradle
s
Is there a way to ignore certain errors in Kotlin Script Gradle? I'm not sure if it's new in Gradle 8.x but we're seeing tons of
Variable 'commonMain' is never used
in our Kotlin multiplatform modules:
Copy code
sourceSets {
        val commonMain by getting {
            dependencies {
            }
        }
It'd be nice if there was a way to ignore this error or turn it off for the whole project.
e
it's not new. you can suppress the warnings with
Copy code
@Suppress("UNUSED_VARIABLE")
but I just use
Copy code
sourceSets {
        getByName("commonMain") {
            dependencies {
            }
        }
when the variable would be unused
(`getting`/`getByName`, `creating`/`create`, `existing`/`named`, `registering`/`register`)
a
since
commonMain
is created proactively in the Kotlin Multiplatform plugin, it should have a DSL accessor generated if you use
.gradle.kts
, so you can do
Copy code
sourceSets {
  commonMain { // generated accessor
    ...
  }
}
accessors for the source sets of other targets (or custom source sets) will be generated if you create a convention plugin for Kotlin Multiplatform and define all the targets/custom source sets in there. Gradle will then be able to generate accessors for them, so you could have
Copy code
// buildSrc/src/main/kotlin/my-kotlin-mp-convention.gradle.kts

plugins {
  kotlin("multiplatform")
}

kotlin {
  jvm()
  
  sourceSets {
    create("foo") { ... }
  }
}
Copy code
// my-subproject/build.gradle.kts

plugins {
  id("my-kotlin-mp-convention")
}

kotlin {
  sourceSets {
    jvmMain { }
    foo { }
  }
}
And just like that, no warnings about unused variables
e
TIL! That's really neat @Adam S, thanks for sharing 🙂
352 Views