我在将Twitter Fabric SDK集成到我的应用程序时遇到了困难.我按照
Twitter tutorial的步骤进行操作,但是当我尝试用gradle构建我的项目时,我得到了这个错误:
Error:Execution Failed for task ':app:dexDebug'. > com.android.ide.common.internal.LoggedErrorException: Failed to run command: D:\Android\android-studio\sdk\build-tools\android-4.4W\dx.bat --dex --output ... Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Lcom/google/gson/JsonSerializer; 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)
我尝试从我的app模块lib文件夹中删除我的静态Gson库,之后一切都很顺利.删除从gradle模块依赖项添加twitter sdk的行时也一样,所以我很确定这两者之间存在某种冲突,我正在寻求解决它.
任何帮助将不胜感激 !
如果它对人们有任何用处,这里有我的gradle app模块文件:
apply plugin: 'com.android.application' apply plugin: 'io.fabric' android { ... buildTypes { release { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' } debug { signingConfig signingConfigs.debug ext.enableCrashlytics = false } } } repositories { jcenter() mavenCentral() maven{ url 'https://maven.fabric.io/repo'} } dependencies { compile fileTree(dir: 'libs',include: ['*.jar']) compile project(':facebook') compile 'com.android.support:support-v13:20.0.0' compile 'com.google.android.gms:play-services:6.1.11' compile('com.twitter.sdk.android:twitter:1.0.0@aar') { transitive = true; } }
解决方法
我是Twitter Android开发人员.
尝试运行./gradlew app:dependencies以查看gradle任务的所有依赖项.您的编译依赖项将包括以下内容:
+--- com.twitter.sdk.android:twitter-core:1.0.0 | | +--- com.squareup.retrofit:retrofit:1.6.1 | | | \--- com.google.code.gson:gson:2.2.4 | | +--- com.google.code.gson:gson:2.2.4 | | \--- io.fabric.sdk.android:fabric:1.0.0
这表明GSON确实被Fabric内部使用.
我的建议是从你的libs /文件夹中删除GSON,并将其作为直接依赖项添加到build.gradle中,而不是将其从Twitter deps图中排除.您也可以利用构建工具中的依赖项解析机制.
dependencies { compile fileTree(dir: 'libs',include: ['*.jar']) // Maybe remove this. compile project(':facebook') compile 'com.android.support:support-v13:20.0.0' compile 'com.google.android.gms:play-services:6.1.11' compile('com.twitter.sdk.android:twitter:1.0.0@aar') { transitive = true; } compile 'com.google.code.gson:gson:2.2.4' // Added. }