Create kapt configurations before gradle script is evaluated
We cannot create kapt configurations in `KotlinGradleSubplugin.apply`, because `apply` is invoked after a script is evaluated, so user cannot refer to kapt configurations in script. We cannot call `apply` before script's evaluation, because then we might not be able to determine if a Gradle plugin corresponding to a subplugin is appled. For example in case of the following script: ``` apply plugin: "kotlin" apply plugin: "kotlin-kapt" ``` subplugin's apply will exit early because Gradle plugin "kotlin-kapt" is not applied yet, when "kotlin"'s apply is evaluated.
This commit is contained in:
+17
-19
@@ -90,6 +90,23 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun findMainKaptConfiguration(project: Project) = project.findKaptConfiguration(MAIN_KAPT_CONFIGURATION_NAME)
|
fun findMainKaptConfiguration(project: Project) = project.findKaptConfiguration(MAIN_KAPT_CONFIGURATION_NAME)
|
||||||
|
|
||||||
|
fun createAptConfigurationIfNeeded(project: Project, sourceSetName: String): Configuration {
|
||||||
|
val configurationName = Kapt3KotlinGradleSubplugin.getKaptConfigurationName(sourceSetName)
|
||||||
|
|
||||||
|
project.configurations.findByName(configurationName)?.let { return it }
|
||||||
|
val aptConfiguration = project.configurations.create(configurationName)
|
||||||
|
|
||||||
|
if (aptConfiguration.name != Kapt3KotlinGradleSubplugin.MAIN_KAPT_CONFIGURATION_NAME) {
|
||||||
|
// The main configuration can be created after the current one. We should handle this case
|
||||||
|
val mainConfiguration = findMainKaptConfiguration(project)
|
||||||
|
?: createAptConfigurationIfNeeded(project, SourceSet.MAIN_SOURCE_SET_NAME)
|
||||||
|
|
||||||
|
aptConfiguration.extendsFrom(mainConfiguration)
|
||||||
|
}
|
||||||
|
|
||||||
|
return aptConfiguration
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val kotlinToKaptGenerateStubsTasksMap = mutableMapOf<KotlinCompile, KaptGenerateStubsTask>()
|
private val kotlinToKaptGenerateStubsTasksMap = mutableMapOf<KotlinCompile, KaptGenerateStubsTask>()
|
||||||
@@ -161,8 +178,6 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
javaSourceSet.name
|
javaSourceSet.name
|
||||||
}
|
}
|
||||||
|
|
||||||
createAptConfigurationIfNeeded(project, sourceSetName)
|
|
||||||
|
|
||||||
val kaptExtension = project.extensions.getByType(KaptExtension::class.java)
|
val kaptExtension = project.extensions.getByType(KaptExtension::class.java)
|
||||||
|
|
||||||
val nonEmptyKaptConfigurations = kaptConfigurations.filter { configuration ->
|
val nonEmptyKaptConfigurations = kaptConfigurations.filter { configuration ->
|
||||||
@@ -189,23 +204,6 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
|||||||
return emptyList()
|
return emptyList()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun createAptConfigurationIfNeeded(project: Project, sourceSetName: String): Configuration {
|
|
||||||
val configurationName = Kapt3KotlinGradleSubplugin.getKaptConfigurationName(sourceSetName)
|
|
||||||
|
|
||||||
project.configurations.findByName(configurationName)?.let { return it }
|
|
||||||
val aptConfiguration = project.configurations.create(configurationName)
|
|
||||||
|
|
||||||
if (aptConfiguration.name != Kapt3KotlinGradleSubplugin.MAIN_KAPT_CONFIGURATION_NAME) {
|
|
||||||
// The main configuration can be created after the current one. We should handle this case
|
|
||||||
val mainConfiguration = findMainKaptConfiguration(project)
|
|
||||||
?: createAptConfigurationIfNeeded(project, SourceSet.MAIN_SOURCE_SET_NAME)
|
|
||||||
|
|
||||||
aptConfiguration.extendsFrom(mainConfiguration)
|
|
||||||
}
|
|
||||||
|
|
||||||
return aptConfiguration
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getSubpluginKotlinTasks(project: Project, kotlinCompile: KotlinCompile): List<AbstractCompile> {
|
override fun getSubpluginKotlinTasks(project: Project, kotlinCompile: KotlinCompile): List<AbstractCompile> {
|
||||||
val kaptGenerateStubsTask = kotlinToKaptGenerateStubsTasksMap[kotlinCompile]
|
val kaptGenerateStubsTask = kotlinToKaptGenerateStubsTasksMap[kotlinCompile]
|
||||||
return if (kaptGenerateStubsTask == null) emptyList() else listOf(kaptGenerateStubsTask)
|
return if (kaptGenerateStubsTask == null) emptyList() else listOf(kaptGenerateStubsTask)
|
||||||
|
|||||||
+2
@@ -144,6 +144,7 @@ internal class Kotlin2JvmSourceSetProcessor(
|
|||||||
|
|
||||||
override fun doTargetSpecificProcessing() {
|
override fun doTargetSpecificProcessing() {
|
||||||
kotlinSourceSet.kotlin.source(sourceSet.java)
|
kotlinSourceSet.kotlin.source(sourceSet.java)
|
||||||
|
Kapt3KotlinGradleSubplugin.createAptConfigurationIfNeeded(project, sourceSet.name)
|
||||||
|
|
||||||
project.afterEvaluate { project ->
|
project.afterEvaluate { project ->
|
||||||
if (project != null) {
|
if (project != null) {
|
||||||
@@ -503,6 +504,7 @@ abstract class AbstractAndroidProjectHandler<V>(private val kotlinConfigurationT
|
|||||||
val kotlinSourceSet = kotlinConfigurationTools.kotlinSourceSetProvider.create(sourceSet.name)
|
val kotlinSourceSet = kotlinConfigurationTools.kotlinSourceSetProvider.create(sourceSet.name)
|
||||||
kotlinSourceSet.kotlin.srcDir(project.file(project.file("src/${sourceSet.name}/kotlin")))
|
kotlinSourceSet.kotlin.srcDir(project.file(project.file("src/${sourceSet.name}/kotlin")))
|
||||||
sourceSet.addConvention(KOTLIN_DSL_NAME, kotlinSourceSet)
|
sourceSet.addConvention(KOTLIN_DSL_NAME, kotlinSourceSet)
|
||||||
|
Kapt3KotlinGradleSubplugin.createAptConfigurationIfNeeded(project, sourceSet.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
val kotlinOptions = KotlinJvmOptionsImpl()
|
val kotlinOptions = KotlinJvmOptionsImpl()
|
||||||
|
|||||||
Reference in New Issue
Block a user