title: Generating flavorDimensions and productFlavor via Gradle Plugin in multi-module project
body: <p>I am building a Gradle Plugin that should generate both flavorDimensions and productFlavors on Android. Everything works fine when this plugin is being applied directly to the Android module. My goal however is to apply this plugin to another module within the same project as the Android module. Currently this does not work due to a AgpDslLockedException being thrown during Gradle Sync.</p>
  <p>My plugin is implemented as follows:</p>
  <pre><code>class MyPlugin : Plugin&lt;Project&gt; {
      override fun apply(project: Project) {
          // Traverse whole project tree to find Android project
          project.rootProject.subprojects.forEach { subproject -&gt;
              val android = subproject.extensions.findByName(&quot;android&quot;) as? AppExtension ?: return@forEach
              // throws AgpDslLockedException when being applied to other module than android
              android.flavorDimensions.add(&quot;dimension&quot;)
              android.productFlavors.create(&quot;flavor&quot;) { it.dimension = &quot;dimension&quot; }
          }
      }
  
  </code></pre>
  <p>A minimal client for the Gradle Plugin could be structured as follows:</p>
  <pre><code>- android
      - build.gradle
  - other
      - build.gradle
  - settings.gradle
  </code></pre>
  <pre><code>// android/build.gradle
  plugins {
      id 'com.android.application'
      // MyPlugin would work if being applied here
  }
  
  android {
      // Neither flavorDimensions nor productFlavors are declared as they should be generated by MyPlugin
  }
  </code></pre>
  <pre><code>// other/build.gradle
  plugins {
      // MyPlugin does not work due to AgpDslLockedException being thrown
  }
  </code></pre>
  <pre><code>// settings.gradle
  include(&quot;:android&quot;)
  include(&quot;:other&quot;)
  </code></pre>
  <p>The AgpDslLockedException:</p>
  <pre><code>It is too late to modify flavorDimensions.
  It has already been read to configure this project.
  Consider either moving this call to be during evaluation, or using the variant API.
  </code></pre>
  <p>Am I missing something or is there another way to generate flavorDimensions and productFlavors from a third-party build.gradle file?</p>
  <p>So far I have tried adding the flavorDimension and creating the productFlavor on project.beforeEvaluate/afterEvaluate but to not avail. Maybe there is a lifecycle method I am missing.</p>
  <p>Thanks in advance and have a nice day!</p>