Selecting Java JDK version for EAS build

Hi all,

my project would need java jdk version 11 for building the app. Is there any way to select jdk version for eas build?

didn’t test that, but sth like this should work

  • add apt-get install openjdk-11-jdk to preinstall step in your package.json (make sure to run that only for android builds)
  • set JAVA_HOME in eas.json to make sure it points to the correct java install

if you are not sure about paths on the worker you can test it on ubuntu:bionic docker image

Or perhaps openjdk-11-jdk-headless?

I think you’ll also need to specify the PATH:

"env": {
  "JAVA_HOME": "/usr/lib/jvm/java-11-openjdk-amd64",
  "PATH": "/usr/lib/jvm/java-11-openjdk-amd64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
}

Btw, @wkozyra, is it possible to reference environment variables when defining them? e.g. like this:

"env": {
  "JAVA_HOME": "/usr/lib/jvm/java-11-openjdk-amd64",
  "PATH": "$JAVA_HOME/bin:$PATH"
}

EDIT: I’ve just found out about update-java-alternatives :astonished:

root@7cb078105ec4:~# update-java-alternatives -l
java-1.11.0-openjdk-amd64      1111       /usr/lib/jvm/java-1.11.0-openjdk-amd64
java-1.8.0-openjdk-amd64       1081       /usr/lib/jvm/java-1.8.0-openjdk-amd64
root@7cb078105ec4:~# update-java-alternatives -s java-1.11.0-openjdk-amd64
update-alternatives: error: no alternatives for appletviewer
update-alternatives: error: no alternatives for jconsole
update-alternatives: error: no alternatives for mozilla-javaplugin.so
update-alternatives: error: no alternatives for policytool
update-java-alternatives: jdk alternative does not exist: /usr/lib/jvm/java-11-openjdk-amd64/bin/jconsole
root@7cb078105ec4:~# java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.18.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.18.04, mixed mode, sharing)
root@7cb078105ec4:~# update-java-alternatives -s java-1.8.0-openjdk-amd64
update-alternatives: error: no alternatives for appletviewer
update-alternatives: error: no alternatives for jconsole
update-alternatives: error: no alternatives for mozilla-javaplugin.so
update-alternatives: error: no alternatives for policytool
update-java-alternatives: jdk alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/bin/appletviewer
update-java-alternatives: jdk alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/bin/jconsole
update-alternatives: error: no alternatives for policytool
update-java-alternatives: plugin alternative does not exist: /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/amd64/IcedTeaPlugin.so
root@7cb078105ec4:~# java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~18.04-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)

The errors it outputs are a bit annoying, but you could always filter those out if you really wanted to.

If I define environment variables in eas.json with this:

{
  "build": {
    "release": {
      "distribution": "store",
      "android": {
        "autoIncrement": true,
        "env": {
          "JAVA_HOME": "/usr/lib/jvm/java-11-openjdk-amd64",
          "PATH": "$JAVA_HOME/bin:$PATH"
        }
      }
    },
    ...
  }
}

Build prints:

Project environment variables:
PATH=$JAVA_HOME/bin:$PATH
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

And it causes error in Install dependencies phase:

Build failed
spawn npm ENOENT

It seems that EAS is not defining default PATH variable but uses only user defined variable. So need to define full PATH variable.

And those JAVA_HOME paths didn’t work. I didn’t define PATH variable but only JAVA_HOME. I tried with these two paths:

JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
and
JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64

prints:

ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/java-11-openjdk-amd64
and
ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/java-1.11.0-openjdk-amd64

It seems that EAS is not defining default PATH variable but uses only user defined variable. So need to define full PATH variable.

values specified in eas.json are not evaluated in any way, we are setting those envs exactly as they are specified

I tried with these two paths:
JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
and
JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64

As I mentioned in previous comment " you can test it on ubuntu:bionic docker image". Easiest way to debug is to start docker image run that apt-get command there and check how path to the java directory looks like

I didn’t test that, but I don’t think modifying PATH is necessary, if the tool respects JAVA_HOME env it should ignore binaries that are in PATH

I suspected it would do that. That’s why I suggested the following:

If that doesn’t work you could echo $PATH in one of the hooks to see what it is by default. Then adjust the above as appropriate.

EDIT: But first try it without modifying the PATH, as @wkozyra suggests.

$ docker run -it --rm ubuntu:bionic bash
root@6dab2977fc9a:/# apt update && apt install -y openjdk-11-jdk-headless
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
[...]
done.
root@6dab2977fc9a:/# ls -l /usr/bin/javac
lrwxrwxrwx 1 root root 23 Oct 13 12:36 /usr/bin/javac -> /etc/alternatives/javac
root@6dab2977fc9a:/# ls -l /etc/alternatives/javac
lrwxrwxrwx 1 root root 44 Oct 13 12:36 /etc/alternatives/javac -> /usr/lib/jvm/java-11-openjdk-amd64/bin/javac

So the JAVA_HOME is /usr/lib/jvm/java-11-openjdk-amd64

Sorry I somehow missed @wkozyra’s first message. I added this script to package.json:

{
  ...
  "scripts": {
    "eas-build-pre-install": "apt-get update; apt-get -y install openjdk-11-jdk-headless",
  ...
}

And in eas.json JAVA_HOME was set to “/usr/lib/jvm/java-11-openjdk-amd64”.

And now it works. Thank you for helping!

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.