Android库项目com.android.dex.DexException:多个dex文件定义Lcom/google/gson/JsonSerializer

Noa*_*gev 9 dependencies android android-gradle-plugin aar

我正在Android Studio中编写一个库项目.我的gradle文件包括gson volley play-services等...
当我将库嵌入项目中时,我得到:

com.android.dex.DexException:多个dex文件定义Lcom/google/gson/JsonSerializer;

有人可以解释创建库项目时gradle的工作原理吗?
我应该如何解释集成我的SDK的开发人员,排除模块如何工作,以及为什么它不适用于包含我的aar的应用程序?

THA*_*rum 7

有两种情况需要考虑

  • 首先,如果你已经在app/build.gradle中声明了它,请考虑删除Gson

    依赖项{compile'c​​om.google.code.gson:gson:2.4'}

  • 其次,如果你还没有在你的声明中app/build.gradle 你可能需要调查哪些库有重复声明Gson依赖.然后你可以从该库中排除Gson.您可能需要检查此排除传递依赖项

在这里,我将提供一个appcompat-v7从库中排除的示例

运行此命令以查看依赖关系图表树

./gradlew app:dependencies
Run Code Online (Sandbox Code Playgroud)

它将显示依赖关系树,如下面的示例所示

|    \--- com.mikepenz:materialdrawer:4.6.3
|         +--- com.android.support:appcompat-v7:23.1.1 (*)
|         +--- com.android.support:recyclerview-v7:23.1.1 (*)
|         +--- com.mikepenz:materialize:0.5.1
|         |    \--- com.android.support:appcompat-v7:23.1.1 (*)
|         +--- com.mikepenz:iconics-core:2.5.3
|         |    \--- com.android.support:appcompat-v7:23.1.1 (*)
|         \--- com.android.support:support-annotations:23.1.1
Run Code Online (Sandbox Code Playgroud)

找到库后声明重复依赖.你可以开始排除它.

dependencies {
    compile("com.mikepenz:materialdrawer:4.6.3") {
        exclude module: 'appcompat-v7'
    }
}
Run Code Online (Sandbox Code Playgroud)