Skip to content

Commit 7419a27

Browse files
A Googlercopybara-github
authored andcommitted
add aar-metadata.properties attribute in android_archive and android-library to support adding AAR metadata file inside final .aar.
PiperOrigin-RevId: 721597551 Change-Id: I5c1c41dacf0b97c1ff97926929ae81a769386aa2
1 parent af0272a commit 7419a27

6 files changed

Lines changed: 428 additions & 186 deletions

File tree

rules/android_library/attrs.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ visibility(PROJECT_VISIBILITY)
2626

2727
ATTRS = _attrs.add(
2828
dict(
29+
aar_metadata = attr.label(
30+
allow_single_file = [".properties"],
31+
doc = ("The name of the AAR metadata file, optional"),
32+
),
2933
baseline_profiles = attr.label_list(
3034
allow_files = [".txt"],
3135
doc = (

rules/android_library/impl.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ def _process_aar(ctx, java_package, resources_ctx, proguard_ctx, **unused_ctx):
333333
resource_files = resources_ctx.starlark_processed_resources if not ctx.attr.neverlink else [],
334334
class_jar = ctx.outputs.lib_jar,
335335
r_txt = resources_ctx.starlark_r_txt,
336+
aar_metadata = ctx.file.aar_metadata,
336337
proguard_specs = proguard_ctx.proguard_configs,
337338
busybox = get_android_toolchain(ctx).android_resources_busybox.files_to_run,
338339
host_javabase = _common.get_host_javabase(ctx),

rules/busybox.bzl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ def _make_aar(
10781078
class_jar = None,
10791079
r_txt = None,
10801080
manifest = None,
1081+
aar_metadata = None,
10811082
proguard_specs = [],
10821083
should_throw_on_conflict = False,
10831084
busybox = None,
@@ -1093,6 +1094,7 @@ def _make_aar(
10931094
class_jar: File. The class jar file.
10941095
r_txt: File. The resource IDs outputted by linking resources in text.
10951096
manifest: File. The primary AndroidManifest.xml.
1097+
aar_metadata: File. Optional metadata file to be included in the output AAR file.
10961098
proguard_specs: List of File. The proguard spec files.
10971099
busybox: FilesToRunProvider. The ResourceBusyBox executable or
10981100
FilesToRunprovider
@@ -1119,6 +1121,12 @@ def _make_aar(
11191121
args.add("--classes", class_jar)
11201122
args.add("--aarOutput", out_aar)
11211123
args.add_all(proguard_specs, before_each = "--proguardSpec")
1124+
1125+
input_files = [r_txt, manifest, class_jar]
1126+
if aar_metadata != None:
1127+
args.add("--aarMetadata", aar_metadata)
1128+
input_files.append(aar_metadata)
1129+
11221130
if should_throw_on_conflict:
11231131
args.add("--throwOnResourceConflict")
11241132

@@ -1134,7 +1142,7 @@ def _make_aar(
11341142
resource_files +
11351143
assets +
11361144
proguard_specs +
1137-
[r_txt, manifest, class_jar]
1145+
input_files
11381146
),
11391147
outputs = [out_aar],
11401148
mnemonic = "StarlarkAARGenerator",

rules/resources.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,7 @@ def _make_aar(
954954
class_jar = None,
955955
r_txt = None,
956956
manifest = None,
957+
aar_metadata = None,
957958
proguard_specs = [],
958959
busybox = None,
959960
host_javabase = None):
@@ -967,6 +968,7 @@ def _make_aar(
967968
class_jar: File. The class jar file.
968969
r_txt: File. The resource IDs outputted by linking resources in text.
969970
manifest: File. The primary AndroidManifest.xml.
971+
aar_metadata: File. Optional metadata file to be included in the `.aar` file.
970972
proguard_specs: List of File. The proguard spec files.
971973
busybox: FilesToRunProvider. The ResourceBusyBox executable or
972974
FilesToRunprovider
@@ -985,6 +987,7 @@ def _make_aar(
985987
class_jar = class_jar,
986988
r_txt = r_txt,
987989
manifest = manifest,
990+
aar_metadata = aar_metadata,
988991
proguard_specs = proguard_specs,
989992
busybox = busybox,
990993
host_javabase = host_javabase,

src/tools/java/com/google/devtools/build/android/AarGeneratorAction.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.annotations.VisibleForTesting;
2424
import com.google.common.base.Joiner;
2525
import com.google.common.base.Stopwatch;
26+
import com.google.common.base.Strings;
2627
import com.google.common.collect.ImmutableList;
2728
import com.google.common.collect.Ordering;
2829
import com.google.devtools.build.android.AndroidDataMerger.MergeConflictException;
@@ -103,6 +104,12 @@ public static final class AarGeneratorOptions {
103104
description = "Path to classes.jar.")
104105
public Path classes;
105106

107+
@Parameter(
108+
names = "--aarMetadata",
109+
converter = CompatExistingPathConverter.class,
110+
description = "Path to aar-metadata.properties file. Optional.")
111+
public Path aarMetadata;
112+
106113
@Parameter(
107114
names = "--proguardSpec",
108115
converter = CompatExistingPathConverter.class,
@@ -164,6 +171,7 @@ public static void main(String[] args) throws ParameterException, IOException {
164171
options.manifest,
165172
options.rtxt,
166173
options.classes,
174+
options.aarMetadata,
167175
options.proguardSpecs);
168176
logger.fine(
169177
String.format("Packaging finished at %dms", timer.elapsed(TimeUnit.MILLISECONDS)));
@@ -203,6 +211,7 @@ static void writeAar(
203211
Path manifest,
204212
Path rtxt,
205213
Path classes,
214+
Path aarMetadata,
206215
List<Path> proguardSpecs)
207216
throws IOException {
208217
try (final ZipOutputStream zipOut =
@@ -213,6 +222,20 @@ static void writeAar(
213222
zipOut.write(Files.readAllBytes(manifest));
214223
zipOut.closeEntry();
215224

225+
if (aarMetadata != null && !Strings.isNullOrEmpty(aarMetadata.toString())) {
226+
if (Files.notExists(aarMetadata)) {
227+
throw new ParameterException(
228+
String.format("AAR metadata file %s does not exist.", aarMetadata));
229+
}
230+
231+
ZipEntry aarMetadataEntry =
232+
new ZipEntry("META-INF/com/android/build/gradle/aar-metadata.properties");
233+
aarMetadataEntry.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
234+
zipOut.putNextEntry(aarMetadataEntry);
235+
zipOut.write(Files.readAllBytes(aarMetadata));
236+
zipOut.closeEntry();
237+
}
238+
216239
ZipEntry classJar = new ZipEntry("classes.jar");
217240
classJar.setTime(DEFAULT_TIMESTAMP.toEpochMilli());
218241
zipOut.putNextEntry(classJar);

0 commit comments

Comments
 (0)