/**
  * Extracts the package name from an apk file.
  *
  * @param apkFile apk file to extract package name from.
  * @return the package name from inside the apk file.
  */
 protected String extractPackageNameFromApk(File apkFile) throws MojoExecutionException {
   CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
   executor.setLogger(this.getLog());
   List<String> commands = new ArrayList<String>();
   commands.add("dump");
   commands.add("xmltree");
   commands.add(apkFile.getAbsolutePath());
   commands.add("AndroidManifest.xml");
   getLog().info(getAndroidSdk().getPathForTool("aapt") + " " + commands.toString());
   try {
     executor.executeCommand(getAndroidSdk().getPathForTool("aapt"), commands, true);
     final String xmlTree = executor.getStandardOut();
     return extractPackageNameFromAndroidManifestXmlTree(xmlTree);
   } catch (ExecutionException e) {
     throw new MojoExecutionException(
         "Error while trying to figure out package name from inside apk file " + apkFile);
   } finally {
     getLog().error(executor.getStandardError());
   }
 }
 /**
  * Undeploys an apk, specified by package name, from a connected emulator or usb device.
  *
  * @param packageName the package name to undeploy.
  * @param deleteDataAndCacheDirectoriesOnDevice <code>true</code> to delete the application's data
  *     and cache directories on the device, <code>false</code> to keep them.
  * @return <code>true</code> if successfully undeployed, <code>false</code> otherwise.
  */
 protected boolean undeployApk(String packageName, boolean deleteDataAndCacheDirectoriesOnDevice)
     throws MojoExecutionException {
   CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
   executor.setLogger(this.getLog());
   List<String> commands = new ArrayList<String>();
   commands.add("uninstall");
   if (!deleteDataAndCacheDirectoriesOnDevice) {
     commands.add("-k"); // ('-k' means keep the data and cache directories)
   }
   commands.add(packageName);
   getLog().info(getAndroidSdk().getPathForTool("adb") + " " + commands.toString());
   try {
     executor.executeCommand(getAndroidSdk().getPathForTool("adb"), commands, false);
     getLog().debug(executor.getStandardOut());
     getLog().debug(executor.getStandardError());
     return true;
   } catch (ExecutionException e) {
     getLog().error(executor.getStandardOut());
     getLog().error(executor.getStandardError());
     return false;
   }
 }
  /**
   * Deploys an apk file to a connected emulator or usb device.
   *
   * @param apkFile the file to deploy
   * @throws MojoExecutionException If there is a problem deploying the apk file.
   */
  protected void deployApk(File apkFile) throws MojoExecutionException {
    CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
    executor.setLogger(this.getLog());
    List<String> commands = new ArrayList<String>();

    // Check if a specific device should be used
    if (StringUtils.isNotBlank(device)) {
      if ("usb".equals(device)) {
        commands.add("-d");
      } else if ("emulator".equals(device)) {
        commands.add("-e");
      } else {
        commands.add("-s");
        commands.add(device);
      }
    }

    commands.add("install");
    commands.add("-r");
    commands.add(apkFile.getAbsolutePath());
    getLog().info(getAndroidSdk().getPathForTool("adb") + " " + commands.toString());
    try {
      executor.executeCommand(getAndroidSdk().getPathForTool("adb"), commands, false);
      final String standardOut = executor.getStandardOut();
      if (standardOut != null && standardOut.contains("Failure")) {
        throw new MojoExecutionException(
            "Error deploying "
                + apkFile
                + " to device. You might want to add command line parameter -Dandroid.undeployBeforeDeploy=true or add plugin configuration tag <undeployBeforeDeploy>true</undeployBeforeDeploy>\n"
                + standardOut);
      }
    } catch (ExecutionException e) {
      getLog().error(executor.getStandardOut());
      getLog().error(executor.getStandardError());
      throw new MojoExecutionException("Error deploying " + apkFile + " to device.", e);
    }
  }