Skip to content

Commit c20d569

Browse files
Simplify friend paths configuration
1 parent 3e95a55 commit c20d569

File tree

2 files changed

+12
-18
lines changed
  • libraries/tools/kotlin-gradle-plugin/src

2 files changed

+12
-18
lines changed

libraries/tools/kotlin-gradle-plugin/src/agp25/kotlin/org/jetbrains/kotlin/gradle/plugin/android/Android25ProjectHandler.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class Android25ProjectHandler(kotlinConfigurationTools: KotlinConfigurationTools
5656
kotlinClasspath + project.files(AndroidGradleWrapper.getRuntimeJars(androidPlugin, androidExt))
5757
}
5858

59-
kotlinTask.javaOutputDir = javaTask.destinationDir
60-
kotlinAfterJavaTask?.javaOutputDir = javaTask.destinationDir
61-
6259
// Use kapt1 annotations file for up-to-date check since annotation processing is done with javac
6360
kotlinTask.kaptOptions.annotationsFile?.let { javaTask.inputs.file(it) }
6461

@@ -72,10 +69,11 @@ class Android25ProjectHandler(kotlinConfigurationTools: KotlinConfigurationTools
7269
}
7370

7471
// Find the classpath entries that comes from the tested variant and register it as the friend path, lazily
75-
kotlinTask.friendClasspathEntries = lazy {
72+
kotlinTask.friendPaths = lazy {
7673
variantData.getCompileClasspathArtifacts(preJavaClasspathKey)
7774
.filter { it.id.componentIdentifier is TestedComponentIdentifier }
7875
.map { it.file.absolutePath }
76+
.toTypedArray()
7977
}
8078
}
8179

libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/tasks/Tasks.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,16 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractCo
112112
// TODO: consider more reliable approach (see usage)
113113
internal var anyClassesCompiled: Boolean = false
114114
internal var friendTaskName: String? = null
115+
internal var javaOutputDir: File? = null
115116
internal var sourceSetName: String by Delegates.notNull()
116117
internal val moduleName: String
117118
get() = "${project.name}_$sourceSetName"
118119

119-
var javaOutputDir: File? = null
120+
@Suppress("UNCHECKED_CAST")
121+
protected val friendTask: AbstractKotlinCompile<T>?
122+
get() = friendTaskName?.let { project.tasks.findByName(it) } as? AbstractKotlinCompile<T>
120123

121-
// In case the way to retrieve friend classpath entries is known at configuration time, it will be set
122-
var friendClasspathEntries: Lazy<List<String>?> = lazyOf(null)
124+
var friendPaths: Lazy<Array<String>?> = lazy { friendTask?.javaOutputDir?.absolutePath?.let { arrayOf(it) } }
123125

124126
override fun compile() {
125127
assert(false, { "unexpected call to compile()" })
@@ -206,15 +208,18 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
206208
val kaptPluginOptions = getKaptPluginOptions()
207209
args.pluginOptions = (pluginOptions.arguments + kaptPluginOptions.arguments).toTypedArray()
208210

209-
args.moduleName = moduleName
210211
args.addCompilerBuiltIns = true
211212

212213
val gradleVersion = getGradleVersion()
213214
if (gradleVersion == null || gradleVersion >= ParsedGradleVersion(3, 2)) {
214215
args.loadBuiltInsFromDependencies = true
215216
}
216217

217-
friendTaskName?.let { addFriendPathForTestTask(it, args) }
218+
args.moduleName = friendTask?.moduleName ?: moduleName
219+
logger.kotlinDebug { "args.moduleName = ${args.moduleName}" }
220+
221+
args.friendPaths = friendPaths.value
222+
logger.kotlinDebug { "args.friendPaths = ${args.friendPaths?.joinToString() ?: "[]"}" }
218223

219224
if (defaultsOnly) return
220225

@@ -226,15 +231,6 @@ open class KotlinCompile : AbstractKotlinCompile<K2JVMCompilerArguments>(), Kotl
226231
logger.kotlinDebug { "$name destinationDir = $destinationDir" }
227232
}
228233

229-
internal fun addFriendPathForTestTask(friendKotlinTaskName: String, args: K2JVMCompilerArguments) {
230-
val friendTask = project.getTasksByName(friendKotlinTaskName, /* recursive = */false).firstOrNull() as? KotlinCompile ?: return
231-
args.friendPaths = arrayOf(
232-
friendTask.javaOutputDir!!.absolutePath,
233-
*friendClasspathEntries.value.orEmpty().toTypedArray())
234-
args.moduleName = friendTask.moduleName
235-
logger.kotlinDebug("java destination directory for production = ${friendTask.javaOutputDir}")
236-
}
237-
238234
override fun getSourceRoots() = SourceRoots.ForJvm.create(getSource(), sourceRootsContainer)
239235

240236
override fun callCompiler(args: K2JVMCompilerArguments, sourceRoots: SourceRoots, changedFiles: ChangedFiles) {

0 commit comments

Comments
 (0)