Changing Java version using jenv in a shell script

937 views Asked by At

I have a React Native project where I have a script in which I need to change the Java versions before and after the script has been executed. I basically need to change to Java 1.8 to run commands connected to the android sdkmanager, and then switch back to Java 11 when done.

I am currently using jenv to manage Java versions. If I do this in the Terminal at the root of the project using either of the following commands, it works:

jenv local 1.8
jenv local 11.0

However if I use the same in my bash script files, which I run via yarn, it doesn't work. The script file looks something like, <project_root>/scripts/my_script.sh:

jenv local 1.8;
java -version;

# Some code here involving sdkmanager...

jenv local 11.0;
java -version;

The java -version correctly displays that the Java version has changed, but the instructions in between with the sdkmanager don't pick up on the change. Not sure what I'm missing here. Any help/insights are welcome.

1

There are 1 answers

1
Viktor On

I suppose Android command tools use same JDK as Android Studio.

Relevant documentation: https://developer.android.com/studio/command-line/variables

STUDIO_JDK - Sets the location of the JDK with which to run Android Studio. When you launch the IDE, it checks the STUDIO_JDK, JDK_HOME, and JAVA_HOME environment variables in that order.

Something in your scripts sets these variables up and 'jenv' doesn't override them.