Commit 4b65bab4 authored by Mohamed Heikal's avatar Mohamed Heikal Committed by Commit Bot

Remove image extensions from android image resources

The android framework does not require an extension to parse image
drawables correctly (only that they do not have the .xml extension).
This cl removes all image extensions from the APK to reduce binary size.

Bug: 1014555
Change-Id: I2100d8d008543a4cd36672d80a8a9a2ceaa98dda
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1902231
Commit-Queue: Mohamed Heikal <mheikal@chromium.org>
Auto-Submit: Mohamed Heikal <mheikal@chromium.org>
Reviewed-by: default avatarAndrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714692}
parent 9e1ab24d
......@@ -396,7 +396,7 @@ def _MoveImagesToNonMdpiFolders(res_root):
dst_dir = os.path.join(res_root, dst_dir_name)
build_utils.MakeDirectory(dst_dir)
for src_file_name in os.listdir(src_dir):
if not os.path.splitext(src_file_name)[1] in ('.png', '.webp'):
if not os.path.splitext(src_file_name)[1] in ('.png', '.webp', ''):
continue
src_file = os.path.join(src_dir, src_file_name)
dst_file = os.path.join(dst_dir, src_file_name)
......@@ -524,8 +524,8 @@ def _ConvertToWebP(webp_binary, png_files):
pool = multiprocessing.pool.ThreadPool(10)
def convert_image(png_path_tuple):
png_path, original_dir = png_path_tuple
root = os.path.splitext(png_path)[0]
webp_path = root + '.webp'
# No need to add an extension, android can load images fine without them.
webp_path = os.path.splitext(png_path)[0]
args = [webp_binary, png_path, '-mt', '-quiet', '-m', '6', '-q', '100',
'-lossless', '-o', webp_path]
subprocess.check_call(args)
......@@ -540,6 +540,26 @@ def _ConvertToWebP(webp_binary, png_files):
return renamed_paths
def _RemoveImageExtensions(directory):
"""Remove extensions from image files in the passed directory.
This reduces binary size but does not affect android's ability to load the
images.
Returns: dict[destination] -> source
"""
renamed_paths = {}
for f in _IterFiles(directory):
if (f.endswith('.png') or f.endswith('.webp')) and not f.endswith('.9.png'):
path_with_extension = f
path_no_extension = os.path.splitext(path_with_extension)[0]
if path_no_extension != path_with_extension:
shutil.move(path_with_extension, path_no_extension)
renamed_paths[os.path.relpath(path_no_extension, directory)] = (
os.path.relpath(path_with_extension, directory))
return renamed_paths
def _CompileDeps(aapt2_path, dep_subdirs, temp_dir):
partials_dir = os.path.join(temp_dir, 'partials')
build_utils.MakeDirectory(partials_dir)
......@@ -688,6 +708,7 @@ def _PackageApk(options, build):
renamed_paths.update(_ConvertToWebP(options.webp_binary, png_paths))
for directory in dep_subdirs:
renamed_paths.update(_MoveImagesToNonMdpiFolders(directory))
renamed_paths.update(_RemoveImageExtensions(directory))
_CreateResourceInfoFile(renamed_paths, build.info_path,
options.dependencies_res_zips)
......
......@@ -284,6 +284,9 @@ def _DoApkAnalysis(apk_filename, apks_path, tool_prefix, out_dir, report_func):
file_groups.append(group)
return group
def has_no_extension(filename):
return os.path.splitext(filename)[1] == ''
native_code = make_group('Native code')
java_code = make_group('Java code')
native_resources_no_translations = make_group('Native resources (no l10n)')
......@@ -354,10 +357,12 @@ def _DoApkAnalysis(apk_filename, apks_path, tool_prefix, out_dir, report_func):
icu_data.AddZipInfo(member)
elif filename.endswith('.bin'):
v8_snapshots.AddZipInfo(member)
elif filename.endswith('.png') or filename.endswith('.webp'):
png_drawables.AddZipInfo(member)
elif filename.startswith('res/'):
res_directory.AddZipInfo(member)
if (filename.endswith('.png') or filename.endswith('.webp')
or has_no_extension(filename)):
png_drawables.AddZipInfo(member)
else:
res_directory.AddZipInfo(member)
elif filename.endswith('.arsc'):
arsc.AddZipInfo(member)
elif filename.startswith('META-INF') or filename == 'AndroidManifest.xml':
......
......@@ -41,7 +41,7 @@ CHROME_SPECIFIC = BuildFileMatchRegex(
WEBVIEW_SPECIFIC = BuildFileMatchRegex(
r'lib/.*/libwebviewchromium\.so',
r'assets/webview_licenses.notice',
r'res/.*/icon_webview.webp',
r'res/.*/icon_webview(.webp)?',
r'META-INF/.*',
# Monochrome doesn't have any res directories
# whose api level is less than v24.
......
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