Debian Codegraph @

build-system/manifest-merger/src/main/java/com/android/manifmerger/ManifestMerger2.java

raw
1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.manifmerger;
18
19 import static com.android.manifmerger.PlaceholderHandler.APPLICATION_ID;
20 import static com.android.manifmerger.PlaceholderHandler.KeyBasedValueResolver;
21 import static com.android.manifmerger.PlaceholderHandler.PACKAGE_NAME;
22
23 import com.android.SdkConstants;
24 import com.android.annotations.NonNull;
25 import com.android.annotations.Nullable;
26 import com.android.annotations.concurrency.Immutable;
27 import com.android.builder.model.AndroidBundle;
28 import com.android.utils.ILogger;
29 import com.android.utils.Pair;
30 import com.google.common.base.Optional;
31 import com.google.common.base.Preconditions;
32 import com.google.common.base.Strings;
33 import com.google.common.collect.ImmutableList;
34 import com.google.common.collect.ImmutableMap;
35 import com.google.common.collect.Sets;
36
37 import org.w3c.dom.Attr;
38
39 import java.io.BufferedInputStream;
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.FileNotFoundException;
43 import java.io.FileWriter;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Map;
49
50 /**
51 * merges android manifest files, idempotent.
52 */
53 @Immutable
54 public class ManifestMerger2 {
55
56 static final String BOOTSTRAP_APPLICATION
57 = "com.android.tools.fd.runtime.BootstrapApplication";
58
59 @NonNull
60 private final File mManifestFile;
61
62 @NonNull
63 private final Map<String, Object> mPlaceHolderValues;
64
65 @NonNull
66 private final KeyBasedValueResolver<ManifestSystemProperty> mSystemPropertyResolver;
67
68 @NonNull
69 private final ILogger mLogger;
70 @NonNull
71 private final ImmutableList<Pair<String, File>> mLibraryFiles;
72 @NonNull
73 private final ImmutableList<File> mFlavorsAndBuildTypeFiles;
74 @NonNull
75 private final ImmutableList<Invoker.Feature> mOptionalFeatures;
76 @NonNull
77 private final MergeType mMergeType;
78 @NonNull
79 private final Optional<File> mReportFile;
80 @NonNull
81 private final FileStreamProvider mFileStreamProvider;
82
83 private ManifestMerger2(
84 @NonNull ILogger logger,
85 @NonNull File mainManifestFile,
86 @NonNull ImmutableList<Pair<String, File>> libraryFiles,
87 @NonNull ImmutableList<File> flavorsAndBuildTypeFiles,
88 @NonNull ImmutableList<Invoker.Feature> optionalFeatures,
89 @NonNull Map<String, Object> placeHolderValues,
90 @NonNull KeyBasedValueResolver<ManifestSystemProperty> systemPropertiesResolver,
91 @NonNull MergeType mergeType,
92 @NonNull Optional<File> reportFile,
93 @NonNull FileStreamProvider fileStreamProvider) {
94 this.mSystemPropertyResolver = systemPropertiesResolver;
95 this.mPlaceHolderValues = placeHolderValues;
96 this.mManifestFile = mainManifestFile;
97 this.mLogger = logger;
98 this.mLibraryFiles = libraryFiles;
99 this.mFlavorsAndBuildTypeFiles = flavorsAndBuildTypeFiles;
100 this.mOptionalFeatures = optionalFeatures;
101 this.mMergeType = mergeType;
102 this.mReportFile = reportFile;
103 this.mFileStreamProvider = fileStreamProvider;
104 }
105
106 /**
107 * Perform high level ordering of files merging and delegates actual merging to
108 * {@link XmlDocument#merge(XmlDocument, com.android.manifmerger.MergingReport.Builder)}
109 *
110 * @return the merging activity report.
111 * @throws MergeFailureException if the merging cannot be completed (for instance, if xml
112 * files cannot be loaded).
113 */
114 @NonNull
115 private MergingReport merge() throws MergeFailureException {
116 // initiate a new merging report
117 MergingReport.Builder mergingReportBuilder = new MergingReport.Builder(mLogger);
118
119 SelectorResolver selectors = new SelectorResolver();
120 // load all the libraries xml files up front to have a list of all possible node:selector
121 // values.
122 List<LoadedManifestInfo> loadedLibraryDocuments =
123 loadLibraries(selectors, mergingReportBuilder);
124
125 // load the main manifest file to do some checking along the way.
126 LoadedManifestInfo loadedMainManifestInfo = load(
127 new ManifestInfo(
128 mManifestFile.getName(),
129 mManifestFile,
130 XmlDocument.Type.MAIN,
131 Optional.<String>absent() /* mainManifestPackageName */),
132 selectors,
133 mergingReportBuilder);
134
135 // first do we have a package declaration in the main manifest ?
136 Optional<XmlAttribute> mainPackageAttribute =
137 loadedMainManifestInfo.getXmlDocument().getPackage();
138 if (!mainPackageAttribute.isPresent()) {
139 mergingReportBuilder.addMessage(
140 loadedMainManifestInfo.getXmlDocument().getSourceFile(),
141 MergingReport.Record.Severity.ERROR,
142 String.format(
143 "Main AndroidManifest.xml at %1$s manifest:package attribute "
144 + "is not declared",
145 loadedMainManifestInfo.getXmlDocument().getSourceFile()
146 .print(true)));
147 return mergingReportBuilder.build();
148 }
149
150 // perform system property injection
151 performSystemPropertiesInjection(mergingReportBuilder,
152 loadedMainManifestInfo.getXmlDocument());
153
154 // force the re-parsing of the xml as elements may have been added through system
155 // property injection.
156 loadedMainManifestInfo = new LoadedManifestInfo(loadedMainManifestInfo,
157 loadedMainManifestInfo.getOriginalPackageName(),
158 loadedMainManifestInfo.getXmlDocument().reparse());
159
160 // invariant : xmlDocumentOptional holds the higher priority document and we try to
161 // merge in lower priority documents.
162 Optional<XmlDocument> xmlDocumentOptional = Optional.absent();
163 for (File inputFile : mFlavorsAndBuildTypeFiles) {
164 mLogger.info("Merging flavors and build manifest %s \n", inputFile.getPath());
165 LoadedManifestInfo overlayDocument = load(
166 new ManifestInfo(null, inputFile, XmlDocument.Type.OVERLAY,
167 Optional.of(mainPackageAttribute.get().getValue())),
168 selectors,
169 mergingReportBuilder);
170
171 // check package declaration.
172 Optional<XmlAttribute> packageAttribute =
173 overlayDocument.getXmlDocument().getPackage();
174 // if both files declare a package name, it should be the same.
175 if (loadedMainManifestInfo.getOriginalPackageName().isPresent() &&
176 packageAttribute.isPresent()
177 && !loadedMainManifestInfo.getOriginalPackageName().get().equals(
178 packageAttribute.get().getValue())) {
179 // no suggestion for library since this is actually forbidden to change the
180 // the package name per flavor.
181 String message = mMergeType == MergeType.APPLICATION
182 ? String.format(
183 "Overlay manifest:package attribute declared at %1$s value=(%2$s)\n"
184 + "\thas a different value=(%3$s) "
185 + "declared in main manifest at %4$s\n"
186 + "\tSuggestion: remove the overlay declaration at %5$s "
187 + "\tand place it in the build.gradle:\n"
188 + "\t\tflavorName {\n"
189 + "\t\t\tapplicationId = \"%2$s\"\n"
190 + "\t\t}",
191 packageAttribute.get().printPosition(),
192 packageAttribute.get().getValue(),
193 mainPackageAttribute.get().getValue(),
194 mainPackageAttribute.get().printPosition(),
195 packageAttribute.get().getSourceFile().print(true))
196 : String.format(
197 "Overlay manifest:package attribute declared at %1$s value=(%2$s)\n"
198 + "\thas a different value=(%3$s) "
199 + "declared in main manifest at %4$s",
200 packageAttribute.get().printPosition(),
201 packageAttribute.get().getValue(),
202 mainPackageAttribute.get().getValue(),
203 mainPackageAttribute.get().printPosition());
204 mergingReportBuilder.addMessage(
205 overlayDocument.getXmlDocument().getSourceFile(),
206 MergingReport.Record.Severity.ERROR,
207 message);
208 return mergingReportBuilder.build();
209 }
210
211 overlayDocument.getXmlDocument().getRootNode().getXml().setAttribute("package",
212 mainPackageAttribute.get().getValue());
213 xmlDocumentOptional = merge(xmlDocumentOptional, overlayDocument, mergingReportBuilder);
214
215 if (!xmlDocumentOptional.isPresent()) {
216 return mergingReportBuilder.build();
217 }
218 }
219
220 mLogger.info("Merging main manifest %s\n", mManifestFile.getPath());
221 xmlDocumentOptional =
222 merge(xmlDocumentOptional, loadedMainManifestInfo, mergingReportBuilder);
223
224 if (!xmlDocumentOptional.isPresent()) {
225 return mergingReportBuilder.build();
226 }
227
228 // force main manifest package into resulting merged file when creating a library manifest.
229 if (mMergeType == MergeType.LIBRARY) {
230 // extract the package name...
231 String mainManifestPackageName = loadedMainManifestInfo.getXmlDocument().getRootNode()
232 .getXml().getAttribute("package");
233 // save it in the selector instance.
234 if (!Strings.isNullOrEmpty(mainManifestPackageName)) {
235 xmlDocumentOptional.get().getRootNode().getXml()
236 .setAttribute("package", mainManifestPackageName);
237 }
238 }
239 for (LoadedManifestInfo libraryDocument : loadedLibraryDocuments) {
240 mLogger.info("Merging library manifest " + libraryDocument.getLocation());
241 xmlDocumentOptional = merge(
242 xmlDocumentOptional, libraryDocument, mergingReportBuilder);
243 if (!xmlDocumentOptional.isPresent()) {
244 return mergingReportBuilder.build();
245 }
246 }
247
248 // done with proper merging phase, now we need to trim unwanted elements, placeholder
249 // substitution and system properties injection.
250 ElementsTrimmer.trim(xmlDocumentOptional.get(), mergingReportBuilder);
251 if (mergingReportBuilder.hasErrors()) {
252 return mergingReportBuilder.build();
253 }
254
255 if (!mOptionalFeatures.contains(Invoker.Feature.NO_PLACEHOLDER_REPLACEMENT)) {
256 // do one last placeholder substitution, this is useful as we don't stop the build
257 // when a library failed a placeholder substitution, but the element might have
258 // been overridden so the problem was transient. However, with the final document
259 // ready, all placeholders values must have been provided.
260 performPlaceHolderSubstitution(loadedMainManifestInfo, xmlDocumentOptional.get(),
261 mergingReportBuilder);
262 if (mergingReportBuilder.hasErrors()) {
263 return mergingReportBuilder.build();
264 }
265 }
266
267 // perform system property injection.
268 performSystemPropertiesInjection(mergingReportBuilder, xmlDocumentOptional.get());
269
270 XmlDocument finalMergedDocument = xmlDocumentOptional.get();
271 PostValidator.validate(finalMergedDocument, mergingReportBuilder);
272 if (mergingReportBuilder.hasErrors()) {
273 finalMergedDocument.getRootNode().addMessage(mergingReportBuilder,
274 MergingReport.Record.Severity.WARNING,
275 "Post merge validation failed");
276 }
277 // finally optional features handling.
278 processOptionalFeatures(finalMergedDocument, mergingReportBuilder);
279
280 MergingReport mergingReport = mergingReportBuilder.build();
281
282 if (mReportFile.isPresent()) {
283 writeReport(mergingReport);
284 }
285
286 return mergingReport;
287 }
288
289 private void processOptionalFeatures(
290 @Nullable XmlDocument document,
291 @NonNull MergingReport.Builder mergingReport) {
292 if (document == null) {
293 return;
294 }
295
296 // perform tools: annotations removal if requested.
297 if (mOptionalFeatures.contains(Invoker.Feature.REMOVE_TOOLS_DECLARATIONS)) {
298 document = ToolsInstructionsCleaner.cleanToolsReferences(mMergeType, document, mLogger);
299 }
300 if (document != null) {
301 if (mOptionalFeatures.contains(Invoker.Feature.EXTRACT_FQCNS)) {
302 extractFcqns(document);
303 }
304
305 mergingReport.setMergedXmlDocument(
306 MergingReport.MergedManifestKind.MERGED, document);
307 if (!mOptionalFeatures.contains(Invoker.Feature.SKIP_XML_STRING)) {
308 mergingReport.setMergedDocument(
309 MergingReport.MergedManifestKind.MERGED, document.prettyPrint());
310 }
311
312 if (!mOptionalFeatures.contains(Invoker.Feature.SKIP_BLAME)) {
313 try {
314 mergingReport.setMergedDocument(MergingReport.MergedManifestKind.BLAME,
315 mergingReport.blame(document));
316 }
317 catch (Exception e) {
318 mLogger.error(e, "Error while saving blame file, build will continue");
319 }
320 }
321
322 if (mOptionalFeatures.contains(Invoker.Feature.MAKE_AAPT_SAFE)) {
323 PlaceholderEncoder.visit(document);
324 mergingReport.setMergedDocument(
325 MergingReport.MergedManifestKind.AAPT_SAFE,
326 document.prettyPrint());
327 }
328
329 // Always save the pre InstantRun state in case some APT plugins require it.
330 if (mOptionalFeatures.contains(Invoker.Feature.INSTANT_RUN_REPLACEMENT)) {
331 mergingReport.setMergedDocument(
332 MergingReport.MergedManifestKind.INSTANT_RUN,
333 instantRunReplacement(document).prettyPrint());
334 }
335 }
336 }
337
338 @NonNull
339 private static XmlDocument instantRunReplacement(XmlDocument document) {
340 Optional<XmlElement> applicationOptional = document
341 .getByTypeAndKey(ManifestModel.NodeTypes.APPLICATION, null /* keyValue */);
342 if (applicationOptional.isPresent()) {
343 XmlElement application = applicationOptional.get();
344 Attr nameAttribute = application.getXml().getAttributeNodeNS(
345 SdkConstants.ANDROID_URI, "name");
346
347 if (nameAttribute != null) {
348 String originalAppName = nameAttribute.getValue();
349 if (BOOTSTRAP_APPLICATION.equals(originalAppName)) {
350 return document;
351 }
352 application.getXml().setAttribute(SdkConstants.ATTR_NAME, originalAppName);
353 application.getXml().setAttributeNS(
354 SdkConstants.ANDROID_URI,
355 nameAttribute.getName(),
356 BOOTSTRAP_APPLICATION);
357 } else {
358 application.getXml().setAttributeNS(
359 SdkConstants.ANDROID_URI,
360 SdkConstants.ANDROID_NS_NAME_PREFIX + SdkConstants.ATTR_NAME,
361 BOOTSTRAP_APPLICATION);
362 }
363 }
364 return document.reparse();
365 }
366
367 /**
368 * Returns the {@link FileStreamProvider} used by this manifest merger. Use this
369 * to read files if you need to access the content of a {@link XmlDocument}.
370 */
371 @SuppressWarnings("unused") // Allow future library usage, if necessary
372 @NonNull
373 public FileStreamProvider getFileStreamProvider() {
374 return mFileStreamProvider;
375 }
376
377 /**
378 * Creates the merging report file.
379 * @param mergingReport the merging activities report to serialize.
380 */
381 private void writeReport(@NonNull MergingReport mergingReport) {
382 FileWriter fileWriter = null;
383 try {
384 if (!mReportFile.get().getParentFile().exists()
385 && !mReportFile.get().getParentFile().mkdirs()) {
386 mLogger.warning(String.format(
387 "Cannot create %1$s manifest merger report file,"
388 + "build will continue but merging activities "
389 + "will not be documented",
390 mReportFile.get().getAbsolutePath()));
391 } else {
392 fileWriter = new FileWriter(mReportFile.get());
393 mergingReport.getActions().log(fileWriter);
394 }
395 } catch (IOException e) {
396 mLogger.warning(String.format(
397 "Error '%1$s' while writing the merger report file, "
398 + "build can continue but merging activities "
399 + "will not be documented ",
400 e.getMessage()));
401 } finally {
402 if (fileWriter != null) {
403 try {
404 fileWriter.close();
405 } catch (IOException e) {
406 mLogger.warning(String.format(
407 "Error '%1$s' while closing the merger report file, "
408 + "build can continue but merging activities "
409 + "will not be documented ",
410 e.getMessage()));
411 }
412 }
413 }
414 }
415
416 /**
417 * shorten all fully qualified class name that belong to the same package as the manifest's
418 * package attribute value.
419 * @param finalMergedDocument the AndroidManifest.xml document.
420 */
421 private static void extractFcqns(@NonNull XmlDocument finalMergedDocument) {
422 extractFcqns(finalMergedDocument.getPackageName(), finalMergedDocument.getRootNode());
423 }
424
425 /**
426 * shorten recursively all attributes that are package dependent of the passed nodes and all
427 * its child nodes.
428 * @param packageName the manifest package name.
429 * @param xmlElement the xml element to process recursively.
430 */
431 private static void extractFcqns(@NonNull String packageName, @NonNull XmlElement xmlElement) {
432 for (XmlAttribute xmlAttribute : xmlElement.getAttributes()) {
433 if (xmlAttribute.getModel() !=null && xmlAttribute.getModel().isPackageDependent()) {
434 String value = xmlAttribute.getValue();
435 if (value.startsWith(packageName) &&
436 value.charAt(packageName.length()) == '.') {
437 xmlAttribute.getXml().setValue(value.substring(packageName.length()));
438 }
439 }
440 }
441 for (XmlElement child : xmlElement.getMergeableElements()) {
442 extractFcqns(packageName, child);
443 }
444 }
445
446 /**
447 * Load an xml file and perform placeholder substitution
448 * @param manifestInfo the android manifest information like if it is a library, an
449 * overlay or a main manifest file.
450 * @param selectors all the libraries selectors
451 * @param mergingReportBuilder the merging report to store events and errors.
452 * @return a loaded manifest info.
453 * @throws MergeFailureException
454 */
455 @NonNull
456 private LoadedManifestInfo load(
457 @NonNull ManifestInfo manifestInfo,
458 @NonNull KeyResolver<String> selectors,
459 @NonNull MergingReport.Builder mergingReportBuilder) throws MergeFailureException {
460
461 File xmlFile = manifestInfo.mLocation;
462 XmlDocument xmlDocument;
463 try {
464 InputStream inputStream = mFileStreamProvider.getInputStream(xmlFile);
465 xmlDocument = XmlLoader.load(selectors,
466 mSystemPropertyResolver,
467 manifestInfo.mName,
468 xmlFile,
469 inputStream,
470 manifestInfo.getType(),
471 manifestInfo.getMainManifestPackageName());
472 } catch (Exception e) {
473 throw new MergeFailureException(e);
474 }
475
476 String originalPackageName = xmlDocument.getPackageName();
477 MergingReport.Builder builder = manifestInfo.getType() == XmlDocument.Type.MAIN
478 ? mergingReportBuilder
479 : new MergingReport.Builder(mergingReportBuilder.getLogger());
480
481 builder.getActionRecorder().recordDefaultNodeAction(
482 xmlDocument.getRootNode());
483
484 // perform place holder substitution, this is necessary to do so early in case placeholders
485 // are used in key attributes.
486 performPlaceHolderSubstitution(manifestInfo, xmlDocument, builder);
487
488 return new LoadedManifestInfo(manifestInfo,
489 Optional.fromNullable(originalPackageName), xmlDocument);
490 }
491
492 private void performPlaceHolderSubstitution(@NonNull ManifestInfo manifestInfo,
493 @NonNull XmlDocument xmlDocument,
494 @NonNull MergingReport.Builder mergingReportBuilder) {
495
496 if (mOptionalFeatures.contains(Invoker.Feature.NO_PLACEHOLDER_REPLACEMENT)) {
497 return;
498 }
499
500 // check for placeholders presence, switch first the packageName and application id if
501 // it is not explicitly set, unless dealing with a library. In case of library, the
502 // implicit ${applicationId} (when not provided though the build.gradle) cannot be
503 // set during the initial library manifest file parsing but during the final
504 // placeholder substitution once the application's applicationId is known.
505 Map<String, Object> finalPlaceHolderValues = mPlaceHolderValues;
506 if ((!mPlaceHolderValues.containsKey(PlaceholderHandler.APPLICATION_ID))
507 && manifestInfo.getType() != XmlDocument.Type.LIBRARY) {
508 String packageName = manifestInfo.getMainManifestPackageName().isPresent()
509 ? manifestInfo.getMainManifestPackageName().get()
510 : xmlDocument.getPackageName();
511 // add all existing placeholders except package name that will be swapped.
512 ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
513 for (Map.Entry<String, Object> entry : mPlaceHolderValues.entrySet()) {
514 if (!entry.getKey().equals(PlaceholderHandler.PACKAGE_NAME)) {
515 builder.put(entry);
516 }
517 }
518 builder.put(PlaceholderHandler.PACKAGE_NAME, packageName);
519 if (mMergeType != MergeType.LIBRARY) {
520 builder.put(PlaceholderHandler.APPLICATION_ID, packageName);
521 }
522 finalPlaceHolderValues = builder.build();
523 }
524
525 KeyBasedValueResolver<String> placeHolderValueResolver =
526 new MapBasedKeyBasedValueResolver<String>(finalPlaceHolderValues);
527 PlaceholderHandler.visit(
528 mMergeType,
529 xmlDocument,
530 placeHolderValueResolver,
531 mergingReportBuilder);
532 }
533
534 // merge the optionally existing xmlDocument with a lower priority xml file.
535 private Optional<XmlDocument> merge(
536 @NonNull Optional<XmlDocument> xmlDocument,
537 @NonNull LoadedManifestInfo lowerPriorityDocument,
538 @NonNull MergingReport.Builder mergingReportBuilder) throws MergeFailureException {
539
540 MergingReport.Result validationResult = PreValidator
541 .validate(mergingReportBuilder, lowerPriorityDocument.getXmlDocument());
542 if (validationResult == MergingReport.Result.ERROR) {
543 mergingReportBuilder.addMessage(
544 lowerPriorityDocument.getXmlDocument().getSourceFile(),
545 MergingReport.Record.Severity.ERROR,
546 "Validation failed, exiting");
547 return Optional.absent();
548 }
549 Optional<XmlDocument> result;
550 if (xmlDocument.isPresent()) {
551 result = xmlDocument.get().merge(
552 lowerPriorityDocument.getXmlDocument(), mergingReportBuilder);
553 } else {
554 mergingReportBuilder.getActionRecorder().recordDefaultNodeAction(
555 lowerPriorityDocument.getXmlDocument().getRootNode());
556 result = Optional.of(lowerPriorityDocument.getXmlDocument());
557 }
558
559 // if requested, dump each intermediary merging stage into the report.
560 if (mOptionalFeatures.contains(Invoker.Feature.KEEP_INTERMEDIARY_STAGES)
561 && result.isPresent()) {
562 mergingReportBuilder.addMergingStage(result.get().prettyPrint());
563 }
564
565 return result;
566 }
567
568 private List<LoadedManifestInfo> loadLibraries(@NonNull SelectorResolver selectors,
569 @NonNull MergingReport.Builder mergingReportBuilder) throws MergeFailureException {
570
571 ImmutableList.Builder<LoadedManifestInfo> loadedLibraryDocuments = ImmutableList.builder();
572 for (Pair<String, File> libraryFile : Sets.newLinkedHashSet(mLibraryFiles)) {
573 mLogger.info("Loading library manifest " + libraryFile.getSecond().getPath());
574 ManifestInfo manifestInfo = new ManifestInfo(libraryFile.getFirst(),
575 libraryFile.getSecond(),
576 XmlDocument.Type.LIBRARY, Optional.<String>absent());
577 File xmlFile = manifestInfo.mLocation;
578 XmlDocument libraryDocument;
579 try {
580 InputStream inputStream = mFileStreamProvider.getInputStream(xmlFile);
581 libraryDocument = XmlLoader.load(selectors,
582 mSystemPropertyResolver,
583 manifestInfo.mName,
584 xmlFile,
585 inputStream,
586 XmlDocument.Type.LIBRARY,
587 Optional.<String>absent() /* mainManifestPackageName */);
588 } catch (Exception e) {
589 throw new MergeFailureException(e);
590 }
591 // extract the package name...
592 String libraryPackage = libraryDocument.getRootNode().getXml().getAttribute("package");
593 // save it in the selector instance.
594 if (!Strings.isNullOrEmpty(libraryPackage)) {
595 selectors.addSelector(libraryPackage, libraryFile.getFirst());
596 }
597
598 // perform placeholder substitution, this is useful when the library is using
599 // a placeholder in a key element, we however do not need to record these
600 // substitutions so feed it with a fake merging report.
601 MergingReport.Builder builder = new MergingReport.Builder(mergingReportBuilder.getLogger());
602 builder.getActionRecorder().recordDefaultNodeAction(libraryDocument.getRootNode());
603 performPlaceHolderSubstitution(manifestInfo, libraryDocument, builder);
604 if (builder.hasErrors()) {
605 // we log the errors but continue, in case the error is of no consequence
606 // to the application consuming the library.
607 builder.build().log(mLogger);
608 }
609
610 loadedLibraryDocuments.add(new LoadedManifestInfo(manifestInfo,
611 Optional.fromNullable(libraryDocument.getPackageName()),
612 libraryDocument));
613 }
614 return loadedLibraryDocuments.build();
615 }
616
617 /**
618 * Creates a new {@link com.android.manifmerger.ManifestMerger2.Invoker} instance to invoke
619 * the merging tool to merge manifest files for an application.
620 *
621 * @param mainManifestFile application main manifest file.
622 * @param logger the logger interface to use.
623 * @return an {@link com.android.manifmerger.ManifestMerger2.Invoker} instance that will allow
624 * further customization and trigger the merging tool.
625 */
626 @NonNull
627 public static Invoker newMerger(@NonNull File mainManifestFile,
628 @NonNull ILogger logger,
629 @NonNull MergeType mergeType) {
630 return new Invoker(mainManifestFile, logger, mergeType);
631 }
632
633 /**
634 * Defines the merging type expected from the tool.
635 */
636 public enum MergeType {
637 /**
638 * Application merging type is used when packaging an application with a set of imported
639 * libraries. The resulting merged android manifest is final and is not expected to be
640 * imported in another application.
641 */
642 APPLICATION,
643
644 /**
645 * Library merging typee is used when packaging a library. The resulting android manifest
646 * file will not merge in all the imported libraries this library depends on. Also the tools
647 * annotations will not be removed as they can be useful when later importing the resulting
648 * merged android manifest into an application.
649 */
650 LIBRARY
651 }
652
653 /**
654 * Defines a property that can add or override itself into an XML document.
655 */
656 public interface AutoAddingProperty {
657
658 /**
659 * Add itself (possibly just override the current value) with the passed value
660 * @param actionRecorder to record actions.
661 * @param document the xml document to add itself to.
662 * @param value the value to set of this property.
663 */
664 void addTo(@NonNull ActionRecorder actionRecorder,
665 @NonNull XmlDocument document,
666 @NonNull String value);
667 }
668
669 /**
670 * Perform {@link ManifestSystemProperty} injection.
671 * @param mergingReport to log actions and errors.
672 * @param xmlDocument the xml document to inject into.
673 */
674 protected void performSystemPropertiesInjection(
675 @NonNull MergingReport.Builder mergingReport,
676 @NonNull XmlDocument xmlDocument) {
677 for (ManifestSystemProperty manifestSystemProperty : ManifestSystemProperty.values()) {
678 String propertyOverride = mSystemPropertyResolver.getValue(manifestSystemProperty);
679 if (propertyOverride != null) {
680 manifestSystemProperty.addTo(
681 mergingReport.getActionRecorder(), xmlDocument, propertyOverride);
682 }
683 }
684 }
685
686 /**
687 * A {@linkplain FileStreamProvider} provides (buffered, if necessary) {@link InputStream}
688 * instances for a given {@link File} handle.
689 */
690 public static class FileStreamProvider {
691 /**
692 * Creates a reader for the given file -- which may not necessarily read the contents of the
693 * file on disk. For example, in the IDE, the client will map the file handle to a document in
694 * the editor, and read the current contents of that editor whether or not it has been saved.
695 * <p>
696 * This method is responsible for providing its own buffering, if necessary (e.g. when
697 * reading from disk, make sure you wrap the file stream in a buffering input stream.)
698 *
699 * @param file the file handle
700 * @return the contents of the file
701 * @throws FileNotFoundException if the file handle is invalid
702 */
703 @SuppressWarnings("MethodMayBeStatic") // Intended for overrides outside this library
704 protected InputStream getInputStream(@NonNull File file) throws FileNotFoundException {
705 return new BufferedInputStream(new FileInputStream(file));
706 }
707 }
708
709 /**
710 * This class will hold all invocation parameters for the manifest merging tool.
711 *
712 * There are broadly three types of input to the merging tool :
713 * <ul>
714 * <li>Build types and flavors overriding manifests</li>
715 * <li>Application main manifest</li>
716 * <li>Library manifest files</li>
717 * </ul>
718 *
719 * Only the main manifest file is a mandatory parameter.
720 *
721 * High level description of the merging will be as follow :
722 * <ol>
723 * <li>Build type and flavors will be merged first in the order they were added. Highest
724 * priority file added first, lowest added last.</li>
725 * <li>Resulting document is merged with lower priority application main manifest file.</li>
726 * <li>Resulting document is merged with each library file manifest file in the order
727 * they were added. Highest priority added first, lowest added last.</li>
728 * <li>Resulting document is returned as results of the merging process.</li>
729 * </ol>
730 *
731 */
732 public static class Invoker<T extends Invoker<T>>{
733
734 protected final File mMainManifestFile;
735
736 protected final ImmutableMap.Builder<ManifestSystemProperty, Object> mSystemProperties =
737 new ImmutableMap.Builder<ManifestSystemProperty, Object>();
738
739 @NonNull
740 protected final ILogger mLogger;
741 @NonNull
742 protected final ImmutableMap.Builder<String, Object> mPlaceholders =
743 new ImmutableMap.Builder<String, Object>();
744 @NonNull
745 private final ImmutableList.Builder<Pair<String, File>> mLibraryFilesBuilder =
746 new ImmutableList.Builder<Pair<String, File>>();
747 @NonNull
748 private final ImmutableList.Builder<File> mFlavorsAndBuildTypeFiles =
749 new ImmutableList.Builder<File>();
750 @NonNull
751 private final ImmutableList.Builder<Feature> mFeaturesBuilder =
752 new ImmutableList.Builder<Feature>();
753 @NonNull
754 private final MergeType mMergeType;
755 @Nullable private File mReportFile;
756
757 @Nullable
758 private FileStreamProvider mFileStreamProvider;
759
760 /**
761 * Sets a value for a {@link ManifestSystemProperty}
762 * @param override the property to set
763 * @param value the value for the property
764 * @return itself.
765 */
766 @NonNull
767 public Invoker setOverride(@NonNull ManifestSystemProperty override, @NonNull String value) {
768 mSystemProperties.put(override, value);
769 return thisAsT();
770 }
771
772 /**
773 * Adds placeholders names and associated values for substitution.
774 * @return itself.
775 */
776 @NonNull
777 public Invoker setPlaceHolderValues(@NonNull Map<String, Object> keyValuePairs) {
778 mPlaceholders.putAll(keyValuePairs);
779 return thisAsT();
780 }
781
782 /**
783 * Adds a new placeholder name and value for substitution.
784 * @return itself.
785 */
786 @NonNull
787 public Invoker setPlaceHolderValue(@NonNull String placeHolderName, @NonNull String value) {
788 mPlaceholders.put(placeHolderName, value);
789 return thisAsT();
790 }
791
792 /**
793 * Optional behavior of the merging tool can be turned on by setting these Feature.
794 */
795 public enum Feature {
796
797 /**
798 * Keep all intermediary merged files during the merging process. This is particularly
799 * useful for debugging/tracing purposes.
800 */
801 KEEP_INTERMEDIARY_STAGES,
802
803 /**
804 * When logging file names, use {@link java.io.File#getName()} rather than
805 * {@link java.io.File#getPath()}
806 */
807 PRINT_SIMPLE_FILENAMES,
808
809 /**
810 * Perform a sweep after all merging activities to remove all fully qualified class
811 * names and replace them with the equivalent short version.
812 */
813 EXTRACT_FQCNS,
814
815 /**
816 * Perform a sweep after all merging activities to remove all tools: decorations.
817 */
818 REMOVE_TOOLS_DECLARATIONS,
819
820 /**
821 * Do no perform placeholders replacement.
822 */
823 NO_PLACEHOLDER_REPLACEMENT,
824
825 /**
826 * Encode unresolved placeholders to be AAPT friendly.
827 */
828 MAKE_AAPT_SAFE,
829
830 /**
831 * Perform InstantRun related swapping in the merged manifest file.
832 */
833 INSTANT_RUN_REPLACEMENT,
834
835 /**
836 * Clients will not request the blame history
837 */
838 SKIP_BLAME,
839
840 /**
841 * Clients will only request the merged XML documents, not XML pretty printed documents
842 */
843 SKIP_XML_STRING,
844 }
845
846 /**
847 * Creates a new builder with the mandatory main manifest file.
848 * @param mainManifestFile application main manifest file.
849 * @param logger the logger interface to use.
850 */
851 private Invoker(
852 @NonNull File mainManifestFile,
853 @NonNull ILogger logger,
854 @NonNull MergeType mergeType) {
855 this.mMainManifestFile = Preconditions.checkNotNull(mainManifestFile);
856 this.mLogger = logger;
857 this.mMergeType = mergeType;
858 }
859
860 /**
861 * Sets the file to use to write the merging report. If not called,
862 * the merging process will not write a report.
863 * @param mergeReport the file to write the report in.
864 * @return itself.
865 */
866 @NonNull
867 public Invoker setMergeReportFile(@Nullable File mergeReport) {
868 mReportFile = mergeReport;
869 return this;
870 }
871
872 /**
873 * Add one library file manifest, will be added last in the list of library files which will
874 * make the parameter the lowest priority library manifest file.
875 * @param file the library manifest file to add.
876 * @return itself.
877 */
878 @NonNull
879 public Invoker addLibraryManifest(@NonNull File file) {
880 if (mMergeType == MergeType.LIBRARY) {
881 throw new IllegalStateException(
882 "Cannot add library dependencies manifests when creating a library");
883 }
884 mLibraryFilesBuilder.add(Pair.of(file.getName(), file));
885 return thisAsT();
886 }
887
888 /**
889 * Sets library dependencies for this merging activity.
890 * @param libraries the list of library dependencies.
891 * @return itself.
892 */
893 @NonNull
894 public Invoker addBundleManifests(@NonNull List<? extends AndroidBundle> libraries) {
895 for (AndroidBundle library : libraries) {
896 if (!library.isProvided()) {
897 mLibraryFilesBuilder.add(Pair.of(library.getName(), library.getManifest()));
898 }
899 }
900 return thisAsT();
901 }
902
903 @NonNull
904 public Invoker addLibraryManifests(@NonNull List<Pair<String, File>> libraries) {
905 mLibraryFilesBuilder.addAll(libraries);
906 return thisAsT();
907 }
908
909 /**
910 * Add several library file manifests at then end of the list which will make them the
911 * lowest priority manifest files. The relative priority between all the files passed as
912 * parameters will be respected.
913 * @param files library manifest files to add last.
914 * @return itself.
915 */
916 @NonNull
917 public Invoker addLibraryManifests(@NonNull File... files) {
918 for (File file : files) {
919 addLibraryManifest(file);
920 }
921 return thisAsT();
922 }
923
924 /**
925 * Add a flavor or build type manifest file last in the list.
926 * @param file build type or flavor manifest file
927 * @return itself.
928 */
929 @NonNull
930 public Invoker addFlavorAndBuildTypeManifest(@NonNull File file) {
931 this.mFlavorsAndBuildTypeFiles.add(file);
932 return thisAsT();
933 }
934
935 /**
936 * Add several flavor or build type manifest files last in the list. Relative priorities
937 * between the passed files as parameters will be respected.
938 * @param files build type of flavor manifest files to add.
939 * @return itself.
940 */
941 @NonNull
942 public Invoker addFlavorAndBuildTypeManifests(File... files) {
943 this.mFlavorsAndBuildTypeFiles.add(files);
944 return thisAsT();
945 }
946
947 /**
948 * Sets some optional features for the merge tool.
949 *
950 * @param features one to many features to set.
951 * @return itself.
952 */
953 @NonNull
954 public Invoker withFeatures(Feature...features) {
955 mFeaturesBuilder.add(features);
956 return thisAsT();
957 }
958
959 /**
960 * Sets a file stream provider which allows the client of the manifest merger to provide
961 * arbitrary content lookup for files. <p> NOTE: There should only be one.
962 *
963 * @param provider the provider to use
964 * @return itself.
965 */
966 @NonNull
967 public Invoker withFileStreamProvider(@Nullable FileStreamProvider provider) {
968 assert mFileStreamProvider == null || provider == null;
969 mFileStreamProvider = provider;
970 return thisAsT();
971 }
972
973 /**
974 * Perform the merging and return the result.
975 *
976 * @return an instance of {@link com.android.manifmerger.MergingReport} that will give
977 * access to all the logging and merging records.
978 *
979 * This method can be invoked several time and will re-do the file merges.
980 *
981 * @throws com.android.manifmerger.ManifestMerger2.MergeFailureException if the merging
982 * cannot be completed successfully.
983 */
984 @NonNull
985 public MergingReport merge() throws MergeFailureException {
986
987 // provide some free placeholders values.
988 ImmutableMap<ManifestSystemProperty, Object> systemProperties = mSystemProperties.build();
989 if (systemProperties.containsKey(ManifestSystemProperty.PACKAGE)) {
990 // if the package is provided, make it available for placeholder replacement.
991 mPlaceholders.put(PACKAGE_NAME, systemProperties.get(ManifestSystemProperty.PACKAGE));
992 // as well as applicationId since package system property overrides everything
993 // but not when output is a library since only the final (application)
994 // application Id should be used to replace libraries "applicationId" placeholders.
995 if (mMergeType != MergeType.LIBRARY) {
996 mPlaceholders.put(APPLICATION_ID, systemProperties.get(ManifestSystemProperty.PACKAGE));
997 }
998 }
999
1000 FileStreamProvider fileStreamProvider = mFileStreamProvider != null
1001 ? mFileStreamProvider : new FileStreamProvider();
1002 ManifestMerger2 manifestMerger =
1003 new ManifestMerger2(
1004 mLogger,
1005 mMainManifestFile,
1006 mLibraryFilesBuilder.build(),
1007 mFlavorsAndBuildTypeFiles.build(),
1008 mFeaturesBuilder.build(),
1009 mPlaceholders.build(),
1010 new MapBasedKeyBasedValueResolver<ManifestSystemProperty>(systemProperties),
1011 mMergeType,
1012 Optional.fromNullable(mReportFile),
1013 fileStreamProvider);
1014 return manifestMerger.merge();
1015 }
1016
1017 @NonNull
1018 @SuppressWarnings("unchecked")
1019 private T thisAsT() {
1020 return (T) this;
1021 }
1022 }
1023
1024 /**
1025 * Helper class for map based placeholders key value pairs.
1026 */
1027 public static class MapBasedKeyBasedValueResolver<T> implements KeyBasedValueResolver<T> {
1028
1029 private final ImmutableMap<T, Object> keyValues;
1030
1031 public MapBasedKeyBasedValueResolver(@NonNull Map<T, Object> keyValues) {
1032 this.keyValues = ImmutableMap.copyOf(keyValues);
1033 }
1034
1035 @Nullable
1036 @Override
1037 public String getValue(@NonNull T key) {
1038 Object value = keyValues.get(key);
1039 return value == null ? null : value.toString();
1040 }
1041 }
1042
1043 private static class ManifestInfo {
1044
1045 private ManifestInfo(
1046 String name,
1047 File location,
1048 XmlDocument.Type type,
1049 Optional<String> mainManifestPackageName) {
1050 mName = name;
1051 mLocation = location;
1052 mType = type;
1053 mMainManifestPackageName = mainManifestPackageName;
1054 }
1055
1056 private final String mName;
1057 private final File mLocation;
1058 private final XmlDocument.Type mType;
1059 private final Optional<String> mMainManifestPackageName;
1060
1061 File getLocation() {
1062 return mLocation;
1063 }
1064
1065 XmlDocument.Type getType() {
1066 return mType;
1067 }
1068
1069 Optional<String> getMainManifestPackageName() {
1070 return mMainManifestPackageName;
1071 }
1072 }
1073
1074 private static class LoadedManifestInfo extends ManifestInfo {
1075
1076 @NonNull private final XmlDocument mXmlDocument;
1077 @NonNull private final Optional<String> mOriginalPackageName;
1078
1079 private LoadedManifestInfo(@NonNull ManifestInfo manifestInfo,
1080 @NonNull Optional<String> originalPackageName,
1081 @NonNull XmlDocument xmlDocument) {
1082 super(manifestInfo.mName,
1083 manifestInfo.mLocation,
1084 manifestInfo.mType,
1085 manifestInfo.getMainManifestPackageName());
1086 mXmlDocument = xmlDocument;
1087 mOriginalPackageName = originalPackageName;
1088 }
1089
1090 @NonNull
1091 public XmlDocument getXmlDocument() {
1092 return mXmlDocument;
1093 }
1094
1095 @NonNull
1096 public Optional<String> getOriginalPackageName() {
1097 return mOriginalPackageName;
1098 }
1099 }
1100
1101 /**
1102 * Implementation a {@link com.android.manifmerger.KeyResolver} capable of resolving all
1103 * selectors value in the context of the passed libraries to this merging activities.
1104 */
1105 static class SelectorResolver implements KeyResolver<String> {
1106
1107 private final Map<String, String> mSelectors = new HashMap<String, String>();
1108
1109 protected void addSelector(String key, String value) {
1110 mSelectors.put(key, value);
1111 }
1112
1113 @Nullable
1114 @Override
1115 public String resolve(String key) {
1116 return mSelectors.get(key);
1117 }
1118
1119 @NonNull
1120 @Override
1121 public Iterable<String> getKeys() {
1122 return mSelectors.keySet();
1123 }
1124 }
1125
1126 // a wrapper exception to all sorts of failure exceptions that can be thrown during merging.
1127 public static class MergeFailureException extends Exception {
1128
1129 protected MergeFailureException(Exception cause) {
1130 super(cause);
1131 }
1132 }
1133 }