Skip to content

Commit bf74d02

Browse files
fix: stop executing avdmanager in case java is not available
In case JAVA_HOME is not set and there's no java executable in PATH, calls to `avdmanager` fail with error. However, on macOS this also leads to system prompt to install JAVA. To handle this, do not execute `avdmanager` in case there's no JAVA_HOME or java in PATH.
1 parent b74d362 commit bf74d02

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

lib/common/declarations.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,18 @@ interface ISysInfo {
992992
*/
993993
getJavaCompilerVersion(): Promise<string>;
994994

995+
/**
996+
* Gets JAVA version based on the executable in PATH.
997+
* @return {Promise<string>}
998+
*/
999+
getJavaVersionFromPath(): Promise<string>;
1000+
1001+
/**
1002+
* Gets JAVA version based on the JAVA from JAVA_HOME.
1003+
* @return {Promise<string>}
1004+
*/
1005+
getJavaVersionFromJavaHome(): Promise<string>;
1006+
9951007
/**
9961008
* Gets all global warnings for the current environment, for example Node.js version compatibility, OS compatibility, etc.
9971009
* @return {Promise<ISystemWarning[]>} All warnings. Empty array is returned in case the system is setup correctly.

lib/common/mobile/android/android-virtual-device-service.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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);

lib/common/test/unit-tests/mobile/android-virtual-device-service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ function createTestInjector(data: { avdManagerOutput?: string, avdManagerError?:
7676
testInjector.register("emulatorHelper", EmulatorHelper);
7777
testInjector.register("hostInfo", {});
7878
testInjector.register("logger", { trace: () => ({}) });
79+
testInjector.register("sysInfo", {
80+
getJavaVersionFromJavaHome: async () => "1.8.0",
81+
getJavaVersionFromPath: async () => "1.8.0"
82+
});
7983

8084
return testInjector;
8185
}

lib/sys-info.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ export class SysInfo implements ISysInfo {
3434
return sysInfo.getJavaCompilerVersion();
3535
}
3636

37+
public getJavaVersionFromPath(): Promise<string> {
38+
return sysInfo.getJavaVersionFromPath();
39+
}
40+
41+
public getJavaVersionFromJavaHome(): Promise<string> {
42+
return sysInfo.getJavaVersionFromJavaHome();
43+
}
44+
3745
@exported("sysInfo")
3846
public async getSystemWarnings(): Promise<ISystemWarning[]> {
3947
const warnings: ISystemWarning[] = [];

0 commit comments

Comments
 (0)