1# Copyright 2015 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""This module wraps Android's split-select tool.""" 5 6from devil.android.sdk import build_tools 7from devil.utils import cmd_helper 8from devil.utils import lazy 9 10_split_select_path = lazy.WeakConstant(lambda: build_tools.GetPath( 11 'split-select')) 12 13 14def _RunSplitSelectCmd(args): 15 """Runs a split-select command. 16 17 Args: 18 args: A list of arguments for split-select. 19 20 Returns: 21 The output of the command. 22 """ 23 cmd = [_split_select_path.read()] + args 24 status, output = cmd_helper.GetCmdStatusAndOutput(cmd) 25 if status != 0: 26 raise Exception('Failed running command "%s" with output "%s".' % 27 (' '.join(cmd), output)) 28 return output 29 30 31def _SplitConfig(device, allow_cached_props=False): 32 """Returns a config specifying which APK splits are required by the device. 33 34 Args: 35 device: A DeviceUtils object. 36 allow_cached_props: Whether to use cached values for device properties. 37 """ 38 return ('%s-r%s-%s:%s' % (device.GetLanguage(cache=allow_cached_props), 39 device.GetCountry(cache=allow_cached_props), 40 device.screen_density, device.product_cpu_abi)) 41 42 43def SelectSplits(device, base_apk, split_apks, allow_cached_props=False): 44 """Determines which APK splits the device requires. 45 46 Args: 47 device: A DeviceUtils object. 48 base_apk: The path of the base APK. 49 split_apks: A list of paths of APK splits. 50 allow_cached_props: Whether to use cached values for device properties. 51 52 Returns: 53 The list of APK splits that the device requires. 54 """ 55 config = _SplitConfig(device, allow_cached_props=allow_cached_props) 56 args = ['--target', config, '--base', base_apk] 57 for split in split_apks: 58 args.extend(['--split', split]) 59 return _RunSplitSelectCmd(args).splitlines() 60