Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # Copyright (c) 2018, the R8 project authors. Please see the AUTHORS file |
| 3 | # for details. All rights reserved. Use of this source code is governed by a |
| 4 | # BSD-style license that can be found in the LICENSE file. |
| 5 | |
| 6 | # Script for building sample apks using the sdk tools directly. |
| 7 | |
| 8 | import apk_utils |
| 9 | import fnmatch |
| 10 | import glob |
| 11 | import optparse |
| 12 | import os |
| 13 | import shutil |
| 14 | import subprocess |
| 15 | import sys |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 16 | import time |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 17 | import utils |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 18 | import uuid |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 19 | |
Rico Wind | 9d70f61 | 2018-08-31 09:17:43 +0200 | [diff] [blame] | 20 | |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 21 | DEFAULT_AAPT = 'aapt' # Assume in path. |
| 22 | DEFAULT_D8 = os.path.join(utils.REPO_ROOT, 'tools', 'd8.py') |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 23 | DEFAULT_DEXSPLITTER = os.path.join(utils.REPO_ROOT, 'tools', 'dexsplitter.py') |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 24 | DEFAULT_JAVAC = 'javac' |
| 25 | SRC_LOCATION = 'src/com/android/tools/r8/sample/{app}/*.java' |
| 26 | DEFAULT_KEYSTORE = os.path.join(os.getenv('HOME'), '.android', 'debug.keystore') |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 27 | PACKAGE_PREFIX = 'com.android.tools.r8.sample' |
| 28 | STANDARD_ACTIVITY = "R8Activity" |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 29 | BENCHMARK_ITERATIONS = 30 |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 30 | |
| 31 | SAMPLE_APKS = [ |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 32 | 'simple', |
| 33 | 'split' |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 34 | ] |
| 35 | |
| 36 | def parse_options(): |
| 37 | result = optparse.OptionParser() |
| 38 | result.add_option('--aapt', |
| 39 | help='aapt executable to use', |
| 40 | default=DEFAULT_AAPT) |
| 41 | result.add_option('--api', |
| 42 | help='Android api level', |
| 43 | default=21, |
| 44 | choices=[14, 15, 19, 21, 22, 23, 24, 25, 26]) |
| 45 | result.add_option('--keystore', |
| 46 | help='Keystore used for signing', |
| 47 | default=DEFAULT_KEYSTORE) |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 48 | result.add_option('--split', |
| 49 | help='Split the app using the split.spec file', |
| 50 | default=False, action='store_true') |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 51 | result.add_option('--install', |
| 52 | help='Install the app (including featuresplit)', |
| 53 | default=False, action='store_true') |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 54 | result.add_option('--benchmark', |
| 55 | help='Benchmark the app on the phone with specialized markers', |
| 56 | default=False, action='store_true') |
| 57 | result.add_option('--benchmark-output-dir', |
| 58 | help='Store benchmark results here.', |
| 59 | default=None) |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 60 | result.add_option('--app', |
| 61 | help='Which app to build', |
| 62 | default='simple', |
| 63 | choices=SAMPLE_APKS) |
| 64 | return result.parse_args() |
| 65 | |
| 66 | def run_aapt(aapt, args): |
| 67 | command = [aapt] |
| 68 | command.extend(args) |
| 69 | utils.PrintCmd(command) |
| 70 | subprocess.check_call(command) |
| 71 | |
| 72 | def get_build_dir(app): |
| 73 | return os.path.join(utils.BUILD, 'sampleApks', app) |
| 74 | |
| 75 | def get_gen_path(app): |
| 76 | gen_path = os.path.join(get_build_dir(app), 'gen') |
| 77 | utils.makedirs_if_needed(gen_path) |
| 78 | return gen_path |
| 79 | |
| 80 | def get_bin_path(app): |
| 81 | bin_path = os.path.join(get_build_dir(app), 'bin') |
| 82 | utils.makedirs_if_needed(bin_path) |
| 83 | return bin_path |
| 84 | |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 85 | |
Rico Wind | f395863 | 2018-08-07 09:39:19 +0200 | [diff] [blame] | 86 | def get_guava_jar(): |
| 87 | return os.path.join(utils.REPO_ROOT, |
| 88 | 'third_party/gradle-plugin/com/google/guava/guava/22.0/guava-22.0.jar') |
| 89 | |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 90 | def get_sample_dir(app): |
| 91 | return os.path.join(utils.REPO_ROOT, 'src', 'test', 'sampleApks', app) |
| 92 | |
| 93 | def get_src_path(app): |
| 94 | return os.path.join(get_sample_dir(app), 'src') |
| 95 | |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 96 | def get_dex_path(app): |
| 97 | return os.path.join(get_bin_path(app), 'classes.dex') |
| 98 | |
| 99 | def get_split_path(app, split): |
| 100 | return os.path.join(get_bin_path(app), split, 'classes.dex') |
| 101 | |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 102 | def get_package_name(app): |
| 103 | return '%s.%s' % (PACKAGE_PREFIX, app) |
| 104 | |
| 105 | def get_qualified_activity(app): |
| 106 | # The activity specified to adb start is PACKAGE_NAME/.ACTIVITY |
| 107 | return '%s/.%s' % (get_package_name(app), STANDARD_ACTIVITY) |
| 108 | |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 109 | def run_aapt_pack(aapt, api, app): |
| 110 | with utils.ChangedWorkingDirectory(get_sample_dir(app)): |
| 111 | args = ['package', |
| 112 | '-v', '-f', |
Rico Wind | 9d70f61 | 2018-08-31 09:17:43 +0200 | [diff] [blame] | 113 | '-I', utils.get_android_jar(api), |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 114 | '-M', 'AndroidManifest.xml', |
| 115 | '-A', 'assets', |
| 116 | '-S', 'res', |
| 117 | '-m', |
| 118 | '-J', get_gen_path(app), |
Søren Gjesse | 12c5209 | 2018-04-05 14:22:29 +0200 | [diff] [blame] | 119 | '-F', os.path.join(get_bin_path(app), 'resources.ap_'), |
| 120 | '-G', os.path.join(get_build_dir(app), 'proguard_options')] |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 121 | run_aapt(aapt, args) |
| 122 | |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 123 | def run_aapt_split_pack(aapt, api, app): |
| 124 | with utils.ChangedWorkingDirectory(get_sample_dir(app)): |
| 125 | args = ['package', |
| 126 | '-v', '-f', |
Rico Wind | 9d70f61 | 2018-08-31 09:17:43 +0200 | [diff] [blame] | 127 | '-I', utils.get_android_jar(api), |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 128 | '-M', 'split_manifest/AndroidManifest.xml', |
| 129 | '-S', 'res', |
| 130 | '-F', os.path.join(get_bin_path(app), 'split_resources.ap_')] |
| 131 | run_aapt(aapt, args) |
| 132 | |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 133 | def compile_with_javac(api, app): |
| 134 | with utils.ChangedWorkingDirectory(get_sample_dir(app)): |
| 135 | files = glob.glob(SRC_LOCATION.format(app=app)) |
Rico Wind | 9d70f61 | 2018-08-31 09:17:43 +0200 | [diff] [blame] | 136 | classpath = '%s:%s' % (utils.get_android_jar(api), get_guava_jar()) |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 137 | command = [DEFAULT_JAVAC, |
Rico Wind | 9d70f61 | 2018-08-31 09:17:43 +0200 | [diff] [blame] | 138 | '-classpath', classpath, |
Rico Wind | f395863 | 2018-08-07 09:39:19 +0200 | [diff] [blame] | 139 | '-sourcepath', '%s:%s:%s' % ( |
| 140 | get_src_path(app), |
| 141 | get_gen_path(app), |
| 142 | get_guava_jar()), |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 143 | '-d', get_bin_path(app)] |
| 144 | command.extend(files) |
| 145 | utils.PrintCmd(command) |
| 146 | subprocess.check_call(command) |
| 147 | |
| 148 | def dex(app, api): |
| 149 | files = [] |
| 150 | for root, dirnames, filenames in os.walk(get_bin_path(app)): |
| 151 | for filename in fnmatch.filter(filenames, '*.class'): |
| 152 | files.append(os.path.join(root, filename)) |
| 153 | command = [DEFAULT_D8, |
| 154 | '--output', get_bin_path(app), |
Rico Wind | 9d70f61 | 2018-08-31 09:17:43 +0200 | [diff] [blame] | 155 | '--classpath', utils.get_android_jar(api), |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 156 | '--min-api', str(api)] |
| 157 | command.extend(files) |
Rico Wind | f395863 | 2018-08-07 09:39:19 +0200 | [diff] [blame] | 158 | command.append(get_guava_jar()) |
| 159 | |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 160 | utils.PrintCmd(command) |
| 161 | subprocess.check_call(command) |
| 162 | |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 163 | def split(app): |
| 164 | split_spec = os.path.join(get_sample_dir(app), 'split.spec') |
| 165 | command = [DEFAULT_DEXSPLITTER, |
| 166 | '--input', get_dex_path(app), |
| 167 | '--output', get_bin_path(app), |
| 168 | '--feature-splits', split_spec] |
| 169 | utils.PrintCmd(command) |
| 170 | subprocess.check_call(command) |
| 171 | |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 172 | def run_adb(args, ignore_exit=False): |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 173 | command = ['adb'] |
| 174 | command.extend(args) |
| 175 | utils.PrintCmd(command) |
Rico Wind | e01a505 | 2018-07-06 07:28:32 +0200 | [diff] [blame] | 176 | # On M adb install-multiple exits 1 but succeed in installing. |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 177 | if ignore_exit: |
| 178 | subprocess.call(command) |
| 179 | else: |
| 180 | subprocess.check_call(command) |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 181 | |
| 182 | def adb_install(apks): |
| 183 | args = [ |
| 184 | 'install-multiple' if len(apks) > 1 else 'install', |
| 185 | '-r', |
| 186 | '-d'] |
| 187 | args.extend(apks) |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 188 | run_adb(args, ignore_exit=True) |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 189 | |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 190 | def create_temp_apk(app, prefix): |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 191 | temp_apk_path = os.path.join(get_bin_path(app), '%s.ap_' % app) |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 192 | shutil.copyfile(os.path.join(get_bin_path(app), '%sresources.ap_' % prefix), |
| 193 | temp_apk_path) |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 194 | return temp_apk_path |
| 195 | |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 196 | def aapt_add_dex(aapt, dex, temp_apk_path): |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 197 | args = ['add', |
| 198 | '-k', temp_apk_path, |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 199 | dex] |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 200 | run_aapt(aapt, args) |
| 201 | |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 202 | def kill(app): |
| 203 | args = ['shell', 'am', 'force-stop', get_package_name(app)] |
| 204 | run_adb(args) |
| 205 | |
| 206 | def start_logcat(): |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 207 | return subprocess.Popen(['adb', 'logcat'], bufsize=1024*1024, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 208 | |
| 209 | def start(app): |
| 210 | args = ['shell', 'am', 'start', '-n', get_qualified_activity(app)] |
| 211 | run_adb(args) |
| 212 | |
| 213 | def clear_logcat(): |
| 214 | args = ['logcat', '-c'] |
| 215 | run_adb(args) |
| 216 | |
| 217 | def stop_logcat(popen): |
| 218 | popen.terminate() |
| 219 | lines = [] |
| 220 | for l in popen.stdout: |
| 221 | if 'System.out' in l: |
| 222 | lines.append(l) |
| 223 | return lines |
| 224 | |
| 225 | def store_or_print_benchmarks(lines, output): |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 226 | results = {} |
| 227 | overall_total = 0 |
| 228 | # We assume that the total times are |
| 229 | # prefixed with 'NAME Total: '. The logcat lines looks like: |
| 230 | # 06-28 12:22:00.991 13698 13698 I System.out: Call Total: 61614 |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 231 | for l in lines: |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 232 | if 'Total: ' in l: |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 233 | split = l.split('Total: ') |
| 234 | time = split[1] |
| 235 | name = split[0].split()[-1] |
| 236 | overall_total += int(time) |
| 237 | print '%s: %s' % (name, time) |
| 238 | results[name] = time |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 239 | |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 240 | print 'Total: %s' % overall_total |
| 241 | if not output: |
| 242 | return overall_total |
| 243 | results['total'] = str(overall_total) |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 244 | output_dir = os.path.join(output, str(uuid.uuid4())) |
| 245 | os.makedirs(output_dir) |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 246 | written_files = [] |
| 247 | for name, time in results.iteritems(): |
| 248 | total_file = os.path.join(output_dir, name) |
| 249 | written_files.append(total_file) |
| 250 | with open(total_file, 'w') as f: |
| 251 | f.write(time) |
| 252 | |
| 253 | print 'Result stored in: \n%s' % ('\n'.join(written_files)) |
| 254 | return overall_total |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 255 | |
| 256 | def benchmark(app, output_dir): |
| 257 | # Ensure app is not running |
| 258 | kill(app) |
| 259 | clear_logcat() |
| 260 | logcat = start_logcat() |
| 261 | start(app) |
| 262 | # We could do better here by continiously parsing the logcat for a marker, but |
| 263 | # this works nicely with the current setup. |
Rico Wind | f395863 | 2018-08-07 09:39:19 +0200 | [diff] [blame] | 264 | time.sleep(12) |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 265 | kill(app) |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 266 | return float(store_or_print_benchmarks(stop_logcat(logcat), output_dir)) |
| 267 | |
| 268 | def ensure_no_logcat(): |
| 269 | output = subprocess.check_output(['ps', 'aux']) |
| 270 | if 'adb logcat' in output: |
| 271 | raise Exception('You have adb logcat running, please close it and rerun') |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 272 | |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 273 | def Main(): |
| 274 | (options, args) = parse_options() |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 275 | apks = [] |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 276 | is_split = options.split |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 277 | run_aapt_pack(options.aapt, options.api, options.app) |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 278 | if is_split: |
| 279 | run_aapt_split_pack(options.aapt, options.api, options.app) |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 280 | compile_with_javac(options.api, options.app) |
| 281 | dex(options.app, options.api) |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 282 | dex_files = { options.app: get_dex_path(options.app)} |
| 283 | dex_path = get_dex_path(options.app) |
| 284 | if is_split: |
| 285 | split(options.app) |
| 286 | dex_path = get_split_path(options.app, 'base') |
| 287 | |
| 288 | temp_apk_path = create_temp_apk(options.app, '') |
| 289 | aapt_add_dex(options.aapt, dex_path, temp_apk_path) |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 290 | apk_path = os.path.join(get_bin_path(options.app), '%s.apk' % options.app) |
| 291 | apk_utils.sign(temp_apk_path, apk_path, options.keystore) |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 292 | apks.append(apk_path) |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 293 | |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 294 | if is_split: |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 295 | split_temp_apk_path = create_temp_apk(options.app, 'split_') |
| 296 | aapt_add_dex(options.aapt, |
| 297 | get_split_path(options.app, 'split'), |
| 298 | temp_apk_path) |
| 299 | split_apk_path = os.path.join(get_bin_path(options.app), 'featuresplit.apk') |
| 300 | apk_utils.sign(temp_apk_path, split_apk_path, options.keystore) |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 301 | apks.append(split_apk_path) |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 302 | |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 303 | print('Generated apks available at: %s' % ' '.join(apks)) |
Rico Wind | e01a505 | 2018-07-06 07:28:32 +0200 | [diff] [blame] | 304 | if options.install or options.benchmark: |
Rico Wind | d0cd1a5 | 2018-06-27 15:26:22 +0200 | [diff] [blame] | 305 | adb_install(apks) |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 306 | grand_total = 0 |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 307 | if options.benchmark: |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 308 | ensure_no_logcat() |
Rico Wind | b1e257e | 2018-06-28 14:17:09 +0200 | [diff] [blame] | 309 | for _ in range(BENCHMARK_ITERATIONS): |
Rico Wind | 097e3eb | 2018-07-05 15:14:10 +0200 | [diff] [blame] | 310 | grand_total += benchmark(options.app, options.benchmark_output_dir) |
| 311 | print 'Combined average: %s' % (grand_total/BENCHMARK_ITERATIONS) |
Rico Wind | a5db71d8 | 2018-06-27 13:45:05 +0200 | [diff] [blame] | 312 | |
Rico Wind | 0ed24cc | 2018-03-23 07:16:35 +0100 | [diff] [blame] | 313 | if __name__ == '__main__': |
| 314 | sys.exit(Main()) |