Search in sources :

Example 6 with MergingException

use of com.android.ide.common.res2.MergingException in project bazel by bazelbuild.

the class AndroidResourceProcessingAction method main.

public static void main(String[] args) throws Exception {
    final Stopwatch timer = Stopwatch.createStarted();
    OptionsParser optionsParser = OptionsParser.newOptionsParser(Options.class, AaptConfigOptions.class);
    optionsParser.enableParamsFileSupport(FileSystems.getDefault());
    optionsParser.parseAndExitUponError(args);
    aaptConfigOptions = optionsParser.getOptions(AaptConfigOptions.class);
    options = optionsParser.getOptions(Options.class);
    final AndroidResourceProcessor resourceProcessor = new AndroidResourceProcessor(STD_LOGGER);
    try (ScopedTemporaryDirectory scopedTmp = new ScopedTemporaryDirectory("android_resources_tmp")) {
        final Path tmp = scopedTmp.getPath();
        final Path mergedAssets = tmp.resolve("merged_assets");
        final Path mergedResources = tmp.resolve("merged_resources");
        final Path filteredResources = tmp.resolve("resources-filtered");
        final Path densityManifest = tmp.resolve("manifest-filtered/AndroidManifest.xml");
        final Path processedManifest = tmp.resolve("manifest-processed/AndroidManifest.xml");
        final Path dummyManifest = tmp.resolve("manifest-aapt-dummy/AndroidManifest.xml");
        Path generatedSources = null;
        if (options.srcJarOutput != null || options.rOutput != null || options.symbolsOut != null) {
            generatedSources = tmp.resolve("generated_resources");
        }
        logger.fine(String.format("Setup finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        List<DependencyAndroidData> data = ImmutableSet.<DependencyAndroidData>builder().addAll(options.directData).addAll(options.transitiveData).build().asList();
        final MergedAndroidData mergedData = AndroidResourceMerger.mergeData(options.primaryData, options.directData, options.transitiveData, mergedResources, mergedAssets, selectPngCruncher(), options.packageType, options.symbolsOut);
        logger.fine(String.format("Merging finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        final DensityFilteredAndroidData filteredData = mergedData.filter(new DensitySpecificResourceFilter(options.densities, filteredResources, mergedResources), new DensitySpecificManifestProcessor(options.densities, densityManifest));
        logger.fine(String.format("Density filtering finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        MergedAndroidData processedData = AndroidManifestProcessor.with(STD_LOGGER).processManifest(options.packageType, options.packageForR, options.applicationId, options.versionCode, options.versionName, filteredData, processedManifest);
        // Write manifestOutput now before the dummy manifest is created.
        if (options.manifestOutput != null) {
            AndroidResourceOutputs.copyManifestToOutput(processedData, options.manifestOutput);
        }
        if (options.packageType == VariantType.LIBRARY) {
            resourceProcessor.writeDummyManifestForAapt(dummyManifest, options.packageForR);
            processedData = new MergedAndroidData(processedData.getResourceDir(), processedData.getAssetDir(), dummyManifest);
        }
        resourceProcessor.processResources(aaptConfigOptions.aapt, aaptConfigOptions.androidJar, aaptConfigOptions.buildToolsVersion, options.packageType, aaptConfigOptions.debug, options.packageForR, new FlagAaptOptions(aaptConfigOptions), aaptConfigOptions.resourceConfigs, aaptConfigOptions.splits, processedData, data, generatedSources, options.packagePath, options.proguardOutput, options.mainDexProguardOutput, options.resourcesOutput != null ? processedData.getResourceDir().resolve("values").resolve("public.xml") : null, options.dataBindingInfoOut);
        logger.fine(String.format("aapt finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        if (options.srcJarOutput != null) {
            AndroidResourceOutputs.createSrcJar(generatedSources, options.srcJarOutput, VariantType.LIBRARY == options.packageType);
        }
        if (options.rOutput != null) {
            AndroidResourceOutputs.copyRToOutput(generatedSources, options.rOutput, VariantType.LIBRARY == options.packageType);
        }
        if (options.resourcesOutput != null) {
            AndroidResourceOutputs.createResourcesZip(processedData.getResourceDir(), processedData.getAssetDir(), options.resourcesOutput, false);
        }
        logger.fine(String.format("Packaging finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    } catch (MergingException e) {
        logger.log(java.util.logging.Level.SEVERE, "Error during merging resources", e);
        throw e;
    } catch (IOException | InterruptedException | LoggedErrorException | UnrecognizedSplitsException e) {
        logger.log(java.util.logging.Level.SEVERE, "Error during processing resources", e);
        throw e;
    } catch (Exception e) {
        logger.log(java.util.logging.Level.SEVERE, "Unexpected", e);
        throw e;
    } finally {
        resourceProcessor.shutdown();
    }
    logger.fine(String.format("Resources processed in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
}
Also used : Path(java.nio.file.Path) AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions) FlagAaptOptions(com.google.devtools.build.android.AndroidResourceProcessor.FlagAaptOptions) FlagAaptOptions(com.google.devtools.build.android.AndroidResourceProcessor.FlagAaptOptions) UnrecognizedSplitsException(com.google.devtools.build.android.SplitConfigurationFilter.UnrecognizedSplitsException) MergingException(com.android.ide.common.res2.MergingException) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) OptionsParser(com.google.devtools.common.options.OptionsParser) UnrecognizedSplitsException(com.google.devtools.build.android.SplitConfigurationFilter.UnrecognizedSplitsException) LoggedErrorException(com.android.ide.common.internal.LoggedErrorException) MergingException(com.android.ide.common.res2.MergingException) IOException(java.io.IOException) LoggedErrorException(com.android.ide.common.internal.LoggedErrorException) AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)

Example 7 with MergingException

use of com.android.ide.common.res2.MergingException in project bazel by bazelbuild.

the class AndroidDataMerger method loadAndMerge.

/**
   * Loads a list of dependency {@link SerializedAndroidData} and merge with the primary {@link
   * ParsedAndroidData}.
   *
   * @see AndroidDataMerger#merge(ParsedAndroidData, ParsedAndroidData, UnvalidatedAndroidData,
   *     boolean) for details.
   */
UnwrittenMergedAndroidData loadAndMerge(List<? extends SerializedAndroidData> transitive, List<? extends SerializedAndroidData> direct, ParsedAndroidData primary, Path primaryManifest, boolean allowPrimaryOverrideAll) throws MergingException {
    Stopwatch timer = Stopwatch.createStarted();
    try {
        final ParsedAndroidData.Builder directBuilder = ParsedAndroidData.Builder.newBuilder();
        final ParsedAndroidData.Builder transitiveBuilder = ParsedAndroidData.Builder.newBuilder();
        final List<ListenableFuture<Boolean>> tasks = new ArrayList<>();
        for (final SerializedAndroidData dependency : direct) {
            tasks.add(executorService.submit(new ParseDependencyDataTask(dependency, directBuilder)));
        }
        for (final SerializedAndroidData dependency : transitive) {
            tasks.add(executorService.submit(new ParseDependencyDataTask(dependency, transitiveBuilder)));
        }
        // Wait for all the parsing to complete.
        FailedFutureAggregator<MergingException> aggregator = FailedFutureAggregator.createForMergingExceptionWithMessage("Failure(s) during dependency parsing");
        aggregator.aggregateAndMaybeThrow(tasks);
        logger.fine(String.format("Merged dependencies read in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        timer.reset().start();
        return doMerge(transitiveBuilder.build(), directBuilder.build(), primary, primaryManifest, allowPrimaryOverrideAll);
    } finally {
        logger.fine(String.format("Resources merged in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    }
}
Also used : Builder(com.google.devtools.build.android.ParsedAndroidData.Builder) MergingException(com.android.ide.common.res2.MergingException) Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture)

Example 8 with MergingException

use of com.android.ide.common.res2.MergingException in project buck by facebook.

the class MergeAndroidResourceSourcesStep method execute.

@Override
public StepExecutionResult execute(ExecutionContext context) throws IOException, InterruptedException {
    ResourceMerger merger = new ResourceMerger(1);
    try {
        for (Path resPath : resPaths) {
            Preconditions.checkState(resPath.isAbsolute());
            ResourceSet set = new ResourceSet(resPath.toString(), true);
            set.setDontNormalizeQualifiers(true);
            set.addSource(resPath.toFile());
            set.loadFromFiles(new ResourcesSetLoadLogger(context.getBuckEventBus()));
            merger.addDataSet(set);
        }
        MergedResourceWriter writer = MergedResourceWriter.createWriterWithoutPngCruncher(outFolderPath.toFile(), null, /*publicFile*/
        null, /*blameLogFolder*/
        new NoOpResourcePreprocessor(), tmpFolderPath.toFile());
        merger.mergeData(writer, /* cleanUp */
        false);
    } catch (MergingException e) {
        LOG.error(e, "Failed merging resources.");
        return StepExecutionResult.ERROR;
    }
    return StepExecutionResult.SUCCESS;
}
Also used : Path(java.nio.file.Path) MergedResourceWriter(com.android.ide.common.res2.MergedResourceWriter) MergingException(com.android.ide.common.res2.MergingException) ResourceMerger(com.android.ide.common.res2.ResourceMerger) ResourceSet(com.android.ide.common.res2.ResourceSet) NoOpResourcePreprocessor(com.android.ide.common.res2.NoOpResourcePreprocessor)

Example 9 with MergingException

use of com.android.ide.common.res2.MergingException in project paraphrase by JakeWharton.

the class ValueResourceParser method parseFile.

/**
   * Parses the file and returns a list of {@link ResourceItem} objects.
   * @return a list of resources.
   *
   * @throws MergingException if a merging exception happens
   */
@NonNull
private List<ResourceItem> parseFile() throws MergingException {
    Document document = parseDocument(file);
    // get the root node
    Node rootNode = document.getDocumentElement();
    if (rootNode == null) {
        return Collections.emptyList();
    }
    NodeList nodes = rootNode.getChildNodes();
    final int count = nodes.getLength();
    // list containing the result
    List<ResourceItem> resources = Lists.newArrayListWithExpectedSize(count);
    for (int i = 0, n = nodes.getLength(); i < n; i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        ResourceItem resource = getResource(node);
        if (resource != null) {
            resources.add(resource);
        }
    }
    return resources;
}
Also used : Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) ResourceItem(com.android.ide.common.res2.ResourceItem) NonNull(com.android.annotations.NonNull)

Aggregations

MergingException (com.android.ide.common.res2.MergingException)8 Stopwatch (com.google.common.base.Stopwatch)5 IOException (java.io.IOException)5 Path (java.nio.file.Path)5 OptionsParser (com.google.devtools.common.options.OptionsParser)4 AaptConfigOptions (com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)3 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 Builder (com.google.devtools.build.android.ParsedAndroidData.Builder)2 ArrayList (java.util.ArrayList)2 NonNull (com.android.annotations.NonNull)1 VariantType (com.android.builder.core.VariantType)1 LoggedErrorException (com.android.ide.common.internal.LoggedErrorException)1 PngException (com.android.ide.common.internal.PngException)1 MergedResourceWriter (com.android.ide.common.res2.MergedResourceWriter)1 NoOpResourcePreprocessor (com.android.ide.common.res2.NoOpResourcePreprocessor)1 ResourceItem (com.android.ide.common.res2.ResourceItem)1 ResourceMerger (com.android.ide.common.res2.ResourceMerger)1 ResourceSet (com.android.ide.common.res2.ResourceSet)1 StdLogger (com.android.utils.StdLogger)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1