android - 无法将请求的类放入单个 dex 文件中,即使对于之前编译良好的早期提交也是如此

标签 android android-studio android-gradle-plugin dex

所以我刚刚达到了我的 android 项目的最大方法计数限制,该项目无法构建并显示以下错误消息:

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

我理解消息的含义,以及如何解决它(运行 proguard、启用 multidex 等)。我的问题是我不明白为什么我突然收到这条消息 - 我正在做的是删除一些多余的旧代码,点击构建,现在我收到这条消息。

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

现在这就是它变得非常奇怪的地方:我想分析 APK 以找出导致问题的原因,但我当然无法构建它。因此,我没有启用 multidex,而是决定将我的代码恢复到昨天(昨天绝对确实构建良好 - 我手机上有应用程序来证明这一点!),但我仍然收到此构建错误消息.我不明白这怎么可能。我尝试恢复到几天前,同样的事情(克隆一个新的 repo 并检查早期的提交)。

那么,问题 2:对于昨天构建良好且没有错误的完全相同的代码,我如何得到这个构建错误?

我唯一能想到的是我用作依赖项的库的大小突然增加了 - 但我在我的 gradle 构建中声明了所有内容的特定版本,例如:

// 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(公共(public)资源、日志等):定义了 15000 个,引用了 15700 个

仅这些就让我达到了极限。我仍然不明白为什么这会突然发生 :( 当然,如果我没有添加任何库,这些数字应该不会改变?

最佳答案

简单地将它添加到您的 gradle(模块:app)>> multiDexEnabled true

android {
    defaultConfig {
        ...
        minSdkVersion 21 
        targetSdkVersion 28
        multiDexEnabled true
    }
    ...
}

然后重建项目 在菜单中单击 => 构建 > 重建项目。

关于android - 无法将请求的类放入单个 dex 文件中,即使对于之前编译良好的早期提交也是如此,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54911906/

相关文章:

javascript - 如何在按下另一个按钮之前更改按钮状态?

java - 在依赖于 Android 插件的自定义 Gradle 插件的单元测试中模拟应用程序变体

android - 我的应用程序需要很长时间才能启动“第一个 Activity ”屏幕

android - 如何将我的应用程序设置为 Espresso 测试中的默认应用程序?

android - Android Studio 3中的 “Sync project with gradle files”按钮在哪里?

android - 有什么方法可以将 Android Studio 的外观/字体全部自定义为 eclipse 样式吗?

Android插件1.3错误

java - 未处理的异常类型 JSONException

android - 以编程方式更改 Android 设置 "Schedule power on and off"

java - 评论对android运行时有什么影响吗?