summaryrefslogtreecommitdiff
path: root/java/dex.go
diff options
context:
space:
mode:
author Jared Duke <jdduke@google.com> 2025-03-10 23:03:00 +0000
committer Jared Duke <jdduke@google.com> 2025-03-11 22:40:57 +0000
commit0ac6ae721d7400e0f5fa6d03f08eca63412390cc (patch)
tree4a3816f5e69ef46b4fa2a887a452180b728e3056 /java/dex.go
parent6a3b2d08fdef380fbced31156e27dea01d35ccd2 (diff)
Add a trace_references_from dex optimization property
Introduce a new property `trace_references_from` in the optimize block. This field allows specifying a set of downstream targets from which to trace references when optimizing the current target. In practice, this requires explicit breaking of any cycles that might occur. A follow-up effort will explore doing this dynamically and implicitly for `libs` references onto an optimized target that enables this feature. Bug: 212737576 Test: m nothing Test: go test ./java Flag: EXEMPT bugfix Change-Id: I088bd8bf89403661a084a43b2822a6efbbc0294d
Diffstat (limited to 'java/dex.go')
-rw-r--r--java/dex.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/java/dex.go b/java/dex.go
index b32d5aee0..ed9c82ba2 100644
--- a/java/dex.go
+++ b/java/dex.go
@@ -116,6 +116,21 @@ type DexProperties struct {
//
// By default all classes are compiled using R8 when Optimize.Enabled is set.
Exclude *string `android:"path"`
+
+ // Optional list of downstream (Java) libraries from which to trace and preserve references
+ // when optimizing. Note that this requires that the source reference does *not* have
+ // a strict lib dependency on this target; dependencies should be on intermediate targets
+ // statically linked into this target, e.g., if A references B, and we want to trace and
+ // keep references from A when optimizing B, you would create an intermediate B.impl (
+ // containing all static code), have A depend on `B.impl` via libs, and set
+ // `trace_references_from: ["A"]` on B.
+ //
+ // Also note that these are *not* inherited across targets, they must be specified at the
+ // top-level target that is optimized.
+ //
+ // TODO(b/212737576): Handle this implicitly using bottom-up deps mutation and implicit
+ // creation of a proxy `.impl` library.
+ Trace_references_from proptools.Configurable[[]string] `android:"arch_variant"`
}
// Keep the data uncompressed. We always need uncompressed dex for execution,
@@ -458,6 +473,20 @@ func (d *dexer) r8Flags(ctx android.ModuleContext, dexParams *compileDexParams,
flagFiles = append(flagFiles, android.PathsForModuleSrc(ctx, opt.Proguard_flags_files)...)
+ traceReferencesSources := android.Paths{}
+ ctx.VisitDirectDepsProxyWithTag(traceReferencesTag, func(m android.ModuleProxy) {
+ if dep, ok := android.OtherModuleProvider(ctx, m, JavaInfoProvider); ok {
+ traceReferencesSources = append(traceReferencesSources, dep.ImplementationJars...)
+ }
+ })
+ if len(traceReferencesSources) > 0 {
+ traceTarget := dexParams.classesJar
+ traceLibs := android.FirstUniquePaths(append(flags.bootClasspath.Paths(), flags.dexClasspath.Paths()...))
+ traceReferencesFlags := android.PathForModuleOut(ctx, "proguard", "trace_references.flags")
+ TraceReferences(ctx, traceReferencesSources, traceTarget, traceLibs, traceReferencesFlags)
+ flagFiles = append(flagFiles, traceReferencesFlags)
+ }
+
flagFiles = android.FirstUniquePaths(flagFiles)
r8Flags = append(r8Flags, android.JoinWithPrefix(flagFiles.Strings(), "-include "))