Android,构建成功,但 apk 构建(后续 运行)失败

Android, Build Successful, but apk build (subsequent run) fails

我最近升级到 Android studio 1.2 beta。 早期编译良好的项目失败了。 我不得不在 gradle 文件中进行一些修改以使项目再次编译。 现在构建成功了,但是,当我尝试构建 apk 时,构建失败了。

这是项目的详细信息。

Gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion '22.0.1'
    defaultConfig {
        applicationId "mcc.abcd"
        minSdkVersion 16
        targetSdkVersion 22
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:22.0.0'
    compile 'com.google.android.gms:play-services:7.0.0'
    //compile files('libs/google-play-services.jar')
    compile project(':app:libraries:PebbleKitAndroid:app')
    compile project(':app:libraries:Weka-for-Android-master:app')
}

构建错误

:app:dexDebug
AGPBI: {"kind":"SIMPLE","text":"UNEXPECTED TOP-LEVEL EXCEPTION:","position":{},"original":"UNEXPECTED TOP-LEVEL EXCEPTION:"}
AGPBI: {"kind":"SIMPLE","text":"com.android.dex.DexIndexOverflowException: Cannot merge new index 68094 into a non-jumbo instruction!","position":{},"original":"com.android.dex.DexIndexOverflowException: Cannot merge new index 68094 into a non-jumbo instruction!"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.InstructionTransformer.jumboCheck(InstructionTransformer.java:109)","position":{},"original":"\tat com.android.dx.merge.InstructionTransformer.jumboCheck(InstructionTransformer.java:109)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.InstructionTransformer.access0(InstructionTransformer.java:26)","position":{},"original":"\tat com.android.dx.merge.InstructionTransformer.access0(InstructionTransformer.java:26)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.InstructionTransformer$StringVisitor.visit(InstructionTransformer.java:72)","position":{},"original":"\tat com.android.dx.merge.InstructionTransformer$StringVisitor.visit(InstructionTransformer.java:72)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.io.CodeReader.callVisit(CodeReader.java:114)","position":{},"original":"\tat com.android.dx.io.CodeReader.callVisit(CodeReader.java:114)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.io.CodeReader.visitAll(CodeReader.java:89)","position":{},"original":"\tat com.android.dx.io.CodeReader.visitAll(CodeReader.java:89)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.InstructionTransformer.transform(InstructionTransformer.java:49)","position":{},"original":"\tat com.android.dx.merge.InstructionTransformer.transform(InstructionTransformer.java:49)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.DexMerger.transformCode(DexMerger.java:842)","position":{},"original":"\tat com.android.dx.merge.DexMerger.transformCode(DexMerger.java:842)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.DexMerger.transformMethods(DexMerger.java:813)","position":{},"original":"\tat com.android.dx.merge.DexMerger.transformMethods(DexMerger.java:813)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.DexMerger.transformClassData(DexMerger.java:785)","position":{},"original":"\tat com.android.dx.merge.DexMerger.transformClassData(DexMerger.java:785)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.DexMerger.transformClassDef(DexMerger.java:682)","position":{},"original":"\tat com.android.dx.merge.DexMerger.transformClassDef(DexMerger.java:682)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:542)","position":{},"original":"\tat com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:542)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)","position":{},"original":"\tat com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.merge.DexMerger.merge(DexMerger.java:189)","position":{},"original":"\tat com.android.dx.merge.DexMerger.merge(DexMerger.java:189)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)","position":{},"original":"\tat com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)","position":{},"original":"\tat com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.command.dexer.Main.run(Main.java:246)","position":{},"original":"\tat com.android.dx.command.dexer.Main.run(Main.java:246)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.command.dexer.Main.main(Main.java:215)","position":{},"original":"\tat com.android.dx.command.dexer.Main.main(Main.java:215)"}
AGPBI: {"kind":"SIMPLE","text":"\tat com.android.dx.command.Main.main(Main.java:106)","position":{},"original":"\tat com.android.dx.command.Main.main(Main.java:106)"}


 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_11\bin\java.exe'' finished with non-zero exit value 2

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2 mins 44.727 secs

我已经尝试过更改buildToolsVersion、sdk版本等,但还没有成功。 请帮忙。

听起来您已经达到了 dex 限制。 dex 格式的限制意味着您的项目中只能有 65536 个方法。

谢天谢地,从 Android API 版本 21 开始,现在支持多 dex。

您可以这样启用它:

android {
   defaultConfig {
      ...
      multiDexEnabled = true
      ...
   }
}

更多信息来自 android 开发者网站 here

还值得注意的是,您可能不需要包括所有 Google Play 服务和 you can pick and choose the required modules your app needs

失败的原因在构建输出的第二行 - "com.android.dex.DexIndexOverflowException: Cannot merge new index 68094 into a non-jumbo instruction!"

这意味着您已达到应用程序的最大方法限制,com.google.android.gms:play-services 很大(在很多方法中)

最简单的解决方案是只使用您实际需要的 Play 服务组件 described in official docs,例如

compile 'com.google.android.gms:play-services-gcm:7.0.0'

而不是你的

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