Commit 9a658449 authored by Andrew Grieve's avatar Andrew Grieve Committed by Commit Bot

Android: Allow use of methods backported by R8.

List of them can be found at third_party/r8/backported_methods.txt

Bug: 1056751
Change-Id: I75506b8a8328552429838bca43cc53dd3a82396e
Cq-Do-Not-Cancel-Tryjobs: true
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2444180Reviewed-by: default avatarPeter Wen <wnwen@chromium.org>
Commit-Queue: Peter Wen <wnwen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813333}
parent 56d97ea7
...@@ -110,6 +110,31 @@ def _GenerateProjectFile(android_manifest, ...@@ -110,6 +110,31 @@ def _GenerateProjectFile(android_manifest,
return project return project
def _RetrieveBackportedMethods(backported_methods_path):
with open(backported_methods_path) as f:
methods = f.read().splitlines()
# Methods look like:
# java/util/Set#of(Ljava/lang/Object;)Ljava/util/Set;
# But error message looks like:
# Call requires API level R (current min is 21): java.util.Set#of [NewApi]
methods = (m.replace('/', '\\.') for m in methods)
methods = (m[:m.index('(')] for m in methods)
return sorted(set(methods))
def _GenerateConfigXmlTree(orig_config_path, backported_methods):
if orig_config_path:
root_node = ElementTree.parse(orig_config_path).getroot()
else:
root_node = ElementTree.fromstring('<lint/>')
issue_node = ElementTree.SubElement(root_node, 'issue')
issue_node.attrib['id'] = 'NewApi'
ignore_node = ElementTree.SubElement(issue_node, 'ignore')
ignore_node.attrib['regexp'] = '|'.join(backported_methods)
return root_node
def _GenerateAndroidManifest(original_manifest_path, extra_manifest_paths, def _GenerateAndroidManifest(original_manifest_path, extra_manifest_paths,
min_sdk_version, android_sdk_version): min_sdk_version, android_sdk_version):
# Set minSdkVersion in the manifest to the correct value. # Set minSdkVersion in the manifest to the correct value.
...@@ -141,6 +166,7 @@ def _GenerateAndroidManifest(original_manifest_path, extra_manifest_paths, ...@@ -141,6 +166,7 @@ def _GenerateAndroidManifest(original_manifest_path, extra_manifest_paths,
def _WriteXmlFile(root, path): def _WriteXmlFile(root, path):
logging.info('Writing xml file %s', path)
build_utils.MakeDirectory(os.path.dirname(path)) build_utils.MakeDirectory(os.path.dirname(path))
with build_utils.AtomicOutput(path) as f: with build_utils.AtomicOutput(path) as f:
# Although we can write it just with ElementTree.tostring, using minidom # Although we can write it just with ElementTree.tostring, using minidom
...@@ -162,6 +188,7 @@ def _CheckLintWarning(expected_warnings, lint_output): ...@@ -162,6 +188,7 @@ def _CheckLintWarning(expected_warnings, lint_output):
def _RunLint(lint_binary_path, def _RunLint(lint_binary_path,
backported_methods_path,
config_path, config_path,
manifest_path, manifest_path,
extra_manifest_paths, extra_manifest_paths,
...@@ -189,8 +216,6 @@ def _RunLint(lint_binary_path, ...@@ -189,8 +216,6 @@ def _RunLint(lint_binary_path,
] ]
if baseline: if baseline:
cmd.extend(['--baseline', baseline]) cmd.extend(['--baseline', baseline])
if config_path:
cmd.extend(['--config', config_path])
if testonly_target: if testonly_target:
cmd.extend(['--disable', ','.join(_DISABLED_FOR_TESTS)]) cmd.extend(['--disable', ','.join(_DISABLED_FOR_TESTS)])
...@@ -198,6 +223,13 @@ def _RunLint(lint_binary_path, ...@@ -198,6 +223,13 @@ def _RunLint(lint_binary_path,
manifest_path = os.path.join(build_utils.DIR_SOURCE_ROOT, 'build', manifest_path = os.path.join(build_utils.DIR_SOURCE_ROOT, 'build',
'android', 'AndroidManifest.xml') 'android', 'AndroidManifest.xml')
logging.info('Generating config.xml')
backported_methods = _RetrieveBackportedMethods(backported_methods_path)
config_xml_node = _GenerateConfigXmlTree(config_path, backported_methods)
generated_config_path = os.path.join(lint_gen_dir, 'config.xml')
_WriteXmlFile(config_xml_node, generated_config_path)
cmd.extend(['--config', generated_config_path])
logging.info('Generating Android manifest file') logging.info('Generating Android manifest file')
android_manifest_tree = _GenerateAndroidManifest(manifest_path, android_manifest_tree = _GenerateAndroidManifest(manifest_path,
extra_manifest_paths, extra_manifest_paths,
...@@ -206,7 +238,6 @@ def _RunLint(lint_binary_path, ...@@ -206,7 +238,6 @@ def _RunLint(lint_binary_path,
# Include the rebased manifest_path in the lint generated path so that it is # Include the rebased manifest_path in the lint generated path so that it is
# clear in error messages where the original AndroidManifest.xml came from. # clear in error messages where the original AndroidManifest.xml came from.
lint_android_manifest_path = os.path.join(lint_gen_dir, manifest_path) lint_android_manifest_path = os.path.join(lint_gen_dir, manifest_path)
logging.info('Writing xml file %s', lint_android_manifest_path)
_WriteXmlFile(android_manifest_tree.getroot(), lint_android_manifest_path) _WriteXmlFile(android_manifest_tree.getroot(), lint_android_manifest_path)
resource_root_dir = os.path.join(lint_gen_dir, _RES_ZIP_DIR) resource_root_dir = os.path.join(lint_gen_dir, _RES_ZIP_DIR)
...@@ -244,7 +275,6 @@ def _RunLint(lint_binary_path, ...@@ -244,7 +275,6 @@ def _RunLint(lint_binary_path,
android_sdk_version) android_sdk_version)
project_xml_path = os.path.join(lint_gen_dir, 'project.xml') project_xml_path = os.path.join(lint_gen_dir, 'project.xml')
logging.info('Writing xml file %s', project_xml_path)
_WriteXmlFile(project_file_root, project_xml_path) _WriteXmlFile(project_file_root, project_xml_path)
cmd += ['--project', project_xml_path] cmd += ['--project', project_xml_path]
...@@ -308,6 +338,8 @@ def _ParseArgs(argv): ...@@ -308,6 +338,8 @@ def _ParseArgs(argv):
parser.add_argument('--lint-binary-path', parser.add_argument('--lint-binary-path',
required=True, required=True,
help='Path to lint executable.') help='Path to lint executable.')
parser.add_argument('--backported-methods',
help='Path to backported methods file created by R8.')
parser.add_argument('--cache-dir', parser.add_argument('--cache-dir',
required=True, required=True,
help='Path to the directory in which the android cache ' help='Path to the directory in which the android cache '
...@@ -391,6 +423,7 @@ def main(): ...@@ -391,6 +423,7 @@ def main():
depfile_deps = [p for p in possible_depfile_deps if p] depfile_deps = [p for p in possible_depfile_deps if p]
_RunLint(args.lint_binary_path, _RunLint(args.lint_binary_path,
args.backported_methods,
args.config_path, args.config_path,
args.manifest_path, args.manifest_path,
args.extra_manifest_paths, args.extra_manifest_paths,
......
...@@ -1027,10 +1027,14 @@ if (enable_java_templates) { ...@@ -1027,10 +1027,14 @@ if (enable_java_templates) {
# Save generated xml files in a consistent location for debugging. # Save generated xml files in a consistent location for debugging.
_lint_gen_dir = "$target_gen_dir/$target_name" _lint_gen_dir = "$target_gen_dir/$target_name"
_backported_methods = "//third_party/r8/backported_methods.txt"
script = "//build/android/gyp/lint.py" script = "//build/android/gyp/lint.py"
depfile = "$target_gen_dir/$target_name.d" depfile = "$target_gen_dir/$target_name.d"
inputs = [ _lint_binary_path ] inputs = [
_lint_binary_path,
_backported_methods,
]
args = [ args = [
"--depfile", "--depfile",
...@@ -1045,6 +1049,8 @@ if (enable_java_templates) { ...@@ -1045,6 +1049,8 @@ if (enable_java_templates) {
"--min-sdk-version=$_min_sdk_version", "--min-sdk-version=$_min_sdk_version",
"--android-sdk-root", "--android-sdk-root",
rebase_path(lint_android_sdk_root, root_build_dir), rebase_path(lint_android_sdk_root, root_build_dir),
"--backported-methods",
rebase_path(_backported_methods, root_build_dir),
] ]
if (defined(invoker.lint_suppressions_file)) { if (defined(invoker.lint_suppressions_file)) {
......
...@@ -51,6 +51,9 @@ git format-patch -3 -o $CHROMIUM_SRC/third_party/r8/patches ...@@ -51,6 +51,9 @@ git format-patch -3 -o $CHROMIUM_SRC/third_party/r8/patches
cd $CHROMIUM_SRC/third_party/r8 cd $CHROMIUM_SRC/third_party/r8
cipd create --pkg-def cipd.yaml # Make note of the instance ID cipd create --pkg-def cipd.yaml # Make note of the instance ID
# Update backported methods list:
java -cp third_party/r8/lib/r8.jar com.android.tools.r8.BackportedMethodList --min-api 16 > third_party/r8/backported_methods.txt
# Manually update: # Manually update:
* README.chromium (version number via "java -cp lib/r8.jar com.android.tools.r8.R8 --version") * README.chromium (version number via "java -cp lib/r8.jar com.android.tools.r8.R8 --version")
* //DEPS (instance ID output by cipd create) * //DEPS (instance ID output by cipd create)
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment