/** * Installs the given platform and its dependencies into the given installation, if necessary. * * @param logger Logs things. * @param launcher Used to launch tasks on the remote node. * @param sdk SDK installation to install components for. * @param platform Specifies the platform to be installed. */ public static void installPlatform( PrintStream logger, Launcher launcher, AndroidSdk sdk, String platform) throws IOException, InterruptedException { // Check whether this platform is already installed if (isPlatformInstalled(logger, launcher, sdk, platform)) { return; } // Check whether we are capable of installing individual components log(logger, Messages.PLATFORM_INSTALL_REQUIRED(platform)); if (!launcher.isUnix() && platform.contains(":") && sdk.getSdkToolsVersion() < 16) { // SDK add-ons can't be installed on Windows until r16 due to http://b.android.com/18868 log(logger, Messages.SDK_ADDON_INSTALLATION_UNSUPPORTED()); return; } if (!sdk.supportsComponentInstallation()) { log(logger, Messages.SDK_COMPONENT_INSTALLATION_UNSUPPORTED()); return; } // Automated installation of ABIs (required for android-14+) is not possible until r17, so // we should warn the user that we can't automatically set up an AVD with older SDK Tools. // See http://b.android.com/21880 if ((platform.endsWith("14") || platform.endsWith("15")) && !sdk.supportsSystemImageInstallation()) { log(logger, Messages.ABI_INSTALLATION_UNSUPPORTED(), true); } // Determine which individual component(s) need to be installed for this platform List<String> components = getSdkComponentsForPlatform(logger, platform); if (components == null || components.size() == 0) { return; } // If a platform expanded to multiple dependencies (e.g. "GoogleMaps:7" -> android-7 + Maps) // then check whether we really need to install android-7, as it may already be installed if (components.size() > 1) { for (Iterator<String> it = components.iterator(); it.hasNext(); ) { String component = it.next(); if (isPlatformInstalled(logger, launcher, sdk, component)) { it.remove(); } } } // Grab the lock and attempt installation Semaphore semaphore = acquireLock(); try { installComponent(logger, launcher, sdk, components.toArray(new String[0])); } finally { semaphore.release(); } }
/** * Installs the given SDK component(s) into the given installation. * * @param logger Logs things. * @param launcher Used to launch tasks on the remote node. * @param sdk Root of the SDK installation to install components for. * @param components Name of the component(s) to install. */ private static void installComponent( PrintStream logger, Launcher launcher, AndroidSdk sdk, String... components) throws IOException, InterruptedException { String proxySettings = getProxySettings(); String list = StringUtils.join(components, ','); log(logger, Messages.INSTALLING_SDK_COMPONENTS(list.toString())); String all = sdk.getSdkToolsVersion() < 17 ? "-o" : "-a"; String upgradeArgs = String.format("update sdk -u %s %s -t %s", all, proxySettings, list); Utils.runAndroidTool(launcher, logger, logger, sdk, Tool.ANDROID, upgradeArgs, null); }
public Boolean call() throws AndroidEmulatorException { if (logger == null) { logger = listener.getLogger(); } final File homeDir = Utils.getHomeDirectory(androidSdk.getSdkHome()); final File avdDirectory = getAvdDirectory(homeDir); final boolean emulatorExists = getAvdConfigFile(homeDir).exists(); // Can't do anything if a named emulator doesn't exist if (isNamedEmulator() && !emulatorExists) { throw new EmulatorDiscoveryException(Messages.AVD_DOES_NOT_EXIST(avdName, avdDirectory)); } // Check whether AVD needs to be created boolean createSdCard = false; boolean createSnapshot = false; File snapshotsFile = new File(getAvdDirectory(homeDir), "snapshots.img"); if (emulatorExists) { // AVD exists: check whether there's anything still to be set up File sdCardFile = new File(getAvdDirectory(homeDir), "sdcard.img"); boolean sdCardRequired = getSdCardSize() != null; // Check if anything needs to be done for snapshot-enabled builds if (shouldUseSnapshots() && androidSdk.supportsSnapshots()) { if (!snapshotsFile.exists()) { createSnapshot = true; } // We should ensure that we start out with a clean SD card for the build if (sdCardRequired && sdCardFile.exists()) { sdCardFile.delete(); } } // Flag that we need to generate an SD card, if there isn't one existing if (sdCardRequired && !sdCardFile.exists()) { createSdCard = true; } // If everything is ready, then return if (!createSdCard && !createSnapshot) { return true; } } else { AndroidEmulator.log(logger, Messages.CREATING_AVD(avdDirectory)); } // We can't continue if we don't know where to find emulator images or tools if (!androidSdk.hasKnownRoot()) { throw new EmulatorCreationException(Messages.SDK_NOT_SPECIFIED()); } final File sdkRoot = new File(androidSdk.getSdkRoot()); if (!sdkRoot.exists()) { throw new EmulatorCreationException(Messages.SDK_NOT_FOUND(androidSdk.getSdkRoot())); } // If we need to initialise snapshot support for an existing emulator, do so if (createSnapshot) { // Copy the snapshots file into place File snapshotDir = new File(sdkRoot, "tools/lib/emulator"); Util.copyFile(new File(snapshotDir, "snapshots.img"), snapshotsFile); // Update the AVD config file mark snapshots as enabled Map<String, String> configValues; try { configValues = parseAvdConfigFile(homeDir); configValues.put("snapshot.present", "true"); writeAvdConfigFile(homeDir, configValues); } catch (IOException e) { throw new EmulatorCreationException(Messages.AVD_CONFIG_NOT_READABLE(), e); } } // If we need create an SD card for an existing emulator, do so if (createSdCard) { AndroidEmulator.log(logger, Messages.ADDING_SD_CARD(sdCardSize, getAvdName())); if (!createSdCard(homeDir)) { throw new EmulatorCreationException(Messages.SD_CARD_CREATION_FAILED()); } // Update the AVD config file Map<String, String> configValues; try { configValues = parseAvdConfigFile(homeDir); configValues.put("sdcard.size", sdCardSize); writeAvdConfigFile(homeDir, configValues); } catch (IOException e) { throw new EmulatorCreationException(Messages.AVD_CONFIG_NOT_READABLE(), e); } } // Return if everything is now ready for use if (emulatorExists) { return true; } // Build up basic arguments to `android` command final StringBuilder args = new StringBuilder(100); args.append("create avd "); // Overwrite any existing files args.append("-f "); // Initialise snapshot support, regardless of whether we will actually use it if (androidSdk.supportsSnapshots()) { args.append("-a "); } if (sdCardSize != null) { args.append("-c "); args.append(sdCardSize); args.append(" "); } args.append("-s "); args.append(screenResolution.getSkinName()); args.append(" -n "); args.append(getAvdName()); boolean isUnix = !Functions.isWindows(); ArgumentListBuilder builder = Utils.getToolCommand(androidSdk, isUnix, Tool.ANDROID, args.toString()); // Tack on quoted platform name at the end, since it can be anything builder.add("-t"); builder.add(osVersion.getTargetName()); if (targetAbi != null) { builder.add("--abi"); builder.add(targetAbi); } // Log command line used, for info AndroidEmulator.log(logger, builder.toStringWithQuote()); // Run! boolean avdCreated = false; final Process process; try { ProcessBuilder procBuilder = new ProcessBuilder(builder.toList()); if (androidSdk.hasKnownHome()) { procBuilder.environment().put("ANDROID_SDK_HOME", androidSdk.getSdkHome()); } process = procBuilder.start(); } catch (IOException ex) { throw new EmulatorCreationException(Messages.AVD_CREATION_FAILED()); } // Redirect process's stderr to a stream, for logging purposes ByteArrayOutputStream stderr = new ByteArrayOutputStream(); ByteArrayOutputStream stdout = new ByteArrayOutputStream(); new StreamCopyThread("", process.getErrorStream(), stderr).start(); // Command may prompt us whether we want to further customise the AVD. // Just "press" Enter to continue with the selected target's defaults. try { boolean processAlive = true; // Block until the command outputs something (or process ends) final PushbackInputStream in = new PushbackInputStream(process.getInputStream(), 10); int len = in.read(); if (len == -1) { // Check whether the process has exited badly, as sometimes no output is valid. // e.g. When creating an AVD with Google APIs, no user input is requested. if (process.waitFor() != 0) { AndroidEmulator.log(logger, Messages.AVD_CREATION_FAILED()); AndroidEmulator.log(logger, stderr.toString(), true); throw new EmulatorCreationException(Messages.AVD_CREATION_FAILED()); } processAlive = false; } in.unread(len); // Write CRLF, if required if (processAlive) { final OutputStream stream = process.getOutputStream(); stream.write('\r'); stream.write('\n'); stream.flush(); stream.close(); } // read the rest of stdout (for debugging purposes) Util.copyStream(in, stdout); in.close(); // Wait for happy ending if (process.waitFor() == 0) { // Do a sanity check to ensure the AVD was really created avdCreated = getAvdConfigFile(homeDir).exists(); } } catch (IOException e) { throw new EmulatorCreationException(Messages.AVD_CREATION_ABORTED(), e); } catch (InterruptedException e) { throw new EmulatorCreationException(Messages.AVD_CREATION_INTERRUPTED(), e); } finally { process.destroy(); } // For reasons unknown, the return code may not be correctly reported on Windows. // So check whether stderr contains failure info (useful for other platforms too). String errOutput = stderr.toString(); String output = stdout.toString(); if (errOutput.contains("list targets")) { AndroidEmulator.log(logger, Messages.INVALID_AVD_TARGET(osVersion.getTargetName())); avdCreated = false; errOutput = null; } else if (errOutput.contains("more than one ABI")) { AndroidEmulator.log( logger, Messages.MORE_THAN_ONE_ABI(osVersion.getTargetName(), output), true); avdCreated = false; errOutput = null; } // Check everything went ok if (!avdCreated) { if (errOutput != null && errOutput.length() != 0) { AndroidEmulator.log(logger, stderr.toString(), true); } throw new EmulatorCreationException(Messages.AVD_CREATION_FAILED()); } // Done! return false; }