def InstallApk()

in tools/android/emulator/emulated_device.py [0:0]


  def InstallApk(self, apk_path, max_tries=5, grant_runtime_permissions=False):
    """Installs the given apk onto the device."""
    assert os.path.exists(apk_path), 'apk doesnt exist at: %s' % apk_path
    attempts = 0
    install_args = [self.android_platform.adb,
                    '-s',
                    self.device_serial,
                    'install']

    # allow downgrades if api supports it.
    if self.GetApiVersion() > 20:
      install_args.append('-d')
    if self.GetApiVersion() >= 23 and grant_runtime_permissions:
      install_args.append('-g')

    install_args.extend(['-r', apk_path])

    pkg_size = os.path.getsize(apk_path)

    install_timeout_secs = 60

    uses_art = self.GetApiVersion() > 20

    if pkg_size > (30 << 20):
      # pkg more than 30mb, extend timeout.
      # TODO: Reduce to previous value (180) once GMM timeouts are
      # under control.
      install_timeout_secs = 240
    elif uses_art and pkg_size > (20 << 20):
      # if pkg more than 20mb, increase by a smaller amount
      # this is especially needed on API >= 21, where presumably art
      # optimization will add an additional time penalty
      install_timeout_secs = 120



    while True:
      logging.info('installing: %s', apk_path)
      install_output = ''
      try:
        if uses_art:
          exit_status, install_output = self._Dex2OatCheckingInstall(
              install_args)
        else:
          install_task = common.SpawnAndWaitWithRetry(
              install_args,
              timeout_seconds=install_timeout_secs,
              exec_env=self._AdbEnv(),
              proc_output=True)
          exit_status = install_task.returncode
          install_output = install_task.borg_out

        if exit_status == 0 and 'Success' in install_output:
          logging.info('install done: %s', apk_path)
          return
        if self._IsPermanentInstallError(install_output):
          logging.warning('install failed: %s %s', apk_path, install_output)
          self._reporter.ReportFailure(
              'tools.android.emulator.install.PermanentInstallError', {
                  'apk': apk_path,
                  'apk_basename': os.path.basename(apk_path),
                  'install_output': install_output,
                  'install_failure_type': _InstallFailureType(install_output),
              })
          raise Exception('permanent install failure')
        else:
          logging.info('Install failed: %s', install_output)
          logging.info('logcat: %s', self.ExecOnDevice(
              ['logcat -v threadtime -b all -d']))
      except common.SpawnError:
        self._reporter.ReportFailure(
            'tools.android.emulator.TimeoutInstallError', {
                'apk': apk_path,
                'apk_basename': os.path.basename(apk_path),
            })
        install_output = 'timeout failure'
      attempts += 1
      if attempts >= max_tries:
        self._reporter.ReportFailure(
            'tools.android.emulator.install.ExceededMaxFailures', {
                'apk': apk_path,
                'apk_basename': os.path.basename(apk_path),
                'attempts': attempts,
                'install_output': install_output,
                'install_failure_type': _InstallFailureType(install_output),
            })
      assert attempts < max_tries
      logging.info('%s: attempting install again due to: %s',
                   apk_path, install_output)
      time.sleep(1)