adb_install_apk.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env vpython3
  2. #
  3. # Copyright (c) 2012 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Utility script to install APKs from the command line quickly."""
  7. import argparse
  8. import glob
  9. import logging
  10. import os
  11. import sys
  12. import devil_chromium
  13. from devil.android import apk_helper
  14. from devil.android import device_denylist
  15. from devil.android import device_errors
  16. from devil.android import device_utils
  17. from devil.utils import run_tests_helper
  18. from pylib import constants
  19. def main():
  20. parser = argparse.ArgumentParser()
  21. apk_group = parser.add_mutually_exclusive_group(required=True)
  22. apk_group.add_argument('--apk', dest='apk_name',
  23. help='DEPRECATED The name of the apk containing the'
  24. ' application (with the .apk extension).')
  25. apk_group.add_argument('apk_path', nargs='?',
  26. help='The path to the APK to install.')
  27. # TODO(jbudorick): Remove once no clients pass --apk_package
  28. parser.add_argument('--apk_package', help='DEPRECATED unused')
  29. parser.add_argument('--split',
  30. action='append',
  31. dest='splits',
  32. help='A glob matching the apk splits. '
  33. 'Can be specified multiple times.')
  34. parser.add_argument('--keep_data',
  35. action='store_true',
  36. default=False,
  37. help='Keep the package data when installing '
  38. 'the application.')
  39. parser.add_argument('--debug', action='store_const', const='Debug',
  40. dest='build_type',
  41. default=os.environ.get('BUILDTYPE', 'Debug'),
  42. help='If set, run test suites under out/Debug. '
  43. 'Default is env var BUILDTYPE or Debug')
  44. parser.add_argument('--release', action='store_const', const='Release',
  45. dest='build_type',
  46. help='If set, run test suites under out/Release. '
  47. 'Default is env var BUILDTYPE or Debug.')
  48. parser.add_argument('-d', '--device', dest='devices', action='append',
  49. default=[],
  50. help='Target device for apk to install on. Enter multiple'
  51. ' times for multiple devices.')
  52. parser.add_argument('--adb-path', type=os.path.abspath,
  53. help='Absolute path to the adb binary to use.')
  54. parser.add_argument('--denylist-file', help='Device denylist JSON file.')
  55. parser.add_argument('-v',
  56. '--verbose',
  57. action='count',
  58. help='Enable verbose logging.',
  59. default=0)
  60. parser.add_argument('--downgrade', action='store_true',
  61. help='If set, allows downgrading of apk.')
  62. parser.add_argument('--timeout', type=int,
  63. default=device_utils.DeviceUtils.INSTALL_DEFAULT_TIMEOUT,
  64. help='Seconds to wait for APK installation. '
  65. '(default: %(default)s)')
  66. args = parser.parse_args()
  67. run_tests_helper.SetLogLevel(args.verbose)
  68. constants.SetBuildType(args.build_type)
  69. devil_chromium.Initialize(
  70. output_directory=constants.GetOutDirectory(),
  71. adb_path=args.adb_path)
  72. apk = args.apk_path or args.apk_name
  73. if not apk.endswith('.apk'):
  74. apk += '.apk'
  75. if not os.path.exists(apk):
  76. apk = os.path.join(constants.GetOutDirectory(), 'apks', apk)
  77. if not os.path.exists(apk):
  78. parser.error('%s not found.' % apk)
  79. if args.splits:
  80. splits = []
  81. base_apk_package = apk_helper.ApkHelper(apk).GetPackageName()
  82. for split_glob in args.splits:
  83. apks = [f for f in glob.glob(split_glob) if f.endswith('.apk')]
  84. if not apks:
  85. logging.warning('No apks matched for %s.', split_glob)
  86. for f in apks:
  87. helper = apk_helper.ApkHelper(f)
  88. if (helper.GetPackageName() == base_apk_package
  89. and helper.GetSplitName()):
  90. splits.append(f)
  91. denylist = (device_denylist.Denylist(args.denylist_file)
  92. if args.denylist_file else None)
  93. devices = device_utils.DeviceUtils.HealthyDevices(denylist=denylist,
  94. device_arg=args.devices)
  95. def denylisting_install(device):
  96. try:
  97. if args.splits:
  98. device.InstallSplitApk(apk, splits, reinstall=args.keep_data,
  99. allow_downgrade=args.downgrade)
  100. else:
  101. device.Install(apk, reinstall=args.keep_data,
  102. allow_downgrade=args.downgrade,
  103. timeout=args.timeout)
  104. except (device_errors.CommandFailedError,
  105. device_errors.DeviceUnreachableError):
  106. logging.exception('Failed to install %s', apk)
  107. if denylist:
  108. denylist.Extend([str(device)], reason='install_failure')
  109. logging.warning('Denylisting %s', str(device))
  110. except device_errors.CommandTimeoutError:
  111. logging.exception('Timed out while installing %s', apk)
  112. if denylist:
  113. denylist.Extend([str(device)], reason='install_timeout')
  114. logging.warning('Denylisting %s', str(device))
  115. device_utils.DeviceUtils.parallel(devices).pMap(denylisting_install)
  116. if __name__ == '__main__':
  117. sys.exit(main())