@@ -16,6 +16,7 @@ export class AndroidVirtualDeviceService implements Mobile.IAndroidVirtualDevice
1616 private $emulatorHelper : Mobile . IEmulatorHelper ,
1717 private $fs : IFileSystem ,
1818 private $hostInfo : IHostInfo ,
19+ private $sysInfo : ISysInfo ,
1920 private $logger : ILogger ) {
2021 this . androidHome = process . env . ANDROID_HOME ;
2122 }
@@ -152,8 +153,12 @@ export class AndroidVirtualDeviceService implements Mobile.IAndroidVirtualDevice
152153 let result : ISpawnResult = null ;
153154 let devices : Mobile . IDeviceInfo [ ] = [ ] ;
154155 let errors : string [ ] = [ ] ;
156+ const canExecuteAvdManagerCommand = await this . canExecuteAvdManagerCommand ( ) ;
157+ if ( ! canExecuteAvdManagerCommand ) {
158+ errors = [ "Unable to execute avdmanager, ensure JAVA_HOME is set and points to correct directory" ] ;
159+ }
155160
156- if ( this . pathToAvdManagerExecutable && this . $fs . exists ( this . pathToAvdManagerExecutable ) ) {
161+ if ( canExecuteAvdManagerCommand ) {
157162 result = await this . $childProcess . trySpawnFromCloseEvent ( this . pathToAvdManagerExecutable , [ "list" , "avds" ] ) ;
158163 } else if ( this . pathToAndroidExecutable && this . $fs . exists ( this . pathToAndroidExecutable ) ) {
159164 result = await this . $childProcess . trySpawnFromCloseEvent ( this . pathToAndroidExecutable , [ "list" , "avd" ] ) ;
@@ -169,6 +174,22 @@ export class AndroidVirtualDeviceService implements Mobile.IAndroidVirtualDevice
169174 return { devices, errors } ;
170175 }
171176
177+ @cache ( )
178+ private async canExecuteAvdManagerCommand ( ) : Promise < boolean > {
179+ let canExecute = false ;
180+ if ( this . pathToAvdManagerExecutable && this . $fs . exists ( this . pathToAvdManagerExecutable ) ) {
181+ if ( process . env . JAVA_HOME ) {
182+ // In case JAVA_HOME is set, but it points to incorrect directory (i.e. there's no java in $JAVA_HOME/bin/java), avdmanager will fail
183+ // no matter if you have correct java in PATH.
184+ canExecute = ! ! ( await this . $sysInfo . getJavaVersionFromJavaHome ( ) ) ;
185+ } else {
186+ canExecute = ! ! ( await this . $sysInfo . getJavaVersionFromPath ( ) ) ;
187+ }
188+ }
189+
190+ return canExecute ;
191+ }
192+
172193 private async getRunningEmulatorData ( runningEmulatorId : string , availableEmulators : Mobile . IDeviceInfo [ ] ) : Promise < Mobile . IDeviceInfo > {
173194 const imageIdentifier = await this . getRunningEmulatorImageIdentifier ( runningEmulatorId ) ;
174195 const runningEmulator = this . $emulatorHelper . getEmulatorByImageIdentifier ( imageIdentifier , availableEmulators ) ;
0 commit comments