Skip to content

Commit 9df06a3

Browse files
committed
Reduce duplicate Android configurations
1 parent b0df0b4 commit 9df06a3

1 file changed

Lines changed: 61 additions & 24 deletions

File tree

tools/flutter/android.bzl

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ load(
2525
"ArtProfileInfo",
2626
"DataBindingV2Info",
2727
)
28+
load("@rules_android//rules:android_split_transition.bzl", "android_split_transition")
2829
load(
2930
"@rules_android//rules:rules.bzl",
3031
"ApkInfo",
@@ -435,8 +436,16 @@ def _flutter_bundle_check_impl(ctx):
435436
mode = ctx.attr._mode[BuildSettingInfo].value
436437
expected = _contributions_for(mode)
437438

439+
contribution_splits = ctx.split_attr.contributions
440+
if len(contribution_splits) != 1:
441+
fail(
442+
"flutter_bundle_check requires exactly one rules_android platform, got {}.".format(
443+
sorted(contribution_splits),
444+
),
445+
)
446+
438447
by_kind = {}
439-
for dep in ctx.attr.contributions:
448+
for dep in contribution_splits.values()[0] + [ctx.attr.assets_contribution]:
440449
info = dep[FlutterBundleContributionInfo]
441450
if info.kind in by_kind:
442451
fail("Two contributions declare kind '{}'.".format(info.kind))
@@ -535,8 +544,26 @@ def _flutter_bundle_check_impl(ctx):
535544

536545
return [DefaultInfo(files = depset([marker]))]
537546

547+
# Pin release builds to `opt`; preserve explicitly non-fastbuild modes.
548+
def _pin_release_compilation_mode_impl(settings, _attr):
549+
mode = settings["//tools/flutter:mode"]
550+
compilation_mode = str(settings["//command_line_option:compilation_mode"])
551+
if mode == "release" and compilation_mode == "fastbuild":
552+
compilation_mode = "opt"
553+
return {"//command_line_option:compilation_mode": compilation_mode}
554+
555+
_pin_release_compilation_mode = transition(
556+
implementation = _pin_release_compilation_mode_impl,
557+
inputs = [
558+
"//tools/flutter:mode",
559+
"//command_line_option:compilation_mode",
560+
],
561+
outputs = ["//command_line_option:compilation_mode"],
562+
)
563+
538564
flutter_bundle_check = rule(
539565
implementation = _flutter_bundle_check_impl,
566+
cfg = _pin_release_compilation_mode,
540567
doc = """Fails the build if the bundle is missing a piece for any ABI it declares.
541568
542569
Instantiated by flutter_android_libs, not written by hand. Three checks, in
@@ -553,7 +580,16 @@ and ships an empty ABI.""",
553580
mandatory = True,
554581
doc = "The ABIs the bundle claims to support. Every native contribution must cover each.",
555582
),
583+
# android_binary reaches its Java/native closure through this transition.
584+
# Matching it makes the check inspect the same configured inputs instead
585+
# of building a third target-configuration copy.
556586
"contributions": attr.label_list(
587+
cfg = android_split_transition,
588+
providers = [FlutterBundleContributionInfo],
589+
mandatory = True,
590+
),
591+
# android_binary's assets attribute stays in its own target configuration.
592+
"assets_contribution": attr.label(
557593
providers = [FlutterBundleContributionInfo],
558594
mandatory = True,
559595
),
@@ -715,7 +751,8 @@ def flutter_android_libs(
715751
Under `mode=debug` the `aot_library` contribution and its `_libapp`
716752
export are dropped: debug ships no AOT snapshot, so nothing supplies
717753
one and android_binary must not expect it.
718-
**kwargs: visibility, tags.
754+
**kwargs: visibility and tags for the caller-facing join and check.
755+
Macro-internal helper and contribution targets append `manual`.
719756
"""
720757
no_plugin_graph = plugins == None
721758
if native_libs == None:
@@ -728,6 +765,11 @@ def flutter_android_libs(
728765
embedding_deps = flutter_embedding_deps(maven_repo)
729766

730767
check_abis(abis, "flutter_android_libs " + name)
768+
internal_kwargs = dict(kwargs)
769+
internal_tags = list(internal_kwargs.get("tags", []))
770+
if "manual" not in internal_tags:
771+
internal_tags.append("manual")
772+
internal_kwargs["tags"] = internal_tags
731773

732774
# A non-empty dict has to cover the declared ABI set exactly. `{}` is the
733775
# deliberate declaration that this app has no recipe or plugin-native
@@ -800,7 +842,7 @@ def flutter_android_libs(
800842
name = libapp_jars[abi],
801843
src = "{}_{}".format(aot, abi),
802844
abi = abi,
803-
**kwargs
845+
**internal_kwargs
804846
)
805847

806848
engine_stripped[abi] = "{}_engine_{}_stripped".format(name, abi)
@@ -811,7 +853,7 @@ def flutter_android_libs(
811853
_MODE_RELEASE: engine_jar_label(abi, "release"),
812854
})),
813855
abi = abi,
814-
**kwargs
856+
**internal_kwargs
815857
)
816858

817859
# One java_import per contribution, not per ABI: android_binary collects
@@ -820,12 +862,12 @@ def flutter_android_libs(
820862
java_import(
821863
name = name + "_libapp",
822864
jars = [libapp_jars[abi] for abi in abis],
823-
**kwargs
865+
**internal_kwargs
824866
)
825867
java_import(
826868
name = name + "_engine",
827869
jars = [engine_stripped[abi] for abi in abis],
828-
**kwargs
870+
**internal_kwargs
829871
)
830872

831873
# Which contributions vary by ABI is the location's property, not a second
@@ -857,6 +899,7 @@ def flutter_android_libs(
857899
"registrant",
858900
]
859901
contributions = []
902+
asset_contribution = None
860903
aot_contribution = None
861904
for kind, location in _ANDROID_CONTRIBUTIONS:
862905
contribution = "{}_{}_contribution".format(name, kind)
@@ -872,17 +915,21 @@ def flutter_android_libs(
872915
# emptiness is still explicit at this boundary; the two structural
873916
# runtime contributions and the asset/AOT/engine never are.
874917
empty = kind in empty_kinds and not srcs and not libraries,
875-
**kwargs
918+
**internal_kwargs
876919
)
877920
if kind == "aot_library":
878921
# Declare AOT for all modes; debug excludes it before inputs are needed.
879922
aot_contribution = contribution
923+
elif kind == "assets":
924+
# android_binary's assets edge does not take its deps split.
925+
asset_contribution = contribution
880926
else:
881927
contributions.append(contribution)
882928

883929
flutter_bundle_check(
884930
name = name + "_check",
885931
abis = abis,
932+
assets_contribution = asset_contribution,
886933
contributions = contributions + select({
887934
_MODE_DEBUG: [],
888935
_MODE_RELEASE: [aot_contribution],
@@ -958,23 +1005,6 @@ _MANIFEST_VALUES = {
9581005
"targetSdkVersion": "36",
9591006
}
9601007

961-
# Pin release builds to `opt`; preserve explicitly non-fastbuild modes.
962-
def _pin_release_compilation_mode_impl(settings, _attr):
963-
mode = settings["//tools/flutter:mode"]
964-
compilation_mode = str(settings["//command_line_option:compilation_mode"])
965-
if mode == "release" and compilation_mode == "fastbuild":
966-
compilation_mode = "opt"
967-
return {"//command_line_option:compilation_mode": compilation_mode}
968-
969-
_pin_release_compilation_mode = transition(
970-
implementation = _pin_release_compilation_mode_impl,
971-
inputs = [
972-
"//tools/flutter:mode",
973-
"//command_line_option:compilation_mode",
974-
],
975-
outputs = ["//command_line_option:compilation_mode"],
976-
)
977-
9781008
def _flutter_apk_impl(ctx):
9791009
apk = ctx.attr.apk
9801010
info = apk[ApkInfo]
@@ -1201,6 +1231,9 @@ def flutter_android_binary(
12011231
`<name>_check_test` takes only visibility and tags.
12021232
"""
12031233
check_abis(abis, "flutter_android_binary " + name)
1234+
internal_tags = list(kwargs.get("tags", []))
1235+
if "manual" not in internal_tags:
1236+
internal_tags.append("manual")
12041237

12051238
if kwargs.get("proguard_specs"):
12061239
fail(
@@ -1270,6 +1303,7 @@ def flutter_android_binary(
12701303
name = versioned_manifest,
12711304
manifest = manifest,
12721305
pubspec = pubspec,
1306+
tags = internal_tags,
12731307
)
12741308

12751309
if resource_files == None:
@@ -1359,10 +1393,12 @@ def flutter_android_binary(
13591393
plugins = plugins,
13601394
registrant = registrant,
13611395
maven_repo = maven_repo,
1396+
tags = internal_tags,
13621397
)
13631398

13641399
packaging_kwargs = dict(kwargs)
13651400
wrapper_visibility = packaging_kwargs.pop("visibility", None)
1401+
packaging_kwargs.pop("tags", None)
13661402

13671403
packaged = target + "_apk"
13681404
android_binary(
@@ -1374,6 +1410,7 @@ def flutter_android_binary(
13741410
resource_files = resource_files,
13751411
deps = deps + [":" + join] + debug_manifest_deps,
13761412
visibility = ["//visibility:private"],
1413+
tags = internal_tags,
13771414
**packaging_kwargs
13781415
)
13791416

0 commit comments

Comments
 (0)