Search code examples
javaflutterversion

Linux - default java version is 8, set 11 for Flutter


On my developer machine:

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /home/michal/Android/Sdk
    • Platform android-31, build-tools 30.0.2
    • ANDROID_HOME = /home/michal/Android/Sdk
    • Java binary at: /home/michal/android/studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7249189)
    • All Android licenses accepted.

But global java version is Java 8:

java -version
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (build 1.8.0_265-8u265-b01-1-b01)
OpenJDK 64-Bit Server VM (build 25.265-b01, mixed mode)

It's somehow set by Android Studio. However, on my build server Flutter uses default 1.8 java version:

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /home/jenkins/Android/Sdk
    • Platform android-31, build-tools 30.0.2
    • ANDROID_HOME = /home/jenkins/Android/Sdk
    • Java binary at: /usr/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2-b11)
    • All Android licenses accepted.

Thus, apps targeted to Android 12 fail to compile.

How to set Java version to 11 (as on my dev machine is set) for Flutter only? System, default java version can't be changed I can't change JAVA_HOME variable or change default java version, it needs to be Java 8.

I can't install Android Studio on build server, I need to figure out where configuration is actually stored.

I guess I needd to set java binary path wihtout changing JAVA_HOME environment variable. Where to set it?


Solution

  • That's more like a workaround, but this worked in my case:

    • copy my Android Studio folder content to dev machine (I'm on Linux, so version is the same)

    • tell Flutter where is my Android Studio installation folder:

      flutter config --android-studio-dir="path"

    Now Flutter uses Java shipped with Android Studio, which is currently Java 11. But my initial question if it's possible to tell Flutter which Java version should be used (without playing with environment variables - globally or on each flutter command call) still remains open