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.
7 """Utility script to install APKs from the command line quickly."""
13 from pylib
import android_commands
14 from pylib
import constants
15 from pylib
.device
import device_utils
18 def AddInstallAPKOption(option_parser
):
19 """Adds apk option used to install the APK to the OptionParser."""
20 option_parser
.add_option('--apk',
21 help=('DEPRECATED The name of the apk containing the'
22 ' application (with the .apk extension).'))
23 option_parser
.add_option('--apk_package',
24 help=('DEPRECATED The package name used by the apk '
25 'containing the application.'))
26 option_parser
.add_option('--keep_data',
29 help=('Keep the package data when installing '
31 option_parser
.add_option('--debug', action
='store_const', const
='Debug',
33 default
=os
.environ
.get('BUILDTYPE', 'Debug'),
34 help='If set, run test suites under out/Debug. '
35 'Default is env var BUILDTYPE or Debug')
36 option_parser
.add_option('--release', action
='store_const', const
='Release',
38 help='If set, run test suites under out/Release. '
39 'Default is env var BUILDTYPE or Debug.')
40 option_parser
.add_option('-d', '--device', dest
='device',
41 help='Target device for apk to install on.')
44 def ValidateInstallAPKOption(option_parser
, options
, args
):
45 """Validates the apk option and potentially qualifies the path."""
50 option_parser
.error('apk target not specified.')
52 if not options
.apk
.endswith('.apk'):
55 if not os
.path
.exists(options
.apk
):
56 options
.apk
= os
.path
.join(constants
.GetOutDirectory(), 'apks',
61 parser
= optparse
.OptionParser()
62 parser
.set_usage("usage: %prog [options] target")
63 AddInstallAPKOption(parser
)
64 options
, args
= parser
.parse_args(argv
)
66 if len(args
) > 1 and options
.apk
:
67 parser
.error("Appending the apk as argument can't be used with --apk.")
69 parser
.error("Too many arguments.")
71 constants
.SetBuildType(options
.build_type
)
72 ValidateInstallAPKOption(parser
, options
, args
)
74 devices
= android_commands
.GetAttachedDevices()
77 if options
.device
not in devices
:
78 raise Exception('Error: %s not in attached devices %s' % (options
.device
,
80 devices
= [options
.device
]
83 raise Exception('Error: no connected devices')
85 device_utils
.DeviceUtils
.parallel(devices
).Install(
86 options
.apk
, reinstall
=options
.keep_data
)
89 if __name__
== '__main__':
90 sys
.exit(main(sys
.argv
))