Error in Unity 2019.2.15f1 While Building Android Game

When I try to build my Android game, I get the error below. I can’t find anything that helps on the internet.
I’ve set up the keystores and chose Android 7.0 and Android 9.0 as minimum and target API levels, which I have downloaded using Andoid Studio.
In my Preferences, I have selected “Android SDK Tools Installed with Unity”.

When I click “Build”, it gets stuck on “Building gradle project” and Unity freezes for 30 minutes, and then I get the error:


CommandInvokationFailure: Gradle build failed.  C:/Program Files/Unity/Hub/Editor/2019.2.15f1/Editor/Data/PlaybackEngines/AndroidPlayer/Tools\OpenJDK\Windows\bin\java.exe
    -classpath "C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-5.1.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"
    
    stderr[
    
    FAILURE: Build failed with an exception.
    
    * What went wrong: A problem occurred configuring root project 'gradleOut'.
    > Could not resolve all artifacts for configuration ':classpath'.
       > Could not download builder.jar (com.android.tools.build:builder:3.4.0)
          > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/builder/3.4.0/builder-3.4.0.jar'.
             > Premature end of Content-Length delimited message body (expected: 31027880; received: 23854928
    
    * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
    
    * Get more help at https://help.gradle.org
    
    BUILD FAILED in 30m 43s ] stdout[
    
    ] exit code: 1 UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.Android.Command.Run (System.String command, System.String args, System.String workingdir, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.Android.AndroidJavaTools.RunJava (System.String args, System.String workingdir, System.Action`1[T] progress, System.String error) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <21da158cd428404389e0b5a08ba9ea0a>:0) Rethrow as GradleInvokationException: Gradle build failed UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <21da158cd428404389e0b5a08ba9ea0a>:0) Rethrow as BuildFailedException: Exception of type 'UnityEditor.Build.BuildFailedException' was thrown. UnityEditor.Android.PostProcessor.CancelPostProcess.AbortBuild (System.String title, System.String message, System.Exception ex) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (UnityEditor.BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args, UnityEditor.BuildProperties& outProperties) (at <21da158cd428404389e0b5a08ba9ea0a>:0) UnityEditor.PostprocessBuildPlayer.Postprocess (UnityEditor.BuildTargetGroup targetGroup, UnityEditor.BuildTarget target, System.String installPath, System.String companyName, System.String productName, System.Int32 width, System.Int32 height, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:281) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)

Build completed with a result of 'Failed'
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)


UnityEditor.BuildPlayerWindow+BuildMethodException: 18 errors
  at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (UnityEditor.BuildPlayerOptions options) [0x00242] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:190 
  at UnityEditor.BuildPlayerWindow.CallBuildMethods (System.Boolean askForBuildLocation, UnityEditor.BuildOptions defaultBuildOptions) [0x0007f] in C:\buildslave\unity\build\Editor\Mono\BuildPlayerWindowBuildMethods.cs:95 
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)

I tried searching all of the errors in the “What went wrong” section but could not find anything useful.

Thanks.



UPDATE: I read somewhere that a bad WiFi connexion could be the case. I tried it again over hotspot and got another error.


CommandInvokationFailure: Gradle build failed. 
C:/Program Files/Unity/Hub/Editor/2019.2.15f1/Editor/Data/PlaybackEngines/AndroidPlayer/Tools\OpenJDK\Windows\bin\java.exe -classpath "C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\Tools\gradle\lib\gradle-launcher-5.1.1.jar" org.gradle.launcher.GradleMain "-Dorg.gradle.jvmargs=-Xmx4096m" "assembleRelease"

stderr[

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':lintVitalRelease'.
> Could not resolve all files for configuration ':lintClassPath'.
   > Could not download lint-checks.jar (com.android.tools.lint:lint-checks:26.4.0)
      > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/lint/lint-checks/26.4.0/lint-checks-26.4.0.jar'.
         > Connection reset
   > Could not download groovy-all.jar (org.codehaus.groovy:groovy-all:2.4.15)
      > Could not get resource 'https://jcenter.bintray.com/org/codehaus/groovy/groovy-all/2.4.15/groovy-all-2.4.15.jar'.
         > Connection reset

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

* Get more help at https://help.gradle.org

BUILD FAILED in 3m 5s
]
stdout[

> Configure project :
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\build-tools\28.0.3\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platform-tools\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platforms\android-28\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK	ools\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\build-tools\28.0.3\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platform-tools\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK\platforms\android-28\package.xml. Probably the SDK is read-only
Exception while marshalling C:\Program Files\Unity\Hub\Editor\2019.2.15f1\Editor\Data\PlaybackEngines\AndroidPlayer\SDK	ools\package.xml. Probably the SDK is read-only

> Task :preBuild UP-TO-DATE
> Task :preReleaseBuild
> Task :compileReleaseAidl NO-SOURCE
> Task :compileReleaseRenderscript NO-SOURCE
> Task :checkReleaseManifest
> Task :generateReleaseBuildConfig
> Task :prepareLintJar
> Task :generateReleaseSources
> Task :javaPreCompileRelease
> Task :mainApkListPersistenceRelease
> Task :generateReleaseResValues
> Task :generateReleaseResources
> Task :mergeReleaseResources
> Task :createReleaseCompatibleScreenManifests
> Task :processReleaseManifest
> Task :processReleaseResources
> Task :compileReleaseJavaWithJavac
> Task :compileReleaseSources
> Task :lintVitalRelease
> Task :lintVitalRelease FAILED

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings
13 actionable tasks: 13 executed
]
exit code: 1
UnityEditor.Android.Command.WaitForProgramToRun (UnityEditor.Utils.Program p, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.Android.Command.Run (System.String command, System.String args, System.String workingdir, UnityEditor.Android.Command+WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.Android.AndroidJavaTools.RunJava (System.String args, System.String workingdir, System.Action`1[T] progress, System.String error) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
Rethrow as GradleInvokationException: Gradle build failed
UnityEditor.Android.GradleWrapper.Run (UnityEditor.Android.AndroidJavaTools javaTools, System.String workingdir, System.String task, System.Action`1[T] progress) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.Android.PostProcessor.Tasks.BuildGradleProject.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
Rethrow as BuildFailedException: Exception of type 'UnityEditor.Build.BuildFailedException' was thrown.
UnityEditor.Android.PostProcessor.CancelPostProcess.AbortBuild (System.String title, System.String message, System.Exception ex) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (UnityEditor.BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args, UnityEditor.BuildProperties& outProperties) (at <21da158cd428404389e0b5a08ba9ea0a>:0)
UnityEditor.PostprocessBuildPlayer.Postprocess (UnityEditor.BuildTargetGroup targetGroup, UnityEditor.BuildTarget target, System.String installPath, System.String companyName, System.String productName, System.Int32 width, System.Int32 height, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:281)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr) (at C:/buildslave/unity/build/Modules/IMGUI/GUIUtility.cs:179)


FINAL UPDATE: I did it again over hotspot and it worked! Can’t believe I wasted 2 days for such a dumb solution.

Good internet connection solved my problem too