OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2015 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Adds the code parts to a resource APK.""" | 7 """Adds the code parts to a resource APK.""" |
8 | 8 |
9 import argparse | 9 import argparse |
10 import itertools | 10 import itertools |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 return '\n'.join(_SplitAssetPath(p)[1] for p in sorted(paths)) + '\n' | 118 return '\n'.join(_SplitAssetPath(p)[1] for p in sorted(paths)) + '\n' |
119 | 119 |
120 | 120 |
121 def main(args): | 121 def main(args): |
122 args = build_utils.ExpandFileArgs(args) | 122 args = build_utils.ExpandFileArgs(args) |
123 options = _ParseArgs(args) | 123 options = _ParseArgs(args) |
124 | 124 |
125 native_libs = [] | 125 native_libs = [] |
126 if options.native_libs_dir: | 126 if options.native_libs_dir: |
127 native_libs = _ListSubPaths(options.native_libs_dir) | 127 native_libs = _ListSubPaths(options.native_libs_dir) |
| 128 native_libs.sort() |
128 | 129 |
129 input_paths = [options.resource_apk, __file__] + native_libs | 130 input_paths = [options.resource_apk, __file__] + native_libs |
130 if options.dex_file: | 131 if options.dex_file: |
131 input_paths.append(options.dex_file) | 132 input_paths.append(options.dex_file) |
132 | 133 |
133 if options.emma_device_jar: | 134 if options.emma_device_jar: |
134 input_paths.append(options.emma_device_jar) | 135 input_paths.append(options.emma_device_jar) |
135 | 136 |
136 input_strings = [options.android_abi, options.native_lib_placeholders] | 137 input_strings = [options.android_abi, options.native_lib_placeholders] |
137 | 138 |
138 for path in itertools.chain(options.assets, options.uncompressed_assets): | 139 for path in itertools.chain(options.assets, options.uncompressed_assets): |
139 src_path, dest_path = _SplitAssetPath(path) | 140 src_path, dest_path = _SplitAssetPath(path) |
140 input_paths.append(src_path) | 141 input_paths.append(src_path) |
141 input_strings.append(dest_path) | 142 input_strings.append(dest_path) |
142 | 143 |
143 def on_stale_md5(): | 144 def on_stale_md5(): |
144 tmp_apk = options.output_apk + '.tmp' | 145 tmp_apk = options.output_apk + '.tmp' |
145 try: | 146 try: |
146 # Use a temp file to avoid creating an output if anything goes wrong. | |
147 shutil.copyfile(options.resource_apk, tmp_apk) | |
148 | |
149 # TODO(agrieve): It would be more efficient to combine this step | 147 # TODO(agrieve): It would be more efficient to combine this step |
150 # with finalize_apk(), which sometimes aligns and uncompresses the | 148 # with finalize_apk(), which sometimes aligns and uncompresses the |
151 # native libraries. | 149 # native libraries. |
152 with zipfile.ZipFile(tmp_apk, 'a', zipfile.ZIP_DEFLATED) as apk: | 150 with zipfile.ZipFile(options.resource_apk) as resource_apk, \ |
| 151 zipfile.ZipFile(tmp_apk, 'w', zipfile.ZIP_DEFLATED) as out_apk: |
| 152 def copy_resource(zipinfo): |
| 153 compress = zipinfo.compress_type != zipfile.ZIP_STORED |
| 154 build_utils.AddToZipHermetic(out_apk, zipinfo.filename, |
| 155 data=resource_apk.read(zipinfo.filename), |
| 156 compress=compress) |
| 157 |
| 158 # Make assets come before resources in order to maintain the same file |
| 159 # ordering as GYP / aapt. http://crbug.com/561862 |
| 160 resource_infos = resource_apk.infolist() |
| 161 |
| 162 # 1. AndroidManifest.xml |
| 163 assert resource_infos[0].filename == 'AndroidManifest.xml' |
| 164 copy_resource(resource_infos[0]) |
| 165 |
| 166 # 2. Assets |
153 if options.write_asset_list: | 167 if options.write_asset_list: |
154 data = _CreateAssetsList( | 168 data = _CreateAssetsList( |
155 itertools.chain(options.assets, options.uncompressed_assets)) | 169 itertools.chain(options.assets, options.uncompressed_assets)) |
156 build_utils.AddToZipHermetic(apk, 'assets/assets_list', data=data) | 170 build_utils.AddToZipHermetic(out_apk, 'assets/assets_list', data=data) |
157 | 171 |
158 _AddAssets(apk, options.assets, disable_compression=False) | 172 _AddAssets(out_apk, options.assets, disable_compression=False) |
159 _AddAssets(apk, options.uncompressed_assets, disable_compression=True) | 173 _AddAssets(out_apk, options.uncompressed_assets, |
| 174 disable_compression=True) |
160 | 175 |
| 176 # 3. Resources |
| 177 for info in resource_infos[1:]: |
| 178 copy_resource(info) |
| 179 |
| 180 # 4. Dex files |
| 181 if options.dex_file and options.dex_file.endswith('.zip'): |
| 182 with zipfile.ZipFile(options.dex_file, 'r') as dex_zip: |
| 183 for dex in (d for d in dex_zip.namelist() if d.endswith('.dex')): |
| 184 build_utils.AddToZipHermetic(out_apk, dex, data=dex_zip.read(dex)) |
| 185 elif options.dex_file: |
| 186 build_utils.AddToZipHermetic(out_apk, 'classes.dex', |
| 187 src_path=options.dex_file) |
| 188 |
| 189 # 5. Native libraries. |
161 for path in native_libs: | 190 for path in native_libs: |
162 basename = os.path.basename(path) | 191 basename = os.path.basename(path) |
163 apk_path = 'lib/%s/%s' % (options.android_abi, basename) | 192 apk_path = 'lib/%s/%s' % (options.android_abi, basename) |
164 build_utils.AddToZipHermetic(apk, apk_path, src_path=path) | 193 build_utils.AddToZipHermetic(out_apk, apk_path, src_path=path) |
165 | 194 |
166 for name in options.native_lib_placeholders: | 195 for name in sorted(options.native_lib_placeholders): |
167 # Make it non-empty so that its checksum is non-zero and is not | 196 # Make it non-empty so that its checksum is non-zero and is not |
168 # ignored by md5_check. | 197 # ignored by md5_check. |
169 apk_path = 'lib/%s/%s.so' % (options.android_abi, name) | 198 apk_path = 'lib/%s/%s.so' % (options.android_abi, name) |
170 build_utils.AddToZipHermetic(apk, apk_path, data=':)') | 199 build_utils.AddToZipHermetic(out_apk, apk_path, data=':)') |
171 | 200 |
172 if options.dex_file and options.dex_file.endswith('.zip'): | 201 # 6. Java resources. Used only when coverage is enabled, so order |
173 with zipfile.ZipFile(options.dex_file, 'r') as dex_zip: | 202 # doesn't matter). |
174 for dex in (d for d in dex_zip.namelist() if d.endswith('.dex')): | |
175 build_utils.AddToZipHermetic(apk, dex, data=dex_zip.read(dex)) | |
176 elif options.dex_file: | |
177 build_utils.AddToZipHermetic(apk, 'classes.dex', | |
178 src_path=options.dex_file) | |
179 | |
180 if options.emma_device_jar: | 203 if options.emma_device_jar: |
181 # Add EMMA Java resources to APK. | 204 # Add EMMA Java resources to APK. |
182 with zipfile.ZipFile(options.emma_device_jar, 'r') as emma_device_jar: | 205 with zipfile.ZipFile(options.emma_device_jar, 'r') as emma_device_jar: |
183 for apk_path in emma_device_jar.namelist(): | 206 for apk_path in emma_device_jar.namelist(): |
184 apk_path_lower = apk_path.lower() | 207 apk_path_lower = apk_path.lower() |
185 if apk_path_lower.startswith('meta-inf/'): | 208 if apk_path_lower.startswith('meta-inf/'): |
186 continue | 209 continue |
187 | 210 |
188 if apk_path_lower.endswith('/'): | 211 if apk_path_lower.endswith('/'): |
189 continue | 212 continue |
190 | 213 |
191 if apk_path_lower.endswith('.class'): | 214 if apk_path_lower.endswith('.class'): |
192 continue | 215 continue |
193 | 216 |
194 build_utils.AddToZipHermetic(apk, apk_path, | 217 build_utils.AddToZipHermetic(out_apk, apk_path, |
195 data=emma_device_jar.read(apk_path)) | 218 data=emma_device_jar.read(apk_path)) |
196 | 219 |
197 shutil.move(tmp_apk, options.output_apk) | 220 shutil.move(tmp_apk, options.output_apk) |
198 finally: | 221 finally: |
199 if os.path.exists(tmp_apk): | 222 if os.path.exists(tmp_apk): |
200 os.unlink(tmp_apk) | 223 os.unlink(tmp_apk) |
201 | 224 |
202 build_utils.CallAndWriteDepfileIfStale( | 225 build_utils.CallAndWriteDepfileIfStale( |
203 on_stale_md5, | 226 on_stale_md5, |
204 options, | 227 options, |
205 input_paths=input_paths, | 228 input_paths=input_paths, |
206 input_strings=input_strings, | 229 input_strings=input_strings, |
207 output_paths=[options.output_apk]) | 230 output_paths=[options.output_apk]) |
208 | 231 |
209 | 232 |
210 if __name__ == '__main__': | 233 if __name__ == '__main__': |
211 main(sys.argv[1:]) | 234 main(sys.argv[1:]) |
OLD | NEW |