Java 错误:Gradle:任务 ':app:preDexDebug' 执行失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23847482/
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
Error: Gradle: execution failed for task ':app:preDexDebug'
提问by Kersch
I had a self created jar from another projected imported as a library into my other project. When I changed code in that project and exported a new jar to replaced the old one I cannot run my app anymore. I only get the following error:
我从另一个项目中创建了一个自己创建的 jar 作为库导入到我的另一个项目中。当我更改该项目中的代码并导出一个新 jar 以替换旧的 jar 时,我无法再运行我的应用程序。我只收到以下错误:
I have tried removing and adding and adding as dependency, adding as library. Nothing seems to work. I have also done clean build and a rebuild.
我尝试删除和添加并添加为依赖项,添加为库。似乎没有任何效果。我也做了干净的构建和重建。
采纳答案by gMale
We've seen this problem in the past when our project was compiling with a version of Java different from the one used to compile the library.The magic numberis just used to identify class filesso that is not the problem here. The issue is the java version (0034.0000 == Java 8).
过去,当我们的项目使用与用于编译库的 Java 版本不同的 Java 版本进行编译时,我们已经看到了这个问题。在神奇的数字只是用来识别类文件,使这里是没有问题的。问题是 Java 版本 (0034.0000 == Java 8)。
The easiest thing to do is target Java 6, which may require removing newer syntax from your code. In our case, both the project and library were ours so we were able to add the following to force the version of Java that we needed:
最简单的方法是针对 Java 6,这可能需要从您的代码中删除较新的语法。在我们的例子中,项目和库都是我们的,所以我们能够添加以下内容来强制我们需要的 Java 版本:
Android Libraries
安卓库
for android libraries, add this code to the "android" extension object:
对于 android 库,将此代码添加到“android”扩展对象:
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
...
}
Java Libraries
Java 库
for java libraries, add this code at the "top level":
对于 Java 库,请在“顶级”添加此代码:
apply plugin: 'java'
version '1.8.1'
group 'com.yourcompany.package'
sourceCompatibility = JavaVersion.VERSION_1_6 //these two lines
targetCompatibility = JavaVersion.VERSION_1_6 //are the only ones that matter
NOTE: the last two lines are the only ones that matter, I added the others just to show where those lines belong, in respect to the rest of your gradle build file.
注意:最后两行是唯一重要的,我添加了其他行只是为了显示这些行所属的位置,相对于 gradle 构建文件的其余部分。
回答by user1794469
Although your problem seems fixed, I ended up here with a similar error (while building the samples from developer.android.com). In case it's helpful to others, I was able to solve it by setting JAVA_HOME to the appropriate value. In my case it was:
尽管您的问题似乎已解决,但我在这里遇到了类似的错误(在从 developer.android.com 构建示例时)。如果对其他人有帮助,我可以通过将 JAVA_HOME 设置为适当的值来解决它。就我而言,它是:
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk-amd64
回答by jwinwood
adding:
添加:
sourceCompatibility = JavaVersion.VERSION_X_X
targetCompatibility = JavaVersion.VERSION_X_X
in the Android project gradle file and the Java library gradle file worked for me.
在 Android 项目 gradle 文件和 Java 库 gradle 文件中为我工作。
Sorry I would have +1'd it but don't have a high enough reputation
抱歉,我会为它 +1 但没有足够高的声誉
回答by Dan Caseley
For us, this was caused by the Android SDK build-tools version. Got the problem on v23.x, but not v22.x. You need to uninstall v23 for cordova to pick v22.
对我们来说,这是由 Android SDK 构建工具版本引起的。在 v23.x 上有问题,但在 v22.x 上没有。您需要卸载 v23,cordova 才能选择 v22。
Possibly a symptom rather than a cause, but this may unstick someone in the same situation.
可能是一种症状而不是原因,但这可能会使处于相同情况的人脱离困境。