我有Android Gradle项目,其中包含许多外部依赖项。在某些时候,我决定提取一些功能,将库与一些依赖项分开,我使用 maven-assembly-plugin 打包。然后我将我新创建的库包含为依赖项+删除已经捆绑在我的库中的一些依赖项。
问题是,在构建时我收到此错误,我不明白:
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)
这是以前直接包含的依赖项之一(okhttp),现在与我的新库一起打包。我创建的图书馆可以找到here。
以下是我的构建文件:
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'
}
库构建配置:
<?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>
这是我的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
答案 0 :(得分:4)
这意味着该类被包含在APK中两次,这是不允许的。必须是您有两个到该库的依赖路径。检查它们并删除多余的一个。如果您在查找时遇到问题,请将构建文件附加到您的问题中。