Index: build/android/gyp/process_resources.py |
diff --git a/build/android/gyp/process_resources.py b/build/android/gyp/process_resources.py |
index 132cdf62f1455b81f4928bd135febec14c4ae9f8..ec19f5943f91a8082f163c3a964f04dc8f6839e5 100755 |
--- a/build/android/gyp/process_resources.py |
+++ b/build/android/gyp/process_resources.py |
@@ -13,8 +13,7 @@ This will crunch images and generate v14 compatible resources |
import optparse |
import os |
import re |
-import shlex |
-import shutil |
+import zipfile |
import generate_v14_compatible_resources |
@@ -27,25 +26,27 @@ def ParseArgs(): |
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 build tools folder') |
- parser.add_option('--R-dir', help='directory to hold generated R.java') |
- parser.add_option('--dependencies-res-dirs', |
- help='directories containing resources to be packaged') |
- parser.add_option('--resource-dir', |
- help='directory containing this target\'s resources.') |
- parser.add_option('--crunch-output-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('--custom-package', help='Java package for R.java') |
+ |
+ parser.add_option('--resource-dirs', |
+ help='Directories containing resources of this target.') |
+ parser.add_option('--dependencies-res-zips', |
+ help='Resources from dependents.') |
+ |
+ parser.add_option('--R-dir', help='directory to hold generated R.java') |
+ parser.add_option('--resource-zip-out', |
+ help='Path for output zipped resources.') |
+ |
parser.add_option('--proguard-file', |
help='Path to proguard.txt generated file') |
- parser.add_option('--res-v14-compatibility-dir', |
- help='output directory into which ' |
- 'v14 compatible resources will be generated') |
parser.add_option( |
'--v14-verify-only', |
action='store_true', |
@@ -74,11 +75,10 @@ def ParseArgs(): |
'android_sdk', |
'android_sdk_tools', |
'android_manifest', |
- 'dependencies_res_dirs', |
- 'resource_dir', |
- 'crunch_output_dir', |
+ 'dependencies_res_zips', |
+ 'resource_dirs', |
+ 'resource_zip_out', |
'R_dir', |
- 'res_v14_compatibility_dir', |
) |
build_utils.CheckOptions(options, parser, required=required_options) |
@@ -110,30 +110,7 @@ def CreateExtraRJavaFiles( |
-def MoveImagesToNonMdpiFolders(res_root): |
- """Move images from drawable-*-mdpi-* folders to drawable-* folders. |
- Why? http://crbug.com/289843 |
- """ |
- for src_dir_name in os.listdir(res_root): |
- src_components = src_dir_name.split('-') |
- if src_components[0] != 'drawable' or 'mdpi' not in src_components: |
- continue |
- src_dir = os.path.join(res_root, src_dir_name) |
- if not os.path.isdir(src_dir): |
- continue |
- dst_components = [c for c in src_components if c != 'mdpi'] |
- assert dst_components != src_components |
- dst_dir_name = '-'.join(dst_components) |
- dst_dir = os.path.join(res_root, dst_dir_name) |
- build_utils.MakeDirectory(dst_dir) |
- for src_file_name in os.listdir(src_dir): |
- if not src_file_name.endswith('.png'): |
- continue |
- src_file = os.path.join(src_dir, src_file_name) |
- dst_file = os.path.join(dst_dir, src_file_name) |
- assert not os.path.lexists(dst_file) |
- shutil.move(src_file, dst_file) |
def DidCrunchFail(returncode, stderr): |
@@ -160,57 +137,95 @@ def main(): |
build_utils.DeleteDirectory(options.R_dir) |
build_utils.MakeDirectory(options.R_dir) |
- generate_v14_compatible_resources.GenerateV14Resources( |
- options.resource_dir, |
- options.res_v14_compatibility_dir, |
- options.v14_verify_only) |
- |
- # 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, |
- '--auto-add-overlay', |
- '-I', android_jar, |
- '--output-text-symbols', options.R_dir, |
- '-J', options.R_dir] |
- all_res_dirs = ([options.resource_dir] |
- + shlex.split(options.dependencies_res_dirs)) |
- for res_dir in all_res_dirs: |
- package_command += ['-S', res_dir] |
- if options.non_constant_id: |
- package_command.append('--non-constant-id') |
- if options.custom_package: |
- package_command += ['--custom-package', options.custom_package] |
- if options.proguard_file: |
- package_command += ['-G', options.proguard_file] |
- |
- build_utils.CheckOutput(package_command) |
- |
- if options.extra_res_packages: |
- CreateExtraRJavaFiles( |
- options.R_dir, |
- build_utils.ParseGypList(options.extra_res_packages), |
- build_utils.ParseGypList(options.extra_r_text_files)) |
- |
- # Crunch image resources. This shrinks png files and is necessary for 9-patch |
- # images to display correctly. |
- build_utils.DeleteDirectory(options.crunch_output_dir) |
- build_utils.MakeDirectory(options.crunch_output_dir) |
- aapt_cmd = [aapt, |
- 'crunch', |
- '-S', options.resource_dir, |
- '-C', options.crunch_output_dir] |
- build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail) |
- |
- MoveImagesToNonMdpiFolders(options.crunch_output_dir) |
- |
- if options.stamp: |
- build_utils.Touch(options.stamp) |
+ with build_utils.TempDir() as temp_dir: |
+ deps_dir = os.path.join(temp_dir, 'deps') |
+ build_utils.MakeDirectory(deps_dir) |
+ v14_dir = os.path.join(temp_dir, 'v14') |
+ build_utils.MakeDirectory(v14_dir) |
+ |
+ input_resource_dirs = build_utils.ParseGypList(options.resource_dirs) |
+ |
+ for resource_dir in input_resource_dirs: |
+ generate_v14_compatible_resources.GenerateV14Resources( |
+ resource_dir, |
+ v14_dir, |
+ options.v14_verify_only) |
+ |
+ # 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, |
+ '--auto-add-overlay', |
+ '-I', android_jar, |
+ '--output-text-symbols', options.R_dir, |
+ '-J', options.R_dir] |
+ |
+ for d in input_resource_dirs: |
+ package_command += ['-S', d] |
+ |
+ dep_zips = build_utils.ParseGypList(options.dependencies_res_zips) |
+ for z in dep_zips: |
+ subdir = os.path.join(deps_dir, os.path.basename(z)) |
+ if os.path.exists(subdir): |
+ raise Exception('Resource zip name conflict: ' + os.path.basename(z)) |
+ build_utils.ExtractAll(z, path=subdir) |
+ package_command += ['-S', subdir] |
+ |
+ if options.non_constant_id: |
+ package_command.append('--non-constant-id') |
+ if options.custom_package: |
+ package_command += ['--custom-package', options.custom_package] |
+ if options.proguard_file: |
+ package_command += ['-G', options.proguard_file] |
+ build_utils.CheckOutput(package_command, print_stderr=False) |
+ |
+ if options.extra_res_packages: |
+ CreateExtraRJavaFiles( |
+ options.R_dir, |
+ build_utils.ParseGypList(options.extra_res_packages), |
+ build_utils.ParseGypList(options.extra_r_text_files)) |
+ |
+ # This is the list of directories with resources to put in the final .zip |
+ # file. The order of these is important so that crunched/v14 resources |
+ # override the normal ones. |
+ zip_resource_dirs = input_resource_dirs + [v14_dir] |
+ |
+ base_crunch_dir = os.path.join(temp_dir, 'crunch') |
+ |
+ # Crunch image resources. This shrinks png files and is necessary for |
+ # 9-patch images to display correctly. 'aapt crunch' accepts only a single |
+ # directory at a time and deletes everything in the output directory. |
+ for idx, d in enumerate(input_resource_dirs): |
+ crunch_dir = os.path.join(base_crunch_dir, str(idx)) |
+ build_utils.MakeDirectory(crunch_dir) |
+ zip_resource_dirs.append(crunch_dir) |
+ aapt_cmd = [aapt, |
+ 'crunch', |
+ '-C', crunch_dir, |
+ '-S', d] |
+ build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail) |
+ |
+ # Python zipfile does not provide a way to replace a file (it just writes |
+ # another file with the same name). So, first collect all the files to put |
+ # in the zip (with proper overriding), and then zip them. |
+ files_to_zip = dict() |
+ for d in zip_resource_dirs: |
+ for root, _, files in os.walk(d): |
+ for f in files: |
+ archive_path = os.path.join(os.path.relpath(root, d), f) |
+ path = os.path.join(root, f) |
+ files_to_zip[archive_path] = path |
+ with zipfile.ZipFile(options.resource_zip_out, 'w') as outzip: |
+ for archive_path, path in files_to_zip.iteritems(): |
+ outzip.write(path, archive_path) |
+ |
+ if options.stamp: |
+ build_utils.Touch(options.stamp) |
if __name__ == '__main__': |