gpt4 book ai didi

android - Android 应用程序中的 Gradle ANTLR4 插件 : build fails

转载 作者:行者123 更新时间:2023-12-03 06:05:16 33 4
gpt4 key购买 nike

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

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

如果我尝试启用 multiDexEnabled在 build.gradle 我得到不同的信息:
* 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 库之类的依赖项——我这样做了,但并不高兴。

这里 go 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
}

安卓模块:
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 上课用于编译的库和 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.treelayout类都存在于 org.antlr:antlr4:4.5 , org.antlr:antlr4-runtime:4.5甚至可能在 org.abego.treelayout:org.abego.treelayout.core:1.0.1 .我猜这是 antlr4 gradle 插件的错误 - 他们错误地将这些类添加到主包中,而它们应该只在依赖项中。可能我应该向插件跟踪器提交一个错误。

此外,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。

关于android - Android 应用程序中的 Gradle ANTLR4 插件 : build fails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39233091/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com