.dex文件中方法引用的数量不能超过64k API 17

我正在构build一个SugarORM库的应用程序,但是当我尝试构buildAPI 17(没有检查其他人)的项目时,它显示生成错误。

Information:Gradle tasks [:app:assembleDebug] :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAnimatedVectorDrawable2330Library UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72330Library UP-TO-DATE :app:prepareComAndroidSupportCardviewV72330Library UP-TO-DATE :app:prepareComAndroidSupportDesign2330Library UP-TO-DATE :app:prepareComAndroidSupportMediarouterV72300Library UP-TO-DATE :app:prepareComAndroidSupportRecyclerviewV72330Library UP-TO-DATE :app:prepareComAndroidSupportSupportV42330Library UP-TO-DATE :app:prepareComAndroidSupportSupportVectorDrawable2330Library UP-TO-DATE :app:prepareComAndroidVolleyVolley100Library UP-TO-DATE :app:prepareComGithubSatyanSugar14Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServices840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAds840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAnalytics840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAppindexing840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAppinvite840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAppstate840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesAuth840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesBase840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesBasement840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesCast840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesDrive840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesFitness840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesGames840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesGcm840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesIdentity840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesLocation840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesMaps840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesMeasurement840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesNearby840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesPanorama840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesPlus840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesSafetynet840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesVision840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesWallet840Library UP-TO-DATE :app:prepareComGoogleAndroidGmsPlayServicesWearable840Library UP-TO-DATE :app:prepareMeDrakeetMaterialdialogLibrary131Library UP-TO-DATE :app:prepareDebugDependencies :app:compileDebugAidl UP-TO-DATE :app:compileDebugRenderscript UP-TO-DATE :app:generateDebugBuildConfig UP-TO-DATE :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets UP-TO-DATE :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources UP-TO-DATE :app:mergeDebugResources UP-TO-DATE :app:processDebugManifest UP-TO-DATE :app:processDebugResources UP-TO-DATE :app:generateDebugSources UP-TO-DATE :app:compileDebugJavaWithJavac Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. :app:compileDebugNdk UP-TO-DATE :app:compileDebugSources :app:prePackageMarkerForDebug :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.8.0_51.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2 Information:BUILD FAILED Information:Total time: 21.663 secs Information:2 errors Information:0 warnings Information:See complete output in console 

但是,当我build立这个项目为android v5.0或以上,它工作正常。如果我删除的SugarORM gradle依赖关系,它适用于这两个设备v4.2.2和v5.0

你有太多的方法。 对于dex只能有65536个方法

build议您可以使用multidex支持

只需在build.gradle添加这些行build.gradle

 android { defaultConfig { ... // Enabling multidex support. multiDexEnabled true } ... } dependencies { compile 'com.android.support:multidex:1.0.0' } 

同样在你的ManifestMultiDexApplication类从multidex支持库添加到应用程序元素

  <?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"> <!--If you are using your own custom Application class then extend --> <!--MultiDexApplication and change above line as--> android:name=".YourCustomApplicationClass"> ... </application> </manifest> 

重写attachBaseContext方法

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

如果您正在使用自己的Application类,请将父类从Application更改为MultiDexApplication

另一个解决scheme是尝试使用ProGuard删除未使用的代码 – 为您的应用程序configurationProGuard设置以运行ProGuard,并确保您已经缩小版本版本。

 android { compileSdkVersion 23 buildToolsVersion '23.0.0' defaultConfig { applicationId "com.dkm.example" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" multiDexEnabled true } 

把它放在你的defaultConfig里面:

 multiDexEnabled true 

它为我工作

我得到了这个错误信息,因为在编译我的项目自动更新编译版本在我build.gradle文件:

 android { ... buildToolsVersion "23.0.2" ... } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:design:23.4.0' } 

解决它通过更正版本:

 android { ... buildToolsVersion "23.0.2" ... } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.android.support:design:23.0.1' } 

这对我来说是有效的:

发生这种情况是因为有太多未使用的方法。 这些方法大部分都来自build.gradle中包含的库

使用缩小和缩小资源来解决这个问题,同时清理你的代码。

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

当您仅使用afew加载所有google play服务apis时,也会发生此错误。

正如谷歌所说:“在6.5之前的Google Play服务版本中,您必须将整个API包编译到您的应用程序中。在某些情况下,这样做会使您的应用程序中的方法数量(包括框架API,库方法和您自己的代码)在65,536的限制之下。

从版本6.5开始,您可以select性地将Google Play服务API编译到您的应用程序中。“

例如,当您的应用需要play-services-maps,play-services-location。您需要在应用级别的build.gradle文件中仅添加两个apis,如下所示:

 compile 'com.google.android.gms:play-services-maps:10.2.1' compile 'com.google.android.gms:play-services-location:10.2.1' 

代替:

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

有关完整的文档和谷歌播放服务列表请点击这里

我一直在面对同样的问题,为了支持multidex,你必须记住你的应用程序的minSdkVersion 。 如果您使用的是minSdkVersion 21或更高版本,那么只需像这样编写multiDexEnabled true即可

 defaultConfig { applicationId ******************* minSdkVersion 21 targetSdkVersion 24 versionCode 1 versionName "1.0" multiDexEnabled true } 

它适用于我,如果你使用minSdkVersion低于21(低于lolipop ),那么你必须做两个额外的简单的事情

1.首先添加这个依赖

编译'com.android.support:multidex:1.0.1'

在你的build.gradle。

2.最后一行将下面的行添加到清单中的应用程序中

机器人:名字= “android.support.multidex.MultiDexApplication”

 <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:name="android.support.multidex.MultiDexApplication" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> 

宾果然后它将工作在较低的版本也.. ..快乐的编码

对于我升级Gradle works.Look更新在Android网站,然后将其添加到您的build.gradle(项目)像这样

  dependencies { classpath 'com.android.tools.build:gradle:2.2.0-alpha4' .... } 

然后同步项目与gradle文件加上它可能会发生有时因为java.exe(在我的情况)只是强制杀死从Windows任务pipe理器的java.exe然后重新运行程序

也可以试试这个:

 android{ . . // to avoid DexIndexOverflowException dexOptions { jumboMode true } } 

希望它可以帮助别人。 谢谢

您可以启用Android Studio上的“即时运行”以获得对multidex的支持。

做到这一点,它的作品:

 defaultConfig { applicationId "com.example.maps" minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName "1.0" multiDexEnabled true }