Search in sources :

Example 1 with SystemProperty

use of com.android.manifmerger.ManifestMerger2.SystemProperty in project bazel by bazelbuild.

the class AndroidManifestProcessor method mergeManifest.

/**
   * Merge several manifests into one and perform placeholder substitutions. This operation uses
   * Gradle semantics.
   *
   * @param manifest The primary manifest of the merge.
   * @param mergeeManifests Manifests to be merged into {@code manifest}.
   * @param mergeType Whether the merger should operate in application or library mode.
   * @param values A map of strings to be used as manifest placeholders and overrides. packageName
   *     is the only disallowed value and will be ignored.
   * @param output The path to write the resultant manifest to.
   * @param logFile The path to write the merger log to.
   * @return The path of the resultant manifest, either {@code output}, or {@code manifest} if no
   *     merging was required.
   * @throws IOException if there was a problem writing the merged manifest.
   */
// TODO(corysmith): Extract manifest processing.
public Path mergeManifest(Path manifest, Map<Path, String> mergeeManifests, MergeType mergeType, Map<String, String> values, Path output, Path logFile) throws IOException {
    if (mergeeManifests.isEmpty() && values.isEmpty()) {
        return manifest;
    }
    Invoker<?> manifestMerger = ManifestMerger2.newMerger(manifest.toFile(), stdLogger, mergeType);
    MergedManifestKind mergedManifestKind = MergedManifestKind.MERGED;
    if (mergeType == MergeType.APPLICATION) {
        manifestMerger.withFeatures(Feature.REMOVE_TOOLS_DECLARATIONS);
    }
    // Add mergee manifests
    List<Pair<String, File>> libraryManifests = new ArrayList<>();
    for (Entry<Path, String> mergeeManifest : mergeeManifests.entrySet()) {
        libraryManifests.add(Pair.of(mergeeManifest.getValue(), mergeeManifest.getKey().toFile()));
    }
    manifestMerger.addLibraryManifests(libraryManifests);
    // Extract SystemProperties from the provided values.
    Map<String, Object> placeholders = new HashMap<>();
    placeholders.putAll(values);
    for (SystemProperty property : SystemProperty.values()) {
        if (values.containsKey(SYSTEM_PROPERTY_NAMES.get(property))) {
            manifestMerger.setOverride(property, values.get(SYSTEM_PROPERTY_NAMES.get(property)));
            // placeholders to have the same value as specified by SystemProperty.PACKAGE.
            if (property == SystemProperty.PACKAGE) {
                placeholders.remove(PlaceholderHandler.APPLICATION_ID);
                placeholders.remove(PlaceholderHandler.PACKAGE_NAME);
            }
        }
    }
    // Add placeholders for all values.
    // packageName is populated from either the applicationId override or from the manifest itself;
    // it cannot be manually specified.
    placeholders.remove(PlaceholderHandler.PACKAGE_NAME);
    manifestMerger.setPlaceHolderValues(placeholders);
    try {
        MergingReport mergingReport = manifestMerger.merge();
        if (logFile != null) {
            logFile.getParent().toFile().mkdirs();
            try (PrintStream stream = new PrintStream(logFile.toFile())) {
                mergingReport.log(new AndroidResourceProcessor.PrintStreamLogger(stream));
            }
        }
        switch(mergingReport.getResult()) {
            case WARNING:
                mergingReport.log(stdLogger);
                Files.createDirectories(output.getParent());
                writeMergedManifest(mergedManifestKind, mergingReport, output);
                break;
            case SUCCESS:
                Files.createDirectories(output.getParent());
                writeMergedManifest(mergedManifestKind, mergingReport, output);
                break;
            case ERROR:
                mergingReport.log(stdLogger);
                throw new RuntimeException(mergingReport.getReportString());
            default:
                throw new RuntimeException("Unhandled result type : " + mergingReport.getResult());
        }
    } catch (MergeFailureException e) {
        throw new RuntimeException(e);
    }
    return output;
}
Also used : Path(java.nio.file.Path) MergingReport(com.android.manifmerger.MergingReport) PrintStream(java.io.PrintStream) MergeFailureException(com.android.manifmerger.ManifestMerger2.MergeFailureException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SystemProperty(com.android.manifmerger.ManifestMerger2.SystemProperty) MergedManifestKind(com.android.manifmerger.MergingReport.MergedManifestKind) Pair(com.android.utils.Pair)

Aggregations

MergeFailureException (com.android.manifmerger.ManifestMerger2.MergeFailureException)1 SystemProperty (com.android.manifmerger.ManifestMerger2.SystemProperty)1 MergingReport (com.android.manifmerger.MergingReport)1 MergedManifestKind (com.android.manifmerger.MergingReport.MergedManifestKind)1 Pair (com.android.utils.Pair)1 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1