Skip aapt2 optimize during linking when optimize_throughput is enabled aapt2 optimize aims to reduce APK size by 1-3%, an unnecessary step for test builds where file size is not a priority. Thus, we can extend optimize_throughput to skip aapt2 optimize, supporting the flag's functionality of trading file size optimizations for faster execution. PiperOrigin-RevId: 959242802 Change-Id: Ifa4141ea48839a40a519f2507adf8151bc9045f0
diff --git a/rules/busybox.bzl b/rules/busybox.bzl index b6ba9b5..b618ace 100644 --- a/rules/busybox.bzl +++ b/rules/busybox.bzl
@@ -295,7 +295,7 @@ resource_apks: Depset of resource only apk files to link against. nocompress_extensions: A list of strings. File extension to leave uncompressed in the apk. - optimize_throughput: A boolean. Whether to instruct aapt to lower the compression level in exchange for faster execution time. + optimize_throughput: A boolean. Whether to disable APK size optimizations in favor of a faster execution time. proto_format: Boolean, whether to generate the resource table in proto format. shrink_resource_cycles: Boolean, flag that enables more shrinking of code and resources by instructing AAPT2 to emit conditional Proguard keep rules.
diff --git a/rules/resources.bzl b/rules/resources.bzl index 6351866..75ad9b6 100644 --- a/rules/resources.bzl +++ b/rules/resources.bzl
@@ -533,7 +533,7 @@ to be processed. nocompress_extensions: sequence of Strings. File extension to leave uncompressed in the apk. - optimize_throughput: A boolean. Whether to instruct aapt to lower the compression level in exchange for faster execution time. + optimize_throughput: A boolean. Whether to disable APK size optimizations in favor of a faster execution time. java_package: String. Java package for which java sources will be generated. By default the package is inferred from the directory where the BUILD file containing the rule is.
diff --git a/src/tools/java/com/google/devtools/build/android/aapt2/ResourceLinker.java b/src/tools/java/com/google/devtools/build/android/aapt2/ResourceLinker.java index 92cdde5..c4dea57 100644 --- a/src/tools/java/com/google/devtools/build/android/aapt2/ResourceLinker.java +++ b/src/tools/java/com/google/devtools/build/android/aapt2/ResourceLinker.java
@@ -522,8 +522,11 @@ .add("--feature-flags", featureFlags) .execute(String.format("Linking %s", compiled.getManifest()))); profiler.recordEndOf("fulllink"); - Path optimized = optimize(compiled, linked, asProto); - return asProto ? optimized : copyAndFixCompression(optimized, workingDirectory); + if (!optimizeThroughput) { + Path optimized = optimize(compiled, linked, asProto); + return asProto ? optimized : copyAndFixCompression(optimized, workingDirectory); + } + return linked; } /** Modes for overriding compression of a given file. */ @@ -674,6 +677,9 @@ return apk; } + Preconditions.checkState( + !optimizeThroughput, "Invalid state: calling optimize() when optimizeThroughput is enabled"); + profiler.startTask("optimize"); final Path optimized = workingDirectory.resolve(isProto ? ("optimized." + PROTO_EXTENSION) : "optimized.apk"); @@ -682,8 +688,6 @@ .forBuildToolsVersion(buildToolsVersion) .forVariantType(VariantTypeImpl.BASE_APK) .add("optimize") - .when(optimizeThroughput) - .thenAdd("-Othroughput") .when(Objects.equals(logger.getLevel(), Level.FINE)) .thenAdd("-v") // TODO(b/138166830): Simplify behavior specific to number of densities. There's likely