在 Android Studio 中包含 Java 模块作为依赖项时错误的 Java 编译器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24741948/
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
Wrong Java Compiler When Including a Java Module as Dependency in Android Studio
提问by StuStirling
I have a java module in my Android Studio project that is a dependency of an Android module. I am having problems on build with the following exception appearing.
我的 Android Studio 项目中有一个 java 模块,它是 Android 模块的依赖项。我在构建时遇到问题,出现以下异常。
Error:Execution failed for task ':myApplication:preDexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
/Applications/Android Studio.app/sdk/build-tools/android-4.4W/dx --dex --output ~/myapplication-app-android/dev/biketracks-app-android/bikeTracks/build/intermediates/pre-dexed/debug/coreUtilities-6ee7e0aafa5a6db72b2acb078f065e51c43124c2.jar ~/myapplication-app-android/dev/myapplication-app-android/libs/coreUtilities/build/libs/coreUtilities.jar
Error Code:
1
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
at com.android.dx.command.dexer.Main.processClass(Main.java:665)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
at com.android.dx.command.dexer.Main.access0(Main.java:78)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:572)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:596)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
...while parsing com/corecoders/coreutilities/GPSUtils.class
1 error; aborting
After some reading I can see that it is something to do with the Java compiler Android Studio is using. However I cannot see a way to change which compiler it uses.
经过一些阅读,我可以看到这与 Android Studio 使用的 Java 编译器有关。但是我看不到改变它使用的编译器的方法。
The Java module I am trying to include in my Android module is one I created using Android Studio by going File > New Module > Java Module so I can't see any option I could make different?
我试图包含在我的 Android 模块中的 Java 模块是我使用 Android Studio 创建的,方法是转到文件 > 新模块 > Java 模块,所以我看不到任何可以改变的选项?
Any ideas would be great.
任何想法都会很棒。
采纳答案by StuStirling
So I have found the solution at this blog post.
所以我在这篇博文中找到了解决方案。
The trick is in your Java Library module's build.gradle file you need to include the following.
诀窍是在您的 Java 库模块的 build.gradle 文件中,您需要包含以下内容。
apply plugin: 'java'
sourceCompatibility = 1.6
targetCompatibility = 1.6
This will then work.
这将起作用。
回答by Nilesh Pawar
Seems things have changed on newer versions of Gradle / Android Studio, so the above solution about selecting source compatibility alone may not suffice. Particularly for complex projects which have a mix of modules which apply more than the simple android plugin ( I have seen following three plugins used on modules of same project: 'android' , 'java' and 'android-library')
似乎在较新版本的 Gradle/Android Studio 上情况发生了变化,因此上述仅选择源兼容性的解决方案可能不够。特别是对于混合模块的复杂项目,这些模块的应用不仅仅是简单的 android 插件(我已经看到在同一项目的模块上使用了以下三个插件:'android'、'java' 和 'android-library')
You need to make sure that the following things are satisfied if source compatibility alone does not resolve your issue.
如果仅靠源代码兼容性不能解决您的问题,您需要确保满足以下条件。
1) For you modules which apply plugin: 'android' select the source compatibility inside your build.gradle:
1) 对于应用 plugin: 'android' 的模块,在 build.gradle 中选择源兼容性:
android {
//... other sections.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
2) Select Project Byte code version from: File -> Other Settings -> Default settings.
2) 从以下位置选择项目字节代码版本:文件 -> 其他设置 -> 默认设置。
3) Explicitly select the JDK environment: File -> Project Structure -> SDK location and set it to the JDK 7 folder.
3)显式选择JDK环境:File -> Project Structure -> SDK location,设置为JDK 7文件夹。
--Update: with the new Android Studio 1.2.x they changed the location where you can select the Java byteCode version to the following: File->Other Settings->Default Settings->Build , Executions Enviromnent-> Compiler.
--更新:使用新的 Android Studio 1.2.x,他们将您可以选择 Java 字节码版本的位置更改为以下内容:文件->其他设置->默认设置->构建,执行环境->编译器。