Gradle Build Error

After updating Unity to version 5.6.3p2, I am getting an error when Unity is “Building Gradle Project” when trying to build an .apk for android platform.
This is what the console says;

CommandInvokationFailure: Gradle build failed.
C:/Program Files (x86)/Java/jdk1.8.0_131\bin\java.exe -classpath “C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-2.14.jar” org.gradle.launcher.GradleMain “assembleRelease”
FAILURE: Build failed with an exception.

  • What went wrong:
    A problem occurred configuring root project ‘gradleOut’.

Cannot add task ‘:prepareUnityAdsLibrary’ as a task with that name already exists.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
    Incremental java compilation is an incubating feature.
    Total time: 26.887 secs
    exit code: 1
    UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg)
    UnityEditor.Android.GradleWrapper.RunJava (System.String args, System.String workingdir, UnityEditor.Android.Progress progress)
    Rethrow as GradleInvokationException: Gradle build failed
    UnityEditor.Android.GradleWrapper.RunJava (System.String args, System.String workingdir, UnityEditor.Android.Progress progress)
    UnityEditor.Android.GradleWrapper.Run (System.String workingdir, System.String task, UnityEditor.Android.Progress progress)
    UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context)
    UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context)

After updating, I was having trouble with Untiy Ads. I was getting compiler errors concerning the Header File Advertisements. Turns out that for some reason after updating, Unity Ads was turned off or not connected to my project and could not reference that header. So I tried to update though the asset store only to have more problems. Then I learned that the latest version of the Ads plug-in is automatically added with newer version of Unity and I didn’t need to download through the Asset store. In fact doing so causes conflicts. I deleted the plug-in I downloaded from the asset store and reloaded and reconnected Unity Ads through the services tab but the problem still persists.

What should I do to solve this issue ? Thanks In advance.

@GODSPIRIT @thedefiancerebels @PouyaAR @Retz_Retro
That’s because you have probably import UnityADS from Asset store and also checked the “Enable built-in Ads extension” in the “Services” → “Unity ADS” → “Advanced”
Unchecking that option tell unity to use the package downloaded from asset store, et voilà…
Build with gradle comes back to work correctly ^^

I am getting the exact same error. Any luck?

I have the same problem. any suggestion?

I was met with the exact same error. I found that UnityAds was being accessed twice. Once within my UnityAds folder within Assets and again within my Plugins/Android folder within Assets. I selected one of the UnityAds.aar files and unchecked Android from the Include Platforms heading, this fixed this issue for me.

Hope it helps.

I have the same issue Cannot add task ‘:prepareUnityAdsLibrary’ as a task with that name already exists.
I imported the new Unity Ads and got the conflict because I have 2 unity ads aar in the plugins android directory that are conflicting and don’t know which one to use.
I removed unity-ads.aar and I can compile now.
Does anyone know if the new UnityAds.aar from the asset store works with AdMob mediation?

UnityAds.aar I believe is packaged with Unity Ads from the asset store and the other unity-ads.aar is from integration with AdMob mediation. I decided to move away from AdMob mediation for the GDPR stuff going on and use Unity Ads directly.