androidWinHelpers.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.enableAMDH = exports.enableHAXM = exports.enableWHPX = exports.getBestHypervisor = exports.createAVD = exports.installComponent = exports.getAndroidSdkRootInstallation = exports.getUserAndroidPath = void 0;
  6. function _fsExtra() {
  7. const data = require("fs-extra");
  8. _fsExtra = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _path() {
  14. const data = require("path");
  15. _path = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. var _executeWinCommand = require("./executeWinCommand");
  21. var _processorType = require("./processorType");
  22. /**
  23. * Returns the path to where all Android related things should be installed
  24. * locally to the user.
  25. */
  26. const getUserAndroidPath = () => {
  27. return (0, _path().join)(process.env.LOCALAPPDATA || '', 'Android');
  28. };
  29. /**
  30. * Deals with ANDROID_HOME, ANDROID_SDK_ROOT or generates a new one
  31. */
  32. exports.getUserAndroidPath = getUserAndroidPath;
  33. const getAndroidSdkRootInstallation = () => {
  34. const env = process.env.ANDROID_SDK_ROOT || process.env.ANDROID_HOME;
  35. const installPath = env ? // Happens if previous installations or not fully completed
  36. env : // All Android zip files have a root folder, using `Android` as the common place
  37. (0, _path().join)(getUserAndroidPath(), 'Sdk');
  38. if ((0, _fsExtra().pathExistsSync)(installPath)) {
  39. return installPath;
  40. } else {
  41. return '';
  42. }
  43. };
  44. /**
  45. * Installs an Android component (e.g.: `platform-tools`, `emulator`)
  46. * using the `sdkmanager` tool and automatically accepting the licenses.
  47. */
  48. exports.getAndroidSdkRootInstallation = getAndroidSdkRootInstallation;
  49. const installComponent = (component, androidSdkRoot) => {
  50. return new Promise((done, error) => {
  51. const sdkmanager = (0, _path().join)(androidSdkRoot, 'tools', 'bin', 'sdkmanager.bat');
  52. const command = `"${sdkmanager}" --sdk_root="${androidSdkRoot}" "${component}"`;
  53. const child = (0, _executeWinCommand.executeCommand)(command);
  54. let stderr = '';
  55. child.stdout.on('data', data => {
  56. if (data.includes('(y/N)')) {
  57. child.stdin.write('y\n');
  58. }
  59. });
  60. child.stderr.on('data', data => {
  61. stderr += data.toString('utf-8');
  62. });
  63. child.on('close', exitStatus => {
  64. if (exitStatus === 0) {
  65. done();
  66. } else {
  67. error({
  68. stderr
  69. });
  70. }
  71. });
  72. child.on('error', error);
  73. });
  74. };
  75. /**
  76. * For the given custom Hypervisor and the output of `emulator-check accel`
  77. * returns the preferred Hypervisor to use and its installation status.
  78. * The recommendation order is:
  79. * 1. WHPX
  80. * 2. HAXM if Intel
  81. * 3. AMDH if AMD
  82. */
  83. exports.installComponent = installComponent;
  84. const parseHypervisor = (status, customHypervisor) => {
  85. /**
  86. * Messages:
  87. * Android Emulator requires an Intel processor with VT-x and NX support. Your CPU: 'AuthenticAMD'
  88. * HAXM is not installed, but Windows Hypervisor Platform is available.
  89. * WHPX (10.0.19041) is installed and usable.
  90. * * This message outputs for WHPX and when the AMD Hypervisor is installed
  91. * HAXM version 6.2.1 (4) is installed and usable.
  92. * HAXM is not installed on this machine
  93. */
  94. if (status.includes('is not installed, but Windows Hypervisor Platform is available.')) {
  95. return {
  96. hypervisor: 'WHPX',
  97. installed: false
  98. };
  99. }
  100. if (/WHPX \((\d|\.)+\) is installed and usable\./.test(status)) {
  101. return {
  102. hypervisor: 'WHPX',
  103. installed: true
  104. };
  105. }
  106. if (/is installed and usable\./.test(status)) {
  107. return {
  108. hypervisor: customHypervisor,
  109. installed: true
  110. };
  111. }
  112. if (status.includes("Your CPU: 'AuthenticAMD'")) {
  113. return {
  114. hypervisor: customHypervisor,
  115. installed: false
  116. };
  117. }
  118. if (status.includes('is not installed on this machine')) {
  119. return {
  120. hypervisor: 'none',
  121. installed: false
  122. };
  123. }
  124. return null;
  125. };
  126. const getEmulatorAccelOutputInformation = async androidSDKRoot => {
  127. /**
  128. * The output of the following command is something like:
  129. *
  130. * ```
  131. * accel:
  132. * 0
  133. * WHPX (10.0.19041) is installed and usable.
  134. * accel
  135. * ```
  136. *
  137. * If it fails it will still output to stdout with a similar format:
  138. *
  139. * ```
  140. * accel:
  141. * 1
  142. * Android Emulator does not support nested virtualization. Your VM host: 'Microsoft Hv' (Hyper-V)
  143. * accel
  144. * ```
  145. *
  146. */
  147. try {
  148. const {
  149. stdout
  150. } = await (0, _executeWinCommand.executeCommand)(`"${(0, _path().join)(androidSDKRoot, 'emulator', 'emulator-check.exe')}" accel`);
  151. return stdout;
  152. } catch (e) {
  153. const {
  154. stdout
  155. } = e;
  156. return stdout;
  157. }
  158. };
  159. /**
  160. * Creates a new Android Virtual Device in the default folder with the
  161. * name, device and system image passed by parameter.
  162. */
  163. const createAVD = async (androidSDKRoot, name, device, image) => {
  164. try {
  165. const abi = image.includes('x86_64') ? 'x86_64' : 'x86';
  166. const tag = image.includes('google_apis') ? 'google_apis' : 'generic';
  167. const avdmanager = (0, _path().join)(androidSDKRoot, 'tools', 'bin', 'avdmanager.bat');
  168. const {
  169. stdout
  170. } = await (0, _executeWinCommand.executeCommand)(`${avdmanager} -s create avd --force --name "${name}" --device "${device}" --package "${image}" --tag "${tag}" --abi "${abi}"`); // For some reason `image.sysdir.1` in `config.ini` points to the wrong location and needs to be updated
  171. const configPath = (0, _path().join)(process.env.HOMEPATH || '', '.android', 'avd', `${name}.avd`, 'config.ini');
  172. const content = await (0, _fsExtra().readFile)(configPath, 'utf-8');
  173. const updatedContent = content.replace(/Sdk\\system-images/g, 'system-images');
  174. await (0, _fsExtra().writeFile)(configPath, updatedContent, 'utf-8');
  175. return stdout;
  176. } catch (e) {
  177. const {
  178. stderr
  179. } = e;
  180. return stderr;
  181. }
  182. };
  183. /**
  184. * Returns what hypervisor should be installed for the Android emulator
  185. * using [Microsoft's official
  186. * documentation](https://docs.microsoft.com/en-us/xamarin/android/get-started/installation/android-emulator/hardware-acceleration?pivots=windows)
  187. * as a reference.
  188. */
  189. exports.createAVD = createAVD;
  190. const getBestHypervisor = async androidSDKRoot => {
  191. const customHypervisor = (0, _processorType.getProcessorType)() === 'Intel' ? 'HAXM' : 'AMDH';
  192. const stdout = await getEmulatorAccelOutputInformation(androidSDKRoot);
  193. const lines = stdout.split('\n');
  194. for (const line of lines) {
  195. const hypervisor = parseHypervisor(line, customHypervisor);
  196. if (hypervisor) {
  197. return hypervisor;
  198. }
  199. } // Couldn't identify the best one to run so not doing anything
  200. return {
  201. hypervisor: 'none',
  202. installed: false
  203. };
  204. };
  205. /**
  206. * Enables the Windows HypervisorPlatform and Hyper-V features.
  207. * Will prompt the User Account Control (UAC)
  208. */
  209. exports.getBestHypervisor = getBestHypervisor;
  210. const enableWHPX = () => {
  211. return (0, _executeWinCommand.executeCommand)('DISM /Quiet /NoRestart /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V /FeatureName:HypervisorPlatform', true);
  212. };
  213. /**
  214. * Installs and enables the [HAXM](https://github.com/intel/haxm)
  215. * version available through the Android SDK manager.
  216. * @param androidSdkInstallPath The path to the Android SDK installation
  217. */
  218. exports.enableWHPX = enableWHPX;
  219. const enableHAXM = async androidSdkInstallPath => {
  220. await installComponent('extras;intel;Hardware_Accelerated_Execution_Manager', androidSdkInstallPath);
  221. await (0, _executeWinCommand.executeCommand)((0, _path().join)(androidSdkInstallPath, 'Sdk', 'extras', 'intel', 'Hardware_Accelerated_Execution_Manager', 'silent_install.bat'));
  222. };
  223. /**
  224. * Installs and enables the
  225. * [Hypervisor Driver for AMD Processors](https://androidstudio.googleblog.com/2019/10/android-emulator-hypervisor-driver-for.html)
  226. * version available through the Android SDK manager.
  227. * @param androidSdkInstallPath The path to the Android SDK installation
  228. */
  229. exports.enableHAXM = enableHAXM;
  230. const enableAMDH = async androidSdkInstallPath => {
  231. await installComponent('extras;google;Android_Emulator_Hypervisor_Driver', androidSdkInstallPath);
  232. await (0, _executeWinCommand.executeCommand)((0, _path().join)(androidSdkInstallPath, 'Sdk', 'extras', 'google', 'Android_Emulator_Hypervisor_Driver', 'silent_install.bat'));
  233. };
  234. exports.enableAMDH = enableAMDH;
  235. //# sourceMappingURL=androidWinHelpers.js.map