Android Twitter Hard SDK conflicts with Google GSON

I find it difficult to integrate the Twitter Fabric SDK into my application. I followed the twitter steps and everything was fine, but when I try to build my project using gradle, I get this error:

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) 

I tried to remove my static Gson library from the folder of my application module, and after that everything went well. Same thing when deleting a line that adds twitter sdk from the dependencies of the gradle module, so I'm sure there is some conflict between these two conflicts, and I'm looking for it.

Any help would be greatly appreciated!

In case this can be useful for people, here is my gradle file of the application module:

 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; } } 
+7
android gson twitter gradle fabric-twitter
source share
2 answers

I am an Android developer on Twitter.

Try running ./gradlew app:dependencies to see all the dependencies of your gradle tasks. Your compilation dependencies will indicate the following:

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

which shows that GSON is actually used by Fabric internally.

My recommendation would be to remove GSON from your libs / folder and add it as a direct dependency in your build.gradle, rather than excluding it from the Twitter escrow schedule. You can also use the dependency resolution mechanisms in the build tool.

 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. } 
+11
source share

The SDK fabric should use Gson internally, so if it is exposed, you can just use it and also remove Gson from your / libs /.

Alternatively import both libraries as dependencies in Gradle, and then you can exclude Gson from Fabric.

Exclude gson from Fabric as follows:

 compile('com.twitter.sdk.android:twitter: 1.0.0@aar ') { transitive = true; exclude module: 'gson' } 
+10
source share

All Articles