2016-07-05 60 views
5

每当我建的项目,并准备发布我的设备上,我不断收到此错误:摇篮构建失败:敏捷文件不能超过64K

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 'C:\Program Files\Java\jdk1.8.0_91\bin\java.exe'' finished with non-zero exit value 2

它说gradle这个构建失败,两个错误

所以我有一些问题:

  • 什么是dex文件
  • 我没有直接使用它们,所以为什么我会得到上面的错误?
  • 什么是dex文件?
  • 这些文件对于.APK文件大小有什么要说的吗?

这些错误在我停止使用proguard进行调试构建之后开始再次出现,因为StackTrace没有显示,并且在激活了proguard之前,我出现了错误。

我以前有过这个错误,我删除了dex文件夹,它解决了它,但现在它突然不够了。

我gradle这个文件:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.3" 

    defaultConfig { 
     applicationId "com.example.myproject" 
     minSdkVersion 15 
     targetSdkVersion 23 

    } 

    buildTypes { 
     release { 
      minifyEnabled true 
      shrinkResources true 

      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 

    } 
} 

dependencies { 

    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha1' 
    compile 'com.android.support:support-v4:23.3.0' 
    compile 'com.google.android.gms:play-services:9.2.0' 
    compile 'com.google.android.gms:play-services-ads:9.2.0' 
} 
+1

你应该**从不**使用完整的'play-services'依赖。只能使用[选择您需要的Play服务API](https://developers.google.com/android/guides/setup#split)。包括整个事情是64k方法限制的45k +。 – ianhanniballake

回答

4

您添加的依赖更多 - 更高的方法计数。改变你的gradle产出,以反映这一点:

defaultConfig { 
    ... 
    minSdkVersion 15 
    targetSdkVersion 23 
    ... 

    // Enabling multidex support. 
    multiDexEnabled true 
} 

这是一个简单的修复,如果你minSdkVerison是14或以上才有效。 也将添加以下的依赖:

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

有时候,你将杠杆(个别)引用同一个库的依赖关系 - 你可能被加倍方法很多,三倍,四倍,等等看看这个所以回答解决这个问题,并总是减少APK的大小。 gradle - library duplicates in dependencies

+0

这完全影响APK的大小吗? – Zoe

+0

全文阅读错误信息中的文章 - 它非常有洞察力!尽管回答这个评论:https://developer.android.com/studio/build/multidex.html#dev-build – apelsoczi

+0

它现在的作品!谢谢 – Zoe

1

从谷歌文档:

Android application (APK) files contain executable bytecode files in the form of Dalvik Executable (DEX) files, which contain the compiled code used to run your app. The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536—including Android framework methods, library methods, and methods in your own code.

为了解决这个问题,你需要做下面的代码中的变化:

的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.0' 
} 

AndroidManifest.xml中

<?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> 

When these configuration settings are added to an app, the Android build tools construct a primary dex (classes.dex) and supporting (classes2.dex, classes3.dex) as needed. The build system will then package them into an APK file for distribution.

请注意:如果你已经在你的代码从应用类扩展类,则只是将其更改为从MultidexApplication延伸。

0

playservices lib非常庞大。即使是一个小应用程序可能会遭受这个问题。使用Proguard后,由于优化会删除未使用的内容,问题将消失。对于开发(调试版本) - 仅限于libs只是你真正使用的东西通常有帮助。您可以尝试缩小库或/并在compile()gradle语句中使用'exclude'过滤器。

例如,当我需要它的位置服务,我使用:

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

起初并得到了该消息。用

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

取代它,并得到它的优雅解决。