4

I've created a standalone plugin along with a simple demo app, that I need for development. Both are added to the same project as an app-module and a plugin-module, so that I can easily develop and test features I write in the plugin. I'm assuming that if I add the sources from the plugin to buildSrc/build.gradle.kts, I'll be able to reference the plugin sources from the app (I need that to build and apply the plugin). The demo project in its entirety can be found here: https://github.com/oizo/gradle-plugin-sample.

Currently, it seems that when I apply the plugin in app/build.gradle.kts it's available (autocomplete is working) but when I try to build with ./gradlew build it fails with this message:

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/MyUser/github/gradle-plugin-sample/app/build.gradle.kts' line: 10

* What went wrong:
Script compilation error:

  Line 10: apply<io.hvam.android.plugin.StringPlugin>()
                 ^ Unresolved reference: io

Clearly, I'm missing something.

I've tried the solution proposed in this post-https://stackoverflow.com/a/42263532/1181023, which seems to be a similar problem, but when I run ./gradlew build --include-build plugin/ it also fails to build, with the following:

FAILURE: Build failed with an exception.

* What went wrong:
Included build in /Users/MyUser/github/gradle-plugin-sample/plugin has a root project whose name 'plugin' is the same as a project of the main build.
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
oiZo
  • 432
  • 3
  • 10

2 Answers2

0

I see two issues:

  • Your plugin must be packaged in such a way that your Plugin implementing class is visible to consumers of the plugin as the entry point. One easy way of doing that is by using gradlePlugin {} block. I would recommend to upgrade to at least Gradle 5.5.1 and use build init plugin to automate all these for you.

    Please try gradle init --type kotlin-gradle-plugin to generate the plugin project. This would add code similar to below in your plugin build.gradle which ensures your consumers can apply the plugin using apply plugin: "stringPlugin".

gradlePlugin {
   plugins {
     stringPlugin {
         id = "stringPlugin"
         implementationClass = "com.example.StringPlugin"
     }
   }
}
  • Secondly, your plugin must be visible to the buildscript classpath so that it can apply your plugin. If you have already published the plugin to maven, then you can add the below in project level build.gradle to let gradle discover your plugin.
buildscript {
    ...
    dependencies {
       classpath "com.example:stringplugin"
    }
}

Publishing to maven might not be convenient during development. You could utilize Gradle's composite builds to help in this regard. In your project level settings.gradle, add below:

includeBuild('./plugin') {
    dependencySubstitution {
        substitute module('com.example:stringplugin') with project(':plugin')
    }
}

This instructs gradle to use locally present :plugin module when com.example:stringplugin is requested. This would be convenient for development.

Arun
  • 700
  • 4
  • 11
  • Thank you for your response. The first point about using `gradlePlugin` only seems to be a scripted way of adding the `META-INF` properties file, and didn't help. I did how ever manage to get it running, by moving the `buildscript` into `buildSrc/build.gradle.kts`, but omitting the actual `buildscript` extension. You can review the specific changes that did the trick for me here: https://github.com/oizo/gradle-plugin-sample/commit/d99c35f9d17bc1daecdadefaf46a007298cfe5c1 The part of dependency substitution is really interesting, and I'll definitely have to give that a look. – oiZo Oct 23 '19 at 11:12
0

It seems I misunderstood how gradle uses the buildSrc folder. I've moved the content of buildscript into buildSrc/build.gradle.kts but omitting the actual buildscript extension. This somehow fixed the issue of the app-module not being able to recognize the plugin sources when building. The specific changes related to the fix can be reviewed int this commit. And the sample code in the repo now builds as expected.

This gives some more clarity about the purpose of the buildSrc folder: https://stackoverflow.com/a/13875350/1181023

oiZo
  • 432
  • 3
  • 10