警告:[选项]源值7已过时,将在将来的版本中删除



当我运行flutter运行时,我得到ff错误。

warning: [options] source value 7 is obsolete and will be removed in a future release
warning: [options] target value 7 is obsolete and will be removed in a future release
warning: [options] To suppress warnings about obsolete options, use -Xlint:-options.
error: warnings found and -Werror specified
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':connectivity:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org

这是我的扑动医生输出:

[√] Flutter (Channel stable, 1.20.4, on Microsoft Windows [Version 10.0.18362.1139], locale en-US)
• Flutter version 1.20.4 at C:srcflutter
• Framework revision fba99f6cf9 (8 weeks ago), 2020-09-14 15:32:52 -0700
• Engine revision d1bc06f032
• Dart version 2.9.2

[√] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
• Android SDK at C:UsersUSER01AppDataLocalAndroidsdk
• Platform android-30, build-tools 30.0.2
• Java binary at: C:PROGRA~1Javajdk-13.0.2binjava
• Java version Java(TM) SE Runtime Environment (build 13.0.2+8)
• All Android licenses accepted.

我在谷歌上似乎找不到任何东西。如何修复此错误?

这是关于编译器针对即将弃用的旧版本Java(本例中为7(的警告。

要解决此问题,您有两个选项:

  1. 您可以将JAVA_HOME设置为旧版本的Java,然后运行Flutter构建。您还需要在任何构建位置(命令行、IDE、CI/CD等(都可以使用此JAVA_HOME
  2. 或者我更喜欢的方法是告诉Gradle使用旧的Java工具链来编译Java源代码

要执行选项2,请将android/build.gradleallprojects部分更新为如下所示:


allprojects {
repositories {
google()
mavenCentral()
}
tasks.withType(JavaCompile).configureEach {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(8)
}
}
}

其中包括一个更新,用于更改已弃用的jcenterMaven存储库。

2021年11月编辑:Kotlin-Gradle插件中也添加了对工具链的支持。

https://blog.jetbrains.com/kotlin/2021/11/gradle-jvm-toolchain-support-in-the-kotlin-plugin/

尝试这样做Android Studio -> File -> Invalid cache and restart

这不是一个修复方法,而是一个临时解决方法。将以下内容放入build.gradle中的allprojects{}部分下:

tasks.withType(JavaCompile) {
options.compilerArgs << '-Xlint:-options' 
}

如果你的Android java代码是未修改的默认生成代码,那么这个错误与你的一个依赖项有关,你必须通过pubspec.yaml找到导致错误的库。

我在一些Android项目中遇到了这个问题。有两个build.gradle文件,一个在顶级文件夹中,另一个在app/中。我通过编辑app/内部的文件来修复它,如下所示:

android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

我把它从JavaVersion.VERSION_1_6改成了JavaVersion.VERSION_1_8

2021年9月,我在Mac上使用Flutter 2.6.0和VSCode时遇到了这个问题,上面的项目级解决方案都不适合我。我尝试了很多东西:

  • 在launch.json中设置java.home
  • 在settings.json中设置JAVA_HOME
  • 上面的所有build.gradle建议
  • 使用jenv为项目目录本地设置java
  • 安装RedHat Java VSCode扩展并篡改那里的各种设置

没有任何特定项目对我有效。然而,使用jenv将我的全局java版本设置为jdk 11确实解决了这个问题,尽管使用了大锤。

相关内容

最新更新