.dex文件中方法引用数超过64K

The number of method references in a .dex file exceed 64K

在我的项目中包含播放服务和 firebase 库后,目前正在处理我的 android 应用程序我收到此错误并且无法 运行 我的代码

:app:prePackageMarkerForDebug :app:transformClassesWithDexForDebug To run dex in process, the Gradle daemon needs a larger heap. It currently has approximately 910 MB. For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB. To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties. For more information see https://docs.gradle.org/current/userguide/build_environment.html 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 :app:transformClassesWithDexForDebug FAILED 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.8.0_60.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2

我的 build.gradle 文件在这里 :

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
    applicationId "xyz.in.network"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        shrinkResources true
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        multiDexEnabled true
    }
}
}

dependencies {
   compile fileTree(dir: 'libs', include: ['*.jar'])
   testCompile 'junit:junit:4.12'
   compile project(':libs:ViewPagerIndicator')
   compile 'com.google.android.gms:play-services:9.0.0'
   compile 'com.android.support:appcompat-v7:23.4.0'
   compile 'com.android.support:design:23.4.0'
   compile 'com.google.android.gms:play-services-maps:9.0.0'
   compile 'com.google.android.gms:play-services-location:9.0.0'
   compile 'com.android.support:cardview-v7:23.4.0'
   compile 'com.getbase:floatingactionbutton:1.10.1'
   compile 'com.squareup.picasso:picasso:2.5.2'
   compile 'com.android.volley:volley:1.0.0'
   compile 'com.google.firebase:firebase-messaging:9.0.0'
   compile 'com.android.support:multidex:1.0.1'
   }

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

我的清单文件在这里

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:name="android.support.multidex.MultiDexApplication"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
<activity android:name=".Util.DisconnectedNetwork"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.Transparent"></activity>

    <service android:name=".FCM.FirebaseMessagingHandler">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service android:name=".FCM.FirebaseRegistrationTokenHandler">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

</application>

将堆大小增加到 2048M 后。 Gradle 给出这个错误

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.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

我按照 android 开发者网站上给出的所有说明进行操作,但仍然遇到这个问题。如何解决这个问题?

你已经为发布风格启用了 multidex,你应该也为调试风格启用了这一行

debug {
    multiDexEnabled true
}

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

您需要在 android 默认配置中启用 multidex 然后:

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.3'

    defaultConfig {
        applicationId "com.example.case"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 43
        versionName "4.0.13"

        // Enabling multidex support.
        multiDexEnabled true
    }

在日常生活中构建应用程序时,通常会使用 debug 默认风格。所以如果你的应用程序有超过 65k 的方法,你需要在所有风格上启用它。

附带说明一下,您可能希望在调试版本上使用 Proguard,这样您就不必在其上启用 multiDex。

完整 app/build.gradle (documentation)

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

最后一部分:在清单中添加 MultiDex 应用程序(或作为您自己的父级 Application

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

我的应用程序遇到了类似的错误,因此我在 Android Studio 中安装了一个名为 Methods Count (http://www.methodscount.com/) 的插件。它显示了每个依赖项使用的方法引用数,并显示了编译路径

compile 'com.google.android.gms:play-services:9.0.1'

有超过 69k 的引用它自己

我将其更改为显示您的依赖项之一:

compile 'com.google.android.gms:play-services:9.0.0'

它显示 69,520 个方法引用 作为此编译路径的依赖项。

您可能没有使用编译路径的范围,并且可以指定更集中的路径以消除大量使用的方法引用并获得 65k 最大值以下的方法。个性化服务列表是 here.

就我而言,我只能想象我在大约一年前将 Firebase 合并到我的应用程序时添加了 gms 服务行,但在 Firebase 网站上找不到相同的参考。

只是意识到对你的问题的评论说了类似的事情,将依赖分解为你所需要的。