2017-02-28 167 views
1

我在嘗試構建APK時遇到下面的錯誤。我已經閱讀了幾篇關於如何解決這個問題的文章,但由於我沒有長時間的發展,我不確定自己在做什麼。所以,希望有人能幫忙。Android Dex錯誤,同時構建APK

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.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 

這是我的build.gradle(模塊)

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    defaultConfig { 
     applicationId "com.example.dawnlp.mymap" 
     minSdkVersion 22 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:25.1.0' 
    compile 'com.google.android.gms:play-services:10.0.1' 
    testCompile 'junit:junit:4.12' 
} 

,這是我的build.gradle項目

/ Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.2.3' 

     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 

allprojects { 
    repositories { 
     jcenter() 
    } 
} 

將不勝感激,如果有人可以幫幫忙,還裸介意我有點缺乏經驗。

+0

你必須啓用multidex,因爲你的方法超過65536,或者你可以用廣告等簡單地替換播放服務庫 – Redman

+0

啓用[multidex](http:// stackoverflow.com/questions/15209831/unable-to-execute-dex-method-id-not-in-0-0xffff-65536) – azizbekian

回答

1

使用它:

android{ 
    defaultConfig { 

      multiDexEnabled true 
     } 
} 

而且使用它太

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

謝謝。你的意思是地方 defaultConfig { multiDexEnabled真正 } 到 依賴{ 編譯 'com.android.support:multidex:1.0.1' } –

+0

像這樣 依賴{ defaultConfig { multiDexEnabled編譯文件樹(dir:'libs',包括:['* .jar']) androidTestCompile de組:'com.android.support',模塊:'support-annotations' }) compile'c​​om.android.support:appcompat-v7:25.1.0' compile'c​​om.google.android.gms:play -services:10.0.1' testCompile'junit:junit:4.12' } –

1

您需要啓用multidex,該行添加到您的defaultConfig:

multiDexEnabled true 

而此行到您的依賴關係:

compile 'com.android.support:multidex:1.0.1' 

而且,當你正在運行單元測試,你需要創建一個擴展應用的一類,並安裝MultiDex:

public class YouApplication extends Application { 
    @Override 
    protected void attachBaseContext(Context base) { 
     super.attachBaseContext(base); 
     MultiDex.install(this); 
    } 
} 
+0

謝謝。 defaultConfig在哪裏? –

+0

在build.gradle(模塊)文件中。 –

0

使用該塊在模塊級的gradle產出: dexOptions { javaMaxHeapSize「 4G」 }

而且在清單改變應用程序的名稱,以.multidex。 希望你的代碼現在可以正常工作

相關問題