Example #1
0
def AddLoggingParent(android_manifest, logging_parent_value):
    """Add logging parent as an additional <meta-data> tag.

  Args:
    android_manifest: A string representing AndroidManifest.xml
    logging_parent_value: A string representing the logging
      parent value.
  Raises:
    RuntimeError: Invalid manifest
  Returns:
    A path to modified AndroidManifest.xml
  """
    doc = minidom.parse(android_manifest)
    manifest = parse_manifest(doc)
    logging_parent_key = 'android.content.pm.LOGGING_PARENT'
    elems = get_children_with_tag(manifest, 'application')
    application = elems[0] if len(elems) == 1 else None
    if len(elems) > 1:
        raise RuntimeError('found multiple <application> tags')
    elif not elems:
        application = doc.createElement('application')
        indent = get_indent(manifest.firstChild, 1)
        first = manifest.firstChild
        manifest.insertBefore(doc.createTextNode(indent), first)
        manifest.insertBefore(application, first)

    indent = get_indent(application.firstChild, 2)
    last = application.lastChild
    if last is not None and last.nodeType != minidom.Node.TEXT_NODE:
        last = None

    if not find_child_with_attribute(application, 'meta-data', android_ns,
                                     'name', logging_parent_key):
        ul = doc.createElement('meta-data')
        ul.setAttributeNS(android_ns, 'android:name', logging_parent_key)
        ul.setAttributeNS(android_ns, 'android:value', logging_parent_value)
        application.insertBefore(doc.createTextNode(indent), last)
        application.insertBefore(ul, last)
        last = application.lastChild

    if last and last.nodeType != minidom.Node.TEXT_NODE:
        indent = get_indent(application.previousSibling, 1)
        application.appendChild(doc.createTextNode(indent))

    with tempfile.NamedTemporaryFile(delete=False) as temp:
        write_xml(temp, doc)
        return temp.name
Example #2
0
def main():
    """Program entry point."""
    try:
        args = parse_args()

        doc = minidom.parse(args.input)

        ensure_manifest_android_ns(doc)

        if args.raise_min_sdk_version:
            raise_min_sdk_version(doc, args.min_sdk_version,
                                  args.target_sdk_version, args.library)

        if args.uses_libraries:
            add_uses_libraries(doc, args.uses_libraries, True)

        if args.optional_uses_libraries:
            add_uses_libraries(doc, args.optional_uses_libraries, False)

        if args.uses_non_sdk_api:
            add_uses_non_sdk_api(doc)

        if args.logging_parent:
            add_logging_parent(doc, args.logging_parent)

        if args.use_embedded_dex:
            add_use_embedded_dex(doc)

        if args.has_no_code:
            set_has_code_to_false(doc)

        if args.test_only:
            set_test_only_flag_to_true(doc)

        if args.extract_native_libs is not None:
            add_extract_native_libs(doc, args.extract_native_libs)

        with open(args.output, 'w') as f:
            write_xml(f, doc)

    # pylint: disable=broad-except
    except Exception as err:
        print('error: ' + str(err), file=sys.stderr)
        sys.exit(-1)
Example #3
0
def main():
    """Program entry point."""
    try:
        args = parse_args()

        doc = minidom.parse(args.input)

        if args.enforce_uses_libraries:
            enforce_uses_libraries(doc, args.uses_libraries,
                                   args.optional_uses_libraries)

        if args.extract_target_sdk_version:
            print(extract_target_sdk_version(doc))

        if args.output:
            with open(args.output, 'wb') as f:
                write_xml(f, doc)

    # pylint: disable=broad-except
    except Exception as err:
        print('error: ' + str(err), file=sys.stderr)
        sys.exit(-1)
def main():
    """Program entry point."""
    try:
        args = parse_args()

        doc = minidom.parse(args.input)

        if args.package_name:
            if not args.manifest:
                raise RuntimeError(
                    '--manifest flag required for --package-name')
            manifest_doc = minidom.parse(args.manifest)
            overwrite_package_name(doc, manifest_doc, args.package_name)

        if args.test_file_name:
            overwrite_test_file_name(doc, args.test_file_name)

        with open(args.output, 'w') as f:
            write_xml(f, doc)

    # pylint: disable=broad-except
    except Exception as err:
        print('error: ' + str(err), file=sys.stderr)
        sys.exit(-1)
Example #5
0
def main():
    """Program entry point."""
    try:
        args = parse_args()

        # The input can be either an XML manifest or an APK, they are parsed and
        # processed in different ways.
        is_apk = args.input.endswith('.apk')
        if is_apk:
            aapt = args.aapt if args.aapt != None else "aapt"
            manifest = subprocess.check_output(
                [aapt, "dump", "badging", args.input])
        else:
            manifest = minidom.parse(args.input)

        if args.enforce_uses_libraries:
            # Load dexpreopt.config files and build a mapping from module names to
            # library names. This is necessary because build system addresses
            # libraries by their module name (`uses_libs`, `optional_uses_libs`,
            # `LOCAL_USES_LIBRARIES`, `LOCAL_OPTIONAL_LIBRARY_NAMES` all contain
            # module names), while the manifest addresses libraries by their name.
            mod_to_lib = load_dexpreopt_configs(args.dexpreopt_configs)
            required = translate_libnames(args.uses_libraries, mod_to_lib)
            optional = translate_libnames(args.optional_uses_libraries,
                                          mod_to_lib)

            # Check if the <uses-library> lists in the build system agree with those
            # in the manifest. Raise an exception on mismatch, unless the script was
            # passed a special parameter to suppress exceptions.
            errmsg = enforce_uses_libraries(manifest, required, optional,
                                            args.enforce_uses_libraries_relax,
                                            is_apk, args.input)

            # Create a status file that is empty on success, or contains an error
            # message on failure. When exceptions are suppressed, dexpreopt command
            # command will check file size to determine if the check has failed.
            if args.enforce_uses_libraries_status:
                with open(args.enforce_uses_libraries_status, 'w') as f:
                    if not errmsg == None:
                        f.write("%s\n" % errmsg)

        if args.extract_target_sdk_version:
            try:
                print(extract_target_sdk_version(manifest, is_apk))
            except:
                # Failed; don't crash, return "any" SDK version. This will result in
                # dexpreopt not adding any compatibility libraries.
                print(10000)

        if args.output:
            # XML output is supposed to be written only when this script is invoked
            # with XML input manifest, not with an APK.
            if is_apk:
                raise RuntimeError('cannot save APK manifest as XML')

            with open(args.output, 'wb') as f:
                write_xml(f, manifest)

    # pylint: disable=broad-except
    except Exception as err:
        print('error: ' + str(err), file=sys.stderr)
        sys.exit(-1)