[英]Gradle Android Project with Java 8 module
As I've seen in this post , Java 8
is not officially supported by Android
right now. 正如我在看到这个帖子 ,
Java 8
不正式支持Android
现在。 So I'm interested if it is possible to build Android
module with Java 7
and Java
module (as a dependency) with Java 8
. 所以,我很感兴趣,如果有可能建立
Android
与模块Java 7
和Java
模块(作为依赖)与Java 8
。
As an example, I'm trying to create a Gradle
project that will contain one Android
module and one Java
module as a dependency. 作为一个例子,我正在尝试创建一个
Gradle
项目,该项目将包含一个Android
模块和一个Java
模块作为依赖项。 With the following compileOptions
set for both modules, everything works fine. 通过为两个模块设置以下
compileOptions
,一切正常。
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
But if I try to change compileOptions
for my Java
module to 但是,如果我尝试将
Java
模块的compileOptions
更改为
compileJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
I get the following error: 我收到以下错误:
Error:Execution failed for task ':fc-android:preDexFreeDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_45\bin\java.exe'' finished with non-zero exit value 1
So the question, is that actually possible to have Android
module compiled with 1.7
version and dependent Java
module compiled with 1.8
? 那么问题是,实际上可能有用
1.7
版本编译的Android
模块和用1.8
编译的依赖Java
模块吗? And if not, so why? 如果没有,为什么呢?
UPDATE: 更新:
Retrolamba for Gradle
(mentioned by @Saeed) is good, however they have only support of lambdas, so not access to Stream API
, DateTime API
and other features. Retrolamba for
Gradle
(由@Saeed提到)很好,但是它们只支持lambda,因此无法访问Stream API
, DateTime API
和其他功能。 Imagine if we have *.jar
file built with Java 8
(no Android
code). 想象一下,如果我们有使用
Java 8
构建的*.jar
文件(没有Android
代码)。 I think that we can't use such *.jar
file as a dependency for Android
module, because it's bytecode will not be supported by ART
or Dalvik
, but only by JVM
for Java 8
. 我认为我们不能使用这样的
*.jar
文件作为Android
模块的依赖项,因为它的字节码不会被ART
或Dalvik
支持,而只能由JVM
for Java 8
。
Android can support java 1.7 since API 19 (as you see in this doc there is no mention of java 1.8) and also it doesn't use JVM and using ART or Dalvik instead ,So it generates Dalvik bytecode . 从API 19开始,Android可以支持java 1.7(正如你在本文档中看到的那样,没有提到java 1.8),而且它不使用JVM并使用ART或Dalvik,所以它生成Dalvik字节码 。
I think if we want to use java 1.8 as compileOptions
maybe android run time can't understand some new features in java 8 like lambda so gradle doesn't allow you to compile your code and you got that exception. 我想如果我们想使用java 1.8作为
compileOptions
也许android运行时无法理解java 8中的一些新功能,比如lambda,所以gradle不允许你编译你的代码而你得到了那个例外。
So you need a bytecode transformer to use 所以你需要一个字节码转换器来使用
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Read this tutorial .It converts your bytecodes to be compatible with java 7.I've tested this before and it works for me. 阅读本教程 。它将您的字节码转换为与java 7兼容。我之前已经测试过,它对我有用。
Update 2016 2016年更新
Android N introduces support for Java 8 language features . Android N 引入了对Java 8语言功能的支持 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.