如何在 Java 模块中指定源和目标兼容性?

How to specify source and target compatibility in Java module?(如何在 Java 模块中指定源和目标兼容性?)

本文介绍了如何在 Java 模块中指定源和目标兼容性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Gradle 项目,其中包含一个 Android 模块(com.android.library 插件应用在 build.gradle 文件中)和一个 Java 模块(java 插件应用在 build.gradle 文件中).在 Java 模块的 build.gradle 文件中,我之前指定了源和目标的兼容性,如下所示:

I have a Gradle project consisting of an Android module (the com.android.library plugin is applied in the build.gradle file) and a Java module (the java plugin is applied in the build.gradle file). Within the build.gradle file of the Java module I was previously specifying the source and target compatibility as follows:

apply plugin: 'java'

compileJava {
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
}

我刚刚将项目的Android Plugin for Gradle"和Gradle Wrapper"版本更新为最新版本(分别为 2.2.3 和 3.2.1),现在我在 Java 模块中看到如下警告:

I've just updated the project's "Android Plugin for Gradle" and "Gradle Wrapper" versions to the latest releases (2.2.3 and 3.2.1 respectively) and I'm now seeing warnings in the Java module as follows:

Access to 'sourceCompatibility' exceeds its access rights
Access to 'targetCompatibility' exceeds its access rights

如果我将 sourceCompatibilitytargetCompatibility 声明移动到模块的 build.gradle 文件的根级别,如下所示...

If I move the sourceCompatibility and targetCompatibility declarations to the root-level of the module's build.gradle file as follows...

apply plugin: 'java'

sourceCompatibility = 1.7
targetCompatibility = 1.7

...然后我收到警告消息,告诉我分配未使用.

... then I get warning messages telling me that the assignments are not used.

在最新的 Gradle 版本中指定 Java 模块的源和目标兼容级别的正确方式是什么?

What is the correct manner of specifying the source and target compatibility level of a Java module in the latest Gradle release?

推荐答案

我认为最后一个(根中的声明)是正确的,它必须正常工作,尽管它会在 IDE 中引起警告.无处可说,声明它的首选方式是什么,official docs,它被用作:

I think that the last one(declaration in the root) is the correct one and it has to work properly, though it causes warnings in IDE. It's nowhere said, what is the prefered way to declare it and there is only one example in the official docs, where it's used as:

sourceCompatibility = 1.6

build.gradle 的根目录中.

这篇关于如何在 Java 模块中指定源和目标兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 Java 模块中指定源和目标兼容性?

基础教程推荐