title: Android Gradle: Custom Plugin with id 'XXXX' not found - Kotlin DSL
body: <p>I'm trying to develop a gradle plugin to use it for generating some objects and methods for our api using some scheme.
  I have followed some tutorials but they all seem not to work, atleast for me.
  Some of these tutorials were:</p>
  <ol>
  <li><a href="https://musings.animus.design/kotlin-poet-building-a-gradle-plugin/" rel="nofollow noreferrer">https://musings.animus.design/kotlin-poet-building-a-gradle-plugin/</a></li>
  <li><a href="https://medium.com/@magicbluepenguin/how-to-create-your-first-custom-gradle-plugin-efc1333d4419" rel="nofollow noreferrer">https://medium.com/@magicbluepenguin/how-to-create-your-first-custom-gradle-plugin-efc1333d4419</a></li>
  </ol>
  <p>I have not used the buildSrc module because I'm already using it for Kotlin DSL, so I decided to create a new module and create my plugin there.</p>
  <p>My plugin's module <code>build.gradle.kts</code> looks like this:</p>
  <pre><code>plugins {
      id(&quot;java-gradle-plugin&quot;)
      id(&quot;kotlin&quot;)
  }
  
  java {
      sourceCompatibility = JavaVersion.VERSION_1_8
      targetCompatibility = JavaVersion.VERSION_1_8
  }
  
  buildscript {
      repositories {
          google()
          jcenter()
      }
  
      dependencies {
          classpath(config.ClassPaths.androidBuildTools)
          classpath(config.ClassPaths.kotlinGradlePlugin)
      }
  }
  
  repositories {
      google()
      jcenter()
  }
  
  dependencies {
      implementation(config.ClassPaths.androidBuildTools)
      implementation(config.ClassPaths.kotlinGradlePlugin)
  }
  
  gradlePlugin {
      plugins {
          create(&quot;Generator&quot;) {
              id = &quot;Generator&quot;
              implementationClass = &quot;Generator&quot;
          }
      }
  }
  </code></pre>
  <p>My projects <code>settings.gradle.kts</code> looks like this:</p>
  <pre><code>include(&quot;:SampleProject&quot;, &quot;:scheme-generator&quot;)
  </code></pre>
  <p>And in my application module's <code>build.gradle.kts</code> I'm applying this plugin like this:</p>
  <pre><code>apply(plugin = &quot;Generator&quot;)
  </code></pre>
  <p>The build script stops here with an error: plugin 'Generator' not found</p>
  <p>My Generator class looks like this:</p>
  <pre><code>class Generator : Plugin&lt;Project&gt; {
      override fun apply(target: Project) {
          target.android().variants().all { variant -&gt;
  
              // Make a task for each combination of build type and product flavor
              val myTask = &quot;myFirstTask${variant.name.capitalize()}&quot;
  
              // Register a simple task as a lambda. We can later move this to its own
              // class to make our code cleaner and also add some niceties.
              target.tasks.create(myTask){task -&gt;
  
                  // Group all our plugin's tasks together
                  task.group = &quot;MyPluginTasks&quot;
                  task.doLast {
                      File(&quot;${target.projectDir.path}/myFirstGeneratedFile.txt&quot;).apply {
                          writeText(&quot;Hello Gradle!\nPrinted at: ${SimpleDateFormat(&quot;HH:mm:ss&quot;, Locale.getDefault()).format(Date())}&quot;)
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  <p>The <code>android</code> and <code>variants</code> methods are declared in a utils file, they look like this:</p>
  <pre><code>object GeneratorUtils {
  
      fun Project.android(): BaseExtension {
          val android = project.extensions.findByType(BaseExtension::class.java)
          if (android != null) {
              return android
          } else {
              throw GradleException(&quot;Project $name is not an Android project&quot;)
          }
      }
  
      fun BaseExtension.variants(): DomainObjectSet&lt;out BaseVariant&gt; {
          return when (this) {
              is AppExtension -&gt; {
                  applicationVariants
              }
  
              is LibraryExtension -&gt; {
                  libraryVariants
              }
  
              else -&gt; throw GradleException(&quot;Unsupported BaseExtension type!&quot;)
          }
      }
  
  }
  </code></pre>
  <p>I have tried many things, but I seem not to get this right.</p>
  <p><strong>EDIT:</strong>
  Using the buildSrc module for my plugin works totally fine, the plugin is applied and the gradle tasks are visible. However, buildSrc is reserved for other purposes, and we would like our plugin to be in a separate module, so we will be able to use it in other projects.</p>
  <p><strong>EDIT 13/04/2021</strong></p>
  <p>I have managed to see the tasks that are added by my plugin in my app tasks list by including this module as a composite build.</p>
  <p>My <code>settings.gradle</code> now looks like this:</p>
  <pre><code>pluginManagement {
      includeBuild(&quot;generator&quot;)
  }
  
  include(&quot;:SampleProject&quot;)
  </code></pre>
  <p><code>build.gradle</code> of my plugin looks like this:</p>
  <pre><code>apply plugin: 'java-gradle-plugin' // Allows us to create and configure custom plugins
  apply plugin: 'kotlin' //Needed as we'll write our plugin in Kotlin
  
  buildscript {
      ext {
          kotlin_version = '1.4.31'
          gradle_version = '4.1.2'
      }
      repositories {
          google()
          jcenter()
      }
      dependencies {
          classpath &quot;org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version&quot;
          classpath &quot;com.android.tools.build:gradle:$gradle_version&quot;
      }
  }
  
  repositories {
      google()
      jcenter()
  }
  
  dependencies {
      // Android gradle plugin will allow us to access Android specific features
  }
  
  gradlePlugin {
      plugins {
          create(&quot;Generator&quot;) {
              id = &quot;Generator&quot;
              implementationClass = &quot;com.example.Generator&quot;
          }
      }
  }
  </code></pre>
  <p>And my generator class now looks like this:</p>
  <pre><code>class Generator : Plugin&lt;Project&gt; {
  
      override fun apply(project: Project) {
  
          project.tasks.register(&quot;generationTask&quot;) { task -&gt;
              task.apply {
                  group = &quot;generation&quot;
                  actions.add(Action {
                      print(&quot;Hello from generation task!&quot;)
                  })
              }
          }
      }
  
  }
  </code></pre>
  <p>I can see <code>generationTask</code> in my tasks list and I can execute it normally. It prints the text without any problems.</p>
  <p>The problem now is to include <code>com.android.tools.build:gradle:4.1.2</code> in my dependecies to use it to access build types and flavors and their paths to save my generated code there. When I add it to my dependencies block, gradle fails with this error:  <code>Could not find com.android.tools.build:gradle:4.1.2.</code></p>
  <p>How can I solve this problem?</p>