Unity Android build error

(idk if this is a bug or Im doing something wrong)
Basically i’ve been working on a 2d game that was initially for PC, i added unity standard assets and android controls, but when i build the project (APK), it shows me this error. I tried clearing cache, restarting Unity, even tried all compression methods and texture compression but nothing seems to work, Every time it’s the same error.

9157475--1273676--upload_2023-7-19_22-22-14.png

I’m also trying to solve this issue. Did you check the specific gradle error? If you click the “command invokation” error and scroll down, you should get something more specific from gradle itself.

The following are all the error messages expanded, I dont know what they mean, even ChatGPT’s solution didn’t help.

  • What went wrong:
    Could not create service of type FileHasher using GradleUserHomeScopeServices.createCachingFileHasher().

Timeout waiting to lock file hash cache (C:\Users\user.gradle\caches\6.1.1\fileHashes). It is currently in use by another Gradle instance.

^^^This is the pertinent part. Don’t know how to fix it though

im pretty sure i did something wrong, im trying to backtrack my steps. I’ve been following a Unity course tutorial and most of what i have done is same as what’s in the tutorial, for some reason, they were able to build the APK for their project without any error/warning, but im stuck here.

If you want i can send you my project’s github repo, or the course link i’ve been following

Sure, for me this only started after I switched to a new PC. Something is preventing gradle from from releasing the lock files, but I don’t know what. Another post said it might be anti-virus, but I don’t have one

Same goes over here, I don’t have an anti virus, I even tried it with my windows defender turned off .
well, this is the github repo, let me know if you have any problem building it. (if it builds on your system, then it might be my laptop)
https://github.com/Anmol-1903/Space-Shooter

How to troubleshoot build failures:

First, make a blank project with a single blank scene and prove that it builds successfully.

If the blank project does NOT build, go fix your Unity installation or your other tools, such as Android SDK, NDK, JDK, etc. It may even be necessary to change to a different version of Unity3D. It is generally best to stay with LTS versions of Unity3D.

Until you can build a blank project to the target platform, don’t fiddle with anything else.

Once you can build a blank project, now bisect the problem by bringing over parts of your current project and building it one subsystem at a time, perhaps stubbing things out that might trigger compiler errors.

Most often things that prevent building are third-party libraries such as Firebase.

Once you identify the subsystem, go to the documentation for it and make sure you are doing it correctly.

It may also be helpful to work through a tutorial or two for whatever subsystem is making the build fail.

Android build not building:

Recently (circa July 2022) there have been reports of Unity’s installer failing to install the Android Tools.

Here was how I brought up Unity2020.3.41 and the Android SDK 31 on October 30, 2022:

Android Gradle errors and other related stuff:

Just tried to build your project, same error. I also agree with Kurt-Dekker, his advice seems like a good way to move forward

Quick Update on my end: I believe the problem is the Android SDK. Right now there appears to be some bugs with which one Unity tries to use, likely leading to the hanging and timeouts on gradle’s end. For example, right now, if you select API 34, it will prompt you to download API 36. This API doesn’t exist yet. Still trying to figure it out, but I feel like this is a solid lead to the problem.

Found another forum post mentioning this: Update Android SDK 36?

I tried all the API versions, as expected nothing changed, same error. Ima try making a new project and slowly adding all the components and building it

Made a completely new project on the same unity version, can’t even build the empty project. I think it’s the version

I think I have figured it out! I was able to get it to build by creating the repositories config file at C:\Users<your username>.android, it was missing. You can do this by pasting this New-Item ~\.android\repositories.cfg -Force into Powershell. I did this and tested on Unity version 2021.3.11f1. I noticed the warning when I was trying to try a different version and swapped over to Android in the build menu. Let me know if this worked for you, and good luck!

I once started to get these same error types (v2021):


Turned out it was due to the Project file path being too long. At least it looked like it, moving the project to the root drive fixed the errors.

Was this ever resolved?

I started seeing the problem. Have Unity 2022.3.7f1 and Unity 2021.2.8f1. Both were working fine, for months in the case of 2021.2.8f1 projects, and weeks in the case of 2022.3.7f1.

Then on Friday I saw the issue on the project built on Unity 2022.3.7f1:

FAILURE: Build failed with an exception.

* What went wrong:
Gradle could not start your build.
> Cannot create service of type BuildTreeActionExecutor using method LauncherServices$ToolingBuildTreeScopeServices.createActionExecutor() as there is a problem with parameter #1 of type List<BuildActionRunner>.
   > Could not create service of type FileHasher using GradleUserHomeServices.createCachingFileHasher().
      > Timeout waiting to lock file hash cache (C:\Users\<username>\.gradle\caches\7.2\fileHashes). It is currently in use by another Gradle instance.
        Owner PID: 36668
        Our PID: 8864
        Owner Operation:
        Our operation:
        Lock file: C:\Users\<username>\.gradle\caches\7.2\fileHashes\fileHashes.lock

Tried everything, including reinstalling Unity 2022.3.7f1 to a shorter path on my C: drive that has no spaces (i.e. avoiding “Program Files”). No luck.

Then today I went back to attempting to build a project on 2021.2.8f1 and that is broken as well.

FAILURE: Build failed with an exception.

* What went wrong:
Could not create service of type FileHasher using .createCachingFileHasher().
> Timeout waiting to lock file hash cache (C:\Users\<username>\.gradle\caches\6.1.1\fileHashes). It is currently in use by another Gradle instance.
  Owner PID: 24728
  Our PID: 23224
  Owner Operation:
  Our operation:
  Lock file: C:\Users\<username>\.gradle\caches\6.1.1\fileHashes\fileHashes.lock

* 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 1m 1s
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF-8
]
stdout[
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details
]
exit code: 1

So looks like I might have to format my HDD and reinstall everything from scratch (which I’d rather not do).

Does anyone at Unity know any workarounds for this issue that actually work?

A bump and link to a new thread I started to fix this issue. I know it doesn’t affect everyone but for those of us it that does, Unity is literally useless since we cannot even build an empty project for the Android platform.

No amount of deleting gradle cache files, teminating processes, restarting the pc, or specifying custom gradle base templates in the player/publishing settings will fix this issue - and I’ve been trying for 6 months now.

The new thread currently has the attention of a Unity employee so maybe we can get some traction:

So did you reformat your HDD to fix this? My issue is IDENTICAL, I used to be able to build for Android, now I can’t, with any Unity editor or version of Android.

This has been going on for 6 months now so my options if we can’t find a solution is also a reformat of my pc or more likely, a move to Unreal since Unity is no longer a functioning VR development platform for me.