blob: 22edf2294ad9d71c9b2702e8e634231651c0aa21 [file] [log] [blame]
Søren Gjessecdae8792018-12-12 09:02:43 +01001#!/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
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +01006import apk_masseur
Søren Gjessecdae8792018-12-12 09:02:43 +01007import apk_utils
Morten Krogh-Jespersenc2fbde22019-02-07 13:59:55 +01008import golem
Søren Gjesseeed839d2019-01-11 15:19:16 +01009import gradle
Ian Zerny3f54e222019-02-12 10:51:17 +010010import jdk
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +010011import json
Søren Gjessecdae8792018-12-12 09:02:43 +010012import os
13import optparse
Christoffer Quist Adamsenbbe5d7d2019-01-23 13:15:21 +010014import shutil
Søren Gjessecdae8792018-12-12 09:02:43 +010015import subprocess
16import sys
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +010017import time
Søren Gjessecdae8792018-12-12 09:02:43 +010018import utils
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +010019import zipfile
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +010020from xml.dom import minidom
Søren Gjessecdae8792018-12-12 09:02:43 +010021
Christoffer Quist Adamsen10b7db82018-12-13 14:50:38 +010022import as_utils
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +010023import update_prebuilds_in_android
Rico Windbc7cab02019-04-04 12:22:09 +020024import download_all_benchmark_dependencies
Christoffer Quist Adamsen10b7db82018-12-13 14:50:38 +010025
Morten Krogh-Jespersend45d95e2019-02-07 13:27:17 +010026SHRINKERS = ['r8', 'r8-full', 'r8-nolib', 'r8-nolib-full', 'pg']
Morten Krogh-Jespersen64740202019-02-07 13:06:04 +010027WORKING_DIR = os.path.join(utils.BUILD, 'opensource_apps')
Christoffer Quist Adamsen10b7db82018-12-13 14:50:38 +010028
Morten Krogh-Jespersenaeb665e2019-02-07 12:29:03 +010029if ('R8_BENCHMARK_DIR' in os.environ
30 and os.path.isdir(os.environ['R8_BENCHMARK_DIR'])):
Christoffer Quist Adamsen10b7db82018-12-13 14:50:38 +010031 WORKING_DIR = os.environ['R8_BENCHMARK_DIR']
Søren Gjessecdae8792018-12-12 09:02:43 +010032
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +010033class Repo(object):
34 def __init__(self, fields):
35 self.__dict__ = fields
36
37 # If there is only one app in this repository, then give the app the same
38 # name as the repository, if it does not already have one.
39 if len(self.apps) == 1:
40 app = self.apps[0]
41 if not app.name:
42 app.name = self.name
43
44class App(object):
45 def __init__(self, fields):
46 module = fields.get('module', 'app')
47 defaults = {
48 'archives_base_name': module,
49 'build_dir': 'build',
50 'compile_sdk': None,
51 'dir': '.',
52 'flavor': None,
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +010053 'has_instrumentation_tests': False,
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +010054 'main_dex_rules': None,
55 'module': module,
56 'min_sdk': None,
57 'name': None,
58 'releaseTarget': None,
59 'signed_apk_name': None,
60 'skip': False
61 }
62 self.__dict__ = dict(defaults.items() + fields.items())
63
64# For running on Golem all third-party repositories are bundled as an x20-
65# dependency and then copied to WORKING_DIR. To update the app-bundle use
66# 'run_on_as_app_x20_packager.py'.
67APP_REPOSITORIES = [
68 # ...
69 # Repo({
70 # 'name': ...,
71 # 'url': ...,
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +010072 # 'revision': ...,
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +010073 # 'apps': [
74 # {
75 # 'id': ...,
76 # 'dir': ...,
77 # 'module': ... (default app)
78 # 'name': ...,
79 # 'archives_base_name': ... (default same as module)
80 # 'flavor': ... (default no flavor)
81 # 'releaseTarget': ... (default <module>:assemble<flavor>Release
82 # },
83 # ...
84 # ]
85 # }),
86 # ...
87 Repo({
88 'name': 'android-suite',
89 'url': 'https://github.com/christofferqa/android-suite',
90 'revision': '46c96f214711cf6cdcb72cc0c94520ef418e3739',
91 'apps': [
92 App({
93 'id': 'com.numix.calculator',
94 'dir': 'Calculator',
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +010095 'name': 'numix-calculator',
96 'has_instrumentation_tests': True
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +010097 })
98 ]
99 }),
100 Repo({
101 'name': 'AnExplorer',
102 'url': 'https://github.com/christofferqa/AnExplorer',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100103 'revision': '365927477b8eab4052a1882d5e358057ae3dee4d',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100104 'apps': [
105 App({
106 'id': 'dev.dworks.apps.anexplorer.pro',
107 'flavor': 'googleMobilePro',
108 'signed_apk_name': 'AnExplorer-googleMobileProRelease-4.0.3.apk',
109 'min_sdk': 17
110 })
111 ]
112 }),
113 Repo({
114 'name': 'AntennaPod',
115 'url': 'https://github.com/christofferqa/AntennaPod.git',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100116 'revision': '77e94f4783a16abe9cc5b78dc2d2b2b1867d8c06',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100117 'apps': [
118 App({
119 'id': 'de.danoeh.antennapod',
120 'flavor': 'play',
121 'min_sdk': 14,
122 'compile_sdk': 26
123 })
124 ]
125 }),
126 Repo({
Morten Krogh-Jespersen2af7daa2019-03-11 13:50:42 +0100127 'name': 'applymapping',
128 'url': 'https://github.com/mkj-gram/applymapping',
Christoffer Quist Adamsene4ef1452019-03-13 10:38:00 +0100129 'revision': 'e3ae14b8c16fa4718e5dea8f7ad00937701b3c48',
Morten Krogh-Jespersen2af7daa2019-03-11 13:50:42 +0100130 'apps': [
131 App({
132 'id': 'com.example.applymapping',
133 'has_instrumentation_tests': True
134 })
135 ]
136 }),
137 Repo({
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100138 'name': 'apps-android-wikipedia',
139 'url': 'https://github.com/christofferqa/apps-android-wikipedia',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100140 'revision': '686e8aa5682af8e6a905054b935dd2daa57e63ee',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100141 'apps': [
142 App({
143 'id': 'org.wikipedia',
144 'flavor': 'prod',
145 'signed_apk_name': 'app-prod-universal-release.apk'
146 })
147 ]
148 }),
149 Repo({
150 'name': 'chanu',
151 'url': 'https://github.com/mkj-gram/chanu.git',
Morten Krogh-Jespersen42fa5cf2019-03-19 14:06:12 +0100152 'revision': '6e53458f167b6d78398da60c20fd0da01a232617',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100153 'apps': [
154 App({
155 'id': 'com.chanapps.four.activity'
156 })
157 ]
158 }),
159 Repo({
160 'name': 'friendlyeats-android',
161 'url': 'https://github.com/christofferqa/friendlyeats-android.git',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100162 'revision': '10091fa0ec37da12e66286559ad1b6098976b07b',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100163 'apps': [
164 App({
165 'id': 'com.google.firebase.example.fireeats'
166 })
167 ]
168 }),
169 Repo({
170 'name': 'Instabug-Android',
171 'url': 'https://github.com/christofferqa/Instabug-Android.git',
172 'revision': 'b8df78c96630a6537fbc07787b4990afc030cc0f',
173 'apps': [
174 App({
175 'id': 'com.example.instabug'
176 })
177 ]
178 }),
179 Repo({
180 'name': 'KISS',
181 'url': 'https://github.com/christofferqa/KISS',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100182 'revision': '093da9ee0512e67192f62951c45a07a616fc3224',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100183 'apps': [
184 App({
185 'id': 'fr.neamar.kiss'
186 })
187 ]
188 }),
189 Repo({
190 'name': 'materialistic',
191 'url': 'https://github.com/christofferqa/materialistic',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100192 'revision': '2b2b2ee25ce9e672d5aab1dc90a354af1522b1d9',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100193 'apps': [
194 App({
195 'id': 'io.github.hidroh.materialistic'
196 })
197 ]
198 }),
199 Repo({
200 'name': 'Minimal-Todo',
201 'url': 'https://github.com/christofferqa/Minimal-Todo',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100202 'revision': '9d8c73746762cd376b718858ec1e8783ca07ba7c',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100203 'apps': [
204 App({
205 'id': 'com.avjindersinghsekhon.minimaltodo'
206 })
207 ]
208 }),
209 Repo({
Christoffer Quist Adamsen493acd82019-03-14 12:20:02 +0100210 'name': 'muzei',
211 'url': 'https://github.com/sgjesse/muzei.git',
212 'revision': 'e7e4ab1039bb18009254b94831b17676bc3b8bc1',
213 'apps': [
214 App({
215 'id': 'net.nurik.roman.muzei',
216 'module': 'main',
217 'archives_base_name': 'muzei',
Morten Krogh-Jespersen137e7322019-04-06 09:47:13 +0200218 'compile_sdk': 28,
Christoffer Quist Adamsen493acd82019-03-14 12:20:02 +0100219 })
220 ]
221 }),
222 Repo({
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100223 'name': 'NewPipe',
224 'url': 'https://github.com/christofferqa/NewPipe',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100225 'revision': 'ed543099c7823be00f15d9340f94bdb7cb37d1e6',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100226 'apps': [
227 App({
228 'id': 'org.schabi.newpipe'
229 })
230 ]
231 }),
232 Repo({
233 'name': 'rover-android',
234 'url': 'https://github.com/mkj-gram/rover-android.git',
Morten Krogh-Jespersen81bf5ab2019-03-11 13:33:16 +0100235 'revision': 'a5e155a1ed7d19b1cecd9a7b075e2852623a06bf',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100236 'apps': [
237 App({
238 'id': 'io.rover.app.debug',
239 'module': 'debug-app'
240 })
241 ]
242 }),
243 Repo({
244 'name': 'Signal-Android',
245 'url': 'https://github.com/mkj-gram/Signal-Android.git',
Morten Krogh-Jespersen81bf5ab2019-03-11 13:33:16 +0100246 'revision': 'cd542cab9bf860e71504ecb1caaf0a8476ba3989',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100247 'apps': [
248 App({
249 'id': 'org.thoughtcrime.securesms',
250 'module': '',
251 'flavor': 'play',
252 'main_dex_rules': 'multidex-config.pro',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100253 'signed_apk_name': 'Signal-play-release-4.32.7.apk'
254 })
255 ]
256 }),
257 Repo({
258 'name': 'Simple-Calendar',
259 'url': 'https://github.com/christofferqa/Simple-Calendar',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100260 'revision': '82dad8c203eea5a0f0ddb513506d8f1de986ef2b',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100261 'apps': [
262 App({
263 'id': 'com.simplemobiletools.calendar.pro',
264 'signed_apk_name': 'calendar-release.apk'
265 })
266 ]
267 }),
268 Repo({
269 'name': 'sqldelight',
270 'url': 'https://github.com/christofferqa/sqldelight.git',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100271 'revision': '2e67a1126b6df05e4119d1e3a432fde51d76cdc8',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100272 'apps': [
273 App({
274 'id': 'com.example.sqldelight.hockey',
275 'module': 'sample/android',
276 'archives_base_name': 'android',
277 'min_sdk': 14,
278 'compile_sdk': 28
279 })
280 ]
281 }),
282 Repo({
283 'name': 'tachiyomi',
284 'url': 'https://github.com/sgjesse/tachiyomi.git',
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100285 'revision': 'b15d2fe16864645055af6a745a62cc5566629798',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100286 'apps': [
287 App({
288 'id': 'eu.kanade.tachiyomi',
Morten Krogh-Jespersen7889f252019-03-19 10:55:23 +0100289 'flavor': 'dev',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100290 'min_sdk': 16
291 })
292 ]
293 }),
294 Repo({
295 'name': 'tivi',
296 'url': 'https://github.com/sgjesse/tivi.git',
Søren Gjessedbe6ebc2019-02-14 09:38:47 +0100297 'revision': '25c52e3593e7c98da4e537b49b29f6f67f88754d',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100298 'apps': [
299 App({
300 'id': 'app.tivi',
301 'min_sdk': 23,
Morten Krogh-Jespersen137e7322019-04-06 09:47:13 +0200302 'compile_sdk': 28,
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100303 })
304 ]
305 }),
306 Repo({
307 'name': 'Tusky',
308 'url': 'https://github.com/mkj-gram/Tusky.git',
Morten Krogh-Jespersen81bf5ab2019-03-11 13:33:16 +0100309 'revision': 'e7fbd190fb53bf9fde72253b816920cb6fe34518',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100310 'apps': [
311 App({
312 'id': 'com.keylesspalace.tusky',
313 'flavor': 'blue'
314 })
315 ]
316 }),
317 Repo({
318 'name': 'Vungle-Android-SDK',
319 'url': 'https://github.com/mkj-gram/Vungle-Android-SDK.git',
Morten Krogh-Jespersen81bf5ab2019-03-11 13:33:16 +0100320 'revision': '138d3f18c027b61b195c98911f1c5ab7d87ad18b',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100321 'apps': [
322 App({
323 'id': 'com.publisher.vungle.sample'
324 })
325 ]
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100326 })
327]
328
329def GetAllApps():
330 apps = []
331 for repo in APP_REPOSITORIES:
332 for app in repo.apps:
333 apps.append((app, repo))
334 return apps
335
336def GetAllAppNames():
337 return [app.name for (app, repo) in GetAllApps()]
338
339def GetAppWithName(query):
340 for (app, repo) in GetAllApps():
341 if app.name == query:
342 return (app, repo)
343 assert False
Søren Gjessecdae8792018-12-12 09:02:43 +0100344
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +0100345def ComputeSizeOfDexFilesInApk(apk):
346 dex_size = 0
347 z = zipfile.ZipFile(apk, 'r')
348 for filename in z.namelist():
349 if filename.endswith('.dex'):
350 dex_size += z.getinfo(filename).file_size
351 return dex_size
352
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +0100353def ExtractMarker(apk, temp_dir, options):
Christoffer Quist Adamsenbbe5d7d2019-01-23 13:15:21 +0100354 r8_jar = os.path.join(temp_dir, 'r8.jar')
Christoffer Quist Adamsenbe2593f2019-02-19 13:01:56 +0100355 r8lib_jar = os.path.join(temp_dir, 'r8lib.jar')
Christoffer Quist Adamsenbbe5d7d2019-01-23 13:15:21 +0100356
Christoffer Quist Adamsenbe2593f2019-02-19 13:01:56 +0100357 # Use the copy of r8.jar or r8lib.jar if one is there.
Christoffer Quist Adamsenbbe5d7d2019-01-23 13:15:21 +0100358 if os.path.isfile(r8_jar):
Ian Zerny3f54e222019-02-12 10:51:17 +0100359 cmd = [jdk.GetJavaExecutable(), '-ea', '-jar', r8_jar, 'extractmarker', apk]
Christoffer Quist Adamsenbe2593f2019-02-19 13:01:56 +0100360 elif os.path.isfile(r8lib_jar):
361 cmd = [jdk.GetJavaExecutable(), '-ea', '-cp', r8lib_jar,
Morten Krogh-Jespersen220e5702019-02-27 12:57:01 +0100362 'com.android.tools.r8.ExtractMarker', apk]
Christoffer Quist Adamsenbbe5d7d2019-01-23 13:15:21 +0100363 else:
364 script = os.path.join(utils.TOOLS_DIR, 'extractmarker.py')
365 cmd = ['python', script, apk]
366
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100367 utils.PrintCmd(cmd, quiet=options.quiet)
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +0100368 stdout = subprocess.check_output(cmd)
369
370 # Return the last line.
371 lines = stdout.strip().splitlines()
372 assert len(lines) >= 1
373 return lines[-1]
374
Morten Krogh-Jespersen0de13732019-03-01 08:56:39 +0100375def CheckIsBuiltWithExpectedR8(apk, temp_dir, shrinker, options):
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +0100376 marker = ExtractMarker(apk, temp_dir, options)
377 expected_version = (
378 options.version
379 if options.version
Morten Krogh-Jespersen0de13732019-03-01 08:56:39 +0100380 else utils.getR8Version(
381 os.path.join(
382 temp_dir,
383 'r8lib.jar' if IsMinifiedR8(shrinker) else 'r8.jar')))
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +0100384 if marker.startswith('~~R8'):
385 actual_version = json.loads(marker[4:]).get('version')
386 if actual_version == expected_version:
387 return True
388 raise Exception(
389 'Expected APK to be built with R8 version {} (was: {})'.format(
390 expected_version, marker))
Christoffer Quist Adamsen10b7db82018-12-13 14:50:38 +0100391
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +0200392def IsR8(shrinker):
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100393 return 'r8' in shrinker
394
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +0200395def IsR8FullMode(shrinker):
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100396 return shrinker == 'r8-full' or shrinker == 'r8-nolib-full'
397
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +0200398def IsLoggingEnabledFor(app, options):
399 if options.no_logging:
400 return False
401 if options.app_logging_filter and app.name not in options.app_logging_filter:
402 return False
403 return True
404
Morten Krogh-Jespersend45d95e2019-02-07 13:27:17 +0100405def IsMinifiedR8(shrinker):
406 return 'nolib' not in shrinker
407
Christoffer Quist Adamsen10b7db82018-12-13 14:50:38 +0100408def IsTrackedByGit(file):
409 return subprocess.check_output(['git', 'ls-files', file]).strip() != ''
410
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100411def GitClone(repo, checkout_dir, quiet):
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100412 result = subprocess.check_output(
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100413 ['git', 'clone', repo.url, checkout_dir]).strip()
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100414 head_rev = utils.get_HEAD_sha1_for_checkout(checkout_dir)
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100415 if repo.revision == head_rev:
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100416 return result
417 warn('Target revision is not head in {}.'.format(checkout_dir))
Morten Krogh-Jespersen54090862019-02-19 11:31:10 +0100418 with utils.ChangedWorkingDirectory(checkout_dir, quiet=quiet):
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100419 subprocess.check_output(['git', 'reset', '--hard', repo.revision])
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100420 return result
Søren Gjessecdae8792018-12-12 09:02:43 +0100421
422def GitCheckout(file):
423 return subprocess.check_output(['git', 'checkout', file]).strip()
424
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100425def InstallApkOnEmulator(apk_dest, options):
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100426 cmd = ['adb', '-s', options.emulator_id, 'install', '-r', '-d', apk_dest]
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100427 if options.quiet:
428 with open(os.devnull, 'w') as devnull:
429 subprocess.check_call(cmd, stdout=devnull)
430 else:
431 subprocess.check_call(cmd)
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100432
Christoffer Quist Adamsen81321b62019-01-15 14:39:53 +0100433def PercentageDiffAsString(before, after):
434 if after < before:
435 return '-' + str(round((1.0 - after / before) * 100)) + '%'
436 else:
437 return '+' + str(round((after - before) / before * 100)) + '%'
438
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100439def UninstallApkOnEmulator(app, options):
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100440 process = subprocess.Popen(
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100441 ['adb', '-s', options.emulator_id, 'uninstall', app.id],
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100442 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
443 stdout, stderr = process.communicate()
444
445 if stdout.strip() == 'Success':
446 # Successfully uninstalled
447 return
448
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100449 if 'Unknown package: {}'.format(app.id) in stderr:
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100450 # Application not installed
451 return
452
453 raise Exception(
454 'Unexpected result from `adb uninstall {}\nStdout: {}\nStderr: {}'.format(
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100455 app.id, stdout, stderr))
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100456
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100457def WaitForEmulator(options):
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100458 stdout = subprocess.check_output(['adb', 'devices'])
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100459 if '{}\tdevice'.format(options.emulator_id) in stdout:
Christoffer Quist Adamsenbae36612019-01-15 12:23:51 +0100460 return True
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100461
462 print('Emulator \'{}\' not connected; waiting for connection'.format(
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100463 options.emulator_id))
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100464
465 time_waited = 0
466 while True:
467 time.sleep(10)
468 time_waited += 10
469 stdout = subprocess.check_output(['adb', 'devices'])
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100470 if '{}\tdevice'.format(options.emulator_id) not in stdout:
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100471 print('... still waiting for connection')
472 if time_waited >= 5 * 60:
Christoffer Quist Adamsenbae36612019-01-15 12:23:51 +0100473 return False
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100474 else:
Christoffer Quist Adamsenbae36612019-01-15 12:23:51 +0100475 return True
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100476
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100477def GetResultsForApp(app, repo, options, temp_dir):
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100478 # Checkout and build in the build directory.
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100479 repo_name = repo.name
480 repo_checkout_dir = os.path.join(WORKING_DIR, repo_name)
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100481
Christoffer Quist Adamsen724bfb02019-01-10 09:54:38 +0100482 result = {}
483
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100484 if not os.path.exists(repo_checkout_dir) and not options.golem:
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100485 with utils.ChangedWorkingDirectory(WORKING_DIR, quiet=options.quiet):
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100486 GitClone(repo, repo_checkout_dir, options.quiet)
Christoffer Quist Adamsen860fa932019-01-10 14:27:39 +0100487
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100488 checkout_rev = utils.get_HEAD_sha1_for_checkout(repo_checkout_dir)
489 if repo.revision != checkout_rev:
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100490 msg = 'Checkout is not target revision for {} in {}.'.format(
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100491 app.name, repo_checkout_dir)
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +0100492 if options.ignore_versions:
493 warn(msg)
494 else:
495 raise Exception(msg)
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100496
Christoffer Quist Adamsen724bfb02019-01-10 09:54:38 +0100497 result['status'] = 'success'
498
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100499 app_checkout_dir = os.path.join(repo_checkout_dir, app.dir)
Christoffer Quist Adamsen724bfb02019-01-10 09:54:38 +0100500 result_per_shrinker = BuildAppWithSelectedShrinkers(
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100501 app, repo, options, app_checkout_dir, temp_dir)
Christoffer Quist Adamsen724bfb02019-01-10 09:54:38 +0100502 for shrinker, shrinker_result in result_per_shrinker.iteritems():
503 result[shrinker] = shrinker_result
504
505 return result
506
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100507def BuildAppWithSelectedShrinkers(
508 app, repo, options, checkout_dir, temp_dir):
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100509 result_per_shrinker = {}
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +0100510
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100511 with utils.ChangedWorkingDirectory(checkout_dir, quiet=options.quiet):
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +0100512 for shrinker in options.shrinker:
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100513 apk_dest = None
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100514
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100515 result = {}
516 try:
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100517 out_dir = os.path.join(checkout_dir, 'out', shrinker)
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100518 (apk_dest, profile_dest_dir, proguard_config_file) = \
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100519 BuildAppWithShrinker(
520 app, repo, shrinker, checkout_dir, out_dir, temp_dir,
521 options)
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100522 dex_size = ComputeSizeOfDexFilesInApk(apk_dest)
Morten Krogh-Jespersend35065e2019-02-07 10:28:52 +0100523 result['apk_dest'] = apk_dest
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100524 result['build_status'] = 'success'
525 result['dex_size'] = dex_size
Christoffer Quist Adamsen1d87ca62019-01-11 12:22:26 +0100526 result['profile_dest_dir'] = profile_dest_dir
Christoffer Quist Adamsen81321b62019-01-15 14:39:53 +0100527
528 profile = as_utils.ParseProfileReport(profile_dest_dir)
529 result['profile'] = {
530 task_name:duration for task_name, duration in profile.iteritems()
531 if as_utils.IsGradleCompilerTask(task_name, shrinker)}
Christoffer Quist Adamsen1d87ca62019-01-11 12:22:26 +0100532 except Exception as e:
Christoffer Quist Adamsen493acd82019-03-14 12:20:02 +0100533 warn('Failed to build {} with {}'.format(app.name, shrinker))
Christoffer Quist Adamsen1d87ca62019-01-11 12:22:26 +0100534 if e:
535 print('Error: ' + str(e))
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100536 result['build_status'] = 'failed'
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +0100537
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100538 if result.get('build_status') == 'success':
539 if options.monkey:
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100540 result['monkey_status'] = 'success' if RunMonkey(
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100541 app, options, apk_dest) else 'failed'
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100542
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100543 if 'r8' in shrinker and options.r8_compilation_steps > 1:
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100544 result['recompilation_results'] = \
545 ComputeRecompilationResults(
546 app, repo, options, checkout_dir, temp_dir, shrinker,
547 proguard_config_file)
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100548
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100549 if options.run_tests and app.has_instrumentation_tests:
550 result['instrumentation_test_results'] = \
551 ComputeInstrumentationTestResults(
552 app, options, checkout_dir, out_dir, shrinker)
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100553
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100554 result_per_shrinker[shrinker] = result
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100555
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100556 if len(options.apps) > 1:
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100557 print('')
558 LogResultsForApp(app, result_per_shrinker, options)
559 print('')
560
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100561 return result_per_shrinker
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +0100562
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100563def BuildAppWithShrinker(
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100564 app, repo, shrinker, checkout_dir, out_dir, temp_dir, options,
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100565 keepRuleSynthesisForRecompilation=False):
566 print('Building {} with {}{}'.format(
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100567 app.name,
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100568 shrinker,
569 ' for recompilation' if keepRuleSynthesisForRecompilation else ''))
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100570
Morten Krogh-Jespersende566ea2019-02-18 11:59:48 +0100571 # Add settings.gradle file if it is not present to prevent gradle from finding
572 # the settings.gradle file in the r8 root when apps are placed under
573 # $R8/build.
Christoffer Quist Adamsen082351d2019-03-08 13:07:50 +0100574 as_utils.add_settings_gradle(checkout_dir, app.name)
Morten Krogh-Jespersende566ea2019-02-18 11:59:48 +0100575
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +0100576 # Add 'r8.jar' to top-level build.gradle.
Morten Krogh-Jespersend45d95e2019-02-07 13:27:17 +0100577 as_utils.add_r8_dependency(checkout_dir, temp_dir, IsMinifiedR8(shrinker))
Christoffer Quist Adamsenf8ad4792019-01-09 13:19:19 +0100578
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100579 archives_base_name = app.archives_base_name
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100580
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100581 if not os.path.exists(out_dir):
582 os.makedirs(out_dir)
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100583
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100584 # Set -printconfiguration in Proguard rules.
585 proguard_config_dest = os.path.abspath(
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100586 os.path.join(out_dir, 'proguard-rules.pro'))
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100587 as_utils.SetPrintConfigurationDirective(
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100588 app, checkout_dir, proguard_config_dest)
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100589
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100590 env_vars = {}
591 env_vars['ANDROID_HOME'] = utils.getAndroidHome()
Morten Krogh-Jespersen0ca5d9e2019-03-18 10:59:47 +0100592 if not options.disable_assertions:
593 env_vars['JAVA_OPTS'] = '-ea:com.android.tools.r8...'
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100594
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100595 releaseTarget = app.releaseTarget
Søren Gjesse8c111482018-12-21 14:47:57 +0100596 if not releaseTarget:
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100597 releaseTarget = app.module.replace('/', ':') + ':' + 'assemble' + (
598 app.flavor.capitalize() if app.flavor else '') + 'Release'
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100599
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100600 # Build using gradle.
601 args = [releaseTarget,
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +0200602 '-Pandroid.enableR8=' + str(IsR8(shrinker)).lower(),
603 '-Pandroid.enableR8.fullMode=' + str(IsR8FullMode(shrinker)).lower()]
Morten Krogh-Jespersen36d95f52019-03-21 15:56:46 +0100604
605 # Warm up gradle if pre_runs > 0. For posterity we generate the same sequence
606 # as the benchmarking at https://github.com/madsager/santa-tracker-android.
607 for i in range(0, options.gradle_pre_runs):
608 if i == 0:
609 utils.RunGradlew(
610 ["--stop"],
611 env_vars=env_vars,
612 quiet=options.quiet,
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +0200613 logging=IsLoggingEnabledFor(app, options),
Morten Krogh-Jespersen36d95f52019-03-21 15:56:46 +0100614 use_daemon=options.use_daemon)
615 utils.RunGradlew(
616 args,
617 env_vars=env_vars,
618 quiet=options.quiet,
619 clean=i > 0,
620 use_daemon=options.use_daemon,
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +0200621 logging=IsLoggingEnabledFor(app, options))
Morten Krogh-Jespersen36d95f52019-03-21 15:56:46 +0100622
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100623 if keepRuleSynthesisForRecompilation:
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100624 args.append('-Dcom.android.tools.r8.keepRuleSynthesisForRecompilation=true')
Søren Gjesse9fb48802019-01-18 11:00:00 +0100625 if options.gradle_flags:
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100626 args.extend(options.gradle_flags.split(' '))
Christoffer Quist Adamsen1d87ca62019-01-11 12:22:26 +0100627
Morten Krogh-Jespersen36d95f52019-03-21 15:56:46 +0100628 args.append('--profile')
629
630 stdout = utils.RunGradlew(
631 args,
632 env_vars=env_vars,
633 quiet=options.quiet,
634 use_daemon=options.use_daemon,
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +0200635 logging=IsLoggingEnabledFor(app, options))
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100636
637 apk_base_name = (archives_base_name
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100638 + (('-' + app.flavor) if app.flavor else '') + '-release')
639 signed_apk_name = (
640 app.signed_apk_name
641 if app.signed_apk_name
642 else apk_base_name + '.apk')
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100643 unsigned_apk_name = apk_base_name + '-unsigned.apk'
644
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100645 build_dir = app.build_dir
646 build_output_apks = os.path.join(app.module, build_dir, 'outputs', 'apk')
647 if app.flavor:
648 build_output_apks = os.path.join(build_output_apks, app.flavor, 'release')
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100649 else:
650 build_output_apks = os.path.join(build_output_apks, 'release')
651
652 signed_apk = os.path.join(build_output_apks, signed_apk_name)
653 unsigned_apk = os.path.join(build_output_apks, unsigned_apk_name)
654
655 if options.sign_apks and not os.path.isfile(signed_apk):
656 assert os.path.isfile(unsigned_apk)
657 if options.sign_apks:
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100658 apk_utils.sign_with_apksigner(
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100659 unsigned_apk,
660 signed_apk,
Christoffer Quist Adamsen3b6f1062019-02-07 09:49:43 +0100661 options.keystore,
662 options.keystore_password,
663 quiet=options.quiet)
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100664
665 if os.path.isfile(signed_apk):
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100666 apk_dest = os.path.join(out_dir, signed_apk_name)
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100667 as_utils.MoveFile(signed_apk, apk_dest, quiet=options.quiet)
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100668 else:
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100669 apk_dest = os.path.join(out_dir, unsigned_apk_name)
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100670 as_utils.MoveFile(unsigned_apk, apk_dest, quiet=options.quiet)
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100671
Morten Krogh-Jespersen8a3450d2019-03-07 07:29:14 +0000672 assert ('r8' not in shrinker
673 or CheckIsBuiltWithExpectedR8(apk_dest, temp_dir, shrinker, options))
Christoffer Quist Adamsen3aa8d252018-12-13 14:56:40 +0100674
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100675 profile_dest_dir = os.path.join(out_dir, 'profile')
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100676 as_utils.MoveProfileReportTo(profile_dest_dir, stdout, quiet=options.quiet)
Christoffer Quist Adamsen1d87ca62019-01-11 12:22:26 +0100677
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100678 return (apk_dest, profile_dest_dir, proguard_config_dest)
679
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100680def ComputeInstrumentationTestResults(
681 app, options, checkout_dir, out_dir, shrinker):
682 args = ['connectedAndroidTest',
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +0200683 '-Pandroid.enableR8=' + str(IsR8(shrinker)).lower(),
684 '-Pandroid.enableR8.fullMode=' + str(IsR8FullMode(shrinker)).lower()]
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100685 env_vars = { 'ANDROID_SERIAL': options.emulator_id }
Morten Krogh-Jespersen36d95f52019-03-21 15:56:46 +0100686 stdout = utils.RunGradlew(
687 args,
688 env_vars=env_vars,
689 quiet=options.quiet,
690 fail=False,
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +0200691 logging=IsLoggingEnabledFor(app, options),
Morten Krogh-Jespersen36d95f52019-03-21 15:56:46 +0100692 use_daemon=options.use_daemon)
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100693
694 xml_test_result_dest = os.path.join(out_dir, 'test_result')
695 as_utils.MoveXMLTestResultFileTo(
696 xml_test_result_dest, stdout, quiet=options.quiet)
697
698 with open(xml_test_result_dest, 'r') as f:
699 xml_test_result_contents = f.read()
700
701 xml_document = minidom.parseString(xml_test_result_contents)
702 testsuite_element = xml_document.documentElement
703
704 return {
705 'xml_test_result_dest': xml_test_result_dest,
706 'tests': int(testsuite_element.getAttribute('tests')),
707 'failures': int(testsuite_element.getAttribute('failures')),
708 'errors': int(testsuite_element.getAttribute('errors')),
709 'skipped': int(testsuite_element.getAttribute('skipped'))
710 }
711
712def ComputeRecompilationResults(
713 app, repo, options, checkout_dir, temp_dir, shrinker, proguard_config_file):
714 recompilation_results = []
715
Morten Krogh-Jespersenb5d45cd2019-03-19 13:58:30 +0100716 # Build app with gradle using -D...keepRuleSynthesisForRecompilation=true.
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100717 out_dir = os.path.join(checkout_dir, 'out', shrinker + '-1')
718 (apk_dest, profile_dest_dir, ext_proguard_config_file) = \
719 BuildAppWithShrinker(
720 app, repo, shrinker, checkout_dir, out_dir,
721 temp_dir, options, keepRuleSynthesisForRecompilation=True)
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100722 recompilation_result = {
723 'apk_dest': apk_dest,
724 'build_status': 'success',
725 'dex_size': ComputeSizeOfDexFilesInApk(apk_dest),
726 'monkey_status': 'skipped'
727 }
728 recompilation_results.append(recompilation_result)
729
730 # Sanity check that keep rules have changed.
731 with open(ext_proguard_config_file) as new:
732 with open(proguard_config_file) as old:
733 assert(
734 sum(1 for line in new
735 if line.strip() and '-printconfiguration' not in line)
736 >
737 sum(1 for line in old
738 if line.strip() and '-printconfiguration' not in line))
739
740 # Extract min-sdk and target-sdk
741 (min_sdk, compile_sdk) = \
742 as_utils.GetMinAndCompileSdk(app, checkout_dir, apk_dest)
743
744 # Now rebuild generated apk.
745 previous_apk = apk_dest
746
747 # We may need main dex rules when re-compiling with R8 as standalone.
748 main_dex_rules = None
749 if app.main_dex_rules:
750 main_dex_rules = os.path.join(checkout_dir, app.main_dex_rules)
751
752 for i in range(1, options.r8_compilation_steps):
753 try:
754 recompiled_apk_dest = os.path.join(
755 checkout_dir, 'out', shrinker, 'app-release-{}.apk'.format(i))
756 RebuildAppWithShrinker(
757 app, previous_apk, recompiled_apk_dest,
758 ext_proguard_config_file, shrinker, min_sdk, compile_sdk,
759 options, temp_dir, main_dex_rules)
760 recompilation_result = {
761 'apk_dest': recompiled_apk_dest,
762 'build_status': 'success',
763 'dex_size': ComputeSizeOfDexFilesInApk(recompiled_apk_dest)
764 }
765 if options.monkey:
766 recompilation_result['monkey_status'] = 'success' if RunMonkey(
767 app, options, recompiled_apk_dest) else 'failed'
768 recompilation_results.append(recompilation_result)
769 previous_apk = recompiled_apk_dest
770 except Exception as e:
771 warn('Failed to recompile {} with {}'.format(
772 app.name, shrinker))
773 recompilation_results.append({ 'build_status': 'failed' })
774 break
775 return recompilation_results
776
Christoffer Quist Adamsene59840b2019-01-22 15:25:15 +0100777def RebuildAppWithShrinker(
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100778 app, apk, apk_dest, proguard_config_file, shrinker, min_sdk, compile_sdk,
Morten Krogh-Jespersen03627262019-02-18 14:30:22 +0100779 options, temp_dir, main_dex_rules):
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100780 assert 'r8' in shrinker
781 assert apk_dest.endswith('.apk')
Søren Gjesse9fb48802019-01-18 11:00:00 +0100782
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100783 print('Rebuilding {} with {}'.format(app.name, shrinker))
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100784
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100785 # Compile given APK with shrinker to temporary zip file.
Christoffer Quist Adamsen17879c12019-01-22 16:13:54 +0100786 android_jar = utils.get_android_jar(compile_sdk)
Morten Krogh-Jespersend45d95e2019-02-07 13:27:17 +0100787 r8_jar = os.path.join(
788 temp_dir, 'r8lib.jar' if IsMinifiedR8(shrinker) else 'r8.jar')
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100789 zip_dest = apk_dest[:-4] + '.zip'
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100790
Christoffer Quist Adamsene59840b2019-01-22 15:25:15 +0100791 # TODO(christofferqa): Entry point should be CompatProguard if the shrinker
792 # is 'r8'.
793 entry_point = 'com.android.tools.r8.R8'
794
Morten Krogh-Jespersen0ca5d9e2019-03-18 10:59:47 +0100795 cmd = ([jdk.GetJavaExecutable()] +
796 (['-ea:com.android.tools.r8...']
797 if not options.disable_assertions
798 else []) +
799 ['-cp', r8_jar, entry_point,
800 '--release', '--min-api', str(min_sdk),
801 '--pg-conf', proguard_config_file,
802 '--lib', android_jar,
803 '--output', zip_dest,
804 apk])
Christoffer Quist Adamsen17879c12019-01-22 16:13:54 +0100805
806 for android_optional_jar in utils.get_android_optional_jars(compile_sdk):
807 cmd.append('--lib')
808 cmd.append(android_optional_jar)
809
Morten Krogh-Jespersen03627262019-02-18 14:30:22 +0100810 if main_dex_rules:
811 cmd.append('--main-dex-rules')
812 cmd.append(main_dex_rules)
813
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100814 utils.RunCmd(cmd, quiet=options.quiet)
Christoffer Quist Adamsen4038bbe2019-01-15 14:31:46 +0100815
816 # Make a copy of the given APK, move the newly generated dex files into the
817 # copied APK, and then sign the APK.
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100818 apk_masseur.masseur(
Christoffer Quist Adamsen2dcea102019-02-20 11:31:38 +0100819 apk, dex=zip_dest, resources='META-INF/services/*', out=apk_dest,
Rico Wind7517c3f2019-04-03 16:18:50 +0200820 quiet=options.quiet, keystore=options.keystore)
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +0100821
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100822def RunMonkey(app, options, apk_dest):
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100823 if not WaitForEmulator(options):
Christoffer Quist Adamsenbae36612019-01-15 12:23:51 +0100824 return False
825
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100826 UninstallApkOnEmulator(app, options)
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100827 InstallApkOnEmulator(apk_dest, options)
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100828
Christoffer Quist Adamsenbae36612019-01-15 12:23:51 +0100829 number_of_events_to_generate = options.monkey_events
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100830
Christoffer Quist Adamsenbae36612019-01-15 12:23:51 +0100831 # Intentionally using a constant seed such that the monkey generates the same
832 # event sequence for each shrinker.
833 random_seed = 42
834
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100835 cmd = ['adb', '-s', options.emulator_id, 'shell', 'monkey', '-p', app.id,
836 '-s', str(random_seed), str(number_of_events_to_generate)]
Christoffer Quist Adamsen88b7ebe2019-01-14 11:22:17 +0100837
838 try:
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100839 stdout = utils.RunCmd(cmd, quiet=options.quiet)
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100840 succeeded = (
841 'Events injected: {}'.format(number_of_events_to_generate) in stdout)
Christoffer Quist Adamsen88b7ebe2019-01-14 11:22:17 +0100842 except subprocess.CalledProcessError as e:
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100843 succeeded = False
Christoffer Quist Adamsen88b7ebe2019-01-14 11:22:17 +0100844
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100845 UninstallApkOnEmulator(app, options)
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +0100846
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100847 return succeeded
848
849def LogResultsForApps(result_per_shrinker_per_app, options):
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100850 print('')
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200851 app_errors = 0
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100852 for (app, result_per_shrinker) in result_per_shrinker_per_app:
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200853 app_errors += (1 if LogResultsForApp(app, result_per_shrinker, options)
854 else 0)
855 return app_errors
Christoffer Quist Adamsen724bfb02019-01-10 09:54:38 +0100856
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100857def LogResultsForApp(app, result_per_shrinker, options):
Morten Krogh-Jespersenaeb665e2019-02-07 12:29:03 +0100858 if options.print_dexsegments:
859 LogSegmentsForApp(app, result_per_shrinker, options)
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200860 return False
Morten Krogh-Jespersenaeb665e2019-02-07 12:29:03 +0100861 else:
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200862 return LogComparisonResultsForApp(app, result_per_shrinker, options)
Morten Krogh-Jespersenaeb665e2019-02-07 12:29:03 +0100863
864def LogSegmentsForApp(app, result_per_shrinker, options):
865 for shrinker in SHRINKERS:
866 if shrinker not in result_per_shrinker:
867 continue
868 result = result_per_shrinker[shrinker];
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100869 benchmark_name = '{}-{}'.format(options.print_dexsegments, app.name)
Morten Krogh-Jespersenaeb665e2019-02-07 12:29:03 +0100870 utils.print_dexsegments(benchmark_name, [result.get('apk_dest')])
871 duration = sum(result.get('profile').values())
872 print('%s-Total(RunTimeRaw): %s ms' % (benchmark_name, duration * 1000))
873 print('%s-Total(CodeSize): %s' % (benchmark_name, result.get('dex_size')))
874
875
876def LogComparisonResultsForApp(app, result_per_shrinker, options):
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100877 print(app.name + ':')
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200878 app_error = False
Christoffer Quist Adamsen1de3dde2019-01-24 13:17:46 +0100879 if result_per_shrinker.get('status', 'success') != 'success':
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100880 error_message = result_per_shrinker.get('error_message')
881 print(' skipped ({})'.format(error_message))
882 return
883
Morten Krogh-Jespersenaeb665e2019-02-07 12:29:03 +0100884 proguard_result = result_per_shrinker.get('pg', {})
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100885 proguard_dex_size = float(proguard_result.get('dex_size', -1))
886 proguard_duration = sum(proguard_result.get('profile', {}).values())
887
888 for shrinker in SHRINKERS:
889 if shrinker not in result_per_shrinker:
Christoffer Quist Adamsen724bfb02019-01-10 09:54:38 +0100890 continue
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100891 result = result_per_shrinker.get(shrinker)
892 build_status = result.get('build_status')
893 if build_status != 'success':
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200894 app_error = True
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100895 warn(' {}: {}'.format(shrinker, build_status))
896 else:
897 print(' {}:'.format(shrinker))
898 dex_size = result.get('dex_size')
899 msg = ' dex size: {}'.format(dex_size)
900 if dex_size != proguard_dex_size and proguard_dex_size >= 0:
901 msg = '{} ({}, {})'.format(
902 msg, dex_size - proguard_dex_size,
903 PercentageDiffAsString(proguard_dex_size, dex_size))
904 success(msg) if dex_size < proguard_dex_size else warn(msg)
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +0100905 else:
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100906 print(msg)
Christoffer Quist Adamsen81321b62019-01-15 14:39:53 +0100907
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100908 profile = result.get('profile')
909 duration = sum(profile.values())
910 msg = ' performance: {}s'.format(duration)
911 if duration != proguard_duration and proguard_duration > 0:
912 msg = '{} ({}s, {})'.format(
913 msg, duration - proguard_duration,
914 PercentageDiffAsString(proguard_duration, duration))
915 success(msg) if duration < proguard_duration else warn(msg)
916 else:
917 print(msg)
918 if len(profile) >= 2:
919 for task_name, task_duration in profile.iteritems():
920 print(' {}: {}s'.format(task_name, task_duration))
Christoffer Quist Adamsen81321b62019-01-15 14:39:53 +0100921
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100922 if options.monkey:
923 monkey_status = result.get('monkey_status')
924 if monkey_status != 'success':
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200925 app_error = True
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100926 warn(' monkey: {}'.format(monkey_status))
927 else:
928 success(' monkey: {}'.format(monkey_status))
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100929
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100930 recompilation_results = result.get('recompilation_results', [])
931 i = 0
932 for recompilation_result in recompilation_results:
933 build_status = recompilation_result.get('build_status')
934 if build_status != 'success':
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200935 app_error = True
Christoffer Quist Adamsend0e0a7c2019-01-22 10:00:30 +0100936 print(' recompilation #{}: {}'.format(i, build_status))
937 else:
938 dex_size = recompilation_result.get('dex_size')
939 print(' recompilation #{}'.format(i))
940 print(' dex size: {}'.format(dex_size))
941 if options.monkey:
942 monkey_status = recompilation_result.get('monkey_status')
943 msg = ' monkey: {}'.format(monkey_status)
944 if monkey_status == 'success':
945 success(msg)
946 elif monkey_status == 'skipped':
947 print(msg)
948 else:
949 warn(msg)
950 i += 1
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +0100951
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100952 if options.run_tests and 'instrumentation_test_results' in result:
953 instrumentation_test_results = \
954 result.get('instrumentation_test_results')
955 succeeded = (
956 instrumentation_test_results.get('failures')
957 + instrumentation_test_results.get('errors')
958 + instrumentation_test_results.get('skipped')) == 0
959 if succeeded:
960 success(' tests: succeeded')
961 else:
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200962 app_error = True
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100963 warn(
964 ' tests: failed (failures: {}, errors: {}, skipped: {})'
965 .format(
966 instrumentation_test_results.get('failures'),
967 instrumentation_test_results.get('errors'),
968 instrumentation_test_results.get('skipped')))
969
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +0200970 return app_error
971
Søren Gjessecdae8792018-12-12 09:02:43 +0100972def ParseOptions(argv):
973 result = optparse.OptionParser()
974 result.add_option('--app',
975 help='What app to run on',
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +0100976 choices=GetAllAppNames())
Morten Krogh-Jespersen0ca5d9e2019-03-18 10:59:47 +0100977 result.add_option('--disable-assertions',
978 help='Disable assertions when compiling',
979 default=False,
980 action='store_true')
Morten Krogh-Jespersen64740202019-02-07 13:06:04 +0100981 result.add_option('--download-only', '--download_only',
982 help='Whether to download apps without any compilation',
983 default=False,
984 action='store_true')
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +0100985 result.add_option('--emulator-id', '--emulator_id',
986 help='Id of the emulator to use',
987 default='emulator-5554')
Morten Krogh-Jespersen64740202019-02-07 13:06:04 +0100988 result.add_option('--golem',
989 help='Running on golem, do not download',
990 default=False,
991 action='store_true')
Rico Wind7517c3f2019-04-03 16:18:50 +0200992 result.add_option('--bot',
993 help='Running on bot, use third_party dependency.',
994 default=False,
995 action='store_true')
Morten Krogh-Jespersend35065e2019-02-07 10:28:52 +0100996 result.add_option('--gradle-flags', '--gradle_flags',
997 help='Flags to pass in to gradle')
Morten Krogh-Jespersen36d95f52019-03-21 15:56:46 +0100998 result.add_option('--gradle-pre-runs', '--gradle_pre_runs',
999 help='Do rounds of compilations to warm up gradle',
1000 default=0,
1001 type=int)
Morten Krogh-Jespersene0ce6a32019-02-07 11:44:45 +01001002 result.add_option('--ignore-versions', '--ignore_versions',
1003 help='Allow checked-out app to differ in revision from '
1004 'pinned',
1005 default=False,
1006 action='store_true')
Christoffer Quist Adamsen3b6f1062019-02-07 09:49:43 +01001007 result.add_option('--keystore',
1008 help='Path to app.keystore',
Rico Wind533e3ce2019-04-04 10:26:12 +02001009 default=os.path.join(utils.TOOLS_DIR, 'debug.keystore'))
Christoffer Quist Adamsen3b6f1062019-02-07 09:49:43 +01001010 result.add_option('--keystore-password', '--keystore_password',
1011 help='Password for app.keystore',
1012 default='android')
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +02001013 result.add_option('--app-logging-filter', '--app_logging_filter',
1014 help='The apps for which to turn on logging',
1015 action='append')
Christoffer Quist Adamsen1d0a0fe2018-12-21 14:28:56 +01001016 result.add_option('--monkey',
1017 help='Whether to install and run app(s) with monkey',
1018 default=False,
1019 action='store_true')
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +02001020 result.add_option('--monkey-events', '--monkey_events',
Christoffer Quist Adamsenbae36612019-01-15 12:23:51 +01001021 help='Number of events that the monkey should trigger',
1022 default=250,
1023 type=int)
Morten Krogh-Jespersend35065e2019-02-07 10:28:52 +01001024 result.add_option('--no-build', '--no_build',
1025 help='Run without building ToT first (only when using ToT)',
1026 default=False,
1027 action='store_true')
Morten Krogh-Jespersend35065e2019-02-07 10:28:52 +01001028 result.add_option('--quiet',
1029 help='Disable verbose logging',
1030 default=False,
1031 action='store_true')
Morten Krogh-Jespersen50f00a52019-04-05 10:27:22 +02001032 result.add_option('--no-logging', '--no_logging',
1033 help='Disable logging except for errors',
1034 default=False,
1035 action='store_true')
Morten Krogh-Jespersenaeb665e2019-02-07 12:29:03 +01001036 result.add_option('--print-dexsegments',
1037 metavar='BENCHMARKNAME',
1038 help='Print the sizes of individual dex segments as ' +
1039 '\'<BENCHMARKNAME>-<APP>-<segment>(CodeSize): '
1040 '<bytes>\'')
Morten Krogh-Jespersend35065e2019-02-07 10:28:52 +01001041 result.add_option('--r8-compilation-steps', '--r8_compilation_steps',
1042 help='Number of times R8 should be run on each app',
1043 default=2,
1044 type=int)
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +01001045 result.add_option('--run-tests', '--run_tests',
1046 help='Whether to run instrumentation tests',
1047 default=False,
1048 action='store_true')
Søren Gjesseeed839d2019-01-11 15:19:16 +01001049 result.add_option('--sign-apks', '--sign_apks',
Christoffer Quist Adamsen10b7db82018-12-13 14:50:38 +01001050 help='Whether the APKs should be signed',
1051 default=False,
1052 action='store_true')
1053 result.add_option('--shrinker',
Christoffer Quist Adamsen860fa932019-01-10 14:27:39 +01001054 help='The shrinkers to use (by default, all are run)',
1055 action='append')
Morten Krogh-Jespersen36d95f52019-03-21 15:56:46 +01001056 result.add_option('--use-daemon', '--use_daemon',
1057 help='Whether to use a gradle daemon',
1058 default=False,
1059 action='store_true')
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +01001060 result.add_option('--version',
1061 help='The version of R8 to use (e.g., 1.4.51)')
Christoffer Quist Adamsen860fa932019-01-10 14:27:39 +01001062 (options, args) = result.parse_args(argv)
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +01001063 if options.app:
1064 options.apps = [GetAppWithName(options.app)]
1065 del options.app
1066 else:
1067 options.apps = GetAllApps()
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +02001068 if options.app_logging_filter:
1069 for app_name in options.app_logging_filter:
1070 assert any(app.name == app_name for (app, repo) in options.apps)
Christoffer Quist Adamsen860fa932019-01-10 14:27:39 +01001071 if options.shrinker:
1072 for shrinker in options.shrinker:
1073 assert shrinker in SHRINKERS
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +01001074 else:
1075 options.shrinker = [shrinker for shrinker in SHRINKERS]
1076 if options.version:
1077 # No need to build R8 if a specific release version should be used.
1078 options.no_build = True
1079 if 'r8-nolib' in options.shrinker:
1080 warn('Skipping shrinker r8-nolib because a specific release version '
1081 + 'of r8 was specified')
1082 options.shrinker.remove('r8-nolib')
1083 if 'r8-nolib-full' in options.shrinker:
1084 warn('Skipping shrinker r8-nolib-full because a specific release version '
1085 + 'of r8 was specified')
1086 options.shrinker.remove('r8-nolib-full')
Christoffer Quist Adamsen860fa932019-01-10 14:27:39 +01001087 return (options, args)
Søren Gjessecdae8792018-12-12 09:02:43 +01001088
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +01001089def clone_repositories(quiet):
1090 # Clone repositories into WORKING_DIR.
Morten Krogh-Jespersen64740202019-02-07 13:06:04 +01001091 with utils.ChangedWorkingDirectory(WORKING_DIR):
Christoffer Quist Adamsene57b73c2019-03-07 15:13:03 +01001092 for repo in APP_REPOSITORIES:
1093 repo_dir = os.path.join(WORKING_DIR, repo.name)
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +01001094 if not os.path.exists(repo_dir):
1095 GitClone(repo, repo_dir, quiet)
Morten Krogh-Jespersen64740202019-02-07 13:06:04 +01001096
1097
Søren Gjessecdae8792018-12-12 09:02:43 +01001098def main(argv):
1099 (options, args) = ParseOptions(argv)
Christoffer Quist Adamsenf8ad4792019-01-09 13:19:19 +01001100
Rico Wind7517c3f2019-04-03 16:18:50 +02001101 if options.bot:
Rico Windbc7cab02019-04-04 12:22:09 +02001102 utils.DownloadFromGoogleCloudStorage(utils.OPENSOURCE_APPS_SHA_FILE)
1103 utils.DownloadFromGoogleCloudStorage(utils.ANDROID_SDK + '.tar.gz.sha1',
1104 bucket='r8-deps-internal',
1105 auth=True)
Rico Wind7517c3f2019-04-03 16:18:50 +02001106 if os.path.exists(WORKING_DIR):
1107 shutil.rmtree(WORKING_DIR)
1108 shutil.copytree(utils.OPENSOURCE_APPS_FOLDER, WORKING_DIR)
1109 os.environ[utils.ANDROID_HOME_ENVIROMENT_NAME] = os.path.join(
1110 utils.ANDROID_SDK)
1111 os.environ[utils.ANDROID_TOOLS_VERSION_ENVIRONMENT_NAME] = '28.0.3'
Christoffer Quist Adamsen64c28c42019-04-10 11:07:42 +02001112 # TODO(b/130051781): Logging temporarily enabled for muzei for debugging.
1113 options.app_logging_filter = ['muzei']
1114 options.no_logging = False
Morten Krogh-Jespersen137e7322019-04-06 09:47:13 +02001115 options.shrinker = [shrinker for shrinker in SHRINKERS if shrinker != 'pg']
1116 print(options.shrinker)
Rico Wind7517c3f2019-04-03 16:18:50 +02001117
Morten Krogh-Jespersen64740202019-02-07 13:06:04 +01001118 if options.golem:
Morten Krogh-Jespersenc2fbde22019-02-07 13:59:55 +01001119 golem.link_third_party()
Morten Krogh-Jespersen64740202019-02-07 13:06:04 +01001120 if os.path.exists(WORKING_DIR):
1121 shutil.rmtree(WORKING_DIR)
1122 shutil.copytree(utils.OPENSOURCE_APPS_FOLDER, WORKING_DIR)
Morten Krogh-Jespersen220e5702019-02-27 12:57:01 +01001123 os.environ[utils.ANDROID_HOME_ENVIROMENT_NAME] = os.path.join(
1124 utils.ANDROID_SDK)
1125 os.environ[utils.ANDROID_TOOLS_VERSION_ENVIRONMENT_NAME] = '28.0.3'
Morten Krogh-Jespersen475f6b42019-03-18 13:28:27 +01001126 options.disable_assertions = True
1127 options.ignore_versions = True
1128 options.no_build = True
1129 options.r8_compilation_steps = 1
1130 options.quiet = True
Morten Krogh-Jespersen36d95f52019-03-21 15:56:46 +01001131 options.gradle_pre_runs = 2
1132 options.use_daemon = True
Morten Krogh-Jespersen50f00a52019-04-05 10:27:22 +02001133 options.no_logging = True
Morten Krogh-Jespersen64740202019-02-07 13:06:04 +01001134
1135 if not os.path.exists(WORKING_DIR):
1136 os.makedirs(WORKING_DIR)
1137
1138 if options.download_only:
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +01001139 clone_repositories(options.quiet)
Morten Krogh-Jespersen64740202019-02-07 13:06:04 +01001140 return
1141
Christoffer Quist Adamsenbbe5d7d2019-01-23 13:15:21 +01001142 with utils.TempDir() as temp_dir:
Morten Krogh-Jespersen2bd5ec82019-02-25 16:01:23 +01001143 if not (options.no_build or options.golem):
Rico Wind1ce8b972019-04-04 14:22:42 +02001144 gradle.RunGradle(['r8', 'r8lib', '-Pno_internal'])
Søren Gjessecdae8792018-12-12 09:02:43 +01001145
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +01001146 if options.version:
1147 # Download r8-<version>.jar from
1148 # http://storage.googleapis.com/r8-releases/raw/.
1149 target = 'r8-{}.jar'.format(options.version)
1150 update_prebuilds_in_android.download_version(
1151 temp_dir, 'com/android/tools/r8/' + options.version, target)
1152 as_utils.MoveFile(
1153 os.path.join(temp_dir, target), os.path.join(temp_dir, 'r8lib.jar'),
1154 quiet=options.quiet)
1155 else:
1156 # Make a copy of r8.jar and r8lib.jar such that they stay the same for
1157 # the entire execution of this script.
Morten Krogh-Jespersenffc73812019-03-19 12:59:17 +01001158 if 'r8-nolib' in options.shrinker or 'r8-nolib-full' in options.shrinker:
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +01001159 assert os.path.isfile(utils.R8_JAR), 'Cannot build without r8.jar'
1160 shutil.copyfile(utils.R8_JAR, os.path.join(temp_dir, 'r8.jar'))
Morten Krogh-Jespersenffc73812019-03-19 12:59:17 +01001161 if 'r8' in options.shrinker or 'r8-full' in options.shrinker:
Christoffer Quist Adamsen2a902752019-02-19 12:33:48 +01001162 assert os.path.isfile(utils.R8LIB_JAR), 'Cannot build without r8lib.jar'
1163 shutil.copyfile(utils.R8LIB_JAR, os.path.join(temp_dir, 'r8lib.jar'))
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +01001164
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +01001165 result_per_shrinker_per_app = []
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +01001166
Christoffer Quist Adamsenb899c112019-03-06 15:25:00 +01001167 for (app, repo) in options.apps:
1168 if app.skip:
1169 continue
Christoffer Quist Adamsen7cf4c562019-03-07 10:57:33 +01001170 result_per_shrinker_per_app.append(
1171 (app, GetResultsForApp(app, repo, options, temp_dir)))
Christoffer Quist Adamsenbbe5d7d2019-01-23 13:15:21 +01001172
Morten Krogh-Jespersene5853302019-04-05 12:46:42 +02001173 return LogResultsForApps(result_per_shrinker_per_app, options)
Christoffer Quist Adamsen404aade2018-12-20 13:00:09 +01001174
1175def success(message):
1176 CGREEN = '\033[32m'
1177 CEND = '\033[0m'
1178 print(CGREEN + message + CEND)
1179
1180def warn(message):
1181 CRED = '\033[91m'
1182 CEND = '\033[0m'
1183 print(CRED + message + CEND)
Søren Gjessecdae8792018-12-12 09:02:43 +01001184
1185if __name__ == '__main__':
1186 sys.exit(main(sys.argv[1:]))