diff --git a/android/ant/apk-codegen.xml b/android/ant/apk-codegen.xml deleted file mode 100644 index 78b8f686c..000000000 --- a/android/ant/apk-codegen.xml +++ /dev/null @@ -1,98 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/android/ant/apk-package-resources.xml b/android/ant/apk-package-resources.xml index 147ebc4cb..b2e16069f 100644 --- a/android/ant/apk-package-resources.xml +++ b/android/ant/apk-package-resources.xml @@ -51,20 +51,6 @@ - - - - - - - - - - - - - - - - - + + ignoreAssets=""> diff --git a/android/gyp/process_resources.py b/android/gyp/process_resources.py index 9bad5c1bc..c57168b69 100755 --- a/android/gyp/process_resources.py +++ b/android/gyp/process_resources.py @@ -8,6 +8,7 @@ import optparse import os +import re import shlex import shutil @@ -24,10 +25,10 @@ def ParseArgs(): 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('--res-dirs', + parser.add_option('--dependencies-res-dirs', help='directories containing resources to be packaged') - parser.add_option('--crunch-input-dir', - help='directory containing images to be crunched') + 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') @@ -35,19 +36,60 @@ def ParseArgs(): parser.add_option('--android-manifest', help='AndroidManifest.xml path') parser.add_option('--stamp', help='File to touch on success') + parser.add_option( + '--extra-res-packages', + help='Additional package names to generate R.java files for') + parser.add_option( + '--extra-r-text-files', + help='For each additional package, the R.txt file should contain a ' + 'list of resources to be included in the R.java file in the format ' + 'generated by aapt') + (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_dirs', 'crunch_input_dir', 'crunch_output_dir') + required_options = ( + 'android_sdk', + 'android_sdk_tools', + 'android_manifest', + 'dependencies_res_dirs', + 'resource_dir', + 'crunch_output_dir', + 'R_dir', + ) build_utils.CheckOptions(options, parser, required=required_options) return options +def CreateExtraRJavaFiles( + r_dir, extra_packages, extra_r_text_files): + if len(extra_packages) != len(extra_r_text_files): + raise Exception('--extra-res-packages and --extra-r-text-files' + 'should have the same length') + + java_files = build_utils.FindInDirectory(r_dir, "R.java") + if len(java_files) != 1: + return + r_java_file = java_files[0] + r_java_contents = open(r_java_file).read() + + for package in extra_packages: + package_r_java_dir = os.path.join(r_dir, *package.split('.')) + build_utils.MakeDirectory(package_r_java_dir) + package_r_java_path = os.path.join(package_r_java_dir, 'R.java') + open(package_r_java_path, 'w').write( + re.sub(r'package [.\w]*;', 'package %s;' % package, r_java_contents)) + # TODO(cjhopman): These extra package's R.java files should be filtered to + # only contain the resources listed in their R.txt files. At this point, we + # have already compiled those other libraries, so doing this would only + # affect how the code in this .apk target could refer to the resources. + + + def MoveImagesToNonMdpiFolders(res_root): """Move images from drawable-*-mdpi-* folders to drawable-* folders. @@ -95,6 +137,7 @@ def main(): android_jar = os.path.join(options.android_sdk, 'android.jar') aapt = os.path.join(options.android_sdk_tools, 'aapt') + build_utils.DeleteDirectory(options.R_dir) build_utils.MakeDirectory(options.R_dir) # Generate R.java. This R.java contains non-final constants and is used only @@ -110,21 +153,30 @@ def main(): '-I', android_jar, '--output-text-symbols', options.R_dir, '-J', options.R_dir] - res_dirs = shlex.split(options.res_dirs) - for res_dir in res_dirs: + 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] + 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.crunch_input_dir, + '-S', options.resource_dir, '-C', options.crunch_output_dir] build_utils.CheckOutput(aapt_cmd, fail_func=DidCrunchFail) diff --git a/android/gyp/util/build_utils.py b/android/gyp/util/build_utils.py index 607f9be27..36c83193c 100644 --- a/android/gyp/util/build_utils.py +++ b/android/gyp/util/build_utils.py @@ -59,7 +59,7 @@ def CheckOptions(options, parser, required=None): if not required: return for option_name in required: - if not getattr(options, option_name): + if getattr(options, option_name) is None: parser.error('--%s is required' % option_name.replace('_', '-')) def WriteJson(obj, path, only_if_changed=False): diff --git a/java.gypi b/java.gypi index 8f61395cb..ce778b1ae 100644 --- a/java.gypi +++ b/java.gypi @@ -156,8 +156,8 @@ 'android_manifest': '<(DEPTH)/build/android/AndroidManifest.xml', # Include the dependencies' res dirs so that references to # resources in dependencies can be resolved. - 'all_res_dirs': ['<@(res_input_dirs)', - '>@(dependencies_res_input_dirs)',], + 'dependencies_res_dirs': ['<@(res_extra_dirs)', + '>@(dependencies_res_input_dirs)',], # Write the inputs list to a file, so that its mtime is updated when # the list of inputs changes. 'inputs_list_file': '>|(java_resources.<(_target_name).gypcmd >@(resource_input_paths) >@(dependencies_res_files))' @@ -177,8 +177,8 @@ '--android-sdk', '<(android_sdk)', '--android-sdk-tools', '<(android_sdk_tools)', '--R-dir', '<(R_dir)', - '--res-dirs', '>(all_res_dirs)', - '--crunch-input-dir', '>(res_dir)', + '--dependencies-res-dirs', '>(dependencies_res_dirs)', + '--resource-dir', '<(res_dir)', '--crunch-output-dir', '<(res_crunched_dir)', '--android-manifest', '<(android_manifest)', '--non-constant-id', diff --git a/java_apk.gypi b/java_apk.gypi index d083afc08..b45397423 100644 --- a/java_apk.gypi +++ b/java_apk.gypi @@ -433,8 +433,13 @@ ], 'actions': [ { - 'action_name': 'ant_codegen_<(_target_name)', - 'message': 'Generating R.java for <(_target_name)', + 'action_name': 'process_resources', + 'message': 'processing resources for <(_target_name)', + 'variables': { + # Write the inputs list to a file, so that its mtime is updated when + # the list of inputs changes. + 'inputs_list_file': '>|(apk_codegen.<(_target_name).gypcmd >@(additional_input_paths) >@(resource_input_paths))', + }, 'conditions': [ ['is_test_apk == 1', { 'variables': { @@ -443,15 +448,9 @@ } }], ], - 'variables': { - # Write the inputs list to a file, so that its mtime is updated when - # the list of inputs changes. - 'inputs_list_file': '>|(apk_codegen.<(_target_name).gypcmd >@(additional_input_paths) >@(resource_input_paths))' - }, 'inputs': [ - '<(DEPTH)/build/android/ant/apk-codegen.xml', '<(DEPTH)/build/android/gyp/util/build_utils.py', - '<(DEPTH)/build/android/gyp/ant.py', + '<(DEPTH)/build/android/gyp/process_resources.py', '<(android_manifest_path)', '>@(additional_input_paths)', '>@(resource_input_paths)', @@ -461,23 +460,22 @@ '<(codegen_stamp)', ], 'action': [ - 'python', '<(DEPTH)/build/android/gyp/ant.py', - '-quiet', - '-DADDITIONAL_RES_DIRS=>(additional_res_dirs)', - '-DADDITIONAL_RES_PACKAGES=>(additional_res_packages)', - '-DADDITIONAL_R_TEXT_FILES=>(additional_R_text_files)', - '-DANDROID_MANIFEST=<(android_manifest_path)', - '-DANDROID_SDK_JAR=<(android_sdk_jar)', - '-DANDROID_SDK_ROOT=<(android_sdk_root)', - '-DANDROID_SDK_VERSION=<(android_sdk_version)', - '-DANDROID_SDK_TOOLS=<(android_sdk_tools)', - '-DOUT_DIR=<(intermediate_dir)', - '-DRESOURCE_DIR=<(resource_dir)', + 'python', '<(DEPTH)/build/android/gyp/process_resources.py', + '--android-sdk', '<(android_sdk)', + '--android-sdk-tools', '<(android_sdk_tools)', - '-DSTAMP=<(codegen_stamp)', - '-Dbasedir=.', - '-buildfile', - '<(DEPTH)/build/android/ant/apk-codegen.xml', + '--android-manifest', '<(android_manifest_path)', + '--dependencies-res-dirs', '>(additional_res_dirs)', + + '--extra-res-packages', '>(additional_res_packages)', + '--extra-r-text-files', '>(additional_R_text_files)', + + '--resource-dir', '<(resource_dir)', + '--crunch-output-dir', '<(intermediate_dir)/res', + + '--R-dir', '<(intermediate_dir)/gen', + + '--stamp', '<(codegen_stamp)', ], }, {