Java源码示例:com.android.ide.common.res2.MergedResourceWriter

示例1
/**
 * 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;
  }
}
 
示例2
@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;
}
 
示例3
@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);
    }
}
 
示例4
@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();
    }
}
 
示例5
@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();
    }
}
 
示例6
@Inject
public FileGenerationWorkAction(MergedResourceWriter.FileGenerationParameters workItem) {
    this.workAction = new MergedResourceWriter.FileGenerationWorkAction(workItem);
}