2323import com .google .common .annotations .VisibleForTesting ;
2424import com .google .common .base .Joiner ;
2525import com .google .common .base .Stopwatch ;
26+ import com .google .common .base .Strings ;
2627import com .google .common .collect .ImmutableList ;
2728import com .google .common .collect .Ordering ;
2829import 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