android - 更新 android studio 后无法将请求的类放入单个 dex 文件中

标签 android gradle

我的项目在更新 android studio 之前编译得很好。但是当我将 android studio 更新到 v3.5 后,我无法编译我的项目。我收到以下错误:

Cannot fit requested classes in a single dex file (# methods: 72945 > 65536)

我什至无法更改一行代码。

我的等级:

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.app"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    def retrofitVersion = "2.5.0"

    def supportVersion = '1.0.0-beta01'
    def glideVersion = "4.8.0"

    def rxjava_version = '2.2.8'
    def rxandroid_version = '2.1.1'

    def dagger_version = "2.19"

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"

    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"

    implementation "com.squareup.okhttp3:logging-interceptor:3.10.0"


    def lifecycle_version = '2.1.0-alpha04'
    // ViewModel and LiveData
    implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"

    implementation "com.google.android.material:material:$supportVersion"

    def reactivestreams_version = '2.0.0-beta01'
    implementation "androidx.lifecycle:lifecycle-reactivestreams:$reactivestreams_version"

    def rxcalladapter_version = '2.5.0'
    implementation "com.squareup.retrofit2:adapter-rxjava2:$rxcalladapter_version"

    // Glide
    implementation "com.github.bumptech.glide:glide:$glideVersion"
    annotationProcessor "com.github.bumptech.glide:compiler:$glideVersion"

    implementation("com.github.bumptech.glide:recyclerview-integration:$glideVersion") {
        transitive = false
    }

    implementation 'de.hdodenhof:circleimageview:3.0.0'


    def room_version = '2.1.0-alpha07'

    // Room
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    implementation('io.socket:socket.io-client:1.0.0') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
    }


    implementation 'com.google.code.gson:gson:2.8.5'

    // RxJava
    implementation "io.reactivex.rxjava2:rxjava:$rxjava_version"

    // RxAndroid
    implementation "io.reactivex.rxjava2:rxandroid:$rxandroid_version"
    implementation 'androidx.room:room-rxjava2:2.0.0-beta01'

    // Dagger

//    def dagger_version = "2.16" // version used by google samples

    // Dagger2 core
    implementation "com.google.dagger:dagger:$dagger_version"
    annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"

    // Dagger Android
    implementation "com.google.dagger:dagger-android:$dagger_version"
    implementation "com.google.dagger:dagger-android-support:$dagger_version"
    annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"
    implementation 'com.makeramen:roundedimageview:2.3.0'
    implementation 'pub.devrel:easypermissions:3.0.0'
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.6.+'


    //Camera library
    implementation 'com.camerakit:camerakit:1.0.0-beta3.11'
    implementation 'com.camerakit:jpegkit:0.1.0'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.0'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'

    //Image Picker
    implementation 'com.github.esafirm.android-image-picker:imagepicker:2.0.0'

    //Audio recorder
    def audio_recorder_version = "1.6.0"
    implementation "com.github.piasy:rxandroidaudio:$audio_recorder_version"
    implementation "com.github.piasy:AudioProcessor:$audio_recorder_version"
    implementation 'com.hhl:gridpagersnaphelper:1.0.0'
    implementation 'com.hhl:recyclerviewindicator:1.0.0'
    implementation 'com.h6ah4i.android.widget.advrecyclerview:advrecyclerview:1.0.0'
    implementation 'com.github.ahmadmuzakki29:subtitle-collapsingtoolbar:v2'
    implementation 'com.airbnb.android:lottie:3.0.7'
    implementation 'com.android.billingclient:billing:2.0.1'
    implementation 'com.jsibbold:zoomage:1.3.0'

    implementation 'me.relex:circleindicator:2.1.4'

    implementation 'com.facebook.shimmer:shimmer:0.4.0'
    implementation 'com.google.firebase:firebase-analytics:17.2.0'
    implementation 'com.google.firebase:firebase-messaging:20.0.0'
}

我在stackoverflow上研究了dex文件错误,有人说“启用即时运行为我解决了这个问题”我怀疑即时运行,因为他们改变了即时运行机制并从android studio中删除了即时运行设置。也许这就是问题所在?

在不启用 multiDex 支持的情况下如何解决此问题?

最佳答案

嗯,这是不寻常的,您不想启用 multidex - 在这种情况下,它是最好的选择。但是,还有更多选项可以相对轻松地做到这一点:

  • minSdkVersion19 更改为 21 - 这样运行时必须能够自行处理 65k 限制。 Documentation
  • minifyEnabledfalse 更改为 true - 如果您幸运,proguard 或 r8 会将您的项目缩小到足以容纳 65k。这样,您可能需要创建一个具有缩小规则的文件。 Documentation
  • 添加{ 传递性=假; } 到您可以的所有依赖项 - 并希望所有需要的依赖项都存在于您的项目中,而不是您使用此命令忽略的那些内部依赖项。 Documentation

您还可以尝试使用 gradle 构建配置来手动缩小类,但这不是一个好主意,因为对于不同版本的 gradle,您可能需要不同的方法(因为弃用、新 API 等)。另外,这样您可能会删除太多内容,并在将来遭受调试的噩梦。

希望它能对你有所帮助。

关于android - 更新 android studio 后无法将请求的类放入单个 dex 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57973743/

相关文章:

java - Eclipse - 无法从 'https://services.gradle.org/distributions/gradle-2.1-bin.zip' 安装 Gradle 发行版

java - Gradle不会将JNI库添加到AAR

android-studio - Android Studio 2.3.0 Gradle 项目刷新失败 profile-2017-03-12-17-53-22-268.rawproto

Android 传感器监听器未注销或传感器仍检索值

android - 将视频输入流转换为 RTMP

android - keystore 错误 : Parse Generalized time, 格式无效

android - ConstraintLayout - 如何垂直对齐两个 View 的中心

android - 在Android Studio 3.4中更新到Gradle 5.1.1后无法同步

android - 在 Gradle 中加载 Material - 下载失败 'android-P'

java - 从 API 获取数据