Android应用中的Gradle ANTLR4插件:构建失败

时间:2016-08-30 16:53:48

标签: android gradle antlr antlr4

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

我正在构建一个包含多个模块的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

这里的一些答案建议增加用于dexing的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
    ...
}

root build.gradle:

buildscript {
    repositories {
        jcenter()
    mavenCentral()
    }

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

分析依赖关系(gradle dependencies)表明问题在于antlr gradle插件包括用于编译的org.abego.treelayout库的antlr4类,以及antlr4-runtime:

+--- 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.treelayoutorg.antlr:antlr4:4.5以及甚至org.antlr:antlr4-runtime:4.5中都存在org.abego.treelayout:org.abego.treelayout.core:1.0.1类。我猜这是一个用于gradle的antlr4插件的错误 - 他们错误地在主包中添加了这些类,而它们应该仅仅依赖于依赖。可能我应该向插件跟踪器提交一个错误。

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

虽然Java应用程序接受额外的java类副本(他们从类路径中取出第一个我认为),但android插件在上面提到的dex阶段报告错误中失败。

1 个答案:

答案 0 :(得分:0)

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

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不好。