diff options
author | 2025-02-19 11:53:03 -0800 | |
---|---|---|
committer | 2025-02-19 11:53:03 -0800 | |
commit | c6161c2848435284b46d9e0d6bb4a38e76acf623 (patch) | |
tree | a913205199ae3dee6f59b42c5b79b2d87cd7d832 | |
parent | 2e8d7cc741b17966a3a85b0bc0906d07eff4bc08 (diff) |
Add flag to not compress fonts in the APK
Font assets can be mmap'ed if they're not compressed.
This is much more efficient than decompressing them first.
Bug: 395153140
Bug: 393177415
Flag: EXEMPT Aconfig not supported on host tools
Change-Id: I7163746e05a637962d48c0c5a1eb47517435ebf7
-rw-r--r-- | tools/aapt2/cmd/Link.cpp | 4 | ||||
-rw-r--r-- | tools/aapt2/cmd/Link.h | 9 | ||||
-rw-r--r-- | tools/aapt2/cmd/Link_test.cpp | 41 | ||||
-rw-r--r-- | tools/aapt2/readme.md | 2 |
4 files changed, 56 insertions, 0 deletions
diff --git a/tools/aapt2/cmd/Link.cpp b/tools/aapt2/cmd/Link.cpp index ff4d8ef2ec25..0a5cb1ff4956 100644 --- a/tools/aapt2/cmd/Link.cpp +++ b/tools/aapt2/cmd/Link.cpp @@ -2649,6 +2649,10 @@ int LinkCommand::Action(const std::vector<std::string>& args) { ".mpg", ".mpeg", ".mp4", ".m4a", ".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2", ".wma", ".wmv", ".webm", ".mkv"}); + if (options_.no_compress_fonts) { + options_.extensions_to_not_compress.insert({".ttf", ".otf", ".ttc"}); + } + // Turn off auto versioning for static-libs. if (context.GetPackageType() == PackageType::kStaticLib) { options_.no_auto_version = true; diff --git a/tools/aapt2/cmd/Link.h b/tools/aapt2/cmd/Link.h index 2f17853718ec..977978834fcd 100644 --- a/tools/aapt2/cmd/Link.h +++ b/tools/aapt2/cmd/Link.h @@ -78,6 +78,7 @@ struct LinkOptions { bool use_sparse_encoding = false; std::unordered_set<std::string> extensions_to_not_compress; std::optional<std::regex> regex_to_not_compress; + bool no_compress_fonts = false; FeatureFlagValues feature_flag_values; // Static lib options. @@ -300,6 +301,14 @@ class LinkCommand : public Command { "use the '$' symbol for end of line. Uses a case-sensitive ECMAScript" "regular expression grammar.", &no_compress_regex); + AddOptionalSwitch("--no-compress-fonts", + "Do not compress files with common extensions for fonts.\n" + "This allows loading fonts directly from the APK, without needing to\n" + "decompress them first. Loading fonts will be faster and use less memory.\n" + "The downside is that the APK will be larger.\n" + "Passing this flag is functionally equivalent to passing the following flags:\n" + "-0 .ttf -0 .otf -0 .ttc", + &options_.no_compress_fonts); AddOptionalSwitch("--warn-manifest-validation", "Treat manifest validation errors as warnings.", &options_.manifest_fixer_options.warn_validation); diff --git a/tools/aapt2/cmd/Link_test.cpp b/tools/aapt2/cmd/Link_test.cpp index a2dc8f8ce0fd..41f8e250efd7 100644 --- a/tools/aapt2/cmd/Link_test.cpp +++ b/tools/aapt2/cmd/Link_test.cpp @@ -98,6 +98,7 @@ TEST_F(LinkTest, NoCompressAssets) { WriteFile(GetTestPath("assets/test.txt"), content); WriteFile(GetTestPath("assets/test.hello.txt"), content); WriteFile(GetTestPath("assets/test.hello.xml"), content); + WriteFile(GetTestPath("assets/fonts/myfont.ttf"), content); const std::string out_apk = GetTestPath("out.apk"); std::vector<std::string> link_args = { @@ -136,6 +137,10 @@ TEST_F(LinkTest, NoCompressAssets) { file = zip->FindFile("assets/test.hello.xml"); ASSERT_THAT(file, Ne(nullptr)); EXPECT_FALSE(file->WasCompressed()); + + file = zip->FindFile("assets/fonts/myfont.ttf"); + ASSERT_THAT(file, Ne(nullptr)); + EXPECT_TRUE(file->WasCompressed()); } TEST_F(LinkTest, NoCompressResources) { @@ -182,6 +187,42 @@ TEST_F(LinkTest, NoCompressResources) { EXPECT_FALSE(file->WasCompressed()); } +TEST_F(LinkTest, NoCompressFonts) { + StdErrDiagnostics diag; + std::string content(500, 'a'); + const std::string compiled_files_dir = GetTestPath("compiled"); + ASSERT_TRUE(CompileFile(GetTestPath("res/raw/test.txt"), content, compiled_files_dir, &diag)); + WriteFile(GetTestPath("assets/fonts/myfont1.ttf"), content); + WriteFile(GetTestPath("assets/fonts/myfont2.ttf"), content); + + const std::string out_apk = GetTestPath("out.apk"); + std::vector<std::string> link_args = { + "--manifest", GetDefaultManifest(), + "-o", out_apk, + "--no-compress-fonts", + "-A", GetTestPath("assets") + }; + + ASSERT_TRUE(Link(link_args, compiled_files_dir, &diag)); + + std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(out_apk, &diag); + ASSERT_THAT(apk, Ne(nullptr)); + io::IFileCollection* zip = apk->GetFileCollection(); + ASSERT_THAT(zip, Ne(nullptr)); + + auto file = zip->FindFile("res/raw/test.txt"); + ASSERT_THAT(file, Ne(nullptr)); + EXPECT_TRUE(file->WasCompressed()); + + file = zip->FindFile("assets/fonts/myfont1.ttf"); + ASSERT_THAT(file, Ne(nullptr)); + EXPECT_FALSE(file->WasCompressed()); + + file = zip->FindFile("assets/fonts/myfont2.ttf"); + ASSERT_THAT(file, Ne(nullptr)); + EXPECT_FALSE(file->WasCompressed()); +} + TEST_F(LinkTest, OverlayStyles) { StdErrDiagnostics diag; const std::string compiled_files_dir = GetTestPath("compiled"); diff --git a/tools/aapt2/readme.md b/tools/aapt2/readme.md index 5c3dfdcadfec..6bdbaaed9858 100644 --- a/tools/aapt2/readme.md +++ b/tools/aapt2/readme.md @@ -3,6 +3,8 @@ ## Version 2.20 - Too many features, bug fixes, and improvements to list since the last minor version update in 2017. This README will be updated more frequently in the future. +- Added a new flag `--no-compress-fonts`. This can significantly speed up loading fonts from APK + assets, at the cost of increasing the storage size of the APK. ## Version 2.19 - Added navigation resource type. |