Home » Android » Android Twitter Fabric SDK conflict with Google GSON

Android Twitter Fabric SDK conflict with Google GSON

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m having difficulties integrating Twitter Fabric SDK to my app. I followed the steps of the Twitter tutorial and everyhting went fine but when I try to build my project with 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 removing my static Gson library from my app module lib folder and everything went fine afterwards. Same when removing the line adding twitter sdk from the gradle module dependencies so I’m pretty sure there is some kind of conflict between these two and I’m looking to resolve it.

Any help would be greatly appreciated !

In case it could be of any use to people out there here is my gradle app module file :

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:[email protected]') {
        transitive = true;
    }
}
How to&Answers:

I’m a Twitter Android developer.

Try running ./gradlew app:dependencies to see all the dependencies of your gradle tasks. Your compile dependencies will include something like:

+--- 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 indeed 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 instead of excluding it from the Twitter deps graph. You may as well take advantage of 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:[email protected]') {
        transitive = true;
    }
    compile 'com.google.code.gson:gson:2.2.4'  // Added.
}

Answer:

The fabric SDK must be using Gson internally, therefore if it is exposed you can just use this as well and remove Gson from your /libs/ .

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

Exclude gson from Fabric like this:

compile('com.twitter.sdk.android:twitter:[email protected]') { 
       transitive = true; 
       exclude module: 'gson' 
}