Search in sources :

Example 1 with FileWrapper

use of com.android.io.FileWrapper in project bazel by bazelbuild.

the class AndroidResourceProcessor method processDataBindings.

/**
   * If resources exist and a data binding layout info file is requested: processes data binding
   * declarations over those resources, populates the output file, and creates a new resources
   * directory with data binding expressions stripped out (so aapt, which doesn't understand
   * data binding, can properly read them).
   *
   * <p>Returns the resources directory that aapt should read.
   */
static Path processDataBindings(Path resourceDir, Path dataBindingInfoOut, VariantType variantType, String packagePath, Path androidManifest) throws IOException {
    if (dataBindingInfoOut == null) {
        return resourceDir;
    } else if (!Files.isDirectory(resourceDir)) {
        // No resources: no data binding needed. Create a dummy file to satisfy declared outputs.
        Files.createFile(dataBindingInfoOut);
        return resourceDir;
    }
    // Strip the file name (the data binding library automatically adds it back in).
    // ** The data binding library assumes this file is called "layout-info.zip". **
    dataBindingInfoOut = dataBindingInfoOut.getParent();
    if (Files.notExists(dataBindingInfoOut)) {
        Files.createDirectory(dataBindingInfoOut);
    }
    Path processedResourceDir = resourceDir.resolveSibling("res_without_databindings");
    if (Files.notExists(processedResourceDir)) {
        Files.createDirectory(processedResourceDir);
    }
    ProcessXmlOptions options = new ProcessXmlOptions();
    options.setAppId(packagePath);
    options.setLibrary(variantType == VariantType.LIBRARY);
    options.setResInput(resourceDir.toFile());
    options.setResOutput(processedResourceDir.toFile());
    options.setLayoutInfoOutput(dataBindingInfoOut.toFile());
    // Aggregate data-bound .xml files into a single .zip.
    options.setZipLayoutInfo(true);
    try {
        Object minSdk = AndroidManifest.getMinSdkVersion(new FileWrapper(androidManifest.toFile()));
        if (minSdk instanceof Integer) {
            options.setMinSdk(((Integer) minSdk).intValue());
        } else {
            // TODO(bazel-team): Enforce the minimum SDK check.
            options.setMinSdk(15);
        }
    } catch (XPathExpressionException | StreamException e) {
        // TODO(bazel-team): Enforce the minimum SDK check.
        options.setMinSdk(15);
    }
    try {
        AndroidDataBinding.doRun(options);
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
    return processedResourceDir;
}
Also used : Path(java.nio.file.Path) XPathExpressionException(javax.xml.xpath.XPathExpressionException) FileWrapper(com.android.io.FileWrapper) ProcessXmlOptions(android.databinding.cli.ProcessXmlOptions) StreamException(com.android.io.StreamException)

Aggregations

ProcessXmlOptions (android.databinding.cli.ProcessXmlOptions)1 FileWrapper (com.android.io.FileWrapper)1 StreamException (com.android.io.StreamException)1 Path (java.nio.file.Path)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1