Following the Test Kit guide, I’m using .withPluginClasspath() to test my plugin. This require to use a plugins {} block in the tested build.gradle.kts file.
My plugin uses the AppExtension from Android Plugin classpath to depends some task on the android prebuild.
The issue is when in my test, I use the plugins {} block:
plugins {
id("com.android.application") version("3.3.0-alpha13")
id("greet") // my plugin
}
.
If I remove the plugins DSL block, and use the legacy buildscript, targetting to a jar, it works, meaning (for me) that the issue probably comes from the Plugins DSL and some classpath loading problem.
Classpath injection puts your plugin in a higher classloader so it can’t see other plugins that are only referenced in a test build script. Make the Android plugin a testRuntime dependency in your main build and apply it without a version in the test for this to work.
OK now I understand the classpath issue, thanks !
I’m trying to use the testRuntime dependency but I can’t make it work.
In my main build.gradle.kts I have:
givenBuildScript("""
plugins {
id "com.android.application"
id "greet"
}
greeting {
message = "$message"
}
""")
without a settings.gradle.kts because it should take the local dependency if I understand what’s happening ? Unfortunatelly it doesn’t seem to locate the plugin (./gradlew cleanTest test --info) :
* Where:
Build file '/private/var/folders/lv/7bz6yszn6hzg2hd1vlzysswc0000gn/T/junit7375830087897982030/build.gradle' line: 3
* What went wrong:
Plugin [id: 'com.android.application'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Gradle TestKit (classpath: /Users/nitrog42/Workspace/IdeaProjects/testplugingithub/build/classes/java/main:/Users/nitrog42/Workspace/IdeaProjects/testplugingithub/build/classes/kotlin/main:/Users/nitrog42/Workspace/IdeaProjects/testplugingithub/build/resources/main)
- Plugin Repositories (plugin dependency must include a version number for this source)
I tried using a settings.gradle.kts, but I don’t really know what I should put in it.
I also tried using the same version that is declared in the testRuntime but same issue.
Also, I’m using Gradle 4.10.2
Sorry I spoke too soon, we only take the runtime classpath, not the test runtime classpath (to minimize the difference between testing and production usage of your plugin). You can change that using
Awesome! It works perfectly in groovy, unfortunatelly it won’t with Kotlin-DSL, it seems to break the gradlePlugins {} block
EDIT ; Was wrong, it works with kotlin-dsl too an old reliquate of code on my side.
Thank you very much for your help!
I have one last question; as I feel very weak on those things, do you have any reading recommandation about those subject ?
The source code of the JavaGradlePluginPlugin is the thing I looked at. Apart from that the solution above requires some knowledge on the use of Configurations to create custom dependency “scopes”.