mkorszun
mkorszun

Reputation: 4571

Android Gradle build fails due to Multiple dex files

I had Android Gradle project with number of external dependencies. 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. 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. Library I have created can be found here.

Here are my build files:

Android app build config:

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:

+--- 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

Upvotes: 2

Views: 5660

Answers (1)

Scott Barta
Scott Barta

Reputation: 80010

What it means is that that class is getting included in the APK twice, which isn't allowed. 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.

Upvotes: 4

Related Questions