Android Gradle几个模块:transformDexArchiveWithDexMergerForDebug任务失败

时间:2018-04-14 13:31:26

标签: android gradle kotlin android-gradle gradlew

不久前,我为我的应用程序编写了一个库,其中包含2个模块:样本和库本身。

与往常一样,库模块包含在samples模块的build.gradle中,尽管它们都位于层次结构的同一级别,就在根目录之后:

----root
    ---samples
    ---tinyarbrowser

在第一次发布后,我使用了几周没有问题的项目。

昨晚我试图建立&运行 在轻微更新之前。所以......整个晚上都花在试图弄明白:) 主要问题是:

:samples:transformDexArchiveWithDexMergerForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':samples:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: 
    com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: 
        D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\17, 
        D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\18, 
        D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\externalLibsDexMerger\debug\0, 
        D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\16.jar
//...

每当我希望尝试通过微小的改变来清理构建项目时,就会发生这种情况,目前没有任何帮助。

当前的gradle文件如下所示:

Root build.gradle

buildscript {
    ext.kotlinVersion = '1.2.30'
    ext.ankoVersion = '0.10.2'
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

allprojects {
    repositories {
        jcenter()
        google()
    }
}

ext {
    compileSdkVersion = 27
    buildToolsVersion = '27.0.3'
    minSdkVersion = 15
    targetSdkVersion = compileSdkVersion

    supportLibVersion = '27.0.2'

    supportDependencies = [
            design           :         "com.android.support:design:${supportLibVersion}",
            appCompat        :         "com.android.support:appcompat-v7:${supportLibVersion}",
    ]

    versionCode = 13
    versionName = '1.3'
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

示例 samples 模块 build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'project-report'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        applicationId "com.kkaun.tinyarbrowser"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    testImplementation 'junit:junit:4.12'

    //Including project lib module here
    api (project(':tinyarbrowser')) {
        exclude group: 'com.android.support', module: 'design'
        exclude group: 'com.android.support', module: 'appcompat-v7'
        exclude group: 'com.android.support', module: 'appcompat-v4'
        exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jre8'
        exclude group: 'org.jetbrains.anko', module: 'anko'
    }

    implementation ('com.android.support:appcompat-v7:27.0.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion"
}


//tasks.whenTaskAdded {task ->
//    if(task.name.contains("transformDexArchiveWithDexMergerForDebug")) {
//        task.enabled = false
//    }
//}

lib tinyarbrowser 模块 build.gradle

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.github.dcendents.android-maven'
group = 'com.github.kkaun'
version = rootProject.ext.versionName

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    implementation supportDependencies.design
    implementation supportDependencies.appCompat

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion"
    implementation "org.jetbrains.anko:anko:$ankoVersion"
}

由于我目前只使用了样本模块,因此这是 samples依赖树输出,尽可能轻量化:

+--- project :tinyarbrowser
+--- com.android.support:appcompat-v7:27.0.2
|    +--- com.android.support:support-core-utils:27.0.2
|    |    \--- com.android.support:support-compat:27.0.2
|    |         \--- android.arch.lifecycle:runtime:1.0.3
|    |              +--- android.arch.lifecycle:common:1.0.3
|    |              \--- android.arch.core:common:1.0.0
|    +--- com.android.support:support-fragment:27.0.2
|    |    +--- com.android.support:support-compat:27.0.2 (*)
|    |    +--- com.android.support:support-core-ui:27.0.2
|    |    |    \--- com.android.support:support-compat:27.0.2 (*)
|    |    \--- com.android.support:support-core-utils:27.0.2 (*)
|    +--- com.android.support:support-vector-drawable:27.0.2
|    |    \--- com.android.support:support-compat:27.0.2 (*)
|    \--- com.android.support:animated-vector-drawable:27.0.2
|         +--- com.android.support:support-vector-drawable:27.0.2 (*)
|         \--- com.android.support:support-core-ui:27.0.2 (*)
+--- com.android.support.constraint:constraint-layout:1.0.2
|    \--- com.android.support.constraint:constraint-layout-solver:1.0.2
\--- org.jetbrains.kotlin:kotlin-stdlib-jre8:1.2.30
     +--- org.jetbrains.kotlin:kotlin-stdlib:1.2.30
     |    \--- org.jetbrains:annotations:13.0
     \--- org.jetbrains.kotlin:kotlin-stdlib-jre7:1.2.30
          \--- org.jetbrains.kotlin:kotlin-stdlib:1.2.30 (*)

我尝试了什么:

  • 使用许多排除项和次要的传递依赖项来清理它 变化;

  • 使缓存/重启/清除本地缓存文件无效;

  • implementation vs api vs compile;

  • 针对小型lib的Multidex?:)试图确保,一切都一样;

  • 将构建版本从3.1降级到3.0(D8与DX编译器):在这种情况下,即使除了重要的Kotlin之外的所有依赖项,我都收到了UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define;

  • 请参阅以下内容:

实际上,我90%肯定会对Kotlin有所了解,因为它的组件在两个模块中都被广泛使用,这是我不能做的最后一件事。 让我自己没有,所以我试图从样本模块中排除一切,除了Kotlin std' s。 所以我也试过这个"脏兮兮的伎俩":

tasks.whenTaskAdded {task ->
    if(task.name.contains("transformDexArchiveWithDexMergerForDebug")) {
        task.enabled = false
    }
}

是的,那个版本实际上完成了,但是每次尝试在小米Redmi 4X上运行它时我都面临另一个问题:

Installation failed with message Failed to finalize session : INSTALL_FAILED_INVALID_APK: 
Package couldn't be installed in /data/app/com.kkaun.tinyarbrowser-1: Package /data/app/com.kkaun.tinyarbrowser-1/base.apk code is missing.
It is possible that this issue is resolved by uninstalling an existing version of the apk if it is present, and then re-installing.

至于这个问题,我尝试禁用/重新启用Instant Run,关闭MIUI特定的开发功能,重新启动等等 - 它仍然是相同的。

出于选择,真实。但是,单个lib模块的项目结构改变才能正常运行。

任何有建设性的想法都将受到赞赏。

编辑:

下面提供的关键问题的完整堆栈跟踪:

> Task :samples:transformDexArchiveWithDexMergerForDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':samples:transformDexArchiveWithDexMergerForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: D:\AndroidStudioPr
ojects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\17, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\18, D:\AndroidStudioProjects\TinyARBrows
er\samples\build\intermediates\transforms\externalLibsDexMerger\debug\0, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\16.jar

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':samples:transformDexArchiveWithDexMergerForDebug'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:100)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70)
        at org.gradle.api.internal.tasks.execution.OutputDirectoryCreatingTaskExecuter.execute(OutputDirectoryCreatingTaskExecuter.java:51)
        at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62)
        at org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:60)
        at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:97)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:87)
        at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54)
        at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43)
        at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241)
        at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:123)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:79)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:104)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:98)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:626)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:581)
        at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:98)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
        at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
Caused by: java.lang.RuntimeException: com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while mer
ging dex archives: D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\17, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\18,
 D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\externalLibsDexMerger\debug\0, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\16.jar
        at com.android.builder.profile.Recorder$Block.handleException(Recorder.java:55)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:104)
        at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:212)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73)
        at org.gradle.api.internal.project.taskfactory.IncrementalTaskAction.doExecute(IncrementalTaskAction.java:46)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:39)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:26)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:121)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:199)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:110)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:110)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92)
        ... 29 more
Caused by: com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: D:\Androi
dStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\17, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\18, D:\AndroidStudioProjects\Ti
nyARBrowser\samples\build\intermediates\transforms\externalLibsDexMerger\debug\0, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\16.jar
        at com.android.build.gradle.internal.transforms.DexMergerTransform.transform(DexMergerTransform.java:225)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:221)
        at com.android.build.gradle.internal.pipeline.TransformTask$2.call(TransformTask.java:217)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102)
        ... 41 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermed
iates\transforms\dexBuilder\debug\17, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\18, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\e
xternalLibsDexMerger\debug\0, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\16.jar
        at com.google.common.collect.ImmutableList.forEach(ImmutableList.java:397)
        at com.android.build.gradle.internal.transforms.DexMergerTransform.transform(DexMergerTransform.java:221)
        ... 44 more
Caused by: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\
debug\17, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\18, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\externalLibsDexMerger\debug\0
, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\16.jar
Caused by: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\17, D:\AndroidStudioPr
ojects\TinyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\18, D:\AndroidStudioProjects\TinyARBrowser\samples\build\intermediates\transforms\externalLibsDexMerger\debug\0, D:\AndroidStudioProjects\T
inyARBrowser\samples\build\intermediates\transforms\dexBuilder\debug\16.jar
        at com.android.builder.dexing.D8DexArchiveMerger.getExceptionToRethrow(D8DexArchiveMerger.java:124)
        at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:109)
        at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:101)
        at com.android.build.gradle.internal.transforms.DexMergerTransformCallable.call(DexMergerTransformCallable.java:36)
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
        at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:76)
        at com.android.tools.r8.utils.ExceptionUtils.withD8CompilationHandler(ExceptionUtils.java:45)
        at com.android.tools.r8.D8.run(D8.java:88)
        at com.android.builder.dexing.D8DexArchiveMerger.mergeDexArchives(D8DexArchiveMerger.java:107)
        ... 2 more
Caused by: com.android.tools.r8.utils.AbortException
        at com.android.tools.r8.utils.Reporter.failIfPendingErrors(Reporter.java:77)
        at com.android.tools.r8.utils.Reporter.fatalError(Reporter.java:58)
        at com.android.tools.r8.utils.ExceptionUtils.withCompilationHandler(ExceptionUtils.java:67)
        ... 5 more

BUILD FAILED in 35s

1 个答案:

答案 0 :(得分:2)

对于这个项目,构建工具最终会创建两个不同的BuildConfig.java文件 - 一个在samples模块中,另一个在tinyarbrowser模块中。它们都具有相同的com.kkaun.tinyarbrowser包。因为它们都在同一个包中,所以构建工具在合并期间会失败。

每个模块中的AndroidManifest.xml文件指示相应的BuildConfig类的位置,因此我们需要更改其中一个模块中的包,以便它们不会发生冲突。

例如,您可以从以下位置更新samples目录中的清单文件:

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.kkaun.tinyarbrowser">

到此:

<manifest 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.kkaun.tinyarbrowser.samples">

这也意味着你需要改变一些课程。 samples模块中当前位于com.kkaun.tinyarbrowser下的类需要移至com.kkaun.tinyarbrowser.samples

此外,还需要移动对资源的引用。例如,如果您这样做:

import com.kkaun.tinyarbrowser.R

然后需要更新到这个

import com.kkaun.tinyarbrowser.samples.R

可能还有一些其他需要更新的小型参考文献,但这应该可以帮到你。