Android 无法在单个dex文件中容纳请求的类(#方法:72443>;65536)

Android 无法在单个dex文件中容纳请求的类(#方法:72443>;65536),android,gson,Android,Gson,我在构建中遇到了这个错误“无法在单个dex文件中容纳请求的类(#methods:72443>65536)”我试图添加IBMChatbot,但我遇到了这个错误 我试图将IBMChatbot包含到应用程序中,并尝试实现gson,但它仍然不起作用 渐变构建:模块 apply plugin: 'com.android.application' ext { bintrayRepo = 'maven' bintrayName = 'chat-message-view' publ

我在构建中遇到了这个错误“无法在单个dex文件中容纳请求的类(#methods:72443>65536)”我试图添加IBMChatbot,但我遇到了这个错误

我试图将IBMChatbot包含到应用程序中,并尝试实现gson,但它仍然不起作用

渐变构建:模块

apply plugin: 'com.android.application'


ext {
    bintrayRepo = 'maven'
    bintrayName = 'chat-message-view'

    publishedGroupId = 'me.himanshusoni.chatmessageview'
    libraryName = 'chat-message-view'
    artifact = 'chat-message-view'

    libraryDescription = 'Android library to create chat message view easily'

    siteUrl = 'https://github.com/himanshu-soni/ChatMessageView'
    gitUrl = 'https://github.com/himanshu-soni/ChatMessageView.git'

    libraryVersion = '1.0.7'

    developerId = 'himanshu-soni'
    developerName = 'Himanshu Soni'
    developerEmail = 'himanshusoni.me@gmail.com'

    licenseName = 'The Apache Software License, Version 2.0'
    licenseUrl = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
    allLicenses = ["Apache-2.0"]
}

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.lrtapp.ardentmap"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.google.android.gms:play-services-base:15.0.1'
    implementation 'com.google.android.gms:play-services-auth:16.0.0'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-messaging:17.3.1'
    implementation 'com.google.firebase:firebase-storage:11.8.0'
    implementation 'com.google.firebase:firebase-auth:11.8.0'
    implementation 'com.google.maps.android:android-maps-utils:0.5+'
    testImplementation 'junit:junit:4.12'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'me.biubiubiu.justifytext:library:1.1'
    implementation 'com.github.cpiz:BubbleView:1.0.3'
    implementation 'com.google.code.gson:gson:2.8.2'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.squareup.okhttp3:okhttp:3.11.0'
    //implementation 'com.google.code.gson:gson:2.8.5'
    //implementation 'com.cpiz.bubbleview:bubbleview:{X.Y.Z}'
    //Google Play Services
    implementation 'com.google.android.gms:play-services-gcm:15.0.1'
    //implementation 'com.google.android.gms:play-services:11.4.0'
    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    implementation 'com.google.android.gms:play-services-location:15.0.1'
    implementation 'com.google.android.gms:play-services-places:15.0.1'
    implementation 'com.github.kittinunf.fuel:fuel-android:1.9.0'
    implementation files('libs/Ab.jar')
    implementation 'me.himanshusoni.chatmessageview:chat-message-view:1.0.3'
    implementation 'com.ibm.watson.developer_cloud:java-sdk:3.7.2'
}
apply plugin: 'com.google.gms.google-services'

Gradle:Project

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }


    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath 'com.google.gms:google-services:4.1.0'



        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

}

allprojects {
    repositories {
        google()
        jcenter()
        maven{
            url "https://maven.google.com"
        }
        maven{
            url "https://jitpack.io"
        }
        configurations {
            all*.exclude group: 'com.google.code.gson'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

您之所以会遇到这个错误,是因为您已经达到(并且超过)了android构建体系结构的极限——一个DEX文件只能容纳大约64000个引用

要解决此问题,您有两个选项:

  • 使用多个dex-这意味着APK中将有多个dex文件,而不是只有一个dex文件。您可以按照以下步骤在生成文件中进行设置。这可能会影响您的min SDK版本
  • 使用Proguard或R8-在其他功能中,这些工具提供代码收缩功能。这意味着所有未使用的代码都将被删除,因此不应达到64k引用的限制。您可以按照以下步骤设置代码收缩

在android\app\build.gradle中,将multiDexEnabled true添加到defaultConfig。并添加以下内容:依赖项{implementation'com.android.support:multidex:1.0.3'}此处阅读更多信息: