title: Gradle ANTLR4 plugin in Android app: build fails
body: <p>I spent few days spotting this issue, and I'd like to post my finding here.</p>
  
  <p>I'm building an Android app with several modules, one of them using ANTLR plugin. While the module with ANTLR builds OK, as it is a Java module, the android module fails in <code>transformClassesWithDexForDebug</code> task:</p>
  
  <pre><code>* What went wrong:
  Execution failed for task ':Android:transformClassesWithDexForDebug'.
  &gt; com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: Translation has been interrupted
  </code></pre>
  
  <p>If I try to enable <code>multiDexEnabled</code> in build.gradle I get a different message:</p>
  
  <pre><code>* What went wrong:
  Execution failed for task ':Android:transformClassesWithJarMergingForDebug'.
  &gt; com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/abego/treelayout/Configuration$AlignmentInLevel.class
  </code></pre>
  
  <p>Some answers here suggests increasing Java RAM for dexing, reducing dependencies such as google-play library - I did that but no joy.</p>
  
  <p>Here go build.gradle samples used:</p>
  
  <p>ANTLR4 module:</p>
  
  <pre><code>apply plugin: 'antlr'
  apply plugin: 'java'
  apply plugin: 'maven'
  
  sourceCompatibility = 1.7
  targetCompatibility = 1.7
  
  final GENERATED_MAIN = "src-gen/main/java"
  final GENERATED_TEST = "src-gen/test/java"
  
  sourceSets {
      main {
          java { srcDirs += [GENERATED_MAIN] }
      }
      main {
          java { srcDirs += [GENERATED_TEST] }
      }
  }
  
  repositories {
       mavenCentral()   
  }
  
  dependencies {
     antlr("org.antlr:antlr4:4.5") {
         exclude group: 'org.antlr', module:'antlr-runtime'
  //       exclude group: 'org.antlr', module:'antlr4-runtime'
         exclude group: 'org.antlr', module:'ST4'
     }
      compile ('com.yuvalshavit:antlr-denter:1.0') {
         exclude group: 'org.antlr', module:'antlr4-runtime'
      }
      testCompile group: 'junit', name: 'junit', version:'4.11'
  }
  
  generateGrammarSource.doFirst {
      outputDirectory = new File(GENERATED_MAIN)
  }
  generateGrammarSource.doLast {
      moveAntlrGeneratedFilesToTheirPackages(source, GENERATED_MAIN)
  }
  generateTestGrammarSource.doFirst {
      outputDirectory = new File(GENERATED_TEST)
  }
  generateTestGrammarSource.doLast {
      moveAntlrGeneratedFilesToTheirPackages(source, GENERATED_TEST)
  }
  
  def moveAntlrGeneratedFilesToTheirPackages(FileTree grammarFiles, generatedFolder) {
      grammarFiles.each {File file -&gt;
          final grammarName = file.name.lastIndexOf('.')&gt;=0 ? file.name[0 .. file.name.lastIndexOf('.')-1] : file.name
          final grammarPackage = extractPackageNameFromGrammerFile(file)
          copy {
              from generatedFolder
              include "${grammarName}*.*"
              into generatedFolder + "/" + grammarPackage.replaceAll("\\.", "/")
          }
      }
      project.delete fileTree(generatedFolder).include('*.*')
  }
  
  def extractPackageNameFromGrammerFile(File grammarFile) {
      def grammarPackage = "unknown.package"
      def packageRegex = ~/[ ]*package[ ]*([a-zA-Z]+[a-zA-Z0-9.-_]*)[ ]*;/
      grammarFile.eachLine { line -&gt;
          def matcher = packageRegex.matcher(line)
          if (matcher.find()){
              grammarPackage = matcher.group(1)
          }
      }
      return grammarPackage
  }
  </code></pre>
  
  <p>android module:</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 23
      buildToolsVersion "23.0.2"
  
      defaultConfig {
          applicationId "&lt;some package name here&gt;"
          minSdkVersion 14
          targetSdkVersion 22
  //        multiDexEnabled true
      }
  
  
      dexOptions {
  //        javaMaxHeapSize "4g"
      preDexLibraries = false
      }
  
      lintOptions {
            abortOnError false
      }
  
      sourceSets {
          main {
              manifest.srcFile 'AndroidManifest.xml'
              java.srcDirs = ['src']
              resources.srcDirs = ['src']
              aidl.srcDirs = ['iadl']
              renderscript.srcDirs = ['src']
              res.srcDirs = ['res']
              assets.srcDirs = ['assets']
          }
      }
  
  }
  
  dependencies {
      compile project(':texas-board')   // this references the ANTLR module mentioned above
      ...
  }
  </code></pre>
  
  <p>root build.gradle:</p>
  
  <pre><code>buildscript {
      repositories {
          jcenter()
      mavenCentral()
      }
  
      dependencies {
          classpath 'com.android.tools.build:gradle:2.1.2'
      }
  }
  </code></pre>
  
  <p>analyzing dependencies (<code>gradle dependencies</code>) shows that the problem is that antlr gradle plugin includes <code>org.abego.treelayout</code> classes both via <code>antlr4</code> library used for compilation, and antlr4-runtime:</p>
  
  <pre><code>+--- project :texas-board
  |    \--- project :rules_engine
  |         +--- org.antlr:antlr4:4.5
  |         |    +--- org.antlr:antlr4-runtime:4.5
  |         |    |    \--- org.abego.treelayout:org.abego.treelayout.core:1.0.1
  |         |    \--- org.antlr:antlr-runtime:3.5.2
  |         \--- com.yuvalshavit:antlr-denter:1.0
  </code></pre>
  
  <p>once again - the matter is that the <code>org.abego.treelayout</code> classes are present both in <code>org.antlr:antlr4:4.5</code>, <code>org.antlr:antlr4-runtime:4.5</code> and probably even in <code>org.abego.treelayout:org.abego.treelayout.core:1.0.1</code>. I guess that it is a bug with antlr4 plugin for gradle - they wrongly added those classes in main package while they are supposed to be in dependencies only. Probably I should submit a bug to the plugin tracker.</p>
  
  <p>Moreover, antlr4 plugin even adds antlr3-runtime dependency which may redefine <code>org.abego.treelayout</code> classes as well (so I also excluded it).</p>
  
  <p>While Java apps accept extra copies of java classes (they take the first one from classpath I believe), android plugin fails in dex stage reporting errors mentioned above.</p>