如何在AndroidStudio中连接GSON lib

时间:2017-01-03 10:00:04

标签: android gson

当我添加gradle compile 'com.google.code.gson:gson:2.2.4'时 我有这个问题

  

错误:将字节码转换为dex时出错:       原因:com.android.dex.DexException:多个dex文件定义Lcom / google / gson / JsonSerializer;

     

错误:执行失败   任务':应用程序:transformClassesWithDexForDebug'&GT。   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'
}

2 个答案:

答案 0 :(得分:0)

您需要启用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"
}

有关multidex的更多信息:

Android MultiDex Page

Configure MultiDex

答案 1 :(得分:0)

不是GSON问题,请尝试this

首先尝试清理并重建项目。

然后打开文件 - >使高速缓存/重新启动无效,单击蓝色按钮使高速缓存/重新启动无效。

如果这不起作用,请尝试此解决方案:

1-添加到您的     的build.gradle(Module.app)

defaultConfig {
    multiDexEnabled true
}

2-在依赖项中添加它:

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

2-在清单文件中添加此

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

它正确地与我合作。

我认为问题是你在Gradle中使用了很多库,你可能已经超过了64K参考限制。