0

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.

Rameez Hussain
  • 6,414
  • 10
  • 56
  • 85
  • Does jenv changes JAVA_HOME environment variable? Check it before and after script. – Viktor Aug 11 '22 at 08:49
  • @Viktor I just checked.`jenv` doesn't change the `JAVA_HOME` when it's run from the script, but it does if I change it directly in the terminal. Not sure why that is. Maybe I should find a way to directly change `JAVA_HOME` instead of using `jenv`? – Rameez Hussain Aug 11 '22 at 08:54

1 Answers1

0

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.

Viktor
  • 374
  • 1
  • 6
  • Hmm. Seems unlikely. I removed all of the code that didn't involve changing the Java version and it still doesn't work. It does work if I manually set the JAVA_HOME from within the script though. The issue seems to lie with `jenv`. – Rameez Hussain Aug 11 '22 at 09:34