Android Studio 中任务 :app:compileDebugJavaWithJavac 的执行失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33404552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:38:07  来源:igfitidea点击:

Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio

javaandroidandroid-studiogradle

提问by MrTimotheos

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.

我正在 Android Studio 中开发一个 Android 应用程序。不太确定出了什么问题。我几天前成功构建。任何帮助都会很棒。

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

这是我的 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'
}

采纳答案by starkshang

Try to upgrade your buildToolsVersionto "23.0.1", like this:

尝试将您的升级buildToolsVersion到“23.0.1”,如下所示:

compileSdkVersion 23
buildToolsVersion "23.0.1"

If you didn't install the buildTools for this version, please download it with SDKManager as hint.

如果您没有安装此版本的buildTools,请使用SDKManager 作为提示下载。

回答by Yuchao Zhou

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

这是因为您的 $JAVA_HOME 未设置。如果您使用的是 Mac:

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

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

回答by S.M.Fazle Rabbi

1st step:

第一步:

1st stape

第一钉

2nd step:

第二步:

2nd Step

第二步

I try both of ans , But nothing work . I thing here is the solution.

我尝试了两个答案,但没有任何效果。我这里的事情是解决方案。

From your project-

从你的项目-

go to Gradle Scripts >> build.gradle (Module:app) then remove below code (if exist)

转到 Gradle Scripts >> build.gradle (Module:app) 然后删除下面的代码(如果存在)

.............................................

……………………………………………………………………………………………………………………………………………………………………

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_6
}

.............................................

……………………………………………………………………………………………………………………………………………………………………

remove it and then clean the project then Run. no java home need to to set, no need to change version

删除它,然后清理项目,然后运行。无需设置java home,无需更改版本

回答by Igor Wojda

Set JDK location in project settings enter image description here

在项目设置中设置 JDK 位置 在此处输入图片说明

回答by carryoner

Please check your app's build.gradle. I had the same problem, finally I found the problem was in my build.gradle file dependencies{}, it add extra .jar file which actually didn't exist in my project as dependency. So I delete this dependency, and the problem has gone.

请检查您的应用程序的 build.gradle。我遇到了同样的问题,最后我发现问题出在我的 build.gradle 文件依赖项{}中,它添加了额外的 .jar 文件,该文件在我的项目中实际上并不存在作为依赖项。所以我删除了这个依赖,问题就解决了。

回答by Charan Reddy

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

在 Android Studio 中打开: File > Project Structure并检查 JDK 位置是否指向您的 JDK 1.8 目录。

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

注意:您可以使用compileSdkVersion 24. 相信我。为此,首先从 Oracle 下载最新的 JDK。

回答by AMIT

in build file change compile files('AF-Android-SDK.jar')to compile files('libs/AF-Android-SDK.jar')it will work

在构建文件中,将compile files('AF-Android-SDK.jar')更改为compile files('libs/AF-Android-SDK.jar')它将起作用

回答by Raj

I got this issue solved.

解决了这个问题。

I was trying to compile this project "Waveform Android" - https://github.com/Semantive/waveform-android

我试图编译这个项目“Waveform Android” - https://github.com/Semantive/waveform-android

and got I this error.

并得到我这个错误。

I am using Android studio on Ubuntu 14.04LTS.

我在 Ubuntu 14.04LTS 上使用 Android Studio。

I have JAVA 8 Installed.

我安装了 JAVA 8。

in my gradle build script file there was some statements as below.

在我的 gradle 构建脚本文件中,有一些语句如下。

retrolambda {
    jdk System.getenv("JAVA8_HOME")
    oldJdk System.getenv("JAVA7_HOME")
    javaVersion JavaVersion.VERSION_1_7
}

I changed the "JAVA8_HOME" to "JAVA_HOME"because in my environment variables the java home directory is set as JAVA_HOME not as JAVA8_HOME and then It built succesfully.

我将“JAVA8_HOME”更改为“JAVA_HOME”,因为在我的环境变量中,java 主目录设置为 JAVA_HOME 而不是 JAVA8_HOME,然后它成功构建。

after changing the build script.

更改构建脚本后。

retrolambda {
    jdk System.getenv("JAVA_HOME")
    oldJdk System.getenv("JAVA7_HOME")
    javaVersion JavaVersion.VERSION_1_7
}

Or the other way you can create a new environment variable named JAVA8_HOMEpointing to the right JDK location, but I have not tried that though because I dont want environment variables for each JDK version.

或者,您可以创建一个名为JAVA8_HOME的新环境变量,指向正确的 JDK 位置,但我还没有尝试过,因为我不想要每个 JDK 版本的环境变量。

回答by user6813755

I had this same issue. I was working with a team of developers using a Mac and everybody else on the team was running Windows. However we were using the Anroid Studio 2.2-beta with jdk 1.8. If you are on a mac and come across this issue, here is how to solve it. DONT USE Android Studio 2.2-beta. I spent hours trying to debug this error in 2.2-beta. I solved it by simply changing to Android Studio 2.1.3. Our app ran instantly after changing the Android Studio version. Also on Android Studio's website before you download it warns mac users about running jdk 1.8 on 2.2 beta. Guess I should have read the fine print haha

我有同样的问题。我和一个使用 Mac 的开发团队一起工作,团队中的其他人都在运行 Windows。但是,我们使用的是带有 jdk 1.8 的 Anroid Studio 2.2-beta。如果您使用 Mac 并遇到此问题,请参阅以下解决方法。不要使用 Android Studio 2.2-beta。我花了几个小时试图在 2.2-beta 中调试这个错误。我通过简单地更改为 Android Studio 2.1.3 解决了它。我们的应用程序在更改 Android Studio 版本后立即运行。同样在 Android Studio 的网站上,在您下载之前,它会警告 Mac 用户在 2.2 beta 上运行 jdk 1.8。我猜我应该读过细则哈哈

回答by Zhang

Update (06/05/2017)

更新 (06/05/2017)

I wanted to use Realmfor Android and that required Retrolambda. Problem is Retrolambda conflicts with Hyman.

我想将Realm用于 Android 并且需要Retrolambda。问题是 Retrolambda 与 Hyman 冲突。

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

因此,我从下面原始答案中显示的 gradle 中删除了我的 Hyman 选项配置,并进行了以下更改:

// ---------------------------------------------
// 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

工具.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
  1. 退出模拟器
  2. 退出 Android Studio
  3. 重新打开 Android Studio
  4. 构建 > 清理项目
  5. 再次在您的设备/模拟器上运行/调试您的应用程序

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:

我不确定 tools.jar 做了什么或者它是否重要。像这个 Stackoverflow 问题中的其他用途一样:

Can't build Java project on OSX yosemite

无法在 OSX Yosemite 上构建 Java 项目

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).

不幸的是,我们不得不使用AUSKey(澳大利亚政府使用的一些古老的恐龙Java身份验证密钥系统来验证我们的计算机,然后才能登录澳大利亚商业门户网站)。

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

我的猜测tools.jar可能是 AUSKey 的 JAR 文件。

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

希望有帮助:D

Original Answer

原答案

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

我今天遇到了这个问题(27/06/2016)。

I downloaded Android Studio 2.2 and updated JDK to 1.8.

我下载了 Android Studio 2.2 并将 JDK 更新到 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:

除了上述指向正确 JDK 路径的答案外,我还必须在我的build.gradle(Module: app)文件中另外指定 JDK 版本:

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"

        HymanOptions {
            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 Hyman enabled, you need to add the following to your gradle file (as shown above):

另请注意,如果您遇到关于 的错误Java 8 language features requires Hyman enabled,您需要将以下内容添加到您的 gradle 文件中(如上所示):

HymanOptions {
    enabled true
}

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

这样做之后,我终于在我的手机上运行了我的新项目应用程序。