OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 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 """Process Android resources to generate R.java, and prepare for packaging. | 7 """Process Android resources to generate R.java, and prepare for packaging. |
8 | 8 |
9 This will crunch images and generate v14 compatible resources | 9 This will crunch images and generate v14 compatible resources |
10 (see generate_v14_compatible_resources.py). | 10 (see generate_v14_compatible_resources.py). |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 def _CreateRJavaFile(package, resources_by_type, shared_resources): | 198 def _CreateRJavaFile(package, resources_by_type, shared_resources): |
199 """Generates the contents of a R.java file.""" | 199 """Generates the contents of a R.java file.""" |
200 # Keep these assignments all on one line to make diffing against regular | 200 # Keep these assignments all on one line to make diffing against regular |
201 # aapt-generated files easier. | 201 # aapt-generated files easier. |
202 create_id = ('{{ e.resource_type }}.{{ e.name }} = ' | 202 create_id = ('{{ e.resource_type }}.{{ e.name }} = ' |
203 '({{ e.resource_type }}.{{ e.name }} & 0x00ffffff) |' | 203 '({{ e.resource_type }}.{{ e.name }} & 0x00ffffff) |' |
204 ' (packageId << 24);') | 204 ' (packageId << 24);') |
205 create_id_arr = ('{{ e.resource_type }}.{{ e.name }}[i] = ' | 205 create_id_arr = ('{{ e.resource_type }}.{{ e.name }}[i] = ' |
206 '({{ e.resource_type }}.{{ e.name }}[i] & 0x00ffffff) |' | 206 '({{ e.resource_type }}.{{ e.name }}[i] & 0x00ffffff) |' |
207 ' (packageId << 24);') | 207 ' (packageId << 24);') |
| 208 # Here we diverge from what aapt does. Because we have so many |
| 209 # resources, the onResourcesLoaded method was exceeding the 64KB limit that |
| 210 # Java imposes. For this reason we split onResourcesLoaded into different |
| 211 # methods for each resource type. |
208 template = Template("""/* AUTO-GENERATED FILE. DO NOT MODIFY. */ | 212 template = Template("""/* AUTO-GENERATED FILE. DO NOT MODIFY. */ |
209 | 213 |
210 package {{ package }}; | 214 package {{ package }}; |
211 | 215 |
212 public final class R { | 216 public final class R { |
213 {% for resource_type in resource_types %} | 217 {% for resource_type in resource_types %} |
214 public static final class {{ resource_type }} { | 218 public static final class {{ resource_type }} { |
215 {% for e in resources[resource_type] %} | 219 {% for e in resources[resource_type] %} |
216 {% if shared_resources %} | 220 {% if shared_resources %} |
217 public static {{ e.java_type }} {{ e.name }} = {{ e.value }}; | 221 public static {{ e.java_type }} {{ e.name }} = {{ e.value }}; |
218 {% else %} | 222 {% else %} |
219 public static final {{ e.java_type }} {{ e.name }} = {{ e.value }}; | 223 public static final {{ e.java_type }} {{ e.name }} = {{ e.value }}; |
220 {% endif %} | 224 {% endif %} |
221 {% endfor %} | 225 {% endfor %} |
222 } | 226 } |
223 {% endfor %} | 227 {% endfor %} |
224 {% if shared_resources %} | 228 {% if shared_resources %} |
225 public static void onResourcesLoaded(int packageId) { | 229 public static void onResourcesLoaded(int packageId) { |
226 {% for resource_type in resource_types %} | 230 {% for resource_type in resource_types %} |
227 {% for e in resources[resource_type] %} | 231 onResourcesLoaded{{ resource_type|title }}(packageId); |
228 {% if resource_type != 'styleable' and e.java_type != 'int[]' %} | |
229 """ + create_id + """ | |
230 {% endif %} | |
231 {% endfor %} | |
232 {% for e in resources[resource_type] %} | 232 {% for e in resources[resource_type] %} |
233 {% if e.java_type == 'int[]' %} | 233 {% if e.java_type == 'int[]' %} |
234 for(int i = 0; i < {{ e.resource_type }}.{{ e.name }}.length; ++i) { | 234 for(int i = 0; i < {{ e.resource_type }}.{{ e.name }}.length; ++i) { |
235 """ + create_id_arr + """ | 235 """ + create_id_arr + """ |
236 } | 236 } |
237 {% endif %} | 237 {% endif %} |
238 {% endfor %} | 238 {% endfor %} |
239 {% endfor %} | 239 {% endfor %} |
240 } | 240 } |
| 241 {% for res_type in resource_types %} |
| 242 private static void onResourcesLoaded{{ res_type|title }}(int packageId) { |
| 243 {% for e in resources[res_type] %} |
| 244 {% if res_type != 'styleable' and e.java_type != 'int[]' %} |
| 245 """ + create_id + """ |
| 246 {% endif %} |
| 247 {% endfor %} |
| 248 } |
| 249 {% endfor %} |
241 {% endif %} | 250 {% endif %} |
242 } | 251 } |
243 """, trim_blocks=True, lstrip_blocks=True) | 252 """, trim_blocks=True, lstrip_blocks=True) |
244 | 253 |
245 return template.render(package=package, | 254 return template.render(package=package, |
246 resources=resources_by_type, | 255 resources=resources_by_type, |
247 resource_types=sorted(resources_by_type), | 256 resource_types=sorted(resources_by_type), |
248 shared_resources=shared_resources) | 257 shared_resources=shared_resources) |
249 | 258 |
250 | 259 |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 options, | 532 options, |
524 input_paths=input_paths, | 533 input_paths=input_paths, |
525 input_strings=input_strings, | 534 input_strings=input_strings, |
526 output_paths=output_paths, | 535 output_paths=output_paths, |
527 # TODO(agrieve): Remove R_dir when it's no longer used (used only by GYP). | 536 # TODO(agrieve): Remove R_dir when it's no longer used (used only by GYP). |
528 force=options.R_dir) | 537 force=options.R_dir) |
529 | 538 |
530 | 539 |
531 if __name__ == '__main__': | 540 if __name__ == '__main__': |
532 main(sys.argv[1:]) | 541 main(sys.argv[1:]) |
OLD | NEW |