user1209216
user1209216

Reputation: 7934

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?

Upvotes: 2

Views: 3499

Answers (3)

Jberg
Jberg

Reputation: 945

Flutter config now support forcing the JDK location which lets you override the search location. By default flutter searches the Android SDK path first and only uses JAVA_HOME if no JDK is found on the Android path.

Here's how you force it to use JAVA_HOME

flutter config --jdk-dir $JAVA_HOME

I banged my head off the wall for hours trying to solve this problem until I found this PR:

https://github.com/flutter/flutter/pull/128264

Upvotes: 1

user1209216
user1209216

Reputation: 7934

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

Upvotes: 1

Stephen C
Stephen C

Reputation: 719299

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.

Upvotes: 0

Related Questions