1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#!/usr/bin/env python3
#
# Copyright 2021 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import argparse
import logging
import os
import sys
from util import build_utils
def _ParseArgs(args):
"""Parses command line options.
Returns:
An options object as from argparse.ArgumentParser.parse_args()
"""
parser = argparse.ArgumentParser()
parser.add_argument('--aapt2-path',
required=True,
help='Path to the Android aapt2 tool.')
parser.add_argument(
'--short-resource-paths',
action='store_true',
help='Whether to shorten resource paths inside the apk or module.')
parser.add_argument(
'--strip-resource-names',
action='store_true',
help='Whether to strip resource names from the resource table of the apk '
'or module.')
parser.add_argument('--proto-path',
required=True,
help='Input proto format resources APK.')
parser.add_argument('--resources-config-paths',
default='[]',
help='GN list of paths to aapt2 resources config files.')
parser.add_argument('--r-text-in',
required=True,
help='Path to R.txt. Used to exclude id/ resources.')
parser.add_argument(
'--resources-path-map-out-path',
help='Path to file produced by aapt2 that maps original resource paths '
'to shortened resource paths inside the apk or module.')
parser.add_argument('--optimized-proto-path',
required=True,
help='Output for `aapt2 optimize`.')
options = parser.parse_args(args)
options.resources_config_paths = build_utils.ParseGnList(
options.resources_config_paths)
if options.resources_path_map_out_path and not options.short_resource_paths:
parser.error(
'--resources-path-map-out-path requires --short-resource-paths')
return options
def _CombineResourceConfigs(resources_config_paths, out_config_path):
with open(out_config_path, 'w') as out_config:
for config_path in resources_config_paths:
with open(config_path) as config:
out_config.write(config.read())
out_config.write('\n')
def _ExtractNonCollapsableResources(rtxt_path):
"""Extract resources that should not be collapsed from the R.txt file
Resources of type ID are references to UI elements/views. They are used by
UI automation testing frameworks. They are kept in so that they don't break
tests, even though they may not actually be used during runtime. See
https://crbug.com/900993
App icons (aka mipmaps) are sometimes referenced by other apps by name so must
be keps as well. See https://b/161564466
Args:
rtxt_path: Path to R.txt file with all the resources
Returns:
List of resources in the form of <resource_type>/<resource_name>
"""
resources = []
_NO_COLLAPSE_TYPES = ['id', 'mipmap']
with open(rtxt_path) as rtxt:
for line in rtxt:
for resource_type in _NO_COLLAPSE_TYPES:
if ' {} '.format(resource_type) in line:
resource_name = line.split()[2]
resources.append('{}/{}'.format(resource_type, resource_name))
return resources
def _OptimizeApk(output, options, temp_dir, unoptimized_path, r_txt_path):
"""Optimize intermediate .ap_ file with aapt2.
Args:
output: Path to write to.
options: The command-line options.
temp_dir: A temporary directory.
unoptimized_path: path of the apk to optimize.
r_txt_path: path to the R.txt file of the unoptimized apk.
"""
optimize_command = [
options.aapt2_path,
'optimize',
unoptimized_path,
'-o',
output,
]
# Optimize the resources.pb file by obfuscating resource names and only
# allow usage via R.java constant.
if options.strip_resource_names:
no_collapse_resources = _ExtractNonCollapsableResources(r_txt_path)
gen_config_path = os.path.join(temp_dir, 'aapt2.config')
if options.resources_config_paths:
_CombineResourceConfigs(options.resources_config_paths, gen_config_path)
with open(gen_config_path, 'a') as config:
for resource in no_collapse_resources:
config.write('{}#no_collapse\n'.format(resource))
optimize_command += [
'--collapse-resource-names',
'--resources-config-path',
gen_config_path,
]
if options.short_resource_paths:
optimize_command += ['--shorten-resource-paths']
if options.resources_path_map_out_path:
optimize_command += [
'--resource-path-shortening-map', options.resources_path_map_out_path
]
logging.debug('Running aapt2 optimize')
build_utils.CheckOutput(optimize_command,
print_stdout=False,
print_stderr=False)
def main(args):
options = _ParseArgs(args)
with build_utils.TempDir() as temp_dir:
_OptimizeApk(options.optimized_proto_path, options, temp_dir,
options.proto_path, options.r_text_in)
if __name__ == '__main__':
main(sys.argv[1:])
|