コード例 #1
0
    public Void call() throws IOException {
      if (logger == null) {
        logger = listener.getLogger();
      }

      final File homeDir = Utils.getHomeDirectory(androidSdkHome);

      // Parse the AVD's config
      Map<String, String> configValues;
      configValues = parseAvdConfigFile(homeDir);

      // Insert any hardware properties we want to override
      AndroidEmulator.log(logger, Messages.SETTING_HARDWARE_PROPERTIES());
      for (HardwareProperty prop : hardwareProperties) {
        AndroidEmulator.log(logger, String.format("%s: %s", prop.key, prop.value), true);
        configValues.put(prop.key, prop.value);
      }

      // Update config file
      writeAvdConfigFile(homeDir, configValues);

      return null;
    }
コード例 #2
0
    public Boolean call() throws Exception {
      if (logger == null) {
        logger = listener.getLogger();
      }

      // Check whether the AVD exists
      final File homeDir = Utils.getHomeDirectory(androidSdkHome);
      final File avdDirectory = getAvdDirectory(homeDir);
      final boolean emulatorExists = avdDirectory.exists();
      if (!emulatorExists) {
        AndroidEmulator.log(logger, Messages.AVD_DIRECTORY_NOT_FOUND(avdDirectory));
        return false;
      }

      // Recursively delete the contents
      new FilePath(avdDirectory).deleteRecursive();

      // Delete the metadata file
      getAvdMetadataFile().delete();

      // Success!
      return true;
    }
コード例 #3
0
    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;
    }