0

When I add in gradle compile 'com.google.code.gson:gson:2.2.4' I have this problem

Error:Error converting bytecode to dex: Cause: com.android.dex.DexException: Multiple dex files define Lcom/google/gson/JsonSerializer;

Error: Execution failed for task':app:transformClassesWithDexForDebug'.> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: java.lang.UnsupportedOperationException

build.gradle

apply plugin: 'com.android.application'

android {
 compileSdkVersion 22
 buildToolsVersion "22.0.0"
 defaultConfig {
    applicationId "com.jsonmyapp.ars.gson_4"
    minSdkVersion 14
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
 buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
 compile 'com.android.support:appcompat-v7:22.0.0'
 testCompile 'junit:junit:4.12'

 compile 'com.google.code.gson:gson:2.2.4'
}
Ankita Shah
  • 1,866
  • 16
  • 31
sebastjan
  • 1
  • 1

2 Answers2

0

You need to enable multiDex

defaultConfig {
   // Enabling multidex support.
   multiDexEnabled true
}

// also add this in your android block. This is use for multidex

dexOptions {
    preDexLibraries = false
    incremental true
    javaMaxHeapSize "4g"
}

More info on multidex:

Android MultiDex Page

Configure MultiDex

Bhoomika Patel
  • 1,895
  • 1
  • 13
  • 30
vanlooverenkoen
  • 2,121
  • 26
  • 50
0

its not GSON problem, try this

First try this to clean and rebuild your project.

Then open File --> Invalidate Caches / Restart , Click on the blue button Invalidate Caches / Restart.

If this didn't work with u try this solution:

1- add to your build.gradle(Module.app)

defaultConfig {
    multiDexEnabled true
}

2- add this in dependencies:

dependencies {
compile 'com.android.support:multidex:1.0.1'
}

2- In manifest file add this

<application android:name="android.support.multidex.MultiDexApplication">

It works with me Correctly.

And I think the Problem is that you are using a lot of Libraries in the Gradle ,You might have exceeded the 64K Reference Limit.

Community
  • 1
  • 1
Malik Abu Qaoud
  • 208
  • 1
  • 9