Esempio n. 1
0
def _GenerateResourcesZip(output_resource_zip, input_resource_dirs, v14_skip,
                          strip_drawables, temp_dir):
    """Generate a .resources.zip file fron a list of input resource dirs.

  Args:
    output_resource_zip: Path to the output .resources.zip file.
    input_resource_dirs: A list of input resource directories.
    v14_skip: If False, then v14-compatible resource will also be
      generated in |{temp_dir}/v14| and added to the final zip.
    temp_dir: Path to temporary directory.
  """
    if not v14_skip:
        # Generate v14-compatible resources in temp_dir.
        v14_dir = os.path.join(temp_dir, 'v14')
        build_utils.MakeDirectory(v14_dir)

        for resource_dir in input_resource_dirs:
            generate_v14_compatible_resources.GenerateV14Resources(
                resource_dir, v14_dir)

        input_resource_dirs.append(v14_dir)

    ignore_pattern = _AAPT_IGNORE_PATTERN
    if strip_drawables:
        ignore_pattern += ':*drawable*'
    _ZipResources(input_resource_dirs, output_resource_zip, ignore_pattern)
Esempio n. 2
0
def _PackageLibrary(options, dep_subdirs, temp_dir, gen_dir):
    v14_dir = os.path.join(temp_dir, 'v14')
    build_utils.MakeDirectory(v14_dir)

    # 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 = [
        options.aapt_path,
        'package',
        '-m',
        '-M',
        _EMPTY_ANDROID_MANIFEST_PATH,
        '--no-crunch',
        '--auto-add-overlay',
        '--no-version-vectors',
        '-I',
        options.android_sdk_jar,
        '--output-text-symbols',
        gen_dir,
        '-J',
        gen_dir,  # Required for R.txt generation.
        '--ignore-assets',
        build_utils.AAPT_IGNORE_PATTERN
    ]

    # Adding all dependencies as sources is necessary for @type/foo references
    # to symbols within dependencies to resolve. However, it has the side-effect
    # that all Java symbols from dependencies are copied into the new R.java.
    # E.g.: It enables an arguably incorrect usage of
    # "mypackage.R.id.lib_symbol" where "libpackage.R.id.lib_symbol" would be
    # more correct. This is just how Android works.
    for d in dep_subdirs:
        package_command += ['-S', d]

    input_resource_dirs = options.resource_dirs

    for d in input_resource_dirs:
        package_command += ['-S', d]

    if not options.v14_skip:
        for resource_dir in input_resource_dirs:
            generate_v14_compatible_resources.GenerateV14Resources(
                resource_dir, v14_dir)

    # This is the list of directories with resources to put in the final .zip
    zip_resource_dirs = input_resource_dirs + [v14_dir]

    if options.resource_zip_out:
        _ZipResources(zip_resource_dirs, options.resource_zip_out,
                      build_utils.AAPT_IGNORE_PATTERN)

    # Only creates an R.txt
    build_utils.CheckOutput(package_command,
                            print_stdout=False,
                            print_stderr=False)
Esempio n. 3
0
def _PackageLibrary(options, package_command, temp_dir):
    v14_dir = os.path.join(temp_dir, 'v14')
    build_utils.MakeDirectory(v14_dir)

    input_resource_dirs = options.resource_dirs

    for d in input_resource_dirs:
        package_command += ['-S', d]

    if not options.v14_skip:
        for resource_dir in input_resource_dirs:
            generate_v14_compatible_resources.GenerateV14Resources(
                resource_dir, v14_dir)

    # 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, input_dir 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)
        _CrunchDirectory(options.aapt_path, input_dir, crunch_dir)

    if options.resource_zip_out:
        _ZipResources(zip_resource_dirs, options.resource_zip_out)

    # Only creates an R.txt
    build_utils.CheckOutput(package_command,
                            print_stdout=False,
                            print_stderr=False)
def main():
    options = ParseArgs()
    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)

    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)
def main():
    args = build_utils.ExpandFileArgs(sys.argv[1:])

    options = ParseArgs(args)
    android_jar = os.path.join(options.android_sdk, 'android.jar')
    aapt = os.path.join(options.android_sdk_tools, 'aapt')

    input_files = []

    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)

        gen_dir = os.path.join(temp_dir, 'gen')
        build_utils.MakeDirectory(gen_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)

        dep_zips = build_utils.ParseGypList(options.dependencies_res_zips)
        input_files += dep_zips
        dep_subdirs = []
        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)
            dep_subdirs.append(subdir)

        # 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',
            gen_dir, '-J', gen_dir
        ]

        for d in input_resource_dirs:
            package_command += ['-S', d]

        for d in dep_subdirs:
            package_command += ['-S', d]

        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(
                gen_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)

        ZipResources(zip_resource_dirs, options.resource_zip_out)

        if options.all_resources_zip_out:
            ZipResources(zip_resource_dirs + dep_subdirs,
                         options.all_resources_zip_out)

        if options.R_dir:
            build_utils.DeleteDirectory(options.R_dir)
            shutil.copytree(gen_dir, options.R_dir)
        else:
            build_utils.ZipDir(options.srcjar_out, gen_dir)

    if options.depfile:
        input_files += build_utils.GetPythonDependencies()
        build_utils.WriteDepfile(options.depfile, input_files)

    if options.stamp:
        build_utils.Touch(options.stamp)
def _OnStaleMd5(options):
  aapt = options.aapt_path
  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)

    gen_dir = os.path.join(temp_dir, 'gen')
    build_utils.MakeDirectory(gen_dir)

    input_resource_dirs = options.resource_dirs

    if not options.v14_skip:
      for resource_dir in input_resource_dirs:
        generate_v14_compatible_resources.GenerateV14Resources(
            resource_dir,
            v14_dir)

    dep_zips = options.dependencies_res_zips
    dep_subdirs = []
    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)
      dep_subdirs.append(subdir)

    # 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',
                       '--no-version-vectors',
                       '-I', options.android_sdk_jar,
                       '--output-text-symbols', gen_dir,
                       '-J', gen_dir,
                       '--ignore-assets', build_utils.AAPT_IGNORE_PATTERN]

    for d in input_resource_dirs:
      package_command += ['-S', d]

    for d in dep_subdirs:
      package_command += ['-S', d]

    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]
    if options.shared_resources:
      package_command.append('--shared-lib')
    if options.app_as_shared_lib:
      package_command.append('--app-as-shared-lib')
    build_utils.CheckOutput(package_command, print_stderr=False)

    if options.extra_res_packages:
      CreateExtraRJavaFiles(
          gen_dir,
          options.extra_res_packages,
          options.extra_r_text_files,
          options.shared_resources or options.app_as_shared_lib,
          options.include_all_resources)

    # 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, input_dir 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)
      CrunchDirectory(aapt, input_dir, crunch_dir)

    ZipResources(zip_resource_dirs, options.resource_zip_out)

    if options.all_resources_zip_out:
      CombineZips([options.resource_zip_out] + dep_zips,
                  options.all_resources_zip_out)

    if options.R_dir:
      build_utils.DeleteDirectory(options.R_dir)
      shutil.copytree(gen_dir, options.R_dir)
    else:
      build_utils.ZipDir(options.srcjar_out, gen_dir)

    if options.r_text_out:
      r_text_path = os.path.join(gen_dir, 'R.txt')
      if os.path.exists(r_text_path):
        shutil.copyfile(r_text_path, options.r_text_out)
      else:
        open(options.r_text_out, 'w').close()
Esempio n. 7
0
def _OnStaleMd5(options):
    aapt = options.aapt_path
    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)

        gen_dir = os.path.join(temp_dir, 'gen')
        build_utils.MakeDirectory(gen_dir)
        r_txt_path = os.path.join(gen_dir, 'R.txt')
        srcjar_dir = os.path.join(temp_dir, 'java')

        input_resource_dirs = options.resource_dirs

        if not options.v14_skip:
            for resource_dir in input_resource_dirs:
                generate_v14_compatible_resources.GenerateV14Resources(
                    resource_dir, v14_dir)

        dep_zips = options.dependencies_res_zips
        dep_subdirs = []
        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)
            dep_subdirs.append(subdir)

        # 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',
            '--no-version-vectors',
            '-I',
            options.android_sdk_jar,
            '--output-text-symbols',
            gen_dir,
            '-J',
            gen_dir,  # Required for R.txt generation.
            '--ignore-assets',
            build_utils.AAPT_IGNORE_PATTERN
        ]

        # aapt supports only the "--include-all-resources" mode, where each R.java
        # file ends up with all symbols, rather than only those that it had at the
        # time it was originally generated. This subtle difference makes no
        # difference when compiling, but can lead to increased unused symbols in the
        # resulting R.class files.
        # TODO(agrieve): See if proguard makes this difference actually translate
        # into a size difference. If not, we can delete all of our custom R.java
        # template code above (and make include_all_resources the default).
        if options.include_all_resources:
            srcjar_dir = gen_dir
            if options.extra_res_packages:
                colon_separated = ':'.join(options.extra_res_packages)
                package_command += ['--extra-packages', colon_separated]
            if options.non_constant_id:
                package_command.append('--non-constant-id')
            if options.custom_package:
                package_command += ['--custom-package', options.custom_package]
            if options.shared_resources:
                package_command.append('--shared-lib')
            if options.app_as_shared_lib:
                package_command.append('--app-as-shared-lib')

        for d in input_resource_dirs:
            package_command += ['-S', d]

        # Adding all dependencies as sources is necessary for @type/foo references
        # to symbols within dependencies to resolve. However, it has the side-effect
        # that all Java symbols from dependencies are copied into the new R.java.
        # E.g.: It enables an arguably incorrect usage of
        # "mypackage.R.id.lib_symbol" where "libpackage.R.id.lib_symbol" would be
        # more correct. This is just how Android works.
        for d in dep_subdirs:
            package_command += ['-S', d]

        if options.proguard_file:
            package_command += ['-G', options.proguard_file]
        if options.proguard_file_main_dex:
            package_command += ['-D', options.proguard_file_main_dex]
        build_utils.CheckOutput(package_command, print_stderr=False)

        # When an empty res/ directory is passed, aapt does not write an R.txt.
        if not os.path.exists(r_txt_path):
            build_utils.Touch(r_txt_path)

        if not options.include_all_resources:
            # --include-all-resources can only be specified for generating final R
            # classes for APK. It makes no sense for APK to have pre-generated R.txt
            # though, because aapt-generated already lists all available resources.
            if options.r_text_in:
                r_txt_path = options.r_text_in

            packages = list(options.extra_res_packages)
            r_txt_files = list(options.extra_r_text_files)

            cur_package = options.custom_package
            if not options.custom_package:
                cur_package = _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),
            # - there was already a dependent android_resources() with the same
            #   package (occurs mostly when an apk target and resources target share
            #   an AndroidManifest.xml)
            if cur_package != 'org.dummy' and cur_package not in packages:
                packages.append(cur_package)
                r_txt_files.append(r_txt_path)

            if packages:
                shared_resources = options.shared_resources or options.app_as_shared_lib
                CreateRJavaFiles(srcjar_dir, r_txt_path, packages, r_txt_files,
                                 shared_resources)

        # 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, input_dir 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)
            CrunchDirectory(aapt, input_dir, crunch_dir)

        ZipResources(zip_resource_dirs, options.resource_zip_out)

        if options.all_resources_zip_out:
            CombineZips([options.resource_zip_out] + dep_zips,
                        options.all_resources_zip_out)

        if options.R_dir:
            build_utils.DeleteDirectory(options.R_dir)
            shutil.copytree(srcjar_dir, options.R_dir)
        else:
            build_utils.ZipDir(options.srcjar_out, srcjar_dir)

        if options.r_text_out:
            shutil.copyfile(r_txt_path, options.r_text_out)