72

I used gradle build command in Centos 7 terminal and I got output:

FAILURE: Build failed with an exception.

* What went wrong:
Could not create service of type InitScriptHandler using BuildScopeServices.createInitScriptHandler().

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
4
  • 2
    Do you have appropriate privileges to run the command?
    – Opal
    Commented Jun 15, 2015 at 8:33
  • I think you are right, problem is with privileges, because in root user gradle work correctly, I try set 777 permissions to gradle folder using command chmod -R 777 ./gradle-2.4 ,but error still occur. Gradle folder I unziped in user home directory with which I get the error described above. Commented Jun 15, 2015 at 10:12
  • Plonking sudo in front of the command I typed fixed this. The error message could be a bit more helpful in this regard.
    – ben3000
    Commented Jun 29, 2017 at 3:09
  • 1
    Worked for me when I killed gradle daemon.
    – czesiek
    Commented Aug 16, 2017 at 7:49

28 Answers 28

52

For me, killing the Gradle daemon (gradle --stop) really helped and fixed the issue.

2
  • 3
    Same here, my disk was full but even after making more room it still wouldn't work because the Gradle daemon didn't get the news for some reason.
    – wkarl
    Commented Feb 15, 2018 at 9:21
  • 2
    Same here with the wrapper: ./gradlew --stop. Thanks!
    – noBillSide
    Commented Jun 25, 2020 at 10:18
50

Try setting your GRADLE_USER_HOME variable to a folder where you have valid access. Then this error will go away.

For ex: I faced the same issue today while I was running gradle clean command on a new slave machine.

My Gradle version was 2.3.

With --stacktrace, I came to know it was trying to create .gradle folder for storing Gradle's cache data (while I invoked Gradle to run clean task on the slave) and it was trying to create that folder under /some/location/where/gradle/exists OR some /path/location/xxx/yyy where the user which was running Gradle on the slave machine didn't have valid access to write (create folder/files).

i.e. the user which I used to connect from Jenkins machine to the slave didn't have write access to touch/mkdir anything in the default location (where Gradle thought, OK I should create .gradle folder here).

To fix it, I added the above GRADLE_USER_HOME variable in the slave's ENVIRONMENT Variable section. Now, as I have valid access in my home directory, I was OK.

enter image description here

Setting:

GRADLE_USER_HOME=~/gradle_2_3_cache/.gradle

resolved the issue.

You can set it to ~/.gradle as well. But I set it under a custom folder inside my ~ home directory (gradle_2_3_cache). This will help me in case I have another job/build run running on the same Slave machine but with a different Gradle version for ex: 2.5 etc version and if I want the .gradle cache for 2.3 and 2.5/x version in separate folders.

NOTE: When using parallel section within Jenkinsfile, it's best to avoid Gradle greatness (i.e. using same Gradle's cache i.e. using same GRADLE_USER_HOME) as otherwise, you'll land into a mine of interesting issues as listed here: Jenkins - java.lang.IllegalArgumentException: Last unit does not have enough valid bits & Gradle error: Task 'null' not found in root project

7
  • Can you please explain it in more details. As I am also facing the issue. And I am a new Bee in Android. Commented Nov 2, 2017 at 13:20
  • what is the slaves environment section?
    – JJ F
    Commented Jan 27, 2019 at 12:45
  • So when you connect a slave to Jenkins master, it appears as one of the node (in jenkins terms). In that node's configuration, you can add an ENV variable for GRADLE_USER_HOME and set the value using the Gradle's location (ex shown above)
    – AKS
    Commented Feb 1, 2019 at 22:16
  • See image snapshot above.
    – AKS
    Commented Feb 1, 2019 at 22:22
  • @Jose See attached image snapshot above. You should start looking into "Jenkinsfile" concept now. Learn what it's and how you can do the above using that. It'll help you better later on.
    – AKS
    Commented Feb 1, 2019 at 22:28
20

The Problem solved by simply using "sudo" and giving access to gradle to create a folder and write cache. use:

sudo ./gradlew
1
  • 4
    This might work, but it is not safe. If a build script runs amok (through malice or through carelessness) it could damage the system it is running on. It will also proliferate the regular user / super user issues by creating build output with the super user's ID.
    – neuralmer
    Commented Sep 27, 2019 at 17:39
12

If you using wrapper gradlew, in root make directory .gradle_new

mkdir .gradle_new
chmod -R 777 .gradle_new

and run gradlew with arguments:

--project-cache-dir .gradle_new
1
  • Thanks it worked, I just gave permission to existing `.gradle1 directory
    – Sanjeev
    Commented Apr 23, 2019 at 7:36
8

I had the same problem. For me it worked after I exclude the .gradle folder if you can not delete try to rename.

1
  • 3
    Running accidentally gradle as root will set the owner of PROJECT/.gradle to root. Fixing permissions worked, thanks.
    – Mr. Fish
    Commented Dec 14, 2018 at 14:41
8

Restarting the machine solved the issue.

2
  • 6
    This should be the first approach before any stackoverflow question :P
    – Emjey
    Commented Mar 25, 2019 at 7:38
  • @Emjey exatly :D Commented Dec 12, 2022 at 9:07
7

Permission issue. This fixed the issue for me:

sudo chown -R $USER dir

6

If you have just updated your JDK version and you have set up a Gradle wrapper in your project, you may want to double-check the wrapper version supports your new JDK. If not, consider removing wrapper-related files from the project (gradlew, gradlew.bat and gradle/wrapper/*) and re-generating them with the Gradle CLI, like so:

gradle wrapper --gradle-version <new-version-number>

e.g. gradle wrapper --gradle-version 4.10.2

This of course assumes your Gradle installation is up-to-date. If not, you will want to update that first.

3
  • The whole point of the gradle wrapper, is to not depend on a gradle installation.
    – Glenn Bech
    Commented Nov 2, 2018 at 10:13
  • 1
    For the end user, yes. However, you need to generate the wrapper files to deliver to that user. As a developer, it is prudent to check your Gradle installation is up-to-date before generating said files - especially if you are experiencing errors after upgrading your JDK.
    – Flic
    Commented Nov 7, 2018 at 1:17
  • For me, deleting gradlew, gradlew.bat and gradle/wrapper/* worked. After that AndroidStudio just showed some message and generated the missing files.
    – Nil Oleart
    Commented Feb 28, 2019 at 11:39
4

I got the same error, got rid of it by using the correct version of Java / JDK. I was trying to build a Java 8 project with the Java 11 JDK. Check which version of Java JDK you are using.

To develop projects with different Java versions in parallel I now use jEnv to manage the different JDK versions: http://www.jenv.be/

4

This is a permission issue. do a gradle wrapper --stacktrace you should see something like this Failed to create parent directory '/home/cloud_user/my-project/gradle' when creating directory '/home/cloud_user/my-project/gradle/wrapper'

the user, cloud_user, has no permission to the directory make cloud_user owner of the folder sudo chown -R cloud_user:cloud_user /home/cloud_user/my-project/

4

I got this error when running code in IntelliJ Idea, and

gradle --stop

really not helped, as it said that "No Gradle daemons are running."

But simple kill of all gradle processes helped:

ps aux | grep gradle
kill -9 $PID
2

For me this was to do with Java versions. I have Java 10 installed and as the default Java on my system. Setting a JAVA_HOME pointing at Java 8 was sufficient for the project (graphql-spring-boot) to build.

2

If anyone is still struggling with this, my issue was caused by Microsoft preventing Java from running due to Controlled Folder Access security restrictions.

enter image description here

I didn't get the popup notification since my computer is set to Do Not Disturb Mode. Once I allowed access, Gradle ran fine.

1

If using the "Invoke Gradle script" build step, click on Advanced to reveal additional options. Locate "Force GRADLE_USER_HOME to use workspace" and check it.

Invoke Gradle script screenshot

0

For future reference. I had the same problem, the issue was that the antivirus was blocking OpenJdk platform binary and java.exe which prevented android studio from being able to modify the files

0

I ran into this exception when trying to build a project that was mounted as read-only filesystem in a VM. The project set its own gradle cache so changing GRADLE_USER_HOME did not work. I had to change the filesystem to be read/write.

0

You Just Have to Run it under the super user (sudo ....) it works for me

1
  • This might work, but it is not safe. If a build script runs amok (through malice or through carelessness) it could damage the system it is running on. It will also proliferate the regular user / super user issues by creating build output with the super user's ID.
    – neuralmer
    Commented Sep 27, 2019 at 17:37
0

If you run Docker-in-Docker and mount the project directory from docker host directly to docker container:

-v ${PWD}:/path_to_project -w /path_to_project

the owners are different and docker container user (either gradle or root) can't override/delete ./buildSrc/build or ./build/

One of the fixes - copy the sources inside the container to temporary directory and build there.

Smth like this (first mounted to project, but then copied to project-copy to "decouple" with the host system real files and run the build in the copy):

docker run -v "${PWD}":/home/gradle/project -w /home/gradle/project-copy \
    --rm \
    --entrypoint sh \
    gradle:5.5.1-jdk11 \
    -- -c "cp -r -T /home/gradle/project ./  &&  ./gradlew build"
0

In my case I had bad credentials to private Maven repository. JIdea does not show the inner exception but running gradle build reveals the problem immediately.

0

I used a Gradle project in Eclipse, Eclipse's Gradle was giving that error. When I installed NetBeans and its Gradle Plugin, the problem has gone, project was building okay. Another way to circumvent this error was using commandline ./gradlew , project also built successfully.

It seems that Eclipse's era is over, they appear to be unable to follow the progress.

0

This can happen if you run Gradle commands from separate terminals at the same time - I assume Gradle somehow locks ~/.gradle when it is running to prevent any problems.

0

I had that same error, while running gradle with java 14 (openjdk) as my default java implementation. Setting default java back to java 8 solved the issue

sudo update-alternatives --auto java
0

just run (taskkill /im java.exe /f) in command

0

In my case the error was different but I landed here, my error was:

Could not create service of type ExecutionHistoryStore using ExecutionGradleServices.createExecutionHistoryStore().
         > Timeout waiting to lock execution history cache (/Volumes/Extreme SSD/FlutterProject/test/android/.gradle/6.7/executionHistory). It is currently in use by another Gradle instance.
           Owner PID: unknown                                           
           Our PID: 94001                                               
           Owner Operation: unknown                                     
           Our operation:                                               
           Lock file: /Volumes/Extreme SSD/FlutterProject/test/android/.gradle/6.7/executionHistory/executionHistory.lock

It happens when you try to run flutter run from two separate terminals. So I solved it by executing cd <projectPath> && flutter run from the active terminal which I was using first. Basically, while executing some Gradle command, in this case building an android file, the terminal locks down some Gradle PID, so either you stop them or just simply use that terminal.

0

in my case manual removing the .gradle and .idea folders helped me you can find them in the Android Studio if Project view selected after this just clean and rebuild app

enter image description here

0

kill -9 'pid' solved the issue. pid can be found in the error log

0

To fix this issue, restarting your PC is the main solution

-1

I got the same issue and for me worked below command.

./gradlew --stop

After that restart my system and wipe data of simulator and run again everything works fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.