use of com.android.manifmerger.ManifestMerger2 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;
}
use of com.android.manifmerger.ManifestMerger2 in project bazel by bazelbuild.
the class AndroidManifestProcessor method processManifest.
public MergedAndroidData processManifest(VariantType variantType, String customPackageForR, String applicationId, int versionCode, String versionName, MergedAndroidData primaryData, Path processedManifest) throws IOException {
ManifestMerger2.MergeType mergeType = variantType == VariantType.DEFAULT ? ManifestMerger2.MergeType.APPLICATION : ManifestMerger2.MergeType.LIBRARY;
String newManifestPackage = variantType == VariantType.DEFAULT ? applicationId : customPackageForR;
if (versionCode != -1 || versionName != null || newManifestPackage != null) {
Files.createDirectories(processedManifest.getParent());
// The generics on Invoker don't make sense, so ignore them.
@SuppressWarnings("unchecked") Invoker<?> manifestMergerInvoker = ManifestMerger2.newMerger(primaryData.getManifest().toFile(), stdLogger, mergeType);
// Stamp new package
if (newManifestPackage != null) {
manifestMergerInvoker.setOverride(SystemProperty.PACKAGE, newManifestPackage);
}
// Stamp version and applicationId (if provided) into the manifest
if (versionCode > 0) {
manifestMergerInvoker.setOverride(SystemProperty.VERSION_CODE, String.valueOf(versionCode));
}
if (versionName != null) {
manifestMergerInvoker.setOverride(SystemProperty.VERSION_NAME, versionName);
}
MergedManifestKind mergedManifestKind = MergedManifestKind.MERGED;
if (mergeType == ManifestMerger2.MergeType.APPLICATION) {
manifestMergerInvoker.withFeatures(Invoker.Feature.REMOVE_TOOLS_DECLARATIONS);
}
try {
MergingReport mergingReport = manifestMergerInvoker.merge();
switch(mergingReport.getResult()) {
case WARNING:
mergingReport.log(stdLogger);
writeMergedManifest(mergedManifestKind, mergingReport, processedManifest);
break;
case SUCCESS:
writeMergedManifest(mergedManifestKind, mergingReport, processedManifest);
break;
case ERROR:
mergingReport.log(stdLogger);
throw new RuntimeException(mergingReport.getReportString());
default:
throw new RuntimeException("Unhandled result type : " + mergingReport.getResult());
}
} catch (IOException | MergeFailureException e) {
throw new RuntimeException(e);
}
return new MergedAndroidData(primaryData.getResourceDir(), primaryData.getAssetDir(), processedManifest);
}
return primaryData;
}
use of com.android.manifmerger.ManifestMerger2 in project atlas by alibaba.
the class MergeManifestAwbsConfigAction method execute.
@Override
public void execute(MtlParallelTask parallelTask) {
super.execute(parallelTask);
AndroidDependencyTree androidDependencyTree = AtlasBuildContext.androidDependencyTrees.get(parallelTask.getVariantName());
if (null == androidDependencyTree) {
return;
}
List<DefaultTask> tasks = new ArrayList<DefaultTask>();
AppVariantOutputContext appVariantOutputContext = getAppVariantOutputContext();
for (final AwbBundle awbBundle : androidDependencyTree.getAwbBundles()) {
List<ManifestMerger2.Invoker.Feature> optionalFeatures = ImmutableList.<ManifestMerger2.Invoker.Feature>of();
MergeAwbManifests.ConfigAction configAction = new MergeAwbManifests.ConfigAction(appVariantOutputContext.getOutputScope(), awbBundle, optionalFeatures, appVariantOutputContext);
MergeAwbManifests mergeAwbManifests = TaskCreater.create(appVariantContext.getProject(), configAction.getName(), configAction.getType());
configAction.execute(mergeAwbManifests);
tasks.add(mergeAwbManifests);
}
parallelTask.parallelTask = tasks;
parallelTask.uniqueTaskName = getName();
}
use of com.android.manifmerger.ManifestMerger2 in project android by JetBrains.
the class AndroidManifestMergingBuilder method doMergeManifests.
private static boolean doMergeManifests(final CompileContext context, File manifestFile, List<File> libManifests, File outputFile) throws IOException {
final AndroidBuildTestingManager testingManager = AndroidBuildTestingManager.getTestingManager();
if (testingManager != null) {
final StringBuilder messageBuilder = new StringBuilder("manifest_merging\n");
messageBuilder.append(manifestFile.getPath()).append('\n');
Collections.sort(libManifests);
for (File libManifest : libManifests) {
messageBuilder.append(libManifest.getPath()).append('\n');
}
messageBuilder.append(outputFile.getPath());
testingManager.getCommandExecutor().log(messageBuilder.toString());
}
ImmutableList.Builder<Pair<String, File>> libraryFiles = ImmutableList.builder();
for (File f : libManifests) {
libraryFiles.add(Pair.of(f.getName(), f));
}
final ManifestMerger2.Invoker manifestMergerInvoker = ManifestMerger2.newMerger(manifestFile, NullLogger.getLogger(), ManifestMerger2.MergeType.APPLICATION).addBundleManifests(libraryFiles.build());
MergingReport mergingReport;
try {
mergingReport = manifestMergerInvoker.merge();
} catch (ManifestMerger2.MergeFailureException e) {
context.processMessage(new CompilerMessage(BUILDER_NAME, BuildMessage.Kind.ERROR, e.getMessage()));
return false;
}
MergingReport.Result result = mergingReport.getResult();
for (MergingReport.Record record : mergingReport.getLoggingRecords()) {
SourceFilePosition position = record.getSourceLocation();
File sourceFile = position.getFile().getSourceFile();
String sourceFilePath = sourceFile != null ? sourceFile.getAbsolutePath() : null;
SourcePosition pos = position.getPosition();
MergingReport.Record.Severity severity = record.getSeverity();
if (severity != MergingReport.Record.Severity.INFO) {
context.processMessage(new CompilerMessage(BUILDER_NAME, toBuildMessageKind(record.getSeverity()), record.getMessage(), sourceFilePath, pos.getStartOffset(), pos.getEndOffset(), pos.getEndOffset(), pos.getEndLine(), pos.getEndColumn()));
}
}
if (!result.isError()) {
String xmlDocument = mergingReport.getMergedDocument(MergingReport.MergedManifestKind.MERGED);
Files.write(xmlDocument, outputFile, Charsets.UTF_8);
}
return result.isSuccess();
}
Aggregations