Recommand · October 22, 2021 0

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?

I can’t change JAVA_HOME variable.

You can change it locally. For example, at the command prompt you can do this:

 $ export JAVA_HOME=/new/setting
 $ flutter ...

So armed with this you can define a shell alias or shell function in your shell initialization script (e.g in "~/.bashrc").

Alternatively you can write a wrapper script that does the same thing; e.g.

 #!/bin/sh
 export JAVA_HOME=/new/setting
 flutter "$@"

and run that instead of flutter.

And so on.