Execution Failed for task :app:compileDebugJavaWit

2019-01-02 20:43发布

I am developing an Android App in Android Studio. Not quite sure what went wrong. I was successfully building a few days ago. Any help would be great.

Here is the error:

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

Here is my build.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "21.1.2"

defaultConfig {
    multiDexEnabled true
    applicationId "com.tubbs.citychurchob"
    minSdkVersion 14
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'),        'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(dir: 'libs', include: 'Parse-*.jar')
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:cardview-v7:23.1.0'
compile 'com.parse.bolts:bolts-android:1+'
compile 'com.android.support:recyclerview-v7:23.1.0'
}

27条回答
爱死公子算了
2楼-- · 2019-01-02 21:09

This may happen if you are using Android+Lambdas. Sometimes I can use Lambdas without any issues but in other situations the project won't compile and gives the exception in the compiler (When I try to pass a Lambda or a method reference to ScheduledExecutorService.scheduleAtFixedRate).

There is a discussion in this link (https://bugs.openjdk.java.net/browse/JDK-8169759) where they explain why this happen. In the mean time I'm just using lambdas in my Android project when the compiler allows me.

查看更多
笑指拈花
3楼-- · 2019-01-02 21:10

I had this error when I tried to use retrolambda inside my unit tests. I didn't find a solution how to make retrolambda work with unit tests. So, I use old fashioned anonymous classes instead of lambdas with my unit tests, and it works for me.

查看更多
与风俱净
4楼-- · 2019-01-02 21:11

In Android Studio open: File > Project Structure and check if JDK location points to your JDK 1.8 directory.

Note: you can use compileSdkVersion 24. It works trust me. For this to work first download latest JDK from Oracle.

查看更多
步步皆殇っ
5楼-- · 2019-01-02 21:13

Set JDK location in project settings enter image description here

查看更多
爱死公子算了
6楼-- · 2019-01-02 21:15

Update (06/05/2017)

I wanted to use Realm for Android and that required Retrolambda. Problem is Retrolambda conflicts with Jack.

So I removed my Jack options config from my gradle shown in original answer below and made the following changes:

// ---------------------------------------------
// Project build.gradle file
// ---------------------------------------------
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.1'
        classpath 'me.tatarka:gradle-retrolambda:3.6.1'
        classpath "io.realm:realm-gradle-plugin:3.1.4"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

and

// ---------------------------------------------
// Module build.gradle file
// ---------------------------------------------
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    ...

Tools.jar

If you made those changes above and you still get the following error:

Execution failed for task ':app:compileDebugJavaWithJavac'.
com.sun.tools.javac.util.Context.put(Ljava/lang/Class;Ljava/lang/Object;)V

Try removing the following file:

/Library/Java/Extensions/tools.jar 

Then:

  1. Quit emulator
  2. Quit Android Studio
  3. Reopen Android Studio
  4. Build > Clean Project
  5. Run/debug your app onto your device/emulator again

All the changes fixed it for me.

Note:

I am not sure what tools.jar does or whether it's important. Like other uses in this Stackoverflow question:

Can't build Java project on OSX yosemite

We were unfortunate enough to have to use AUSKey (some ancient dinosaur Java authentication key system used by Australian Government to authenticate our computer before we can log into Australian business portal website).

My speculation is tools.jar might have been a JAR file for/by AUSKey.

If you're worried, instead of deleting this file, you can make a backup of the whole folder and save it somewhere just in case you can't login to Australian Business Portal again.

Hope that helps :D

Original Answer

I came across this problem today (27/06/2016).

I downloaded Android Studio 2.2 and updated JDK to 1.8.

In addition to the above answers of pointing to the correct JDK path, I had to additionally specify the JDK version in my build.gradle(Module: app) file:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

The resulting file looks like this:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.mycompany.appname"
        minSdkVersion 17
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        jackOptions {
            enabled true
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
}

Please also notice if you came across an error about Java 8 language features requires Jack enabled, you need to add the following to your gradle file (as shown above):

jackOptions {
    enabled true
}

After doing that, I finally got my new project app running on my phone.

查看更多
千与千寻千般痛.
7楼-- · 2019-01-02 21:19

This is because your $JAVA_HOME is not set. If you are using a Mac:

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home" in .bash_profile

查看更多
登录 后发表回答