title: Gradle script which selects a build variant of the Android library
body: <p>I am using Eclipse and working with Android projects. I cannot use Android Studio for many reasons. Unfortunately, Eclipse is not capable to work with AAR archives. After a long struggle we decided to make Eclipse projects NON-GRADLE (eliminate gradle nature from all Eclipse projects) and prepare a list of dependencies with a special gradle script.
  In order to make it possible to work with Android, I have written the following gradle script, which does the following:</p>
  
  <ol>
  <li>search for all dependencies in all projects</li>
  <li><p>copy the dependencies from gradle cache to a special Eclipse project "jars-from-gradle"</p></li>
  <li><p>compose the ".classpath" for all projects to just use the found libraries</p></li>
  </ol>
  
  <p>Here is the gradle script:</p>
  
  <pre><code>apply plugin: "eclipse"
  
  configurations {
      eclipseOnly {
          description = 'this is used only to build eclipse classpath'
      }
  }
  
  afterEvaluate{
  
    project.tasks['eclipseProject'].dependsOn(project.tasks['cleanEclipseProject'])
    project.tasks['eclipseClasspath'].dependsOn(project.tasks['cleanEclipseClasspath'])
  
    eclipse{
  
      File f = rootProject.ext.find("explodedAarsDir");
      if(f == null) {
          f = new File("${rootProject.projectDir}/jars-from-gradle/explodedAars/");
          rootProject.ext.set("explodedAarsDir", f)
          f.mkdirs();
      }
      f = rootProject.ext.find("globalDependenciesRepo");
      if(f == null) {
          f = new File("${rootProject.projectDir}/jars-from-gradle/libs");
          rootProject.ext.set("globalDependenciesRepo", f)
          f.mkdirs();
      }
      org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory frf = rootProject.ext.find("fileReferenceFactory");
      if(frf == null) {
          frf = new org.gradle.plugins.ide.eclipse.model.internal.FileReferenceFactory();
          rootProject.ext.set("fileReferenceFactory", frf)
      }
  
      if(!rootProject.ext.has("eclipseFileMapping")) {
          rootProject.ext.set("eclipseFileMapping", new HashMap&lt;File, File&gt;())
      }
      Map&lt;File, File&gt; eclipseFileMapping = rootProject.ext.get("eclipseFileMapping")
      eclipseFileMapping.put(new File("bin/main"), new File("bin"))
      eclipseFileMapping.put(new File("bin/test"), new File("bin"))
      eclipseFileMapping.put(new File("${buildDir}/classes/java/main"), new File("${projectDir.parentFile}/${project.name}/bin"))
      eclipseFileMapping.put(new File("${buildDir}/classes/java/test"), new File("${projectDir.parentFile}/${project.name}/bin"))
      eclipseFileMapping.put(new File("${buildDir}/resources/main"), new File("${projectDir.parentFile}/${project.name}/bin"))
      eclipseFileMapping.put(new File("${buildDir}/resources/test"), new File("${projectDir.parentFile}/${project.name}/bin"))
  
      configurations {
        eclipsePlusConfig {
          description = "files to include into eclipse classpath"
          transitive = false
        }
        eclipseMinusConfig {
          description = "files to exclude from eclipse classpath"
          transitive = false
        }
      }
  
      project{
        setupEclipseProject()
      }
  
      classpath{
        defaultOutputDir = new File("${projectDir}/bin")
        plusConfigurations += [project.configurations.eclipseOnly]
        if(project.extensions.findByName("android") != null) {
            plusConfigurations += [project.configurations.compile]
            plusConfigurations += [project.configurations.runtime]
            project.eclipse.project{
                natures 'org.eclipse.andmore.AndroidNature'
                buildCommands.clear()
                buildCommand "org.eclipse.andmore.ResourceManagerBuilder"
                buildCommand "org.eclipse.andmore.PreCompilerBuilder"
                buildCommand "org.eclipse.jdt.core.javabuilder"
                buildCommand "org.eclipse.andmore.ApkBuilder"
                containers 'org.eclipse.andmore.DEPENDENCIES', 'org.eclipse.andmore.LIBRARIES', 'org.eclipse.andmore.ANDROID_FRAMEWORK'
            }
        } else {
            plusConfigurations += [project.configurations.compile]
            plusConfigurations += [project.configurations.runtime]
        }
  
        file {
          beforeMerged { classpath -&gt;
             eclipse.classpath.sourceSets.each {
                 println "    source set "+ it.getName()
             }
             eclipse.classpath.plusConfigurations.each{ processConf(it, "    ", "plus conf: ") }
             eclipse.classpath.minusConfigurations.each{ processConf(it, "    ", "minus conf: ") }
  
             eclipse.classpath.plusConfigurations.add(project.configurations['eclipsePlusConfig'])
             eclipse.classpath.minusConfigurations.add(project.configurations['eclipseMinusConfig'])
          }
          whenMerged { classpath -&gt;
             List&lt;org.gradle.plugins.ide.eclipse.model.ClasspathEntry&gt; replacementEclipseClasspath = createEclipseReplacementClasspath(classpath);
             classpath.setEntries(replacementEclipseClasspath)
          }
          withXml { n -&gt;
              Set&lt;File&gt; existingPaths = new HashSet&lt;File&gt;();
              def rootNode = n.asNode();
              for(int nodeIndex = 0; nodeIndex&lt;rootNode.children().size(); nodeIndex++) {
                 def chld = rootNode.children().get(nodeIndex);
                 if("classpathentry".equals(chld.name())) {
                    removeGradleAttributes(chld);
                    chld.attributes().remove("output");
                    String kind = chld.attributes().get("kind");
                    for(Map.Entry entry : chld.attributes().entrySet()) {
                        if("path".equals(entry.key) || "sourcepath".equals(entry.key)) {
                          f = new File(entry.value);
                          if(f.toPath().isAbsolute()) {
                              String relativeName = rootProject.projectDir.toPath().relativize(f.toPath()).toString();
                              entry.value = "/"+ relativeName.replace('\\', '/');
                          }
                          if("path".equals(entry.key) &amp;&amp; existingPaths.contains(f)) {
                              rootNode.children().remove(nodeIndex--);
                              break;
                          }
                          if(entry.value.startsWith("/")) {
                              if("src".equals(kind)) {
                                  chld.attributes().put("combineaccessrules", "false");
                              }
                          }
                          existingPaths.add(f);
                        }
                    }
                    if("lib".equals(kind)) {
                        chld.attributes().put("exported", "true");
                    }
                 }
              }
          }
        }
      }
  
      task prepareEclipse{
        doFirst{
          mkDirIfNotExists(new File("${projectDir}/src/main/java"))
          mkDirIfNotExists(new File("${projectDir}/src/main/resources"))
          mkDirIfNotExists(new File("${projectDir}/src/test/java"))
          mkDirIfNotExists(new File("${projectDir}/src/test/resources"))
        }
      }
  
      tasks['eclipseClasspath'].dependsOn(prepareEclipse)
    }
  }
  
  List&lt;org.gradle.plugins.ide.eclipse.model.ClasspathEntry&gt; createEclipseReplacementClasspath(org.gradle.plugins.ide.eclipse.model.Classpath eclipseClasspath) {
      Map&lt;String, org.gradle.plugins.ide.eclipse.model.ClasspathEntry&gt; replacementEclipseClasspathAsMap = new HashMap&lt;String, org.gradle.plugins.ide.eclipse.model.ClasspathEntry&gt;(); 
      eclipseClasspath.entries.each{ clspthentry -&gt;
          dumpClassPathEntry(clspthentry)
          if (clspthentry instanceof org.gradle.plugins.ide.eclipse.model.Library) {
              org.gradle.plugins.ide.eclipse.model.Library library = clspthentry;
              String moduleId = library.getModuleVersion().toString();
              String groupId = null;
              String artifactId = null;
              String artifactVersion = null;
              int index = moduleId.indexOf(":");
              if(index &gt;= 0) {
                  groupId = moduleId.substring(0, index);
                  String tmp = moduleId.substring(++index);
                  index = tmp.indexOf(":")
                  if(index &gt;= 0) {
                      artifactId = tmp.substring(0, index);
                      artifactVersion = tmp.substring(++index);
                  }
              }
              moduleId = moduleId.replaceAll(":", "-");
              println("    classpath entry found: moduleId="+ moduleId);
              if (library.getPath().endsWith(".aar")) {
                  explodeAarJarFiles(moduleId, groupId, artifactId, artifactVersion, library, replacementEclipseClasspathAsMap);
              } else {
                  copyLibraryFromGradleCache(moduleId, groupId, artifactId, artifactVersion, library, replacementEclipseClasspathAsMap)
              }
          } else {
              replacementEclipseClasspathAsMap.put(clspthentry.kind+ "_"+ clspthentry.path, clspthentry);
          }
      }
      List&lt;org.gradle.plugins.ide.eclipse.model.ClasspathEntry&gt; replacementEclipseClasspath = new ArrayList&lt;org.gradle.plugins.ide.eclipse.model.ClasspathEntry&gt;();
      replacementEclipseClasspath.addAll(replacementEclipseClasspathAsMap.values());
      List&lt;String&gt; KINDS = new ArrayList&lt;String&gt;();
      KINDS.add('src');
      KINDS.add('con');
      KINDS.add('lib');
      KINDS.add('output');
      Collections.sort(replacementEclipseClasspath, new Comparator&lt;org.gradle.plugins.ide.eclipse.model.ClasspathEntry&gt;() {
  
          private int detectKindIndex(String entryKind) {
              for(int i = 0; i&lt;KINDS.size(); i++) {
                  if(KINDS[i].equals(entryKind)) {
                      return i;
                  }
              }
              return KINDS.size();
          }
  
          public int compare(org.gradle.plugins.ide.eclipse.model.ClasspathEntry entry1, org.gradle.plugins.ide.eclipse.model.ClasspathEntry entry2) {
              int kindDiff = detectKindIndex(entry1.getKind()) - detectKindIndex(entry2.getKind());
              if(kindDiff != 0) {
                  return kindDiff;
              }
              if(entry1 instanceof org.gradle.plugins.ide.eclipse.model.ProjectDependency) {
                  if(!(entry2 instanceof org.gradle.plugins.ide.eclipse.model.ProjectDependency)) {
                      return 11;
                  }
              } else if(entry2 instanceof org.gradle.plugins.ide.eclipse.model.ProjectDependency) {
                  return -1;
              }
              return entry1.path.compareTo(entry2.path);
          }
      });
      return replacementEclipseClasspath;
  }
  
  void copyLibraryFromGradleCache(String moduleId, String groupId, String artifactId, String artifactVersion, org.gradle.plugins.ide.eclipse.model.Library library, Map&lt;String, org.gradle.plugins.ide.eclipse.model.Library&gt; replacementEclipseClasspathAsMap) {
      String artifactIdAndVersion = artifactId + "-"+ artifactVersion;
      int fileSuffixIndex = -1;
      if(artifactId != null) {
          fileSuffixIndex = library.getPath().lastIndexOf(artifactIdAndVersion);
      }
      if(fileSuffixIndex &gt;= 0) {
          fileSuffixIndex += artifactIdAndVersion.length();
      } else {
          fileSuffixIndex = library.getPath().lastIndexOf(".");
      }
      if(moduleId == null || fileSuffixIndex &lt;= 0) {
          println("      non-movable library found: "+ library.getPath())
          replacementEclipseClasspathAsMap.put(moduleId, library);
      } else {
          File targetGroupFolder = null; 
          if (groupId==null || groupId.trim().length()==0) {
              targetGroupFolder = new File(globalDependenciesRepo.getAbsolutePath());
          } else {
              targetGroupFolder = new File(globalDependenciesRepo.getAbsolutePath(), groupId);
              if(!targetGroupFolder.exists()){
                  targetGroupFolder.mkdirs()
              }
          }
          String fileSuffix = library.getPath().substring(fileSuffixIndex);
          String targetFileName = artifactIdAndVersion;
          println("      target filename: "+ targetGroupFolder+ " -&gt; "+ targetFileName)
          java.nio.file.Path targetFile = java.nio.file.Paths.get(targetGroupFolder.getAbsolutePath(), targetFileName + fileSuffix);
          java.nio.file.Path sourceFile = java.nio.file.Paths.get(library.getPath());
          if(sourceFile.toFile().exists() &amp;&amp; !sourceFile.toFile().isDirectory()) {
              java.nio.file.Files.copy(sourceFile, targetFile, java.nio.file.StandardCopyOption.REPLACE_EXISTING, java.nio.file.StandardCopyOption.COPY_ATTRIBUTES);
              library.setPath(targetFile.toString());
          }
          if(library.getSourcePath() != null) {
              java.nio.file.Path sourceSourceFile = java.nio.file.Paths.get(library.getSourcePath().getPath());
              if(sourceFile.toFile().exists() &amp;&amp; !sourceFile.toFile().isDirectory()) {
                  java.nio.file.Path sourceTargetFile = java.nio.file.Paths.get(targetGroupFolder.getAbsolutePath(), targetFileName + "_source"+ fileSuffix);
                  println("      copying source file: "+ sourceSourceFile + " into "+ sourceTargetFile);
                  java.nio.file.Files.copy(sourceSourceFile, sourceTargetFile, java.nio.file.StandardCopyOption.REPLACE_EXISTING, java.nio.file.StandardCopyOption.COPY_ATTRIBUTES);
                  //println( "          TROLOLO "+ rootProject.projectDir.toPath().relativize(sourceTargetFile) );
                  library.setSourcePath(fileReferenceFactory.fromFile(sourceTargetFile.toFile()));
              }
          }
          replacementEclipseClasspathAsMap.put(moduleId + "_"+ targetFileName + fileSuffix, library);
      }
  }
  
  void explodeAarJarFiles(String moduleId, String groupId, String artifactId, String artifactVersion, org.gradle.plugins.ide.eclipse.model.Library aarLibrary, Map&lt;String, org.gradle.plugins.ide.eclipse.model.Library&gt; replacementEclipseClasspathAsMap) {
      File aarFile = new File(aarLibrary.getPath());
      println("    exploding AAR dependency: "+ aarFile.getAbsolutePath());
      File targetFolder = new File(explodedAarsDir, moduleId);
      println("      target folder: "+ targetFolder.getAbsolutePath());
      if (targetFolder.exists()) {
          println("        target folder exists. deleting ");
          project.delete(files(targetFolder))
      }
      if (!targetFolder.mkdirs()) {
          throw new RuntimeException("Cannot create folder: ${targetFolder.getAbsolutePath()}");
      }
      try {
          if(aarLibrary.getSourcePath() != null) {
              java.nio.file.Path sourceSourceFile = java.nio.file.Paths.get(aarLibrary.getSourcePath().getPath());
              if(sourceSourceFile.toFile().exists() &amp;&amp; !sourceSourceFile.toFile().isDirectory()) {
                  String sourceFileExt = sourceSourceFile.toString();
                  int extensionIndex = sourceFileExt.lastIndexOf(".");
                  if(extensionIndex &gt;= 0) {
                      sourceFileExt = sourceFileExt.substring(extensionIndex);
                  } else {
                      sourceFileExt = ".jar";
                  }
                  java.nio.file.Path sourceTargetFile = java.nio.file.Paths.get(targetFolder.toString(), moduleId+ "_source"+ sourceFileExt);
                  println("      copying source file: "+ sourceSourceFile + " into "+ sourceTargetFile);
                  java.nio.file.Files.copy(sourceSourceFile, sourceTargetFile, java.nio.file.StandardCopyOption.REPLACE_EXISTING, java.nio.file.StandardCopyOption.COPY_ATTRIBUTES);
                  aarLibrary.setSourcePath(fileReferenceFactory.fromFile(sourceTargetFile.toFile()));
              }
          }
          java.util.zip.ZipFile zipFile = new java.util.zip.ZipFile(aarFile);
          zipFile.entries().each{ fileInsideAar -&gt; 
              if (fileInsideAar.getName().endsWith(".jar")) {
                  String targetName = moduleId+ "_"+ fileInsideAar.getName().replace('/', '_').replace('\\', '_');
                  println("          jar inside aar: "+ fileInsideAar.getName());
                  println("          copying to: "+ targetName);
                  File targetFile = new File(targetFolder, targetName);
                  int index = 1;
                  while (targetFile.exists()) {
                      targetFile = new File(targetFolder, format("${targetName}_${++index}"));
                  }
                  try {
                      InputStream inputStream = zipFile.getInputStream(fileInsideAar);
                      java.nio.file.Files.copy(inputStream, targetFile.toPath());
                      org.gradle.plugins.ide.eclipse.model.Library library = new org.gradle.plugins.ide.eclipse.model.Library(fileReferenceFactory.fromFile(targetFile));
                      library.setSourcePath(aarLibrary.getSourcePath())
                      replacementEclipseClasspathAsMap.put(targetFile.getName(), library);
                  } catch (IOException e) {
                      throw new RuntimeException(
                              "Cannot write entry to file: ${e.getMessage()}: ${targetFile.getAbsolutePath()}", e);
                  }
              }
          };
      } catch (IOException e) {
          throw new RuntimeException(
                 "Cannot explode aar: ${e.getMessage()}: ${aarFile.getAbsolutePath()}", e);
      }
  }
  
  void removeGradleAttributes(Node node) {
      for(int i = 0; i&lt;node.children().size(); i++) {
          Node attrs = node.children().get(i);
          if("attributes".equals(attrs.name())) {
              for(int j = 0; j&lt;attrs.children().size(); j++) {
                  Node attr = attrs.children().get(j);
                  boolean isGradleAttr = false;
                  for(Map.Entry entry : attr.attributes().entrySet()) {
                      if(entry.key.toLowerCase().contains("gradle") || entry.value.toLowerCase().contains("gradle")) {
                          isGradleAttr = true;
                      }
                  }
                  if(isGradleAttr) {
                      attrs.remove(attr);
                      j--;
                  }
              }
              if(attrs.children().size()==0) {
                  node.remove(attrs);
              }
          }
      }
  }
  
  void mkDirIfNotExists(File file) {
      if(!file.exists()) {
          file.mkdir()
      }
  }
  
  void processConf(org.gradle.api.internal.artifacts.configurations.DefaultConfiguration cnf, String startIndent, String prefix) {
      println(startIndent + prefix + cnf.name+ ", path: "+ cnf.path)
      StringBuilder indent = new StringBuilder();
      for(int i = 0; i&lt;startIndent.length(); i++) {
          indent.append(" ");
      }
      indent.append("  ");
  
      cnf.dependencies.each{ dep -&gt;
          maskDependencyIfNeeded(indent.toString(), dep)
      }
      cnf.allDependencies.each{ dep -&gt;
          maskDependencyIfNeeded(indent.toString(), dep)
      }
  }
  
  void maskDependencyIfNeeded(String indent, org.gradle.api.internal.artifacts.dependencies.AbstractDependency dep) {
      if(dep instanceof org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency) {
          boolean needToExcludeDep = false;
          Set&lt;File&gt; maskedDepFiles = new HashSet&lt;File&gt;();
          dep.files.each{ depFile -&gt;
              File f = findMaskedFile(depFile, null, true);
              if(f != null) {
                maskedDepFiles.add(f)
                needToExcludeDep = true;
                println(indent.toString()+ "  mask dep file "+ depFile+ " -&gt; "+ f)
              }
          }
          if(needToExcludeDep) {
              project.configurations['eclipseMinusConfig'].dependencies.add(dep)
              if(!maskedDepFiles.isEmpty()) {
                  org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency newDep = new org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency(dep.targetComponentId, project.files(maskedDepFiles))
                  project.configurations['eclipsePlusConfig'].dependencies.add(newDep)
              }
          }
      }
  }
  
  File findMaskedFile(File f, String postfix, boolean initiallyUnmasked) {
    if(f != null) {
        for(Map.Entry&lt;File, File&gt; efm : eclipseFileMapping) {
             boolean masked = false;
             if(initiallyUnmasked) {
                 if(efm.key.equals(f)) {
                     masked = true;
                 }
             } else {
                 if(efm.value.equals(f)) {
                     masked = true;
                 }
             }
             if(masked) {
                 if(postfix != null) {
                     return new File(efm.value, postfix)
                 } else {
                     return efm.value;
                 }
             }
        }
        return findMaskedFile(f.parentFile, postfix==null ? f.name : f.name + File.pathSeparator+ postfix, initiallyUnmasked);
    }
    return null;
  }
  
  void dumpClassPathEntry(org.gradle.plugins.ide.eclipse.model.ClasspathEntry clspthentry) {
      if("output".equals(clspthentry.kind)) {
          // the clspthentry is instance of org.gradle.plugins.ide.eclipse.model.Output
          println("  output: "+ clspthentry.path)
      } else if("src".equals(clspthentry.kind)) {
          if(clspthentry instanceof org.gradle.plugins.ide.eclipse.model.ProjectDependency) {
               // the clspthentry is instance of org.gradle.plugins.ide.eclipse.model.ProjectDependency
               println("  project: exported="+ clspthentry.exported+ "; path="+ clspthentry.path)
          } else {
               // the clspthentry is instance of org.gradle.plugins.ide.eclipse.model.SourceFolder
               println("  src folder: "+ clspthentry.name+ " ("+ clspthentry.dir+ ") -&gt; output: "+ clspthentry.output)
               if(clspthentry.excludes != null &amp;&amp; clspthentry.excludes.size()&gt;0) {
                   println("    excludes:")
                   clspthentry.excludes.each{ excl -&gt;
                       println("      "+ excl)
                   }
               }
               if(clspthentry.includes != null &amp;&amp; clspthentry.includes.size()&gt;0) {
                   println("    includes:")
                   clspthentry.includes.each{ incl -&gt;
                       println("      "+ incl)
                   }
               }
           }
      } else if("con".equals(clspthentry.kind)) {
          //the clspthentry is instance of org.gradle.plugins.ide.eclipse.model.Container
          println("  con: exported="+ clspthentry.exported+ "; path="+ clspthentry.path)
      } else if("lib".equals(clspthentry.kind)) {
          //the clspthentry is instance of org.gradle.plugins.ide.eclipse.model.Library
          println("  lib: file="+ clspthentry.library.path)
      } else {
          println("  UNKNOWN "+ clspthentry.kind+ " -&gt; "+ clspthentry.getClass().getName())
      }
  }
  
  // Gradle adds all custom sourceSets to eclipse's linkedResources. We do not need them in eclipse project, but we do not understand how and when the source is linked.
  // So, JUST HACK IT: clear the linked resourcces after evaluating the project!
  // But gradle is such a misterious thing! just clearing does not help. We need to put something there
  // so lets put the existing linked resource, but with relative path :(
  void setupEclipseProject() {
    if(project.name.contains("-android")) {
      project.eclipse.project{
  
          linkedResource name: 'AndroidManifest.xml', type: '1', location: 'PROJECT_LOC/src/main/AndroidManifest.xml'
          linkedResource name: 'android-java', type: '2', location: 'PARENT-1-PROJECT_LOC/assets/build/android/java'
          linkedResource name: 'res', type: '2', location: 'PROJECT_LOC/src/main/res'
      }
    }
  }
  </code></pre>
  
  <p>I know that this is not the top of gradle programming, but it just works.
  The main problem with this script is that it REQUIRES all dependencies to be of type 'compile', but for Android this is obsolete. The new types of dependencies are 'api' and 'implementation'.
  Now the 'compile' is just deprecated, but I am afraid it will disappear at all.</p>
  
  <p>There is a simple and ugly solution: duplicate all non-standard dependencies with 'eclipseOnly' type of dependency. This solution is used in the script above. This works, but is really ugly because we have to modify gradle scripts in all projects. But my goal is not to touch all projects. Now the script is contained in a separate file 'eclipseHelper.gradle' and included in the root project like this:</p>
  
  <pre><code>subprojects{
     apply from: "${rootProject.projectDir}/eclipseHelper.gradle"
  }
  </code></pre>
  
  <p>What I am trying to achieve is to add android's specific types of dependencies into Eclipse. First of all, I excluded all 'release' variants from dependencies:</p>
  
  <pre><code>def androidExtension = project.extensions.findByName("android")
    if (androidExtension != null) {
        android.variantFilter { variant -&gt;
            def names = variant.flavors*.name
            def buildTypeName = variant.buildType.name
            // if buildtype is required for filtering use
            // the above field
            if (variant.name.contains("elease")) {
                variant.ignore = true
            }
        }
     }
  </code></pre>
  
  <p>secondly, I tried to add Android variant configurations into Eclipse classpath ('plusConfigurations'):</p>
  
  <pre><code>def androidExtension = project.extensions.findByName("android")
      if (androidExtension != null) {
        boolean applicationBuild = rootProject.hasProperty("applicationBuild")
        if (androidExtension.getClass().getName().contains("LibraryExtension")){
            android.libraryVariants.all { variant -&gt;
                eclipse.classpath.plusConfigurations += variant.compileConfiguration
                eclipse.classpath.plusConfigurations += variant.runtimeConfiguration
            }
        } else {
            android.applicationVariants.all { variant -&gt;
                eclipse.classpath.plusConfigurations += variant.compileConfiguration
                eclipse.classpath.plusConfigurations += variant.runtimeConfiguration
            }
        }
      }
  </code></pre>
  
  <p>We have some Android libraries among our projects, and these libraries are build with different flavors. So I got the following exception:</p>
  
  <pre><code>org.gradle.internal.component.AmbiguousVariantSelectionException: More than one variant of project :proj1-android matches the consumer a
  ttributes:
    - Configuration ':proj1-android:debugApiElements' variant android-aidl:
        - Found artifactType 'android-aidl' but wasn't required.
        - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
        - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
        - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
        - Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
    - ...
    - ...
    - Configuration ':proj1-android:debugApiElements' variant jar:
        - Found artifactType 'jar' but wasn't required.
        - Required com.android.build.api.attributes.BuildTypeAttr 'debug' and found compatible value 'debug'.
        - Found com.android.build.api.attributes.VariantAttr 'debug' but wasn't required.
        - Required com.android.build.gradle.internal.dependency.AndroidTypeAttr 'Aar' and found compatible value 'Aar'.
        - Required org.gradle.usage 'java-api' and found compatible value 'java-api'.
  </code></pre>
  
  <p>Could someone help me to make any of the solutions?</p>
  
  <ol>
  <li>select any configuration ('debug') from the set</li>
  <li>totally skip all configurations of the subproject and include it into .classpath as project dependency</li>
  </ol>
  
  <p>Thanks in advance,
  Andrei</p>