Skip to content

Commit 6246827

Browse files
ajsinclaircopybara-androidxtest
authored andcommitted
Filter split-transitioned deps to a single split in collect_maven_apk_info.
When `android_binary` builds with multiple splits (for example via `--android_platforms` with multiple platforms), `ctx.rule.attr.deps` exposes every split configuration of each dependency. Collecting `transitive_included_src_jars` across all splits passes duplicate `-src.jar` files from different output roots to `jarcombiner`, failing with duplicate entry errors. Track each target's platform in `collect_maven_apk_info` via `ctx.fragments.platform.platform` and filter `deps` to the primary platform split, falling back to deduplicating by label when splits share a platform. PiperOrigin-RevId: 1030050132
1 parent 1b9fea3 commit 6246827

1 file changed

Lines changed: 55 additions & 16 deletions

File tree

‎build_extensions/maven/maven_info.bzl‎

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -165,44 +165,83 @@ collect_maven_info = aspect(
165165
implementation = _collect_maven_info_impl,
166166
)
167167

168+
_PlatformInfo = provider(
169+
doc = "Internal provider tracking the target platform for filtering split deps.",
170+
fields = {
171+
"platform": "The target platform label of the configured target.",
172+
},
173+
)
174+
175+
def _filter_primary_split(deps, primary_platform):
176+
"""Filters split-transitioned deps down to a single split.
177+
178+
Prefers deps matching the target's primary platform (for multi-platform
179+
android_binary builds). Falls back to deduplicating by label if splits
180+
share a platform.
181+
"""
182+
if primary_platform:
183+
primary_deps = [
184+
dep
185+
for dep in deps
186+
if _PlatformInfo in dep and dep[_PlatformInfo].platform == primary_platform
187+
]
188+
if primary_deps:
189+
deps = primary_deps
190+
seen = {}
191+
unique_deps = []
192+
for dep in deps:
193+
if dep.label not in seen:
194+
seen[dep.label] = True
195+
unique_deps.append(dep)
196+
return unique_deps
197+
168198
def _collect_maven_apk_info_impl(target, ctx):
169199
neverlink = getattr(ctx.rule.attr, "neverlink", False)
170200
deps = getattr(ctx.rule.attr, "deps", [])
171201
exports = getattr(ctx.rule.attr, "exports", [])
202+
platform_info = _PlatformInfo(platform = ctx.fragments.platform.platform)
172203

173204
# ignore non-runtime or non-java dependencies eg proto_library or non-axt-labels
174205
if neverlink or JavaInfo not in target or not is_axt_label(target.label):
175-
return MavenInfo(
176-
artifact = None,
177-
is_compileOnly = True,
178-
is_shaded = False,
179-
transitive_included_runtime_jars = depset(),
180-
transitive_included_src_jars = depset(),
181-
transitive_maven_direct_deps = depset(),
182-
)
206+
return [
207+
MavenInfo(
208+
artifact = None,
209+
is_compileOnly = True,
210+
is_shaded = False,
211+
transitive_included_runtime_jars = depset(),
212+
transitive_included_src_jars = depset(),
213+
transitive_maven_direct_deps = depset(),
214+
),
215+
platform_info,
216+
]
183217

184218
artifact = get_artifact_from_label(target.label)
185219
included_src_jars = target[JavaInfo].source_jars
186220
transitive_included_src_jars = []
187221

222+
deps = _filter_primary_split(deps, ctx.fragments.platform.platform)
188223
for dep in (deps + exports):
189224
transitive_included_src_jars.append(dep[MavenInfo].transitive_included_src_jars)
190225

191226
maven_deps = get_maven_apk_deps(artifact)
192-
return [MavenInfo(
193-
artifact = artifact,
194-
is_compileOnly = False,
195-
is_shaded = False,
196-
transitive_included_runtime_jars = depset(),
197-
transitive_included_src_jars = depset(included_src_jars, transitive = transitive_included_src_jars),
198-
transitive_maven_direct_deps = depset(maven_deps),
199-
)]
227+
return [
228+
MavenInfo(
229+
artifact = artifact,
230+
is_compileOnly = False,
231+
is_shaded = False,
232+
transitive_included_runtime_jars = depset(),
233+
transitive_included_src_jars = depset(included_src_jars, transitive = transitive_included_src_jars),
234+
transitive_maven_direct_deps = depset(maven_deps),
235+
),
236+
platform_info,
237+
]
200238

201239
collect_maven_apk_info = aspect(
202240
attr_aspects = [
203241
"deps",
204242
"exports",
205243
],
244+
fragments = ["platform"],
206245
doc = """
207246
Collects the Maven apk information for targets, their dependencies, and their transitive exports.
208247
""",

0 commit comments

Comments
 (0)