def main(args): args = build_utils.ExpandFileArgs(args) options = _ParseArgs(args) debug_temp_resources_dir = os.environ.get(_ENV_DEBUG_VARIABLE) if debug_temp_resources_dir: debug_temp_resources_dir = os.path.join( debug_temp_resources_dir, os.path.basename(options.apk_path)) build_utils.DeleteDirectory(debug_temp_resources_dir) build_utils.MakeDirectory(debug_temp_resources_dir) with resource_utils.BuildContext(debug_temp_resources_dir) as build: dep_subdirs = resource_utils.ExtractDeps(options.dependencies_res_zips, build.deps_dir) _PackageApk(options, dep_subdirs, build.temp_dir, build.gen_dir, build.r_txt_path) r_txt_path = _WriteFinalRTxtFile(options, build.r_txt_path) # If --shared-resources-whitelist is used, the all resources listed in # the corresponding R.txt file will be non-final, and an onResourcesLoaded() # will be generated to adjust them at runtime. # # Otherwise, if --shared-resources is used, the all resources will be # non-final, and an onResourcesLoaded() method will be generated too. # # Otherwise, all resources will be final, and no method will be generated. # rjava_build_options = resource_utils.RJavaBuildOptions() if options.shared_resources_whitelist: rjava_build_options.ExportSomeResources( options.shared_resources_whitelist) rjava_build_options.GenerateOnResourcesLoaded() elif options.shared_resources or options.app_as_shared_lib: rjava_build_options.ExportAllResources() rjava_build_options.GenerateOnResourcesLoaded() resource_utils.CreateRJavaFiles(build.srcjar_dir, None, r_txt_path, options.extra_res_packages, options.extra_r_text_files, rjava_build_options) if options.srcjar_out: build_utils.ZipDir(options.srcjar_out, build.srcjar_dir) if options.check_resources_pkg_id is not None: expected_id = options.check_resources_pkg_id package_id = _ExtractPackageIdFromApk(options.apk_path, options.aapt_path) if package_id != expected_id: raise Exception('Invalid package ID 0x%x (expected 0x%x)' % (package_id, expected_id)) if options.depfile: build_utils.WriteDepfile(options.depfile, options.apk_path, inputs=options.dependencies_res_zips + options.extra_r_text_files, add_pydeps=False)
def _OnStaleMd5(options): with resource_utils.BuildContext() as build: if options.sources: _CheckAllFilesListed(options.sources, options.resource_dirs) if options.r_text_in: r_txt_path = options.r_text_in else: # Extract dependencies to resolve @foo/type references into # dependent packages. dep_subdirs = resource_utils.ExtractDeps( options.dependencies_res_zips, build.deps_dir) _GenerateRTxt(options, dep_subdirs, build.gen_dir) r_txt_path = build.r_txt_path # 'aapt' doesn't generate any R.txt file if res/ was empty. if not os.path.exists(r_txt_path): build_utils.Touch(r_txt_path) if options.r_text_out: shutil.copyfile(r_txt_path, options.r_text_out) if options.srcjar_out: package = options.custom_package if not package and options.android_manifest: _, manifest_node, _ = manifest_utils.ParseManifest( options.android_manifest) package = manifest_utils.GetPackage(manifest_node) # Don't create a .java file for the current resource target when no # package name was provided (either by manifest or build rules). if package: # All resource IDs should be non-final here, but the # onResourcesLoaded() method should only be generated if # --shared-resources is used. rjava_build_options = resource_utils.RJavaBuildOptions() rjava_build_options.ExportAllResources() rjava_build_options.ExportAllStyleables() if options.shared_resources: rjava_build_options.GenerateOnResourcesLoaded() # Not passing in custom_root_package_name or parent to keep # file names unique. resource_utils.CreateRJavaFiles(build.srcjar_dir, package, r_txt_path, options.extra_res_packages, options.extra_r_text_files, rjava_build_options, options.srcjar_out) build_utils.ZipDir(options.srcjar_out, build.srcjar_dir) if options.resource_zip_out: ignore_pattern = resource_utils.AAPT_IGNORE_PATTERN if options.strip_drawables: ignore_pattern += ':*drawable*' _ZipResources(options.resource_dirs, options.resource_zip_out, ignore_pattern)
def _OnStaleMd5(options): with resource_utils.BuildContext() as build: if options.r_text_in: r_txt_path = options.r_text_in else: # Extract dependencies to resolve @foo/type references into # dependent packages. dep_subdirs = resource_utils.ExtractDeps( options.dependencies_res_zips, build.deps_dir) _GenerateRTxt(options, dep_subdirs, build.gen_dir) r_txt_path = build.r_txt_path # 'aapt' doesn't generate any R.txt file if res/ was empty. if not os.path.exists(r_txt_path): build_utils.Touch(r_txt_path) if options.r_text_out: shutil.copyfile(r_txt_path, options.r_text_out) if options.srcjar_out: package = options.custom_package if not package and options.android_manifest: package = resource_utils.ExtractPackageFromManifest( options.android_manifest) # Don't create a .java file for the current resource target when no # package name was provided (either by manifest or build rules). if package: # All resource IDs should be non-final here, but the # onResourcesLoaded() method should only be generated if # --shared-resources is used. rjava_build_options = resource_utils.RJavaBuildOptions() rjava_build_options.ExportAllResources() rjava_build_options.ExportAllStyleables() if options.shared_resources: rjava_build_options.GenerateOnResourcesLoaded() resource_utils.CreateRJavaFiles(build.srcjar_dir, package, r_txt_path, options.extra_res_packages, options.extra_r_text_files, rjava_build_options, options.srcjar_out) build_utils.ZipDir(options.srcjar_out, build.srcjar_dir) if options.resource_zip_out: _GenerateResourcesZip(options.resource_zip_out, options.resource_dirs, options.v14_skip, options.strip_drawables, build.temp_dir)
def _CreateRJava(rtxts, package_name, srcjar_out): with resource_utils.BuildContext() as build: _ConcatRTxts(rtxts, build.r_txt_path) rjava_build_options = resource_utils.RJavaBuildOptions() rjava_build_options.ExportAllResources() rjava_build_options.ExportAllStyleables() rjava_build_options.GenerateOnResourcesLoaded(fake=True) resource_utils.CreateRJavaFiles( build.srcjar_dir, package_name, build.r_txt_path, extra_res_packages=[], rjava_build_options=rjava_build_options, srcjar_out=srcjar_out, ignore_mismatched_values=True) build_utils.ZipDir(srcjar_out, build.srcjar_dir)
def _OnStaleMd5(options): with resource_utils.BuildContext() as build: dep_subdirs = resource_utils.ExtractDeps(options.dependencies_res_zips, build.deps_dir) _PackageApk(options, dep_subdirs, build.temp_dir, build.gen_dir, build.r_txt_path) r_txt_path = _WriteFinalRTxtFile(options, build.r_txt_path) package = resource_utils.ExtractPackageFromManifest( options.android_manifest) # If --shared-resources-whitelist is used, the all resources listed in # the corresponding R.txt file will be non-final, and an onResourcesLoaded() # will be generated to adjust them at runtime. # # Otherwise, if --shared-resources is used, the all resources will be # non-final, and an onResourcesLoaded() method will be generated too. # # Otherwise, all resources will be final, and no method will be generated. # rjava_build_options = resource_utils.RJavaBuildOptions() if options.shared_resources_whitelist: rjava_build_options.ExportSomeResources( options.shared_resources_whitelist) rjava_build_options.GenerateOnResourcesLoaded() elif options.shared_resources or options.app_as_shared_lib: rjava_build_options.ExportAllResources() rjava_build_options.GenerateOnResourcesLoaded() resource_utils.CreateRJavaFiles(build.srcjar_dir, package, r_txt_path, options.extra_res_packages, options.extra_r_text_files, rjava_build_options) if options.srcjar_out: build_utils.ZipDir(options.srcjar_out, build.srcjar_dir) if options.check_resources_pkg_id is not None: expected_id = options.check_resources_pkg_id package_id = _ExtractPackageIdFromApk(options.apk_path, options.aapt_path) if package_id != expected_id: raise Exception('Invalid package ID 0x%x (expected 0x%x)' % (package_id, expected_id))
def _CreateRJava(resource_zips, package_name, srcjar_out, rtxt_out): with resource_utils.BuildContext() as build: # TODO(mheikal): maybe we can rewrite resources_parser to just read out of a # zipfile without needing to extract first. dep_subdirs = resource_utils.ExtractDeps(resource_zips, build.deps_dir) resources_parser.RTxtGenerator(dep_subdirs).WriteRTxtFile(rtxt_out) rjava_build_options = resource_utils.RJavaBuildOptions() rjava_build_options.ExportAllResources() rjava_build_options.ExportAllStyleables() resource_utils.CreateRJavaFiles( build.srcjar_dir, package_name, rtxt_out, extra_res_packages=[], rjava_build_options=rjava_build_options, srcjar_out=srcjar_out) build_utils.ZipDir(srcjar_out, build.srcjar_dir)
def main(args): build_utils.InitLogging('RESOURCE_DEBUG') args = build_utils.ExpandFileArgs(args) options = _ParseArgs(args) path = options.arsc_path or options.proto_path debug_temp_resources_dir = os.environ.get('TEMP_RESOURCES_DIR') if debug_temp_resources_dir: path = os.path.join(debug_temp_resources_dir, os.path.basename(path)) else: # Use a deterministic temp directory since .pb files embed the absolute # path of resources: crbug.com/939984 path = path + '.tmpdir' build_utils.DeleteDirectory(path) build_utils.MakeDirectory(path) with resource_utils.BuildContext( temp_dir=path, keep_files=bool(debug_temp_resources_dir)) as build: manifest_package_name = _PackageApk(options, build) # If --shared-resources-whitelist is used, the all resources listed in # the corresponding R.txt file will be non-final, and an onResourcesLoaded() # will be generated to adjust them at runtime. # # Otherwise, if --shared-resources is used, the all resources will be # non-final, and an onResourcesLoaded() method will be generated too. # # Otherwise, all resources will be final, and no method will be generated. # rjava_build_options = resource_utils.RJavaBuildOptions() if options.shared_resources_whitelist: rjava_build_options.ExportSomeResources( options.shared_resources_whitelist) rjava_build_options.GenerateOnResourcesLoaded() elif options.shared_resources or options.app_as_shared_lib: rjava_build_options.ExportAllResources() rjava_build_options.GenerateOnResourcesLoaded() custom_root_package_name = options.r_java_root_package_name grandparent_custom_package_name = None if options.package_name and not options.arsc_package_name: # Feature modules have their own custom root package name and should # inherit from the appropriate base module package. This behaviour should # not be present for test apks with an apk under test. Thus, # arsc_package_name is used as it is only defined for test apks with an # apk under test. custom_root_package_name = options.package_name grandparent_custom_package_name = options.r_java_root_package_name if options.shared_resources or options.app_as_shared_lib: package_for_library = manifest_package_name else: package_for_library = None logging.debug('Creating R.srcjar') resource_utils.CreateRJavaFiles( build.srcjar_dir, package_for_library, build.r_txt_path, options.extra_res_packages, options.extra_r_text_files, rjava_build_options, options.srcjar_out, custom_root_package_name, grandparent_custom_package_name, options.extra_main_r_text_files) build_utils.ZipDir(build.srcjar_path, build.srcjar_dir) # Sanity check that the created resources have the expected package ID. logging.debug('Performing sanity check') if options.package_id: expected_id = options.package_id elif options.shared_resources: expected_id = 0 else: expected_id = 127 # == '0x7f'. _, package_id = resource_utils.ExtractArscPackage( options.aapt2_path, build.arsc_path if options.arsc_path else build.proto_path) if package_id != expected_id: raise Exception('Invalid package ID 0x%x (expected 0x%x)' % (package_id, expected_id)) logging.debug('Copying outputs') _WriteOutputs(options, build) if options.depfile: build_utils.WriteDepfile(options.depfile, options.srcjar_out, inputs=options.dependencies_res_zips + options.extra_r_text_files, add_pydeps=False)
def main(args): build_utils.InitLogging('RESOURCE_DEBUG') args = build_utils.ExpandFileArgs(args) options = _ParseArgs(args) if options.expected_file: actual_data = _CreateNormalizedManifestForVerification(options) diff_utils.CheckExpectations(actual_data, options) if options.only_verify_expectations: return path = options.arsc_path or options.proto_path debug_temp_resources_dir = os.environ.get('TEMP_RESOURCES_DIR') if debug_temp_resources_dir: path = os.path.join(debug_temp_resources_dir, os.path.basename(path)) else: # Use a deterministic temp directory since .pb files embed the absolute # path of resources: crbug.com/939984 path = path + '.tmpdir' build_utils.DeleteDirectory(path) with resource_utils.BuildContext( temp_dir=path, keep_files=bool(debug_temp_resources_dir)) as build: manifest_package_name = _PackageApk(options, build) # If --shared-resources-allowlist is used, all the resources listed in the # corresponding R.txt file will be non-final, and an onResourcesLoaded() # will be generated to adjust them at runtime. # # Otherwise, if --shared-resources is used, the all resources will be # non-final, and an onResourcesLoaded() method will be generated too. # # Otherwise, all resources will be final, and no method will be generated. # rjava_build_options = resource_utils.RJavaBuildOptions() if options.shared_resources_allowlist: rjava_build_options.ExportSomeResources( options.shared_resources_allowlist) rjava_build_options.GenerateOnResourcesLoaded() if options.shared_resources: # The final resources will only be used in WebLayer, so hardcode the # package ID to be what WebLayer expects. rjava_build_options.SetFinalPackageId( protoresources.SHARED_LIBRARY_HARDCODED_ID) elif options.shared_resources or options.app_as_shared_lib: rjava_build_options.ExportAllResources() rjava_build_options.GenerateOnResourcesLoaded() custom_root_package_name = options.r_java_root_package_name grandparent_custom_package_name = None # Always generate an R.java file for the package listed in # AndroidManifest.xml because this is where Android framework looks to find # onResourcesLoaded() for shared library apks. While not actually necessary # for application apks, it also doesn't hurt. apk_package_name = manifest_package_name if options.package_name and not options.arsc_package_name: # Feature modules have their own custom root package name and should # inherit from the appropriate base module package. This behaviour should # not be present for test apks with an apk under test. Thus, # arsc_package_name is used as it is only defined for test apks with an # apk under test. custom_root_package_name = options.package_name grandparent_custom_package_name = options.r_java_root_package_name # Feature modules have the same manifest package as the base module but # they should not create an R.java for said manifest package because it # will be created in the base module. apk_package_name = None logging.debug('Creating R.srcjar') resource_utils.CreateRJavaFiles( build.srcjar_dir, apk_package_name, build.r_txt_path, options.extra_res_packages, rjava_build_options, options.srcjar_out, custom_root_package_name, grandparent_custom_package_name, options.extra_main_r_text_files) build_utils.ZipDir(build.srcjar_path, build.srcjar_dir) # Sanity check that the created resources have the expected package ID. logging.debug('Performing sanity check') if options.package_id: expected_id = options.package_id elif options.shared_resources: expected_id = 0 else: expected_id = 127 # == '0x7f'. _, package_id = resource_utils.ExtractArscPackage( options.aapt2_path, build.arsc_path if options.arsc_path else build.proto_path) if package_id != expected_id: raise Exception('Invalid package ID 0x%x (expected 0x%x)' % (package_id, expected_id)) logging.debug('Copying outputs') _WriteOutputs(options, build) if options.depfile: depfile_deps = (options.dependencies_res_zips + options.dependencies_res_zip_overlays + options.extra_main_r_text_files + options.include_resources) build_utils.WriteDepfile(options.depfile, options.srcjar_out, depfile_deps)
def main(args): args = build_utils.ExpandFileArgs(args) options = _ParseArgs(args) debug_temp_resources_dir = os.environ.get(_ENV_DEBUG_VARIABLE) if debug_temp_resources_dir: debug_temp_resources_dir = os.path.join( debug_temp_resources_dir, os.path.basename(options.arsc_path)) build_utils.DeleteDirectory(debug_temp_resources_dir) build_utils.MakeDirectory(debug_temp_resources_dir) with resource_utils.BuildContext(debug_temp_resources_dir) as build: _PackageApk(options, build) # If --shared-resources-whitelist is used, the all resources listed in # the corresponding R.txt file will be non-final, and an onResourcesLoaded() # will be generated to adjust them at runtime. # # Otherwise, if --shared-resources is used, the all resources will be # non-final, and an onResourcesLoaded() method will be generated too. # # Otherwise, all resources will be final, and no method will be generated. # rjava_build_options = resource_utils.RJavaBuildOptions() if options.shared_resources_whitelist: rjava_build_options.ExportSomeResources( options.shared_resources_whitelist) rjava_build_options.GenerateOnResourcesLoaded() elif options.shared_resources or options.app_as_shared_lib: rjava_build_options.ExportAllResources() rjava_build_options.GenerateOnResourcesLoaded() custom_root_package_name = None grandparent_custom_package_name = None if options.arsc_package_name: # This is for test apks with an apk under test. If we have an apk under # test we don't want to name the resources for both the test apk and the # under test apk "base". This special case lets us name the test apk's # resources "test". custom_root_package_name = 'test' elif options.package_name: # If there exists a custom package name such as vr for a feature module, # pass the name and base for the parent_custom_root_package_name. custom_root_package_name = options.package_name grandparent_custom_package_name = 'base' else: # No grandparent_custom_package_name for base module custom_root_package_name = 'base' resource_utils.CreateRJavaFiles( build.srcjar_dir, None, build.r_txt_path, options.extra_res_packages, options.extra_r_text_files, rjava_build_options, options.srcjar_out, custom_root_package_name, grandparent_custom_package_name) build_utils.ZipDir(build.srcjar_path, build.srcjar_dir) # Sanity check that the created resources have the expected package ID. expected_id = _PackageIdFromOptions(options) if expected_id is None: expected_id = '0x00' if options.shared_resources else '0x7f' expected_id = int(expected_id, 16) _, package_id = resource_utils.ExtractArscPackage( options.aapt2_path, build.arsc_path if options.arsc_path else build.proto_path) if package_id != expected_id: raise Exception('Invalid package ID 0x%x (expected 0x%x)' % (package_id, expected_id)) _WriteOutputs(options, build) if options.depfile: build_utils.WriteDepfile(options.depfile, options.srcjar_out, inputs=options.dependencies_res_zips + options.extra_r_text_files, add_pydeps=False)
def main(args): args = build_utils.ExpandFileArgs(args) options = _ParseArgs(args) debug_temp_resources_dir = os.environ.get(_ENV_DEBUG_VARIABLE) if debug_temp_resources_dir: debug_temp_resources_dir = os.path.join( debug_temp_resources_dir, os.path.basename(options.arsc_path)) build_utils.DeleteDirectory(debug_temp_resources_dir) build_utils.MakeDirectory(debug_temp_resources_dir) with resource_utils.BuildContext(debug_temp_resources_dir) as build: _PackageApk(options, build) # If --shared-resources-whitelist is used, the all resources listed in # the corresponding R.txt file will be non-final, and an onResourcesLoaded() # will be generated to adjust them at runtime. # # Otherwise, if --shared-resources is used, the all resources will be # non-final, and an onResourcesLoaded() method will be generated too. # # Otherwise, all resources will be final, and no method will be generated. # rjava_build_options = resource_utils.RJavaBuildOptions() if options.shared_resources_whitelist: rjava_build_options.ExportSomeResources( options.shared_resources_whitelist) rjava_build_options.GenerateOnResourcesLoaded() elif options.shared_resources or options.app_as_shared_lib: rjava_build_options.ExportAllResources() rjava_build_options.GenerateOnResourcesLoaded() custom_root_package_name = options.r_java_root_package_name grandparent_custom_package_name = None if options.package_name and not options.arsc_package_name: # Feature modules have their own custom root package name and should # inherit from the appropriate base module package. This behaviour should # not be present for test apks with an apk under test. Thus, # arsc_package_name is used as it is only defined for test apks with an # apk under test. custom_root_package_name = options.package_name grandparent_custom_package_name = options.r_java_root_package_name resource_utils.CreateRJavaFiles( build.srcjar_dir, None, build.r_txt_path, options.extra_res_packages, options.extra_r_text_files, rjava_build_options, options.srcjar_out, custom_root_package_name, grandparent_custom_package_name, options.extra_main_r_text_files) build_utils.ZipDir(build.srcjar_path, build.srcjar_dir) # Sanity check that the created resources have the expected package ID. expected_id = _PackageIdFromOptions(options) if expected_id is None: expected_id = '0x00' if options.shared_resources else '0x7f' expected_id = int(expected_id, 16) _, package_id = resource_utils.ExtractArscPackage( options.aapt2_path, build.arsc_path if options.arsc_path else build.proto_path) if package_id != expected_id: raise Exception('Invalid package ID 0x%x (expected 0x%x)' % (package_id, expected_id)) _WriteOutputs(options, build) if options.depfile: build_utils.WriteDepfile(options.depfile, options.srcjar_out, inputs=options.dependencies_res_zips + options.extra_r_text_files, add_pydeps=False)