예제 #1
0
def _PackageApk(options, dep_subdirs, temp_dir, gen_dir, r_txt_path):
    _DuplicateZhResources(dep_subdirs)

    keep_predicate = _CreateKeepPredicate(dep_subdirs, options.exclude_xxxhdpi,
                                          options.xxxhdpi_whitelist)
    png_paths = []
    for directory in dep_subdirs:
        for f in build_utils.IterFiles(directory):
            if not keep_predicate(f):
                os.remove(f)
            elif f.endswith('.png'):
                png_paths.append(f)
    if png_paths and options.png_to_webp:
        _ConvertToWebP(options.webp_binary, png_paths)
    for directory in dep_subdirs:
        _MoveImagesToNonMdpiFolders(directory)

    link_command = _CreateLinkApkArgs(options)
    link_command += ['--output-text-symbols', r_txt_path]
    link_command += ['--java', gen_dir]

    fixed_manifest = _FixManifest(options, temp_dir)
    link_command += ['--manifest', fixed_manifest]

    partials = _CompileDeps(options.aapt_path, dep_subdirs, temp_dir)
    for partial in partials:
        link_command += ['-R', partial]

    # Creates a .zip with AndroidManifest.xml, resources.arsc, res/*
    # Also creates R.txt
    build_utils.CheckOutput(link_command,
                            print_stdout=False,
                            print_stderr=False)
예제 #2
0
def _PackageApk(options, package_command, dep_subdirs):
    _DuplicateZhResources(dep_subdirs)

    package_command += _CreatePackageApkArgs(options)

    keep_predicate = _CreateKeepPredicate(dep_subdirs, options.exclude_xxxhdpi,
                                          options.xxxhdpi_whitelist)
    png_paths = []
    for directory in dep_subdirs:
        for f in build_utils.IterFiles(directory):
            if not keep_predicate(f):
                os.remove(f)
            elif f.endswith('.png'):
                png_paths.append(f)
    if png_paths and options.png_to_webp:
        _ConvertToWebP(options.webp_binary, png_paths)
    for directory in dep_subdirs:
        _MoveImagesToNonMdpiFolders(directory)

    # Creates a .zip with AndroidManifest.xml, resources.arsc, res/*
    # Also creates R.txt
    build_utils.CheckOutput(package_command,
                            print_stdout=False,
                            print_stderr=False)

    if options.create_density_splits or options.language_splits:
        _CheckForMissedConfigs(options.apk_path, options.create_density_splits,
                               options.language_splits)

    if options.create_density_splits:
        _RenameDensitySplits(options.apk_path)
예제 #3
0
def _DuplicateZhResources(zip_files, temp_dir):
  new_zip_files = []
  for i, zip_path in enumerate(zip_files):
    # We use zh-TW resources for zh-HK (if we have zh-TW resources). If no
    # zh-TW resources exists (ex. api specific resources), then just use the
    # original zip.
    if not _ZipContains(zip_path, r'zh-r(HK|TW)'):
      new_zip_files.append(zip_path)
      continue

    resource_dir = os.path.join(temp_dir, str(i))
    new_zip_path = os.path.join(temp_dir, str(i) + '.zip')

    # Exclude existing zh-HK resources so that we don't mess up any resource
    # IDs. This can happen if the type IDs in the existing resources don't
    # align with ours (since they've already been generated at this point).
    build_utils.ExtractAll(
        zip_path, path=resource_dir, predicate=lambda x: not 'zh-rHK' in x)
    for path in build_utils.IterFiles(resource_dir):
      if 'zh-rTW' in path:
        hk_path = path.replace('zh-rTW', 'zh-rHK')
        build_utils.Touch(hk_path)
        shutil.copyfile(path, hk_path)

    build_utils.ZipDir(new_zip_path, resource_dir)
    new_zip_files.append(new_zip_path)
  return new_zip_files
예제 #4
0
def _DuplicateZhResources(resource_dirs):
  for resource_dir in resource_dirs:
    # We use zh-TW resources for zh-HK (if we have zh-TW resources).
    for path in build_utils.IterFiles(resource_dir):
      if 'zh-rTW' in path:
        hk_path = path.replace('zh-rTW', 'zh-rHK')
        build_utils.MakeDirectory(os.path.dirname(hk_path))
        shutil.copyfile(path, hk_path)
예제 #5
0
def _DuplicateZhResources(resource_dirs):
    """Duplicate Taiwanese resources into Hong-Kong specific directory."""
    renamed_paths = dict()
    for resource_dir in resource_dirs:
        # We use zh-TW resources for zh-HK (if we have zh-TW resources).
        for path in build_utils.IterFiles(resource_dir):
            if 'zh-rTW' in path:
                hk_path = path.replace('zh-rTW', 'zh-rHK')
                build_utils.MakeDirectory(os.path.dirname(hk_path))
                shutil.copyfile(path, hk_path)
                renamed_paths[os.path.relpath(hk_path,
                                              resource_dir)] = os.path.relpath(
                                                  path, resource_dir)
    return renamed_paths
예제 #6
0
def _PackageApk(options, dep_subdirs, temp_dir, gen_dir, r_txt_path):
    """Compile resources with aapt2 and generate intermediate .ap_ file.

  Args:
    options: The command-line options tuple. E.g. the generated apk
      will be written to |options.apk_path|.
    dep_subdirs: The list of directories where dependency resource zips
      were extracted (its content will be altered by this function).
    temp_dir: A temporary directory.
    gen_dir: Another temp directory where some intermediate files are
      generated.
    r_txt_path: The path where the R.txt file will written to.
  """
    renamed_paths = dict()
    renamed_paths.update(_DuplicateZhResources(dep_subdirs))

    keep_predicate = _CreateKeepPredicate(dep_subdirs, options.exclude_xxxhdpi,
                                          options.xxxhdpi_whitelist)
    png_paths = []
    for directory in dep_subdirs:
        for f in build_utils.IterFiles(directory):
            if not keep_predicate(f):
                os.remove(f)
            elif f.endswith('.png'):
                png_paths.append((f, directory))
    if png_paths and options.png_to_webp:
        renamed_paths.update(_ConvertToWebP(options.webp_binary, png_paths))
    for directory in dep_subdirs:
        renamed_paths.update(_MoveImagesToNonMdpiFolders(directory))

    link_command = _CreateLinkApkArgs(options)
    link_command += ['--output-text-symbols', r_txt_path]
    # TODO(digit): Is this below actually required for R.txt generation?
    link_command += ['--java', gen_dir]

    fixed_manifest = _FixManifest(options, temp_dir)
    link_command += ['--manifest', fixed_manifest]

    partials = _CompileDeps(options.aapt_path, dep_subdirs, temp_dir)
    for partial in partials:
        link_command += ['-R', partial]

    # Creates a .zip with AndroidManifest.xml, resources.arsc, res/*
    # Also creates R.txt
    build_utils.CheckOutput(link_command,
                            print_stdout=False,
                            print_stderr=False)
    _CreateResourceInfoFile(renamed_paths, options.apk_info_path,
                            options.dependencies_res_zips)
예제 #7
0
def _CreateKeepPredicate(resource_dirs, exclude_xxxhdpi, xxxhdpi_whitelist):
  if not exclude_xxxhdpi:
    # Do not extract dotfiles (e.g. ".gitkeep"). aapt ignores them anyways.
    return lambda path: os.path.basename(path)[0] != '.'

  # Returns False only for xxxhdpi non-mipmap, non-whitelisted drawables.
  naive_predicate = lambda path: (
      not re.search(r'[/-]xxxhdpi[/-]', path) or
      re.search(r'[/-]mipmap[/-]', path) or
      build_utils.MatchesGlob(path, xxxhdpi_whitelist))

  # Build a set of all non-xxxhdpi drawables to ensure that we never exclude any
  # xxxhdpi drawable that does not exist in other densities.
  non_xxxhdpi_drawables = set()
  for resource_dir in resource_dirs:
    for path in build_utils.IterFiles(resource_dir):
      if re.search(r'[/-]drawable[/-]', path) and naive_predicate(path):
        non_xxxhdpi_drawables.add(_ResourceNameFromPath(path))

  return lambda path: (naive_predicate(path) or
                       _ResourceNameFromPath(path) not in non_xxxhdpi_drawables)