无法将请求的类放入单个dex文件中,即使对于先前编译成功的提交也是如此。

11

我刚刚遇到了Android项目方法数量的最大限制,无法构建并显示以下错误消息:

Error: null, Cannot fit requested classes in a single dex file (# methods: 117407 > 65536)

我了解此消息的含义以及如何解决问题(运行proguard、启用multidex等)。我的问题是,我不明白为什么会突然出现这个消息-我正在删除一些过时的代码,点击“构建”按钮,然后就出现了这个消息。

问题1:即使我没有添加任何库依赖项,我的方法数(根据错误消息为117407)怎么可能突然超过了限制(65536)?实际上,我删除了代码,但突然间多了约5万个方法?

现在情况变得非常奇怪:我想分析APK以找出问题所在,但我不能构建它。因此,我决定恢复到昨天的代码版本(昨天绝对可以成功构建-我有安装在手机上的应用程序可证明!),但我仍然收到构建错误消息。我不明白这是怎么回事。我尝试还原到几天前,结果还是一样(克隆新存储库并检查较早的提交)。

所以,问题2:我如何在完全相同的代码中获得这个构建错误,而昨天它却可以顺利构建没有出现错误?

我唯一能想到的是,我作为依赖项使用的库突然增加了大小-但我在gradle build中声明了每个库的特定版本,例如:

// RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'

所以,我的依赖关系肯定没有改变吧?

非常感谢任何帮助我找到解决方法的建议。我已经尝试过清理项目,并在Android Studio中无效化缓存/重启。我真的不想在我的调试版本上启用multidex或运行proguard。

以下是完整的build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
    applicationId "XXXXXXXXX"
    minSdkVersion 19
    targetSdkVersion 28
    versionCode 1
    versionName "0.1"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    vectorDrawables.useSupportLibrary = true  // see https://developer.android.com/studio/write/vector-asset-studio#sloption
}
buildTypes {
    release {
        minifyEnabled false
        // Do code shrinking!
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

// Core stuff
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:support-vector-drawable:28.0.0'
implementation 'com.google.android.gms:play-services-wearable:16.0.1'

// Dagger
implementation 'com.google.dagger:dagger:2.21'
kapt 'com.google.dagger:dagger-compiler:2.21'
// Dagger for Android
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21' // if you use the support libraries
kapt 'com.google.dagger:dagger-android-processor:2.21'

// Constraint layout
implementation 'com.android.support.constraint:constraint-layout:1.1.3'

// Associated WearOS project
wearApp project(':wear')

// Common library project
implementation project(':common')

// These were added to resolve gradle error on the 'com.android.support:appcompat-v7:28.0.0' implementation:
// All com.android.support libraries must use the exact same version specification (mixing versions can lead to
// runtime crashes). Found versions 28.0.0, 26.1.0. Examples include com.android.support:animated-vector-drawable:28.0.0
// and com.android.support:support-media-compat:26.1.0
// This seems to be related to linking the wear project. If the wear project was not linked, the error went away.
implementation 'com.android.support:support-media-compat:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'

// RxJava
implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.4'

// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
// Retrofit RxJava
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
// Retrofit logging:
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.1'

// Room
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
implementation "android.arch.persistence.room:common:$room_version"
implementation "android.arch.persistence.room:rxjava2:$room_version"
kapt "android.arch.persistence.room:compiler:$room_version"

// For modern time handling (java.time requires API 26 or higher)
implementation 'com.jakewharton.threetenabp:threetenabp:1.1.1'

// Graphing
implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'

// Dropbox
implementation 'com.dropbox.core:dropbox-core-sdk:3.0.11'

// OpenCSV
implementation 'com.opencsv:opencsv:4.5'

}

编辑

启用multidex后,使用Android Studio分析APK时,以下TLD下显示了一些重要的依赖项(我不确定是否应该查看定义或引用的方法编号?):

  • com.dropbox:26000个已定义的方法,34000个已引用的方法
  • com.android(主要是支持库):18700个已定义,24600个已引用
  • org.apache(commons、log等):15000个已定义,15700个已引用

这些单独就已经接近极限了。但我仍然不理解为什么会突然发生这种情况 :( 如果我没有添加任何库,这些数字应该不会改变,对吗?


返回:

编辑

启用multidex后,使用Android Studio分析APK时,以下TLD下显示了一些重要的依赖项(我不确定是否应该查看定义或引用的方法编号?):

  • com.dropbox:26000个已定义的方法,34000个已引用的方法
  • com.android(主要是支持库):18700个已定义,24600个已引用
  • org.apache(commons、log等):15000个已定义,15700个已引用

这些单独就已经超过了极限。但我仍然不理解为什么会突然发生这种情况 :( 如果我没有添加任何库,这些数字应该不会改变,对吗?


你能否发布你的整个build.gradle文件(如果你愿意,可以去除标识)? - Andres S
我已经添加了有关该项目的build.gradle文件。它引用了一个名为“common”的常用库项目和一个链接的WearOS项目。 - James Allen
@JamesAllen 这个错误来自于d8/r8,它在Android Studio 3.1中成为了默认编译器。可能这个新的编译器与dx/proguard工具链的行为不同(也许你之前的版本是用dx/proguard构建的)。你可以尝试使用android.enableD8=false回退到dx并查看是否出现相同的错误。 - Alex Lipov
1
今天我也遇到了这个问题。即使回滚到先前的提交也没有用。我还发现,如果只是在调试或发布模式下运行应用程序,它就能正常工作。但是从构建菜单中运行“构建”会导致此问题。什么鬼??我的应用程序非常小,没有大量依赖项。 - Johann
12个回答

0

android > app > build.gradle

  1. android {
    
        defaultConfig {
            versionCode 1
            versionName "1.0"
          +  multiDexEnabled true
        }
    }
    
  2. 在依赖块中添加 implementation 'com.android.support:multidex:1.0.3'

    dependencies {
     + implementation 'com.android.support:multidex:1.0.3'
    }
    

0
我尝试了这个。 希望它有所帮助,是在某些文档中找到的 (忘记了网址 :( )

build.gradle 应用程序

dependencies {...
grdef multidex_version ='2.0.1'

implementation "androidx.multidex:multidex:$multidex_version"
}...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接