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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 def _AddAssets(apk, paths, disable_compression=False): | 85 def _AddAssets(apk, paths, disable_compression=False): |
86 """Adds the given paths to the apk. | 86 """Adds the given paths to the apk. |
87 | 87 |
88 Args: | 88 Args: |
89 apk: ZipFile to write to. | 89 apk: ZipFile to write to. |
90 paths: List of paths (with optional :zipPath suffix) to add. | 90 paths: List of paths (with optional :zipPath suffix) to add. |
91 disable_compression: Whether to disable compression. | 91 disable_compression: Whether to disable compression. |
92 """ | 92 """ |
93 # Group all uncompressed assets together in the hope that it will increase | 93 # Group all uncompressed assets together in the hope that it will increase |
94 # locality of mmap'ed files. | 94 # locality of mmap'ed files. |
95 for target_compress_type in (zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED): | 95 for target_compress in (False, True): |
96 for path in paths: | 96 for path in paths: |
97 src_path, dest_path = _SplitAssetPath(path) | 97 src_path, dest_path = _SplitAssetPath(path) |
98 | 98 |
99 compress_type = zipfile.ZIP_DEFLATED | 99 compress = not disable_compression and ( |
100 if disable_compression or ( | 100 os.path.splitext(src_path)[1] not in _NO_COMPRESS_EXTENSIONS) |
101 os.path.splitext(src_path)[1] in _NO_COMPRESS_EXTENSIONS): | 101 if target_compress == compress: |
102 compress_type = zipfile.ZIP_STORED | |
103 | |
104 if target_compress_type == compress_type: | |
105 apk_path = 'assets/' + dest_path | 102 apk_path = 'assets/' + dest_path |
106 try: | 103 try: |
107 apk.getinfo(apk_path) | 104 apk.getinfo(apk_path) |
108 # Should never happen since write_build_config.py handles merging. | 105 # Should never happen since write_build_config.py handles merging. |
109 raise Exception('Multiple targets specified the asset path: %s' % | 106 raise Exception('Multiple targets specified the asset path: %s' % |
110 apk_path) | 107 apk_path) |
111 except KeyError: | 108 except KeyError: |
112 apk.write(src_path, apk_path, compress_type) | 109 build_utils.AddToZipHermetic(apk, apk_path, src_path=src_path, |
| 110 compress=compress) |
113 | 111 |
114 | 112 |
115 def main(args): | 113 def main(args): |
116 args = build_utils.ExpandFileArgs(args) | 114 args = build_utils.ExpandFileArgs(args) |
117 options = _ParseArgs(args) | 115 options = _ParseArgs(args) |
118 | 116 |
119 native_libs = [] | 117 native_libs = [] |
120 if options.native_libs_dir: | 118 if options.native_libs_dir: |
121 native_libs = _ListSubPaths(options.native_libs_dir) | 119 native_libs = _ListSubPaths(options.native_libs_dir) |
122 | 120 |
(...skipping 18 matching lines...) Expand all Loading... |
141 shutil.copyfile(options.resource_apk, tmp_apk) | 139 shutil.copyfile(options.resource_apk, tmp_apk) |
142 | 140 |
143 # TODO(agrieve): It would be more efficient to combine this step | 141 # TODO(agrieve): It would be more efficient to combine this step |
144 # with finalize_apk(), which sometimes aligns and uncompresses the | 142 # with finalize_apk(), which sometimes aligns and uncompresses the |
145 # native libraries. | 143 # native libraries. |
146 with zipfile.ZipFile(tmp_apk, 'a', zipfile.ZIP_DEFLATED) as apk: | 144 with zipfile.ZipFile(tmp_apk, 'a', zipfile.ZIP_DEFLATED) as apk: |
147 _AddAssets(apk, options.assets, disable_compression=False) | 145 _AddAssets(apk, options.assets, disable_compression=False) |
148 _AddAssets(apk, options.uncompressed_assets, disable_compression=True) | 146 _AddAssets(apk, options.uncompressed_assets, disable_compression=True) |
149 for path in native_libs: | 147 for path in native_libs: |
150 basename = os.path.basename(path) | 148 basename = os.path.basename(path) |
151 apk.write(path, 'lib/%s/%s' % (options.android_abi, basename)) | 149 apk_path = 'lib/%s/%s' % (options.android_abi, basename) |
| 150 build_utils.AddToZipHermetic(apk, apk_path, src_path=path) |
| 151 |
152 for name in options.native_lib_placeholders: | 152 for name in options.native_lib_placeholders: |
153 # Make it non-empty so that its checksum is non-zero and is not | 153 # Make it non-empty so that its checksum is non-zero and is not |
154 # ignored by md5_check. | 154 # ignored by md5_check. |
155 apk.writestr('lib/%s/%s' % (options.android_abi, name), ':)', | 155 apk_path = 'lib/%s/%s.so' % (options.android_abi, name) |
156 zipfile.ZIP_STORED) | 156 build_utils.AddToZipHermetic(apk, apk_path, data=':)') |
157 if options.dex_file: | 157 |
158 if options.dex_file.endswith('.zip'): | 158 if options.dex_file and options.dex_file.endswith('.zip'): |
159 with zipfile.ZipFile(options.dex_file, 'r') as dex_zip: | 159 with zipfile.ZipFile(options.dex_file, 'r') as dex_zip: |
160 for dex in (d for d in dex_zip.namelist() if d.endswith('.dex')): | 160 for dex in (d for d in dex_zip.namelist() if d.endswith('.dex')): |
161 apk.writestr(dex, dex_zip.read(dex)) | 161 build_utils.AddToZipHermetic(apk, dex, data=dex_zip.read(dex)) |
162 else: | 162 elif options.dex_file: |
163 apk.write(options.dex_file, 'classes.dex') | 163 build_utils.AddToZipHermetic(apk, 'classes.dex', |
| 164 src_path=options.dex_file) |
164 | 165 |
165 if options.emma_device_jar: | 166 if options.emma_device_jar: |
166 # Add EMMA Java resources to APK. | 167 # Add EMMA Java resources to APK. |
167 with zipfile.ZipFile(options.emma_device_jar, 'r') as emma_device_jar: | 168 with zipfile.ZipFile(options.emma_device_jar, 'r') as emma_device_jar: |
168 for emma_device_jar_entry in emma_device_jar.namelist(): | 169 for apk_path in emma_device_jar.namelist(): |
169 entry_name_lower = emma_device_jar_entry.lower() | 170 apk_path_lower = apk_path.lower() |
170 if entry_name_lower.startswith('meta-inf/'): | 171 if apk_path_lower.startswith('meta-inf/'): |
171 continue | 172 continue |
172 | 173 |
173 if entry_name_lower.endswith('/'): | 174 if apk_path_lower.endswith('/'): |
174 continue | 175 continue |
175 | 176 |
176 if entry_name_lower.endswith('.class'): | 177 if apk_path_lower.endswith('.class'): |
177 continue | 178 continue |
178 | 179 |
179 apk.writestr(emma_device_jar_entry, | 180 build_utils.AddToZipHermetic(apk, apk_path, |
180 emma_device_jar.read(emma_device_jar_entry)) | 181 data=emma_device_jar.read(apk_path)) |
181 | 182 |
182 shutil.move(tmp_apk, options.output_apk) | 183 shutil.move(tmp_apk, options.output_apk) |
183 finally: | 184 finally: |
184 if os.path.exists(tmp_apk): | 185 if os.path.exists(tmp_apk): |
185 os.unlink(tmp_apk) | 186 os.unlink(tmp_apk) |
186 | 187 |
187 build_utils.CallAndWriteDepfileIfStale( | 188 build_utils.CallAndWriteDepfileIfStale( |
188 on_stale_md5, | 189 on_stale_md5, |
189 options, | 190 options, |
190 input_paths=input_paths, | 191 input_paths=input_paths, |
191 input_strings=input_strings, | 192 input_strings=input_strings, |
192 output_paths=[options.output_apk]) | 193 output_paths=[options.output_apk]) |
193 | 194 |
194 | 195 |
195 if __name__ == '__main__': | 196 if __name__ == '__main__': |
196 main(sys.argv[1:]) | 197 main(sys.argv[1:]) |
OLD | NEW |