Skip to content

Commit

Permalink
Avoid NPE on kotlin 1.7.20
Browse files Browse the repository at this point in the history
With kotlin 1.7.20, some of the source sets (that are nullable properties)
are actually null. In order to avoid crash, we create an empty source set
and use it as a fallback.
  • Loading branch information
vudzkostek authored and MiSikora committed Sep 29, 2022
1 parent 81d4a87 commit 9c48cb3
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ private fun TaskProvider<out Task>.contributeToAndroid(dir: File, project: Proje
}
val sources = extension.sourceSets.associate { set -> set.name to set.kotlin }
for (variant in extension.variants) {
val kotlinSourceSet = requireNotNull(sources[variant.name]) {
"Did not find Kotlin source set for variant ${variant.name}"
}
val kotlinSourceSet = sources[variant.name] ?: project.createEmptySourceSet(variant.name)
kotlinSourceSet.srcDir(dir.toRelativeString(project.projectDir))
variant.addJavaSourceFoldersToModel(dir)

Expand All @@ -58,9 +56,7 @@ private fun TaskProvider<out Task>.contributeToAndroid(dir: File, project: Proje

private fun contributeToKotlin(dir: File, project: Project) {
val sourceSets = project.property("sourceSets") as SourceSetContainer
val kotlinSourceSet = requireNotNull(sourceSets.getByName("main").kotlin) {
"Did not find Kotlin source set"
}
val kotlinSourceSet = sourceSets.getByName("main").kotlin ?: project.createEmptySourceSet("empty")
kotlinSourceSet.srcDir(dir)
}

Expand All @@ -83,3 +79,6 @@ private val Any.kotlin: SourceDirectorySet?
}

private fun Any.getConvention(name: String) = (this as HasConvention).convention.plugins[name]

private fun Project.createEmptySourceSet(name: String) =
objects.sourceDirectorySet(name, "Empty kotlin source set")

0 comments on commit 9c48cb3

Please sign in to comment.