From 4514bdf1b7a1dca90b8389993a641024ce439922 Mon Sep 17 00:00:00 2001 From: Jared Duke Date: Thu, 18 Nov 2021 13:18:21 -0800 Subject: [PATCH] Allow flag-guarded Java optimizations for services Introduce a set of optional Java optimization and shrinking settings for system server, based on the Soong variable added in aosp/1896612. Opting in to optimizations can be achieved with either: * Env: export SYSTEM_OPTIMIZE_JAVA=true * Make: $(call add_soong_config_var_value,ANDROID,SYSTEM_OPTIMIZE_JAVA,true) Note that the initial Proguard configuration is extremely conservative. Follow-up work will refine the rules to both allow additional shrinking while avoiding developer friction and overhead when adding services and dynamically loaded jars. As these optimiations can change the resulting stack traces, a parallel effort is working to simplify retracing of stack traces for more accurate debugging and diagnostics. Additional stabiliation and testing will occur before any effort to enable these optimizations by default for specific targets. Also note that there are no plans to enable obfuscation for any of these targets. Preliminary results: * Conservative keep rules (this CL w/ opt-in flag): * services.jar (19MB -> 15MB) * services.odex (52MB -> 54MB) (mostly from increased inlining) * Refined keep rules (follow-up CL): * services.jar (19MB -> 12MB) * services.odex (52MB -> 45MB) Bug: 203088572 Test: SYSTEM_OPTIMIZE_JAVA=true m (validate services.jar/odex change) Change-Id: I4130233310323611f63cd9bfcc0646080cf95875 --- services/Android.bp | 31 +++++++++++++++++++++++++++++++ services/proguard.flags | 11 +++++++++++ 2 files changed, 42 insertions(+) create mode 100644 services/proguard.flags diff --git a/services/Android.bp b/services/Android.bp index cc0fd98c70604..e11b0442377eb 100644 --- a/services/Android.bp +++ b/services/Android.bp @@ -23,6 +23,36 @@ java_defaults { }, } +// Opt-in config for optimizing and shrinking the services target using R8. +// Enabled via `export SYSTEM_OPTIMIZE_JAVA=true`, or explicitly in Make via the +// `SOONG_CONFIG_ANDROID_SYSTEM_OPTIMIZE_JAVA` variable. +// TODO(b/196084106): Enable optimizations by default after stabilizing and +// building out retrace infrastructure. +soong_config_module_type { + name: "system_optimized_java_defaults", + module_type: "java_defaults", + config_namespace: "ANDROID", + bool_variables: ["SYSTEM_OPTIMIZE_JAVA"], + properties: ["optimize"], +} + +system_optimized_java_defaults { + name: "services_java_defaults", + soong_config_variables: { + SYSTEM_OPTIMIZE_JAVA: { + optimize: { + enabled: true, + optimize: true, + shrink: true, + proguard_flags_files: ["proguard.flags"], + }, + // Note: Optimizations are disabled by default if unspecified in + // the java_library rule. + conditions_default: {}, + }, + }, +} + filegroup { name: "services-main-sources", srcs: [ @@ -81,6 +111,7 @@ java_library { // ============================================================ java_library { name: "services", + defaults: ["services_java_defaults"], installable: true, dex_preopt: { diff --git a/services/proguard.flags b/services/proguard.flags new file mode 100644 index 0000000000000..30dd6cf545b97 --- /dev/null +++ b/services/proguard.flags @@ -0,0 +1,11 @@ +# TODO(b/196084106): Refine and optimize this configuration. Note that this +# configuration is only used when `SOONG_CONFIG_ANDROID_SYSTEM_OPTIMIZE_JAVA=true`. +-keep,allowoptimization,allowaccessmodification class ** { + *; +} + +# Various classes subclassed in ethernet-service (avoid marking final). +-keep public class android.net.** { *; } + +# Referenced via CarServiceHelperService in car-frameworks-service (avoid removing). +-keep public class com.android.server.utils.Slogf { *; } \ No newline at end of file