比 lollipop 更旧的 API 超过了 64k 的限制,但不是更新的

64k limit exceeded on older APIs than lollipop, but not newer

所以我想知道为什么我在尝试 运行 我的应用程序时遇到 64k dex 方法限制 android 比 lollipop 更早的版本,而它 运行 在更新的版本。

可能是因为在旧版本上 运行ning 时实际上正在引用支持库?

这是我的gradle:

    apply plugin: 'com.android.application'

    android {
        compileSdkVersion 23
        buildToolsVersion '23.0.2'
    lintOptions {
        checkReleaseBuilds true
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }

    defaultConfig {
        applicationId "com.domain.myapp"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 27
        versionName "1.2"

        // Vector compat
        vectorDrawables.useSupportLibrary = true 
    }

    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

dependencies {

    compile files('libs/commons-io-2.4.jar')
    compile files('libs/activation.jar')
    compile files('libs/additionnal.jar')
    compile files('libs/mail.jar')
    compile project(':libraries:preferencefragment')

    // Gmail API
    compile('com.google.api-client:google-api-client-android:1.20.0') {
        exclude group: 'org.apache.httpcomponents'
    }
    compile('com.google.apis:google-api-services-gmail:v1-rev29-1.20.0') {
        exclude group: 'org.apache.httpcomponents'
    }

    // Play Services
    compile 'com.google.android.gms:play-services-location:8.4.0'
    compile 'com.google.android.gms:play-services-maps:8.4.0'
    compile 'com.google.android.gms:play-services-ads:8.4.0'
    compile 'com.google.android.gms:play-services-analytics:8.4.0'
    compile 'com.google.android.gms:play-services-identity:8.4.0'

    // Support libraries
    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:cardview-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'

    compile 'com.github.bumptech.glide:glide:3.6.1'
    compile 'com.anjlab.android.iab.v3:library:1.0.20'
    compile 'com.sothree.slidinguppanel:library:2.0.3'
    compile 'com.commit451:PhotoView:1.2.5'

    compile('com.github.afollestad.material-dialogs:core:0.8.5.7@aar') {
        transitive = true
    }
}

编辑:

澄清一下:当我尝试 运行 模拟器上的应用 运行 时会发生这种情况,例如奇巧 API 19

来自 gradle 控制台的崩溃日志:

...
:App:generateDebugInstantRunAppInfo
:App:transformClassesWithDexForDebug
AGPBI: {"kind":"error","text":"The number of method references in a .dex file cannot exceed 64K.\nLearn how to resolve this issue at https://developer.android.com/tools/building/multidex.html","sources":[{}],"original":"UNEXPECTED TOP-LEVEL EXCEPTION:\ncom.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536\n\tat com.android.dx.merge.DexMerger.updateIndex(DexMerger.java:484)\n\tat com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:261)\n\tat com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:473)\n\tat com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:161)\n\tat com.android.dx.merge.DexMerger.merge(DexMerger.java:188)\n\tat com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)\n\tat com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)\n\tat com.android.dx.command.dexer.Main.run(Main.java:277)\n\tat com.android.dx.command.dexer.Main.main(Main.java:245)\n\tat com.android.dx.command.Main.main(Main.java:106)\n","tool":"Dex"}

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':App:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

来自消息window的崩溃消息:

:App:transformClassesWithDexForDebug
Error:The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html
Error:Execution failed for task ':App:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

回答您的具体问题是:

  • 此方法计数限制在 DEX(Dalvik 可执行文件)文件中。
  • 此限制的常见解决方法是拥有多个 DEX 文件。
  • 旧版本 Android 本身不支持多个 DEX 文件。
  • 从 Lollipop 开始,系统原生支持它。

这就是它在旧设备上失败但在新设备上运行的原因。它与支持库无关。

为我的回答添加更多有用的内容:

但也许有些人可能想知道如何在旧设备上解决它,请注意我在上面的要点中使用了 natively 这个词。这意味着,有一些变通办法可以让旧平台支持它,但您必须将这些变通办法编码到您的应用程序中。

这个 Google 指南详细解释了解决方法(甚至问题本身):http://developer.android.com/tools/building/multidex.html

它的基数是:

  • 将 multidex 添加到您的依赖项中 compile 'com.android.support:multidex:+'
  • 在 android-> 默认配置 multiDexEnabled true
  • 中的构建脚本中启用 multiDex
  • 在清单上使您的应用程序从 MultidexApplication android:name="android.support.multidex.MultiDexApplication" 扩展,或者,如果您需要为自己的应用程序子 class Application,请使您的应用程序扩展来自它。

完全同意@Budius。您必须在清单中提及应用程序 class。并让您的应用程序 class 扩展 MultiDexApplication。

还需要做一件事。在您的 MultiDexApplication class

中调用附加基础上下文
public class MyApplicationClass extends MultiDexApplication... {
....

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(MyApplicationClass.this);
    }
   ...
}

这对我来说在 pre 和 post 棒棒糖设备上效果很好。

使用 Instant 运行 和 pre-lollipop 设备时出现问题!

https://code.google.com/p/android/issues/detail?id=208577 https://code.google.com/p/android/issues/detail?id=196809

您应该将 multiDexEnabled = true 添加到 gradle 文件中的 defaultConfig