summaryrefslogtreecommitdiffstats
path: root/build/android/process_resources.py
blob: f5de24df07d70d10e669821c0812aae45588fe33 (plain)
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
#!/usr/bin/env python
#
# Copyright (c) 2012 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.

"""Process Android library resources to generate R.java and crunched images."""

import optparse
import os
import subprocess

from pylib import build_utils

def ParseArgs():
  """Parses command line options.

  Returns:
    An options object as from optparse.OptionsParser.parse_args()
  """
  parser = optparse.OptionParser()
  parser.add_option('--android-sdk', help='path to the Android SDK folder')
  parser.add_option('--android-sdk-tools',
                    help='path to the Android SDK platform tools folder')
  parser.add_option('--R-dir', help='directory to hold generated R.java')
  parser.add_option('--res-dir', help='directory containing resources')
  parser.add_option('--out-res-dir',
                    help='directory to hold crunched resources')
  parser.add_option('--non-constant-id', action='store_true')
  parser.add_option('--custom-package', help='Java package for R.java')
  parser.add_option('--android-manifest', help='AndroidManifest.xml path')
  parser.add_option('--stamp', help='File to touch on success')

  # This is part of a temporary fix for crbug.com/177552.
  # TODO(newt): remove this once crbug.com/177552 is fixed in ninja.
  parser.add_option('--ignore', help='this argument is ignored')
  (options, args) = parser.parse_args()

  if args:
    parser.error('No positional arguments should be given.')

  # Check that required options have been provided.
  required_options = ('android_sdk', 'android_sdk_tools',
                      'R_dir', 'res_dir', 'out_res_dir')
  for option_name in required_options:
    if getattr(options, option_name) is None:
      parser.error('--%s is required' % option_name.replace('_', '-'))

  return options


def main():
  options = ParseArgs()
  android_jar = os.path.join(options.android_sdk, 'android.jar')
  aapt = os.path.join(options.android_sdk_tools, 'aapt')

  build_utils.MakeDirectory(options.R_dir)

  # Generate R.java. This R.java contains non-final constants and is used only
  # while compiling the library jar (e.g. chromium_content.jar). When building
  # an apk, a new R.java file with the correct resource -> ID mappings will be
  # generated by merging the resources from all libraries and the main apk
  # project.
  package_command = [aapt,
                     'package',
                     '-m',
                     '-M', options.android_manifest,
                     '-S', options.res_dir,
                     '--auto-add-overlay',
                     '-I', android_jar,
                     '--output-text-symbols', options.R_dir,
                     '-J', options.R_dir]
  # If strings.xml was generated from a grd file, it will be in out_res_dir.
  if os.path.isdir(options.out_res_dir):
    package_command += ['-S', options.out_res_dir]
  if options.non_constant_id:
    package_command.append('--non-constant-id')
  if options.custom_package:
    package_command += ['--custom-package', options.custom_package]
  subprocess.check_call(package_command)

  # Crunch image resources. This shrinks png files and is necessary for 9-patch
  # images to display correctly.
  subprocess.check_call([aapt,
                         'crunch',
                         '-S', options.res_dir,
                         '-C', options.out_res_dir])

  build_utils.Touch(options.stamp)


if __name__ == '__main__':
  main()