Gradle 构建失败并出现异常:java.exe' 以非零退出值 2 完成

Gradle Build failed with an exception : java.exe' finished with non-zero exit value 2

build.gradle 文件:

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "xxxxx.com.myapp"
        minSdkVersion 9
        targetSdkVersion 22
        versionCode 3
        versionName "1.2"


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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.google.android.gms:play-services-ads:7.3.0'
    compile files('libs/libGoogleAnalyticsServices.jar')
}

错误日志:

失败:构建失败,出现异常。

您包含 libGoogleAnalyticsServices.jar 两次。基于路径,它将被以下行包含(包括 libs 目录中的所有 jar)--

compile fileTree(dir: 'libs', include: ['*.jar'])

删除此行以避免两次包含库 --

compile files('libs/libGoogleAnalyticsServices.jar')

尝试从您的依赖项中删除以下内容:

compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/libGoogleAnalyticsServices.jar')

如果您查看 here,分析 API 已内置于 Google 播放服务中,因此两者存在冲突。

有时由于 multidex 问题,会出现此构建失败的错误。根据查看您的构建脚本,您似乎不需要启用 multidex。但是你仍然可以试试这个,因为我只能看到你的 gradle build script.Use

不能说太多
    defaultConfig {
            applicationId "xxxxx.com.myapp"
            minSdkVersion 9
            targetSdkVersion 22
            versionCode 3
            versionName "1.2"
            // enable Mutidex.
            multiDexEnabled true


        }