Unity Gradle构建失败。 DexException:多个dex文件定义

时间:2018-09-01 11:06:45

标签: android unity3d android-gradle admob

我想制作一个显示Admob横幅广告的Unity应用。因此,在this之后,我添加了Admob Unity插件。但是,在构建时会发生以下错误。

这是控制台:

CommandInvokationFailure: Gradle build failed. 
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/java -classpath "/Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/gradle/lib/gradle-launcher-4.0.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx2048m" "assembleRelease"

stderr[
/Users/yuukimatsushima/work/unity/BreakPlates2/Temp/gradleOut/src/main/AndroidManifest.xml:25:13-35 Error:
 Attribute meta-data#android.support.VERSION@value value=(26.1.0) from [:com.android.support.customtabs-26.1.0:] AndroidManifest.xml:25:13-35
 is also present at [:support-compat-26.0.2:] AndroidManifest.xml:28:13-35 value=(26.0.2).
 Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:23:9-25:38 to override.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':processReleaseManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.1.0) from [:com.android.support.customtabs-26.1.0:] AndroidManifest.xml:25:13-35
   is also present at [:support-compat-26.0.2:] AndroidManifest.xml:28:13-35 value=(26.0.2).
   Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:23:9-25:38 to override.
...

上面的日志显示add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:23:9-25:38 to override.,因此我添加了以下代码。

资产/插件/Android/AndroidManifest.xml:

<manifest>
    <application>
        ...
        <meta-data
            tools:replace="android:value"
            android:name="android.support.VERSION"
            android:value="26.0.2" />

    </application>
</manifest>

然后上述错误已解决。但是,会发生以下错误。

这是控制台:

CommandInvokationFailure: Gradle build failed. 
/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/bin/java -classpath "/Applications/Unity/PlaybackEngines/AndroidPlayer/Tools/gradle/lib/gradle-launcher-4.2.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx2048m" "assembleRelease"

stderr[

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':transformDexWithDexForRelease'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lcom/google/android/gms/common/api/zzd;
...

请告诉我如何解决此错误。 预先感谢。

注意: Unity版本:2018.2.6f1个人版

4 个答案:

答案 0 :(得分:0)

在Android Studio中检查您的外部库,然后添加此代码的build.gradle设置

implementation fileTree(include: ['*.jar'], dir: 'libs', exclude: ['classes.jar']) compileOnly files('libs/classes.jar')

(classes.jar是文件名,包括com / google / android / gms / common / api / zzd)

答案 1 :(得分:0)

最后,我自己解决了此错误。就我而言,以下解决方案有效。

  1. Unity>文件>构建设置>导出
  2. 在Android项目中,删除libs中的所有.jar和.aar,但 unity-classes.jar和NcmbGcmPlugins.arr。
  3. 编辑build.gradle
  4. 运行

这是build.gradle(应用程序级别)

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

buildscript {
    repositories {
        jcenter()
        google()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

allprojects {
    repositories {
        flatDir {
            dirs 'libs'
        }
        // EDIT: added following 3 lines.
        maven {
            url "https://maven.google.com"
        }
    }
}

apply plugin: 'com.android.application'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation(name: 'NcmbGcmPlugin', ext:'aar')
//    implementation(name: 'android.arch.lifecycle.runtime-1.0.0', ext:'aar')  // EDIT: removed
//    implementation(name: 'com.android.support.customtabs-26.1.0', ext:'aar') // EDIT: removed
//    implementation(name: 'com.android.support.support-compat-26.1.0', ext:'aar') // EDIT: removed
//    implementation(name: 'com.android.support.support-core-ui-26.1.0', ext:'aar') // EDIT: removed
//    implementation(name: 'com.android.support.support-core-utils-26.1.0', ext:'aar') // EDIT: removed
//    implementation(name: 'com.android.support.support-fragment-26.1.0', ext:'aar') // EDIT: removed
//    implementation(name: 'com.android.support.support-media-compat-26.1.0', ext:'aar') // EDIT: removed
//    implementation(name: 'com.android.support.support-v4-26.1.0', ext:'aar') // EDIT: removed
//    implementation(name: 'com.google.android.gms.play-services-ads-15.0.1', ext:'aar') // EDIT: removed
//    implementation(name: 'com.google.android.gms.play-services-ads-base-15.0.1', ext:'aar') // EDIT: removed
//    implementation(name: 'com.google.android.gms.play-services-ads-identifier-15.0.1', ext:'aar') // EDIT: removed
//    implementation(name: 'com.google.android.gms.play-services-ads-lite-15.0.1', ext:'aar') // EDIT: removed
//    implementation(name: 'com.google.android.gms.play-services-basement-15.0.1', ext:'aar') // EDIT: removed
//    implementation(name: 'com.google.android.gms.play-services-gass-15.0.1', ext:'aar') // EDIT: removed
//    implementation(name: 'play-services-base-11.0.0', ext:'aar') // EDIT: removed
//    implementation(name: 'play-services-basement-11.0.0', ext:'aar') // EDIT: removed
//    implementation(name: 'play-services-gcm-11.0.0', ext:'aar') // EDIT: removed
//    implementation(name: 'play-services-iid-11.0.0', ext:'aar') // EDIT: removed
//    implementation(name: 'play-services-tasks-11.0.0', ext:'aar') // EDIT: removed
//    implementation(name: 'support-compat-26.0.2', ext:'aar') // EDIT: removed
//    implementation(name: 'support-core-utils-26.0.2', ext:'aar') // EDIT: removed
//    implementation(name: 'support-v4-26.0.2', ext:'aar') // EDIT: removed

    implementation 'com.android.support:support-v4:26.1.0' // EDIT: added
    implementation 'com.google.android.gms:play-services-ads:15.0.1' // EDIT: added

    implementation project(':GoogleMobileAdsPlugin')
    implementation project(':unity-android-resources')

}

android {
    compileSdkVersion 28
    buildToolsVersion '28.0.0'

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        applicationId 'blue.macho.breakplates2'
        ndk {
            abiFilters 'armeabi-v7a', 'x86'
        }
        versionCode 1
        versionName '1.0'
    }
    // ...
}

答案 2 :(得分:0)

找到了对我有用的统一论坛的解决方案。将构建系统从Gradle更改为Internal

引用https://forum.unity.com/threads/android-build-multiple-dex-files-error-multiple-dex-files-define-lcom-google-unity-buildconfig.542843/

来自sRn ” 嘿, 我希望这是Unity错误,并且会在某个时候得到解决,因为在我将项目更新到2018.2.1f1 Unity版本后,我开始收到此错误。 要解决此问题,您可以尝试启用Multidex,但是在这样做之后,我陷入了另一个问题,因此我不再打扰,返回了2018.1.x-这对我来说很好。 另外,您也可以将Build System从Gradle更改为Internal(在File-> Build Settings-> Build System中),这对我也很有效,但是我想留在Gradle进行更多控制,因此重新找回是更有意义的至2018.1。 “

答案 3 :(得分:0)

我通过以下方法解决了错误: 服务->广告->高级->取消选中:启用内置广告扩展。

积分:https://forum.unity.com/threads/android-build-multiple-dex-files-error-multiple-dex-files-define-lcom-google-unity-buildconfig.542843/