warning: [options] source value 7 is obsolete and will be removed in a future release

Viewed 9898

I get the ff error when i run flutter run.

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

This is my flutter doctor output:

[√] Flutter (Channel stable, 1.20.4, on Microsoft Windows [Version 10.0.18362.1139], locale en-US)
    • Flutter version 1.20.4 at C:\src\flutter
    • 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:\Users\USER01\AppData\Local\Android\sdk
    • Platform android-30, build-tools 30.0.2
    • Java binary at: C:\PROGRA~1\Java\jdk-13.0.2\bin\java
    • Java version Java(TM) SE Runtime Environment (build 13.0.2+8)
    • All Android licenses accepted.

I cant seem to find anything on google. How do i fix this error ?

4 Answers

This is a warning about the compiler targeting an older version of Java (7 in this case) that will be deprecated soon.

To fix this you have two options:

  1. You can set your JAVA_HOME to an older version of Java and then run your Flutter build. You would also need to have this JAVA_HOME available anywhere you build from (command line, IDE, CI/CD, etc)
  2. Or my preferred way is to tell Gradle to use an older Java toolchain for compiling Java source.

To do option 2, update your allprojects section of android/build.gradle to look like the following:


allprojects {
  repositories {
    google()
    mavenCentral()
  }

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

This includes an update to change the deprecated jcenter Maven repository.

EDIT Nov 2021: Support for toolchains has also been added to the Kotlin Gradle plugin.

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

Try doing this Android Studio -> File -> Invalid cache and restart

This is not a fix but a temporary workaround. Put the following in your build.gradle, under the allprojects{} section:

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

If your Android java code is the unmodified default generated code, then the bug is with one of your dependencies, you will have to go through pubspec.yaml and find the library that is causing the error.

I encountered this problem in Sep 2021 with Flutter 2.6.0 and VSCode on a Mac, and none of the project-level solutions above worked for me. I tried a bunch of stuff:

  • setting java.home in launch.json
  • setting JAVA_HOME in settings.json
  • all the build.gradle suggestions above
  • using jenv to set java locally for the project dir
  • installing the RedHat Java VSCode extension and fiddling with various settings there

Nothing project-specific worked for me. However, using jenv to set my global java version to jdk 11 did fix the problem, albeit with a sledgehammer.

Related