更改build.gradle后,“.dex文件中的方法引用不能超过64K”

时间:2016-08-11 03:39:07

标签: android android-studio gradle

我收到的错误是说我有太多的方法引用:

from mock import patch, PropertyMock

@patch.object(Test, 'haveCode', new_callable=PropertyMock)
def myTest(test_haveCode_mock):
    test_haveCode_mock.return_value = True
    checkTest = Test()
    assertEqual(checkTest.haveCode, True)
    patch.stopall()  # when you want to release all mocks

当我尝试通过gradle app文件将targetSdkVersion更改为24并将minSdkVersion更改为较低的sdk(19)时,一切都开始了。这导致我不得不在整个文件中更改一些值,如

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

 compile 'com.android.support:appcompat-v7:23.0.0'

我读过,当build.gradle中包含错误的值时,人们会收到此错误。这是我在改变之前的原始build.gradle:

 compile 'com.android.support:appcompat-v7:24.1.1'

是否有任何想法导致这种情况?

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    applicationId "com.delbridge.seth.alarm"
    minSdkVersion 23
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),        
    'proguard-rules.pro'
    }
    debug {
        debuggable true
    }
}
}

目前这是我的build.gradle:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'

compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
compile 'org.jsoup:jsoup:1.9.2'
compile 'org.jdeferred:jdeferred-android-aar:1.2.4'
compile 'com.google.android.gms:play-services-appindexing:9.0.2'
compile 'com.google.android.gms:play-services:9.0.2'
compile 'com.android.support:support-v4:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:cardview-v7:23.0.+'
compile 'com.android.support:recyclerview-v7:23.0.+'
compile 'com.google.firebase:firebase-ads:9.0.2'

}

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

2 个答案:

答案 0 :(得分:4)

您使用此行不必要地包含所有Google Play服务:

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

因此,删除该行并保留包含各个组件的行:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.1.1'

    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    compile 'org.jsoup:jsoup:1.9.2'
    compile 'org.jdeferred:jdeferred-android-aar:1.2.4'
    compile 'com.google.android.gms:play-services-appindexing:9.4.0'

    //remove this line:
    //compile 'com.google.android.gms:play-services:9.4.0'

    compile 'com.google.firebase:firebase-ads:9.4.0'
    compile 'com.google.android.gms:play-services-ads:9.4.0'
    compile 'com.google.android.gms:play-services-auth:9.4.0'
    compile 'com.google.android.gms:play-services-gcm:9.4.0'

    compile 'com.android.support:support-v4:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'com.android.support:cardview-v7:24.1.1'
    compile 'com.android.support:recyclerview-v7:24.1.1'
}

注意:

如果上述解决方案无法解决您的问题,则需要启用multidex。

答案 1 :(得分:-1)

在build.gradle文件中尝试:

        android {
      compileSdkVersion 22
       buildToolsVersion "23.0.0"
        defaultConfig {
         minSdkVersion 14 //lower than 14 doesn't support multidex
         targetSdkVersion 22

         // Enabling multidex support.
         multiDexEnabled true
     }
 }

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

查看此页面:https://developers.google.com/android/guides/setup 并使用所需的谷歌播放服务