Skip to content

Commit

Permalink
Defer resolution of processor classpath
Browse files Browse the repository at this point in the history
AGP doesn't like it being resolved during configuration time.
  • Loading branch information
ting-yuan committed Feb 8, 2023
1 parent fb1c6a6 commit 0e1b3dd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.UnknownTaskException
import org.gradle.api.artifacts.Configuration
import org.gradle.api.attributes.Attribute
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.FileCollection
Expand Down Expand Up @@ -112,7 +111,6 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
private fun getSubpluginOptions(
project: Project,
kspExtension: KspExtension,
classpath: Configuration,
sourceSetName: String,
target: String,
isIncremental: Boolean,
Expand Down Expand Up @@ -140,7 +138,6 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
)
options += InternalSubpluginOption("projectBaseDir", project.project.projectDir.canonicalPath)
options += SubpluginOption("allWarningsAsErrors", allWarningsAsErrors.toString())
options += FilesSubpluginOption("apclasspath", classpath.toList())
// Turn this on by default to work KT-30172 around. It is off by default in the compiler plugin.
options += SubpluginOption(
"returnOkOnError",
Expand Down Expand Up @@ -303,7 +300,6 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
getSubpluginOptions(
project,
kspExtension,
processorClasspath,
sourceSetName,
target,
isIncremental,
Expand Down Expand Up @@ -460,7 +456,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
getKspCachesDir(project, sourceSetName, target),
project.provider { project.files() },
project.provider { project.files() },
project.provider { project.files() },
project.provider { processorClasspath }
)
)
}
Expand All @@ -483,7 +479,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
getKspCachesDir(project, sourceSetName, target),
project.provider { project.files() },
project.provider { project.files() },
project.provider { project.files() },
project.provider { processorClasspath }
)
)
}
Expand Down Expand Up @@ -512,6 +508,7 @@ class KspGradleSubplugin @Inject internal constructor(private val registry: Tool
kspTask.compilerPluginOptions.addPluginArgument(kotlinCompileTask.compilerPluginOptions)
}
kspTask.commonSources.from(kotlinCompileTask.commonSources)
kspTask.options.add(FilesSubpluginOption("apclasspath", processorClasspath.files.toList()))
val kspOptions = kspTask.options.get().flatMap { listOf("-P", it.toArg()) }
kspTask.compilerOptions.freeCompilerArgs.value(
kspOptions + kotlinCompileTask.compilerOptions.freeCompilerArgs.get()
Expand Down Expand Up @@ -726,6 +723,7 @@ internal fun createIncrementalChangesTransformer(
processorCP: Provider<FileCollection>,
): (ChangedFiles) -> List<SubpluginOption> = { changedFiles ->
val options = mutableListOf<SubpluginOption>()
val apClasspath = processorCP.get().files.toList()
if (isKspIncremental) {
if (isIntermoduleIncremental) {
// findClasspathChanges may clear caches, if there are
Expand All @@ -736,7 +734,7 @@ internal fun createIncrementalChangesTransformer(
cacheDir,
classpathStructure.get().files,
libraries.get().files.toList(),
processorCP.get().files.toList()
apClasspath
)
options += classpathChanges.toSubpluginOptions()
} else {
Expand All @@ -749,5 +747,7 @@ internal fun createIncrementalChangesTransformer(
}
options += changedFiles.toSubpluginOptions()

options += FilesSubpluginOption("apclasspath", apClasspath)

options
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2020 Google LLC
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.devtools.ksp.test

import org.gradle.testkit.runner.GradleRunner
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import java.io.File

class AGP741IT {
@Rule
@JvmField
val project: TemporaryTestProject = TemporaryTestProject("playground-android-multi", "playground")

@Test
fun testDependencyResolutionCheck() {
val gradleRunner = GradleRunner.create().withProjectDir(project.root).withGradleVersion("7.6")

File(project.root, "gradle.properties").appendText("\nagpVersion=7.4.1")
gradleRunner.withArguments(":workload:compileDebugKotlin").build().let { result ->
Assert.assertFalse(result.output.contains("was resolved during configuration time."))
}
}
}

0 comments on commit 0e1b3dd

Please sign in to comment.