Exemple #1
0
def GenerateJNIHeader(java_file_paths, output_file, args):
    """Generate a header file including two registration functions.

  Forward declares all JNI registration functions created by jni_generator.py.
  Calls the functions in RegisterMainDexNatives() if they are main dex. And
  calls them in RegisterNonMainDexNatives() if they are non-main dex.

  Args:
      java_file_paths: A list of java file paths.
      output_file: A relative path to output file.
      args: All input arguments.
  """
    # Without multiprocessing, script takes ~13 seconds for chrome_public_apk
    # on a z620. With multiprocessing, takes ~2 seconds.
    pool = multiprocessing.Pool()
    paths = (p for p in java_file_paths if p not in args.no_register_java)
    results = [d for d in pool.imap_unordered(_DictForPath, paths) if d]
    pool.close()

    # Sort to make output deterministic.
    results.sort(key=lambda d: d['FULL_CLASS_NAME'])

    combined_dict = {}
    for key in MERGEABLE_KEYS:
        combined_dict[key] = ''.join(d.get(key, '') for d in results)

    header_content = CreateFromDict(combined_dict)
    if output_file:
        jni_generator.WriteOutput(output_file, header_content)
    else:
        print header_content
Exemple #2
0
def GenerateJNIHeader(java_file_paths, output_file, args):
    """Generate a header file including two registration functions.

  Forward declares all JNI registration functions created by jni_generator.py.
  Calls the functions in RegisterMainDexNatives() if they are main dex. And
  calls them in RegisterNonMainDexNatives() if they are non-main dex.

  Args:
      java_file_paths: A list of java file paths.
      output_file: A relative path to output file.
      args: All input arguments.
  """
    registration_dict = {}
    # Sort the file list to make sure the order is deterministic.
    java_file_paths.sort()
    for path in java_file_paths:
        if path in args.no_register_java:
            continue
        with open(path) as f:
            contents = jni_generator.RemoveComments(f.read())
        natives = jni_generator.ExtractNatives(contents, 'long')
        if len(natives) == 0:
            continue
        namespace = jni_generator.ExtractJNINamespace(contents)
        fully_qualified_class = jni_generator.ExtractFullyQualifiedJavaClassName(
            path, contents)
        jni_params = jni_generator.JniParams(fully_qualified_class)
        jni_params.ExtractImportsAndInnerClasses(contents)
        main_dex = jni_generator.IsMainDexJavaClass(contents)
        header_generator = HeaderGenerator(namespace, fully_qualified_class,
                                           natives, jni_params,
                                           registration_dict, main_dex)
        header_generator.AddContent()

    header_content = CreateFromDict(registration_dict)
    if output_file:
        jni_generator.WriteOutput(output_file, header_content)
    else:
        print header_content
def GenerateJNIHeader(java_file_paths, output_file, args):
  """Generate a header file including two registration functions.

  Forward declares all JNI registration functions created by jni_generator.py.
  Calls the functions in RegisterMainDexNatives() if they are main dex. And
  calls them in RegisterNonMainDexNatives() if they are non-main dex.

  Args:
      java_file_paths: A list of java file paths.
      output_file: A relative path to output file.
      args: All input arguments.
  """
  registration_dict = {
      'FORWARD_DECLARATIONS': '',
      'REGISTER_MAIN_DEX_NATIVES': '',
      'REGISTER_NON_MAIN_DEX_NATIVES': ''
  }
  # Sort the file list to make sure the order is deterministic.
  java_file_paths.sort()
  for path in java_file_paths:
    if path in args.no_register_java:
      continue
    with open(path) as f:
      contents = f.read()
    natives = jni_generator.ExtractNatives(contents, 'long')
    if len(natives) == 0:
      continue
    fully_qualified_class = jni_generator.ExtractFullyQualifiedJavaClassName(
        path, contents)
    main_dex = jni_generator.IsMainDexJavaClass(contents)
    header_generator = HeaderGenerator(
        fully_qualified_class, registration_dict, main_dex)
    registration_dict = header_generator.GetContent()

  header_content = CreateFromDict(registration_dict)
  if output_file:
    jni_generator.WriteOutput(output_file, header_content)
  else:
    print header_content