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

sas*_*uke 10 heap android dex android-multidex

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

:app:prePackageMarkerForDebug:app:transformClassesWithDexForDebug要在进程中运行dex,Gradle守护程序需要更大的堆.它目前有大约910 MB.要加快构建速度,请将Gradle守护程序的最大堆大小增加到2048 MB以上.为此,请在项目gradle.properties中设置org.gradle.jvmargs = -Xmx2048M.有关详细信息,请参阅https://docs.gradle.org/current/userguide/build_environment.html 错误:.dex文件中的方法引用数不能超过64K.在https://developer.android.com/tools/building/multidex.html上了解如何解决此问题 :app:transformClassesWithDexForDebug FAILED错误:任务':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:进程'命令'/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/bin/java''以非零退出值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'
Run Code Online (Sandbox Code Playgroud)

我的manifestfile就在这里

<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>
Run Code Online (Sandbox Code Playgroud)

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

错误:任务':app:transformClassesWithDexForDebug'的执行失败.com.android.build.api.transform.TransformException:com.android.ide.common.process.ProcessException:java.util.concurrent.ExecutionException:com.android.dex.DexIndexOverflowException:方法ID不在[0,0xffff]中: 65536

我按照Android开发者网站上给出的所有说明但仍然遇到了这个问题.如何解决这个问题呢?

Hug*_*sse 15

你需要multidex在Android默认配置中启用然后:

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
    }
Run Code Online (Sandbox Code Playgroud)

当您在日常例程中构建应用程序时,通常使用debug默认的flavor.因此,如果您的应用程序具有超过65k的方法,您需要在所有风格上启用它.

作为旁注,您可能希望在调试版本中使用Proguard,因此您不必在其上启用multiDex.

完整的app/build.gradle(文档)

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'
}
Run Code Online (Sandbox Code Playgroud)

最后一部分:在清单中添加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>
Run Code Online (Sandbox Code Playgroud)


小智 5

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

compile 'com.google.android.gms:play-services:9.0.1'
Run Code Online (Sandbox Code Playgroud)

有超过69K的引用在它自己的

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

compile 'com.google.android.gms:play-services:9.0.0'
Run Code Online (Sandbox Code Playgroud)

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

您可能没有使用编译路径的范围,并且可能能够指定更多聚焦路径以消除一大块已使用的方法引用,并在65k最大值下获得路径.这里列出了个性化服务.

在我的情况下,我只能想象我在大约一年前添加了gms服务,当时我将Firebase整合到我的应用程序中,但在Firebase网站上找不到相同的参考.

只是意识到对你的问题的评论就说明了将依赖性分解为你所需要的东西的类似事情.