简体   繁体   English

由于多个dex文件,Android Gradle构建失败

[英]Android Gradle build fails due to Multiple dex files

I had Android Gradle project with number of external dependencies. 我有很多外部依赖项的Android Gradle项目。 At some point of time I have decided to extract some functionality to separate library together with some dependencies, which I have packaged using maven-assembly-plugin . 在某个时间点,我决定提取一些功能以将库与某些依赖项分离开来,这些依赖项已使用maven-assembly-plugin打包。 Then I am including my newly created library as a dependency + remove some of the dependencies already bundled in my library. 然后,我将新创建的库作为依赖项包括在内+删除库中已经捆绑的一些依赖项。

The problem is, while building I am getting this error, which I do not understand: 问题是,在构建时出现此错误,我不理解:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/squareup/okhttp/Address;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
    at com.android.dx.command.dexer.Main.run(Main.java:230)
    at com.android.dx.command.dexer.Main.main(Main.java:199)
    at com.android.dx.command.Main.main(Main.java:103)

This is one of the dependencies (okhttp) previously included directly and right now packaged with my new library. 这是以前直接包含的依赖项(okhttp)之一,现在打包在我的新库中。 Library I have created can be found here . 我创建的库可以在这里找到。

Here are my build files: 这是我的构建文件:

Android app build config: Android应用程式建立设定:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
    mavenRepo urls: "https://github.com/mkorszun/okrest/raw/master/repo"
}

android {
    compileSdkVersion 15
    buildToolsVersion "19.0.2"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 15
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
    }

}

dependencies {
    compile 'com.android.support:appcompat-v7:19.0.1'
//    compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.11'
//    compile 'com.sun.jersey:jersey-client:1.8'
//    compile 'com.sun.jersey:jersey-core:1.8'
//    compile 'com.squareup.okhttp:okhttp:1.2.1'
//    compile 'com.google.guava:guava:15.0'
    compile 'com.google.android.gms:play-services:4.0.30'
    compile files('libs/GraphView-3.0.jar')
    compile 'com.okrest:okrest:0.0.3'
}

Library build config: 库构建配置:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.okrest</groupId>
    <artifactId>okrest</artifactId>
    <version>0.0.3</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <distributionManagement>
        <repository>
            <id>local.repo</id>
            <name>Internal Repository</name>
            <url>file://repo</url>
        </repository>
    </distributionManagement>

    <dependencies>

        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.13</version>
        </dependency>

        <dependency>
            <groupId>com.sun.jersey</groupId>
            <artifactId>jersey-client</artifactId>
            <version>1.18.1</version>
        </dependency>

        <dependency>
            <groupId>com.squareup.okhttp</groupId>
            <artifactId>okhttp</artifactId>
            <version>1.5.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.github.rest-driver</groupId>
            <artifactId>rest-client-driver</artifactId>
            <version>1.1.37</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <format>xml</format>
                    <maxmem>256m</maxmem>
                    <aggregate>true</aggregate>
                    <check/>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.eluder.coveralls</groupId>
                <artifactId>coveralls-maven-plugin</artifactId>
                <version>2.1.0</version>
            </plugin>

        </plugins>
    </build>

    <reporting>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.6</version>
            </plugin>
        </plugins>
    </reporting>

</project>

And here is dependency tree for my android app: 这是我的android应用程序的依赖树:

+--- com.android.support:appcompat-v7:19.0.1
|    \--- com.android.support:support-v4:19.0.1
+--- com.google.android.gms:play-services:4.0.30
|    \--- com.android.support:support-v4:13.0.0 -> 19.0.1
\--- com.okrest:okrest:0.0.3
     +--- org.codehaus.jackson:jackson-mapper-asl:1.9.13
     |    \--- org.codehaus.jackson:jackson-core-asl:1.9.13
     +--- com.sun.jersey:jersey-client:1.18.1
     \--- com.squareup.okhttp:okhttp:1.5.0

What it means is that that class is getting included in the APK twice, which isn't allowed. 这意味着该类被两次包含在APK中,这是不允许的。 It must be that you have two dependency paths to that library. 必须是您具有该库的两个依赖路径。 Examine them and remove the redundant one. 检查它们并删除多余的一个。 If you're having trouble finding it, then attach your build files to your question. 如果您找不到它,则将构建文件附加到您的问题中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM