我现在可以在 Android 开发中“使用”Java 8 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34780565/
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-11-02 23:20:07  来源:igfitidea点击:

Can I "use" Java 8 with Android Development now?

javaandroidjava-8

提问by wayway

I currently have Java 7 JDK installed. It's been like that for awhile. However, recently, my professor is instructing the class to uninstall Java JDK 7 and install Java JDK 8 instead to be able to work on homework and such.

我目前安装了 Java 7 JDK。已经有一段时间了。但是,最近,我的教授指示全班卸载 Java JDK 7 并安装 Java JDK 8,以便能够完成家庭作业等。

I've been using Java JDK 7 to write and deploy Android apps without any problems so I'm wondering if it's safe to make the upgrade now the JDK 8? I'm assuming that I won't be needing any Java 8 specific methods or functions in my Android development. So if I stay away from that, will my Android development be at all affected?

我一直在使用 Java JDK 7 编写和部署 Android 应用程序,没有任何问题,所以我想知道现在升级 JDK 8 是否安全?我假设在我的 Android 开发中不需要任何 Java 8 特定的方法或函数。那么如果我远离它,我的Android开发会受到影响吗?

On a side note, I haven't been able to find any official documentation that states whether or not Java JDK 8 is safe or not or how to make it safe. Either way, I'm going to have to upgrade to Java 8 soon...

附带说明一下,我找不到任何说明 Java JDK 8 是否安全或如何使其安全的官方文档。无论哪种方式,我都将不得不很快升级到 Java 8...

采纳答案by ChiefTwoPencils

If you've been using Java 7 to deploy Android apps then it's certain, up to this point, you haven't used any Java 8 features so I don't see how it would matter.

如果您一直在使用 Java 7 来部署 Android 应用程序,那么可以肯定,到目前为止,您还没有使用任何 Java 8 功能,所以我看不出这有什么关系。

Follow your instructor's directions and when you do an assignment for school simply select either the JDK or Language Level in the Project Structure.

按照老师的指示,当你为学校做作业时,只需在项目结构中选择 JDK 或语言级别。

CTRL+ ALT+ S, select Project

CTRL+ ALT+ S,选择Project

You can default to the Java 8 SDK but limit it to Java 7's features for your Android apps. Or you can simply set your homework projects to the Java 8 SDK.

您可以默认使用 Java 8 SDK,但将其限制为 Java 7 的 Android 应用程序功能。或者,您可以简单地将作业项目设置为 Java 8 SDK。

Going out on a limb here assuming Android Studio includes the core settings of Intellij.

假设 Android Studio 包含 Intellij 的核心设置,这里就有点冒险了。

回答by Zeyad Assem

In Google I/O 2016, they announced that Android now supports some features of Java 8, but not all the features.

在 Google I/O 2016 上,他们宣布 Android 现在支持 Java 8 的一些功能,但不是所有功能。

Features like

功能如

  • Default and static interface methods
  • Lambda expressions (also available on API level 23 and lower)
  • Repeatable annotations
  • Method References (also available on API level 23 and lower)
  • Type Annotations (also available on API level 23 and lower)
  • 默认和静态接口方法
  • Lambda 表达式(也可用于 API 级别 23 及更低级别)
  • 可重复的注释
  • 方法参考(也可用于 API 级别 23 及更低级别)
  • 类型注释(也可用于 API 级别 23 及更低级别)

Additionally, the following Java 8 language APIs are also available:

此外,还提供以下 Java 8 语言 API:

  • Reflection and language-related APIs
    • java.lang.FunctionalInterface
    • java.lang.annotation.Repeatable
    • java.lang.reflect.Method.isDefault()
    • and Reflection APIs associated with repeatable annotations, such as AnnotatedElement.getAnnotationsByType(Class)
  • Utility APIs
    • java.util.function
    • java.util.stream
  • 反射和语言相关的 API
    • java.lang.FunctionalInterface
    • java.lang.annotation.Repeatable
    • java.lang.reflect.Method.isDefault()
    • 和与可重复注释关联的反射 API,例如 AnnotatedElement.getAnnotationsByType(Class)
  • 实用程序 API
    • java.util.function
    • java.util.stream

All what you should do is to use the new Hyman compiler, to do this you have just to add this to your gradle file build.gradle (Module: app):

您应该做的就是使用新的 Hyman 编译器,为此您只需将其添加到您的 gradle 文件中build.gradle (Module: app)

android {
  ...
  defaultConfig {
    ...
    HymanOptions {
      enabled true
    }
  }
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

References:

参考:

回答by Angel Koh

It's safe if you don't use the java 8 features.

如果您不使用 java 8 功能,那是安全的。

although you may be keen on java 8 with retroLambda and collectionsQuery. https://github.com/evant/gradle-retrolambdaand https://bitbucket.org/mart_bogdan/collectionsquery/src

尽管您可能对带有 retroLambda 和 collectionsQuery 的 Java 8 感兴趣。 https://github.com/evant/gradle-retrolambdahttps://bitbucket.org/mart_bogdan/collectionsquery/src

This will allow you to write your code to something like the following: -

这将允许您将代码编写为如下所示:-

mButton.setOnClickListener( v-> doClickEvent()); 

mView.postDelayed( () -> someMethodToRun() , 1000);

Queryable.from(listOfObject).forEachR(r -> doProcess(r));

as opposed to the clunky

相对于笨重

mButton.setOnClickListener( new View.OnClickListener(){
     @Override
      public void onClick(View v) {
         doClickEvent();
      }
});

to use java 8 with retrolambda just add in the gradle file.

要将 java 8 与 retrolambda 结合使用,只需在 gradle 文件中添加即可。

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'

android {

    // :
    // Snips the rest of configuration.
    // :
    compileOptions {
        encoding "UTF-8"
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'  

    compile 'com.android.support:appcompat-v7:23.1.1' 

    compile 'com.innahema:collections-query:0.2.9'
}

and at the project gradle.build file add the following...

并在项目 gradle.build 文件中添加以下内容...

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'

        classpath 'me.tatarka:gradle-retrolambda:3.2.3'
        classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2' //fix lint issue

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

回答by Robert McHardy

I guess you should just give it a try. On a side note: You can easily run JDK 7 and JDK 8 side by side. I don't know about Android Studio, but in Eclipse you can configure the build path.

我想你应该试一试。附带说明:您可以轻松地同时运行 JDK 7 和 JDK 8。我不了解 Android Studio,但在 Eclipse 中您可以配置构建路径。

Despite that: Are you using Java 8 specific features for your homework? If you don't use lambdas or JavaFX 8, I don't think you will needJDK 8. Sounds more like your professor just wants to use the latest version of Java (judging from your first sentence).

尽管如此:您是否在作业中使用 Java 8 特定功能?如果您不使用 lambdas 或 JavaFX 8,我认为您不需要JDK 8。听起来更像是您的教授只想使用最新版本的 Java(从您的第一句话判断)。