android - Execution failed for task ':app:comp

2019-01-17 21:35发布

问题:

New to Android Studio + Gradle.

I'm trying to run my app from the command line using:

gradlew installDebug

The app gets installed on my device only when I execute that command when Studio just got opened and do that initial syncing/building thing.

Whenever I modified my code, and try to run that command again, the build fails and shows this error:

Execution failed for task ':app:compileDebugJavaWithJavac'
> Could not find tools.jar

How do I solve this problem? Thanks in advance.

回答1:

Setting the JAVA_HOME variable to C:\Program Files\Java\jdkX.XX worked for me.

It was previously set to the JRE directory for some reason.



回答2:

Are you on Linux? What does echo $JAVA_HOME outputs?

I had a similar issue when running react-native run-android. After setting $JAVA_HOME (which hadn't existed) to a JDK folder, the problem was solved. In my case, I added the following line to the end of .bashrc file:

export JAVA_HOME="path/to/a/folder/with/JDK/jdk1.8.0_60"

and restarted the terminal.



回答3:

I deleted .gradle file from project root directory and rebuilt the app and then everything was working fine.



回答4:

For anyone facing this issue after updating to AndroidStudio-2.0

I previously had AndroidStudio-1.3 and jdk-1.7.X_XX. When I updated to AndroidStudio-2.0, things worked fine until I rebooted the computer. After reboot, I encountered this error and none of the above answers worked for me. Here's what I did:

  1. Copied the project into a new folder
  2. Upgraded my jdk version from 1.7.X_XX to 1.8.X_XX
  3. Changed the JDK's path in the project structure to the new path:

    C:\Program Files\Java\jdk1.8.X_XX

  4. Gradle sync

And things worked.



回答5:

ALL above answers are correct. I just wanna mention that if you are using MAC, and the full path of JDK path should be

"/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home"

Do not miss /Contents/Home



回答6:

The previous answers did not work for me. This is what worked: (Ubuntu, but should work for others as well)

  1. Go to your project folder:/.../projectlocation

  2. Then to android folder: /.../projectlocation/android

  3. Find gradle.properties file and add this line anywhere in the file: org.gradle.java.home=/usr/local/jdk1.8.0_91

Notice that there are no quotes and the right side of the example is my JDK folder.



回答7:

I faced the same issue.I was looking for several hours on stack overflow and tried all of the way they said but was not able to solve the problem.Then I found that problem was in my project folder name.I just rename the folder name and everything worked fine.