【问题标题】:How to connect GSON lib in AndroidStudio如何在 Android Studio 中连接 GSON 库
【发布时间】:2023-03-09 18:51:01
【问题描述】:

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

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

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

【问题讨论】:

标签: android gson


【解决方案1】:

不是GSON问题,试试this

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

然后打开 File --> Invalidate Caches / Restart ,点击蓝色按钮 Invalidate Caches / Restart。

如果这对你不起作用,请尝试以下解决方案:

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 参考限制。

【讨论】:

    【解决方案2】:

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

    更多关于多索引的信息:

    Android MultiDex Page

    Configure MultiDex

    【讨论】:

      猜你喜欢
      • 2016-08-31
      • 2020-03-24
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 2021-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多