com.android.ide.common.res2.MergingException Java Examples

The following examples show how to use com.android.ide.common.res2.MergingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AARLibraries.java    From appinventor-extensions with Apache License 2.0 6 votes vote down vote up
/**
 * Merges the resources from all of the dependent AAR libraries into the main resource bundle for
 * the compiling app.
 *
 * @param outputDir the output directory to write the R.java files.
 * @param mainResDir the resource directory where the resource descriptors for the app reside.
 * @param cruncher configured PNG cruncher utility for reducing the size of PNG assets.
 * @return true if the merge was successful, otherwise false.
 */
public boolean mergeResources(File outputDir, File mainResDir, PngCruncher cruncher) {
  List<ResourceSet> resourceSets = getResourceSets();
  ResourceSet mainResSet = new ResourceSet("main");
  mainResSet.addSource(mainResDir);
  resourceSets.add(mainResSet);
  ResourceMerger merger = new ResourceMerger();

  try {
    for (ResourceSet resourceSet : resourceSets) {
      resourceSet.loadFromFiles(LOG);
      merger.addDataSet(resourceSet);
    }

    MergedResourceWriter writer = new MergedResourceWriter(outputDir, cruncher, false, false, null);
    writer.setInsertSourceMarkers(true);
    merger.mergeData(writer, false);
    return true;
  } catch(MergingException e) {
    e.printStackTrace();
    return false;
  }
}
 
Example #2
Source File: MergeAndroidResourceSourcesStep.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public StepExecutionResult execute(ExecutionContext context) {
  ResourceMerger merger = new ResourceMerger(1);
  try {
    for (Path resPath : getResPaths()) {
      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(
            getOutFolderPath().toFile(),
            null /*publicFile*/,
            null /*blameLogFolder*/,
            new NoOpResourcePreprocessor(),
            getTmpFolderPath().toFile());
    merger.mergeData(writer, /* cleanUp */ false);
  } catch (MergingException e) {
    LOG.error(e, "Failed merging resources.");
    return StepExecutionResults.ERROR;
  }
  return StepExecutionResults.SUCCESS;
}
 
Example #3
Source File: MergeResources.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void doFullTaskAction() throws IOException {
    // this is full run, clean the previous output
    File destinationDir = getOutputDir();
    FileUtils.emptyFolder(destinationDir);

    List<ResourceSet> resourceSets = getConfiguredResourceSets();

    // create a new merger and populate it with the sets.
    ResourceMerger merger = new ResourceMerger();

    try {
        for (ResourceSet resourceSet : resourceSets) {
            resourceSet.loadFromFiles(getILogger());
            merger.addDataSet(resourceSet);
        }

        // get the merged set and write it down.
        MergedResourceWriter writer = new MergedResourceWriter(
                destinationDir, getCruncher(),
                getCrunchPng(), getProcess9Patch(), getPublicFile(), preprocessor);
        writer.setInsertSourceMarkers(getInsertSourceMarkers());

        merger.mergeData(writer, false /*doCleanUp*/);

        // No exception? Write the known state.
        merger.writeBlobTo(getIncrementalFolder(), writer);
        throw new StopExecutionException("Stop for now.");
    } catch (MergingException e) {
        System.out.println(e.getMessage());
        merger.cleanBlob(getIncrementalFolder());
        throw new ResourceException(e.getMessage(), e);
    }
}
 
Example #4
Source File: LoggerWrapper.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void error(Throwable throwable, String s, Object... objects) {
    if (throwable instanceof MergingException) {
        // MergingExceptions have a known cause: they aren't internal errors, they
        // are errors in the user's code, so a full exception is not helpful (and
        // these exceptions should include a pointer to the user's error right in
        // the message).
        //
        // Furthermore, these exceptions are already caught by the MergeResources
        // and MergeAsset tasks, so don't duplicate the output
        return;
    }

    if (!logger.isEnabled(LogLevel.ERROR)) {
        return;
    }

    if (objects != null && objects.length > 0) {
        s = String.format(s, objects);
    }

    if (throwable == null) {
        logger.log(LogLevel.ERROR, s);

    } else {
        logger.log(LogLevel.ERROR, s, throwable);
    }
}
 
Example #5
Source File: ValueResourceParser.java    From paraphrase with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the file and returns a list of {@link ResourceItem} objects.
 * @return a list of resources.
 *
 * @throws MergingException if a merging exception happens
 */
private @NonNull 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;
}
 
Example #6
Source File: MergeResources.java    From javafxmobile-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void doFullTaskAction() throws IOException {
    ResourcePreprocessor preprocessor = getPreprocessor();

    // this is full run, clean the previous output
    File destinationDir = getOutputDir();
    FileUtils.cleanOutputDir(destinationDir);

    List<ResourceSet> resourceSets = getConfiguredResourceSets(preprocessor);

    // create a new merger and populate it with the sets.
    ResourceMerger merger = new ResourceMerger(minSdk);
    MergingLog mergingLog =
            getBlameLogFolder() != null ? new MergingLog(getBlameLogFolder()) : null;

    try (QueueableResourceCompiler resourceCompiler =
                 processResources
                         ? makeAapt(
                         buildToolInfo.get(),
                         aaptGeneration,
                         getBuilder(),
                         crunchPng,
                         mergingLog)
                         : QueueableResourceCompiler.NONE) {

        for (ResourceSet resourceSet : resourceSets) {
            resourceSet.loadFromFiles(getILogger());
            merger.addDataSet(resourceSet);
        }

        MergedResourceWriter writer =
                new MergedResourceWriter(
                        workerExecutorFacade,
                        destinationDir,
                        getPublicFile(),
                        mergingLog,
                        preprocessor,
                        resourceCompiler,
                        getIncrementalFolder(),
                        null,
                        null,
                        false,
                        getCrunchPng());

        merger.mergeData(writer, false /*doCleanUp*/);

        // No exception? Write the known state.
        merger.writeBlobTo(getIncrementalFolder(), writer, false);
    } catch (MergingException e) {
        System.out.println(e.getMessage());
        merger.cleanBlob(getIncrementalFolder());
        throw new ResourceException(e.getMessage(), e);
    } finally {
        cleanup();
    }
}
 
Example #7
Source File: MergeResources.java    From javafxmobile-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected void doIncrementalTaskAction(Map<File, FileStatus> changedInputs)
        throws IOException {
    ResourcePreprocessor preprocessor = getPreprocessor();

    // create a merger and load the known state.
    ResourceMerger merger = new ResourceMerger(minSdk);
    try {
        if (!merger.loadFromBlob(getIncrementalFolder(), true /*incrementalState*/)) {
            doFullTaskAction();
            return;
        }

        for (ResourceSet resourceSet : merger.getDataSets()) {
            resourceSet.setPreprocessor(preprocessor);
        }

        List<ResourceSet> resourceSets = getConfiguredResourceSets(preprocessor);

        // compare the known state to the current sets to detect incompatibility.
        // This is in case there's a change that's too hard to do incrementally. In this case
        // we'll simply revert to full build.
        if (!merger.checkValidUpdate(resourceSets)) {
            getLogger().info("Changed Resource sets: full task run!");
            doFullTaskAction();
            return;
        }

        // The incremental process is the following:
        // Loop on all the changed files, find which ResourceSet it belongs to, then ask
        // the resource set to update itself with the new file.
        for (Map.Entry<File, FileStatus> entry : changedInputs.entrySet()) {
            File changedFile = entry.getKey();

            merger.findDataSetContaining(changedFile, fileValidity);
            if (fileValidity.getStatus() == FileValidity.FileStatus.UNKNOWN_FILE) {
                doFullTaskAction();
                return;
            } else if (fileValidity.getStatus() == FileValidity.FileStatus.VALID_FILE) {
                if (!fileValidity.getDataSet().updateWith(
                        fileValidity.getSourceFile(), changedFile, entry.getValue(),
                        getILogger())) {
                    getLogger().info(
                            String.format("Failed to process %s event! Full task run",
                                    entry.getValue()));
                    doFullTaskAction();
                    return;
                }
            }
        }

        MergingLog mergingLog =
                getBlameLogFolder() != null ? new MergingLog(getBlameLogFolder()) : null;

        try (QueueableResourceCompiler resourceCompiler =
                     processResources
                             ? makeAapt(
                             buildToolInfo.get(),
                             aaptGeneration,
                             getBuilder(),
                             crunchPng,
                             mergingLog)
                             : QueueableResourceCompiler.NONE) {

            MergedResourceWriter writer =
                    new MergedResourceWriter(
                            workerExecutorFacade,
                            getOutputDir(),
                            getPublicFile(),
                            mergingLog,
                            preprocessor,
                            resourceCompiler,
                            getIncrementalFolder(),
                            null,
                            null,
                            false,
                            getCrunchPng());

            merger.mergeData(writer, false /*doCleanUp*/);

            // No exception? Write the known state.
            merger.writeBlobTo(getIncrementalFolder(), writer, false);
        }
    } catch (MergingException e) {
        merger.cleanBlob(getIncrementalFolder());
        throw new ResourceException(e.getMessage(), e);
    } finally {
        cleanup();
    }
}
 
Example #8
Source File: ValueResourceParser.java    From paraphrase with Apache License 2.0 4 votes vote down vote up
static List<ResourceItem> parse(File file) throws MergingException {
  return new ValueResourceParser(file).parseFile();
}