我花了几天的时间发现这个问题,我想在这里发表我的发现。

我正在构建一个具有多个模块的Android应用,其中一个模块使用ANTLR插件。虽然具有ANTLR的模块可以正常构建,因为它是Java模块,但是android模块在transformClassesWithDexForDebug任务中失败:

* What went wrong:
Execution failed for task ':Android:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: Translation has been interrupted

如果我尝试在build.gradle中启用multiDexEnabled,则会收到不同的消息:
* What went wrong:
Execution failed for task ':Android:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/abego/treelayout/Configuration$AlignmentInLevel.class

这里的一些答案建议增加Java RAM来进行解编,减少诸如google-play库之类的依赖关系-我做到了,但没有喜悦。

这里去build.gradle使用的样本:

ANTLR4模块:
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 ->
        final grammarName = file.name.lastIndexOf('.')>=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 ->
        def matcher = packageRegex.matcher(line)
        if (matcher.find()){
            grammarPackage = matcher.group(1)
        }
    }
    return grammarPackage
}

android模块:
apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "<some package name here>"
        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
    ...
}

根build.gradle:
buildscript {
    repositories {
        jcenter()
    mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
    }
}

分析依赖关系(gradle dependencies)表明,问题在于antlr gradle插件通过用于编译的org.abego.treelayout库和antlr4-runtime都包括antlr4类:
+--- 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

再次-问题在于org.abego.treelayout类既存在于org.antlr:antlr4:4.5中,又存在于org.antlr:antlr4-runtime:4.5中,甚至可能存在于org.abego.treelayout:org.abego.treelayout.core:1.0.1中。我猜这是使用grat的antlr4插件的错误-他们错误地在主程序包中添加了这些类,而应该只将它们放在依赖中。可能我应该将错误提交给插件跟踪器。

而且,antlr4插件甚至添加了antlr3-runtime依赖关系,该依赖关系也可以重新定义org.abego.treelayout类(因此我也将其排除在外)。

虽然Java应用程序接受Java类的额外副本(我相信它们会从类路径中获取第一个副本),但android插件在上述dex阶段报告错误中失败。

最佳答案

我的解决方案是删除重复的依赖项:

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' // just in case :-)
   }
    compile ('com.yuvalshavit:antlr-denter:1.0') {
       // denter library also has dependency on antlr4-runtime ...
       exclude group: 'org.antlr', module:'antlr4-runtime'
    }
    testCompile group: 'junit', name: 'junit', version:'4.11'
}
...

我愿意寻求任何更好的解决方案-我对gradle不太满意。

10-07 17:10