11

I can't seem to get my Android applications to compile due to a really annoying exception from Android Studio:

]()![enter image description here

Execution failed for task ':myapp-services:compileDebugJava'.
    Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory.

I'm on Ubuntu 12.04, and I'm running Oracle's Java 7 JRE/JDK.

In ~/.bashrc:

export JAVA_HOME="/usr/lib/jvm/java-7-oracle/"

In android-studio/bin/studio.sh:

export JAVA_HOME="/usr/lib/jvm/java-7-oracle/"

Output of javac -version:

javac 1.7.0_51

Output of java -version:

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

Output of printenv JAVA_HOME:

/usr/lib/jvm/java-7-oracle

Output of which javac:

/usr/bin/javac

I have also logged out and logged back in again to my session.

I'm really not sure what I'm missing here. Why can't Android Studio compile my application?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411

9 Answers9

16

I solved it by updating build.gradle (Top-Level) with

classpath 'com.android.tools.build:gradle:2.1.2'
Don Chakkappan
  • 7,397
  • 5
  • 44
  • 59
10

I found a solution in a different answer:

rm ~/.AndroidStudioPreview/config/options/jdk.table.xml

What seems to have happened is that something was configured for a previous version of Android Studio and this configuration lived too long :)

Community
  • 1
  • 1
Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • Brilliant! For me it was ~/.AndroidStudioBeta/config/options/jdk.table.xml but that's the same principle. I switched from opensdk to oracle jdk and couldn't compile anymore. Note that you need to restart studio in order to re-index and make it work. – Yano Aug 27 '14 at 20:56
  • On Windows, this is C:\Users\\.AndroidStudio\config\options\jdk.table.xml, but removing it still didn't fix the error. – Roy Oliver Mar 10 '17 at 16:39
  • I guess because my error starts with "Error:Execution failed for task ':CordovaLib:compileDebugJavaWithJavac'.". – Roy Oliver Mar 10 '17 at 16:51
2

I was getting this error too. After many hours trying to solve it, I've managed to solve the problem.

In my case, executing the app from command line did the trick!

Here's what I did (I'm using Windows 10). Please, run these commands on the root of the project (where we have the app directory):

1) gradlew assembleDebug

  • Gradle will execute its tasks and you should see the message: BUILD SUCCESSFUL

2) adb install -r app\build\outputs\apk\app-debug-unaligned.apk

  • After executing this line, you'll see the message: [100%] /data/local/tmp/app-debug-unaligned.apk pkg: /data/local/tmp/app-debug-unaligned.apk Success

3) adb shell am start -n package_name/package_name.MainActivity, where "package_name" must be replaced by your package name (you can find it on AndroidManifest.xml). If "MainActivity" is not your launcher activity, replace it by your own activity.

  • You'll see the message: Starting: Intent { cmp=package_name/.MainActivity }, where "package_name" is your app' package name.

It will install the app on your device and after that, Android Studio seems to find the JAVA_HOME variable again. You should be able to run the app by hitting the "Run" button on Android Studio.

Hope it helps someone!

Loubake
  • 71
  • 2
2

I have solved it by updating classpath of (build.gradle (Project:projectName))

classpath 'com.android.tools.build:gradle:2.2.3' 
Ankit Saini
  • 342
  • 3
  • 11
2

Go To Settings->Project Structure->JDK path and change it to the system JDK instead of Studio JDK, you will be up and running.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
1

update build.gradle module

    classpath 'com.android.tools.build:gradle:3.0.1'

Solved my issue

Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
0

Just downloaded the latest version (android-studio-ide-1641136.dmg) and I just changed the Info.plist file content. It's originally configured to use 1.6* as of Java version. I have 1.8 installed, so just changed to 1.8* and worked.

I am on Mac Yosemite.

P. Lalonde
  • 694
  • 1
  • 7
  • 17
0

I have found a problem with the Android Studio studio.bat file and here it is

    ::------------------------------------------------------
    :: Locate a JDK installation directory which will be used to run the IDE.
    :: Try (in order): ANDROID_STUDIO_JDK, ..\jre, JDK_HOME, JAVA_HOME.
    :: ---------------------------------------------------------------------
    IF EXIST "%ANDROID_STUDIO_JDK%" SET JDK=%ANDROID_STUDIO_JDK%
    IF NOT "%JDK%" == "" GOTO jdk
    IF EXIST "%~dp0\..\jre" SET JDK=%~dp0\..\jre
    IF NOT "%JDK%" == "" GOTO jdk
    IF EXIST "%JDK_HOME%" SET JDK=%JDK_HOME%
    IF NOT "%JDK%" == "" GOTO jdk
    IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME%
    IF "%JDK%" == "" GOTO error

Do you get the problem in the last 2 lines of code?
It seems that if your environmental variable is %JAVA_HOME% the batch file will GOTO error and an error message will be displayed and that's it NO ANDROID STUDIO FOR YOU, even though %JAVA_HOME% contains a valid path but if the environmental variable is something like %ANDROID_STUDIO_JDK% like mine is because I created it myself then it will work


PROPOSED SOLUTIONS

(1) Edit the batch file Where you see this IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME% IF "%JDK%" == "" GOTO error
Change it to this
IF EXIST "%JAVA_HOME%" SET JDK=%JAVA_HOME% IF NOT "%JDK%" == "" GOTO jdk IF "%JDK%" == "" GOTO error
(2) Create an environmental variable named %ANDROID_STUDIO_JDK% as shown here https://kb.wisc.edu/cae/page.php?id=24500

FalconBot
  • 419
  • 1
  • 4
  • 12
0

Check echo $JAVA_HOME

if it doesn't gives you correct path. follow setting JAVA_HOME path on Ubuntu

if it gives you correct path set then you need to update your gradle plugin version (It worked for me) and rebuild project.

Community
  • 1
  • 1
user3024215
  • 196
  • 1
  • 7