2016-07-05 103 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' 

取代它,並得到它的優雅解決。