Slackbot
05/07/2022, 2:51 AMephemient
05/07/2022, 2:52 AMbuild.gradle.kts
plugins {
`kotlin-dsl`
}
dependencies {
compileOnly("com.android.tools.build:gradle-api:7.1.3")
testImplementation("junit:junit:4.13.2")
}
src/main/kotlin/example.gradle.kts
val android: com.android.build.api.dsl.ApplicationExtension by extensions
println(android)
src/test/kotlin/ExamplePluginTest.kt
import java.io.File
import org.gradle.testkit.runner.GradleRunner
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class ExamplePluginTest {
@get:Rule
val tempDir = TemporaryFolder()
@Test
fun test() {
File(tempDir.root, "settings.gradle").writeText(
"""
|include 'app'
|""".trimMargin()
)
File(tempDir.root, "build.gradle").writeText(
"""
|buildscript {
| repositories {
| google()
| mavenCentral()
| }
| dependencies {
| classpath 'com.android.tools.build:gradle:7.1.3'
| }
|}
|""".trimMargin()
)
File(tempDir.newFolder("app"), "build.gradle").writeText(
"""
|plugins {
| id 'com.android.application'
| id 'example'
|}
|""".trimMargin()
)
GradleRunner.create()
.withProjectDir(tempDir.root)
.withPluginClasspath()
.forwardOutput()
.build()
}
}ephemient
05/07/2022, 2:52 AMFAILURE: Build failed with an exception.
* What went wrong:
* com/android/build/api/dsl/ApplicationExtension
* > com.android.build.api.dsl.ApplicationExtension
*
* * Try:
* > Run with --stacktrace option to get the stack trace.
* > Run with --info or --debug option to get more log output.
* > Run with --scan to get full insights.
*
* * Get more help at <https://help.gradle.org>
*
* BUILD FAILED in 3s
*ephemient
05/07/2022, 2:57 AMFile(tempDir.root, "build.gradle").writeText(
"""
|buildscript {
| repositories {
| google()
| mavenCentral()
| }
| dependencies {
| classpath 'com.android.tools.build:gradle:7.1.3'
| classpath files(${
PluginUnderTestMetadataReading.readImplementationClasspath(javaClass.classLoader)
.joinToString(separator = ", ") { "'${it.absolutePath}'" }
})
| }
|}
|""".trimMargin()
)
instead of .withPluginClasspath(), but I'm not sure if it's a good approach since PluginUnderTestMetadataReading is in an internal packageephemient
05/07/2022, 2:57 AMGradleRunner.withPluginClasspath() is doing behaving differently from this?ephemient
05/07/2022, 3:01 AMbuild.gradle.kts to
plugins {
`kotlin-dsl`
}
dependencies {
implementation("com.android.tools.build:gradle-api:7.1.3")
testImplementation("junit:junit:4.13.2")
}
doesn't fix things, it just changes the errorCristianGM
05/07/2022, 11:30 AMCristianGM
05/07/2022, 11:31 AMephemient
05/07/2022, 11:40 AMephemient
05/07/2022, 11:41 AMCristianGM
05/07/2022, 11:43 AMCristianGM
05/07/2022, 11:49 AMephemient
05/07/2022, 1:48 PMtasks.pluginUnderTestMetadata {
pluginClasspath.from(configurations.compileOnly)
}
for my use caseCristianGM
05/07/2022, 2:07 PMephemient
05/07/2022, 2:07 PMCristianGM
05/07/2022, 2:07 PM.configure {...}CristianGM
05/07/2022, 2:08 PMephemient
05/07/2022, 2:08 PMCristianGM
05/07/2022, 2:08 PMThomas Broyer
05/07/2022, 3:34 PMThomas Broyer
05/07/2022, 3:38 PM// See <https://github.com/gradle/gradle/issues/7974>
val additionalPluginClasspath by configurations.creating
dependencies {
compileOnly("com.android.tools.build:gradle:${Version.ANDROID_GRADLE_PLUGIN_VERSION}")
additionalPluginClasspath("com.android.tools.build:gradle:${Version.ANDROID_GRADLE_PLUGIN_VERSION}")
}
tasks {
pluginUnderTestMetadata {
this.pluginClasspath.from(additionalPluginClasspath)
}
}
I prefer declaring the dependency twice, in different configurations, than adding compileOnly to the pluginClasspath, as that allows me to add other things to compileOnly (or compileOnlyApi) independently of that testing need.
I could also have made compileOnly.extendsFrom(additionalPluginClasspath) fwiw.ephemient
05/07/2022, 3:39 PMCristianGM
05/07/2022, 3:41 PMCristianGM
05/07/2022, 3:42 PMThomas Broyer
05/07/2022, 3:53 PMCristianGM
05/07/2022, 4:02 PMThomas Broyer
05/07/2022, 4:16 PMjava-base plugin vs AGP.
This is https://plugins.gradle.org/plugin/net.ltgt.errorprone fwiw (plugin extends annotation processor configurations and tries to determine which JavaCompile tasks are test)Thomas Broyer
05/07/2022, 4:16 PMtony
05/07/2022, 4:47 PM