parameters: (options, build)
return_statement: return desired_manifest_package_name
docstring: Compile and link resources with aapt2.
  
    Args:
      options: The command-line options.
      build: BuildContext object.
    Returns:
      The manifest package name for the APK.
docstring_summary: Compile and link resources with aapt2.
docstring_tokens: Compile and link resources with aapt2 .
function: def _PackageApk(options, build):
    """Compile and link resources with aapt2.
  
    Args:
      options: The command-line options.
      build: BuildContext object.
    Returns:
      The manifest package name for the APK.
    """
    logging.debug('Extracting resource .zips')
    dep_subdirs = []
    dep_subdir_overlay_set = set()
    for dependency_res_zip in options.dependencies_res_zips:
      extracted_dep_subdirs = resource_utils.ExtractDeps([dependency_res_zip],
                                                         build.deps_dir)
      dep_subdirs += extracted_dep_subdirs
      if dependency_res_zip in options.dependencies_res_zip_overlays:
        dep_subdir_overlay_set.update(extracted_dep_subdirs)
  
    logging.debug('Applying locale transformations')
    path_info = resource_utils.ResourceInfoFile()
    _RenameLocaleResourceDirs(dep_subdirs, path_info)
  
    logging.debug('Applying file-based exclusions')
    keep_predicate = _CreateKeepPredicate(options.resource_exclusion_regex,
                                          options.resource_exclusion_exceptions)
    png_paths = _FilterResourceFiles(dep_subdirs, keep_predicate)
  
    if options.locale_allowlist or options.shared_resources_allowlist_locales:
      logging.debug('Applying locale-based string exclusions')
      _RemoveUnwantedLocalizedStrings(dep_subdirs, options)
  
    if png_paths and options.png_to_webp:
      logging.debug('Converting png->webp')
      _ConvertToWebP(options.webp_binary, png_paths, path_info,
                     options.webp_cache_dir)
    logging.debug('Applying drawable transformations')
    for directory in dep_subdirs:
      _MoveImagesToNonMdpiFolders(directory, path_info)
      _RemoveImageExtensions(directory, path_info)
  
    logging.debug('Running aapt2 compile')
    exclusion_rules = [x.split(':', 1) for x in options.values_filter_rules]
    partials = _CompileDeps(options.aapt2_path, dep_subdirs,
                            dep_subdir_overlay_set, build.temp_dir,
                            exclusion_rules)
  
    link_command = [
        options.aapt2_path,
        'link',
        '--auto-add-overlay',
        '--no-version-vectors',
        # Set SDK versions in case they are not set in the Android manifest.
        '--min-sdk-version',
        options.min_sdk_version,
        '--target-sdk-version',
        options.target_sdk_version,
        '--output-text-symbols',
        build.r_txt_path,
    ]
  
    for j in options.include_resources:
      link_command += ['-I', j]
    if options.version_code:
      link_command += ['--version-code', options.version_code]
    if options.version_name:
      link_command += ['--version-name', options.version_name]
    if options.proguard_file:
      link_command += ['--proguard', build.proguard_path]
      link_command += ['--proguard-minimal-keep-rules']
    if options.proguard_file_main_dex:
      link_command += ['--proguard-main-dex', build.proguard_main_dex_path]
    if options.emit_ids_out:
      link_command += ['--emit-ids', build.emit_ids_path]
  
    # Note: only one of --proto-format, --shared-lib or --app-as-shared-lib
    #       can be used with recent versions of aapt2.
    if options.shared_resources:
      link_command.append('--shared-lib')
  
    if options.no_xml_namespaces:
      link_command.append('--no-xml-namespaces')
  
    if options.package_id:
      link_command += [
          '--package-id',
          hex(options.package_id),
          '--allow-reserved-package-id',
      ]
  
    fixed_manifest, desired_manifest_package_name, fixed_manifest_package = (
        _FixManifest(options, build.temp_dir))
    if options.rename_manifest_package:
      desired_manifest_package_name = options.rename_manifest_package
  
    link_command += [
        '--manifest', fixed_manifest, '--rename-manifest-package',
        desired_manifest_package_name
    ]
  
    # Creates a .zip with AndroidManifest.xml, resources.arsc, res/*
    # Also creates R.txt
    if options.use_resource_ids_path:
      _CreateStableIdsFile(options.use_resource_ids_path, build.stable_ids_path,
                           fixed_manifest_package)
      link_command += ['--stable-ids', build.stable_ids_path]
  
    link_command += partials
  
    # We always create a binary arsc file first, then convert to proto, so flags
    # such as --shared-lib can be supported.
    link_command += ['-o', build.arsc_path]
  
    logging.debug('Starting: aapt2 link')
    link_proc = subprocess.Popen(link_command)
  
    # Create .res.info file in parallel.
    _CreateResourceInfoFile(path_info, build.info_path,
                            options.dependencies_res_zips)
    logging.debug('Created .res.info file')
  
    exit_code = link_proc.wait()
    logging.debug('Finished: aapt2 link')
  
    if options.shared_resources:
      logging.debug('Resolving styleables in R.txt')
      # Need to resolve references because unused resource removal tool does not
      # support references in R.txt files.
      resource_utils.ResolveStyleableReferences(build.r_txt_path)
  
    if exit_code:
      raise subprocess.CalledProcessError(exit_code, link_command)
  
    if options.proguard_file and (options.shared_resources
                                  or options.app_as_shared_lib):
      # Make sure the R class associated with the manifest package does not have
      # its onResourcesLoaded method obfuscated or removed, so that the framework
      # can call it in the case where the APK is being loaded as a library.
      with open(build.proguard_path, 'a') as proguard_file:
        keep_rule = '''
                    -keep,allowoptimization class {package}.R {{
                      public static void onResourcesLoaded(int);
                    }}
                    '''.format(package=desired_manifest_package_name)
        proguard_file.write(textwrap.dedent(keep_rule))
  
    logging.debug('Running aapt2 convert')
    build_utils.CheckOutput([
        options.aapt2_path, 'convert', '--output-format', 'proto', '-o',
        build.proto_path, build.arsc_path
    ])
  
    # Workaround for b/147674078. This is only needed for WebLayer and does not
    # affect WebView usage, since WebView does not used dynamic attributes.
    if options.shared_resources:
      logging.debug('Hardcoding dynamic attributes')
      protoresources.HardcodeSharedLibraryDynamicAttributes(
          build.proto_path, options.is_bundle_module,
          options.shared_resources_allowlist)
  
      build_utils.CheckOutput([
          options.aapt2_path, 'convert', '--output-format', 'binary', '-o',
          build.arsc_path, build.proto_path
      ])
  
    return desired_manifest_package_name
function_tokens: def _PackageApk ( options , build ) : logging . debug ( 'Extracting resource .zips' ) dep_subdirs = [ ] dep_subdir_overlay_set = set ( ) for dependency_res_zip in options . dependencies_res_zips : extracted_dep_subdirs = resource_utils . ExtractDeps ( [ dependency_res_zip ] , build . deps_dir ) dep_subdirs += extracted_dep_subdirs if dependency_res_zip in options . dependencies_res_zip_overlays : dep_subdir_overlay_set . update ( extracted_dep_subdirs ) logging . debug ( 'Applying locale transformations' ) path_info = resource_utils . ResourceInfoFile ( ) _RenameLocaleResourceDirs ( dep_subdirs , path_info ) logging . debug ( 'Applying file-based exclusions' ) keep_predicate = _CreateKeepPredicate ( options . resource_exclusion_regex , options . resource_exclusion_exceptions ) png_paths = _FilterResourceFiles ( dep_subdirs , keep_predicate ) if options . locale_allowlist or options . shared_resources_allowlist_locales : logging . debug ( 'Applying locale-based string exclusions' ) _RemoveUnwantedLocalizedStrings ( dep_subdirs , options ) if png_paths and options . png_to_webp : logging . debug ( 'Converting png->webp' ) _ConvertToWebP ( options . webp_binary , png_paths , path_info , options . webp_cache_dir ) logging . debug ( 'Applying drawable transformations' ) for directory in dep_subdirs : _MoveImagesToNonMdpiFolders ( directory , path_info ) _RemoveImageExtensions ( directory , path_info ) logging . debug ( 'Running aapt2 compile' ) exclusion_rules = [ x . split ( ':' , 1 ) for x in options . values_filter_rules ] partials = _CompileDeps ( options . aapt2_path , dep_subdirs , dep_subdir_overlay_set , build . temp_dir , exclusion_rules ) link_command = [ options . aapt2_path , 'link' , '--auto-add-overlay' , '--no-version-vectors' , # Set SDK versions in case they are not set in the Android manifest. '--min-sdk-version' , options . min_sdk_version , '--target-sdk-version' , options . target_sdk_version , '--output-text-symbols' , build . r_txt_path , ] for j in options . include_resources : link_command += [ '-I' , j ] if options . version_code : link_command += [ '--version-code' , options . version_code ] if options . version_name : link_command += [ '--version-name' , options . version_name ] if options . proguard_file : link_command += [ '--proguard' , build . proguard_path ] link_command += [ '--proguard-minimal-keep-rules' ] if options . proguard_file_main_dex : link_command += [ '--proguard-main-dex' , build . proguard_main_dex_path ] if options . emit_ids_out : link_command += [ '--emit-ids' , build . emit_ids_path ] # Note: only one of --proto-format, --shared-lib or --app-as-shared-lib #       can be used with recent versions of aapt2. if options . shared_resources : link_command . append ( '--shared-lib' ) if options . no_xml_namespaces : link_command . append ( '--no-xml-namespaces' ) if options . package_id : link_command += [ '--package-id' , hex ( options . package_id ) , '--allow-reserved-package-id' , ] fixed_manifest , desired_manifest_package_name , fixed_manifest_package = ( _FixManifest ( options , build . temp_dir ) ) if options . rename_manifest_package : desired_manifest_package_name = options . rename_manifest_package link_command += [ '--manifest' , fixed_manifest , '--rename-manifest-package' , desired_manifest_package_name ] # Creates a .zip with AndroidManifest.xml, resources.arsc, res/* # Also creates R.txt if options . use_resource_ids_path : _CreateStableIdsFile ( options . use_resource_ids_path , build . stable_ids_path , fixed_manifest_package ) link_command += [ '--stable-ids' , build . stable_ids_path ] link_command += partials # We always create a binary arsc file first, then convert to proto, so flags # such as --shared-lib can be supported. link_command += [ '-o' , build . arsc_path ] logging . debug ( 'Starting: aapt2 link' ) link_proc = subprocess . Popen ( link_command ) # Create .res.info file in parallel. _CreateResourceInfoFile ( path_info , build . info_path , options . dependencies_res_zips ) logging . debug ( 'Created .res.info file' ) exit_code = link_proc . wait ( ) logging . debug ( 'Finished: aapt2 link' ) if options . shared_resources : logging . debug ( 'Resolving styleables in R.txt' ) # Need to resolve references because unused resource removal tool does not # support references in R.txt files. resource_utils . ResolveStyleableReferences ( build . r_txt_path ) if exit_code : raise subprocess . CalledProcessError ( exit_code , link_command ) if options . proguard_file and ( options . shared_resources or options . app_as_shared_lib ) : # Make sure the R class associated with the manifest package does not have # its onResourcesLoaded method obfuscated or removed, so that the framework # can call it in the case where the APK is being loaded as a library. with open ( build . proguard_path , 'a' ) as proguard_file : keep_rule = '''
                    -keep,allowoptimization class {package}.R {{
                      public static void onResourcesLoaded(int);
                    }}
                    ''' . format ( package = desired_manifest_package_name ) proguard_file . write ( textwrap . dedent ( keep_rule ) ) logging . debug ( 'Running aapt2 convert' ) build_utils . CheckOutput ( [ options . aapt2_path , 'convert' , '--output-format' , 'proto' , '-o' , build . proto_path , build . arsc_path ] ) # Workaround for b/147674078. This is only needed for WebLayer and does not # affect WebView usage, since WebView does not used dynamic attributes. if options . shared_resources : logging . debug ( 'Hardcoding dynamic attributes' ) protoresources . HardcodeSharedLibraryDynamicAttributes ( build . proto_path , options . is_bundle_module , options . shared_resources_allowlist ) build_utils . CheckOutput ( [ options . aapt2_path , 'convert' , '--output-format' , 'binary' , '-o' , build . arsc_path , build . proto_path ] ) return desired_manifest_package_name