Search in sources :

Example 1 with MergingException

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

the class LibraryRClassGeneratorAction 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 aaptConfigOptions = optionsParser.getOptions(AaptConfigOptions.class);
    Options options = optionsParser.getOptions(Options.class);
    logger.fine(String.format("Option parsing finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    try (ScopedTemporaryDirectory scopedTmp = new ScopedTemporaryDirectory("android_resource_generated")) {
        AndroidResourceClassWriter resourceClassWriter = AndroidResourceClassWriter.createWith(aaptConfigOptions.androidJar, scopedTmp.getPath(), Strings.nullToEmpty(options.packageForR));
        resourceClassWriter.setIncludeClassFile(true);
        resourceClassWriter.setIncludeJavaFile(false);
        final AndroidResourceProcessor resourceProcessor = new AndroidResourceProcessor(stdLogger);
        logger.fine(String.format("Setup finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        final ParsedAndroidData data = resourceProcessor.deserializeSymbolsToData(options.symbols);
        logger.fine(String.format("Deserialization finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        data.writeResourcesTo(resourceClassWriter);
        resourceClassWriter.flush();
        logger.fine(String.format("R writing finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        AndroidResourceOutputs.createClassJar(scopedTmp.getPath(), options.classJarOutput);
        logger.fine(String.format("Creating class jar finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    } catch (IOException | MergingException | DeserializationException e) {
        logger.log(Level.SEVERE, "Errors during R generation.", e);
        throw e;
    }
}
Also used : AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions) MergingException(com.android.ide.common.res2.MergingException) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) OptionsParser(com.google.devtools.common.options.OptionsParser) AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)

Example 2 with MergingException

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

the class AndroidResourceProcessor method deserializeSymbolsToData.

/** Deserializes a list of serialized resource paths to a {@link ParsedAndroidData}. */
public ParsedAndroidData deserializeSymbolsToData(List<Path> symbolPaths) throws IOException, MergingException {
    AndroidDataDeserializer deserializer = AndroidDataDeserializer.create();
    final ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(15));
    final Builder deserializedDataBuilder = ParsedAndroidData.Builder.newBuilder();
    try (Closeable closeable = ExecutorServiceCloser.createWith(executorService)) {
        List<ListenableFuture<Boolean>> deserializing = new ArrayList<>();
        for (final Path symbolPath : symbolPaths) {
            deserializing.add(executorService.submit(new Deserialize(deserializer, symbolPath, deserializedDataBuilder)));
        }
        FailedFutureAggregator<MergingException> aggregator = FailedFutureAggregator.createForMergingExceptionWithMessage("Failure(s) during dependency parsing");
        aggregator.aggregateAndMaybeThrow(deserializing);
    }
    return deserializedDataBuilder.build();
}
Also used : Path(java.nio.file.Path) MergingException(com.android.ide.common.res2.MergingException) Builder(com.google.devtools.build.android.ParsedAndroidData.Builder) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService)

Example 3 with MergingException

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

the class AarGeneratorAction method main.

public static void main(String[] args) {
    Stopwatch timer = Stopwatch.createStarted();
    OptionsParser optionsParser = OptionsParser.newOptionsParser(Options.class);
    optionsParser.parseAndExitUponError(args);
    Options options = optionsParser.getOptions(Options.class);
    checkFlags(options);
    AndroidResourceProcessor resourceProcessor = new AndroidResourceProcessor(new StdLogger(com.android.utils.StdLogger.Level.VERBOSE));
    try (ScopedTemporaryDirectory scopedTmp = new ScopedTemporaryDirectory("aar_gen_tmp")) {
        Path tmp = scopedTmp.getPath();
        Path resourcesOut = tmp.resolve("merged_resources");
        Files.createDirectories(resourcesOut);
        Path assetsOut = tmp.resolve("merged_assets");
        Files.createDirectories(assetsOut);
        logger.fine(String.format("Setup finished at %dms", timer.elapsed(TimeUnit.MILLISECONDS)));
        // There aren't any dependencies, but we merge to combine primary resources from different
        // res/assets directories into a single res and single assets directory.
        MergedAndroidData mergedData = AndroidResourceMerger.mergeData(options.mainData, ImmutableList.<DependencyAndroidData>of(), ImmutableList.<DependencyAndroidData>of(), resourcesOut, assetsOut, null, VariantType.LIBRARY, null);
        logger.fine(String.format("Merging finished at %dms", timer.elapsed(TimeUnit.MILLISECONDS)));
        writeAar(options.aarOutput, mergedData, options.manifest, options.rtxt, options.classes);
        logger.fine(String.format("Packaging finished at %dms", timer.elapsed(TimeUnit.MILLISECONDS)));
    } catch (IOException | MergingException e) {
        logger.log(Level.SEVERE, "Error during merging resources", e);
        System.exit(1);
    }
    System.exit(0);
}
Also used : Path(java.nio.file.Path) MergingException(com.android.ide.common.res2.MergingException) StdLogger(com.android.utils.StdLogger) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) OptionsParser(com.google.devtools.common.options.OptionsParser)

Example 4 with MergingException

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

the class ValueResourceParser method parseDocument.

private static Document parseDocument(File file) throws MergingException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    BufferedInputStream stream = null;
    try {
        stream = new BufferedInputStream(new FileInputStream(file));
        InputSource is = new InputSource(stream);
        factory.setNamespaceAware(true);
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return builder.parse(is);
    } catch (SAXParseException e) {
        String message = e.getLocalizedMessage();
        MergingException exception = new MergingException(message, e);
        exception.setFile(file);
        int lineNumber = e.getLineNumber();
        if (lineNumber != -1) {
            // make line numbers 0-based
            exception.setLine(lineNumber - 1);
            exception.setColumn(e.getColumnNumber() - 1);
        }
        throw exception;
    } catch (ParserConfigurationException | SAXException | IOException e) {
        throw new MergingException(e).setFile(file);
    } finally {
        Closeables.closeQuietly(stream);
    }
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) BufferedInputStream(java.io.BufferedInputStream) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) MergingException(com.android.ide.common.res2.MergingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException)

Example 5 with MergingException

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

the class AndroidResourceMergingAction 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 aaptConfigOptions = optionsParser.getOptions(AaptConfigOptions.class);
    Options options = optionsParser.getOptions(Options.class);
    Preconditions.checkNotNull(options.primaryData);
    Preconditions.checkNotNull(options.primaryManifest);
    Preconditions.checkNotNull(options.classJarOutput);
    try (ScopedTemporaryDirectory scopedTmp = new ScopedTemporaryDirectory("android_resource_merge_tmp")) {
        Path tmp = scopedTmp.getPath();
        Path mergedAssets = tmp.resolve("merged_assets");
        Path mergedResources = tmp.resolve("merged_resources");
        Path generatedSources = tmp.resolve("generated_resources");
        Path processedManifest = tmp.resolve("manifest-processed/AndroidManifest.xml");
        logger.fine(String.format("Setup finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        VariantType packageType = VariantType.LIBRARY;
        AndroidResourceClassWriter resourceClassWriter = AndroidResourceClassWriter.createWith(aaptConfigOptions.androidJar, generatedSources, Strings.nullToEmpty(options.packageForR));
        resourceClassWriter.setIncludeClassFile(true);
        resourceClassWriter.setIncludeJavaFile(false);
        final MergedAndroidData mergedData = AndroidResourceMerger.mergeData(options.primaryData, options.primaryManifest, options.directData, options.transitiveData, mergedResources, mergedAssets, new StubPngCruncher(), packageType, options.symbolsBinOut, resourceClassWriter);
        logger.fine(String.format("Merging finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        // the manifests compatible with the old manifest merger.
        if (options.manifestOutput != null) {
            MergedAndroidData processedData = AndroidManifestProcessor.with(stdLogger).processManifest(packageType, options.packageForR, null, /* applicationId */
            -1, /* versionCode */
            null, /* versionName */
            mergedData, processedManifest);
            AndroidResourceOutputs.copyManifestToOutput(processedData, options.manifestOutput);
        }
        AndroidResourceOutputs.createClassJar(generatedSources, options.classJarOutput);
        logger.fine(String.format("Create classJar finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        if (options.resourcesOutput != null) {
            Path resourcesDir = AndroidResourceProcessor.processDataBindings(mergedData.getResourceDir(), options.dataBindingInfoOut, packageType, options.packageForR, options.primaryManifest);
            // For now, try compressing the library resources that we pass to the validator. This takes
            // extra CPU resources to pack and unpack (~2x), but can reduce the zip size (~4x).
            AndroidResourceOutputs.createResourcesZip(resourcesDir, mergedData.getAssetDir(), options.resourcesOutput, true);
            logger.fine(String.format("Create resources.zip finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        }
    } catch (MergingException e) {
        logger.log(Level.SEVERE, "Error during merging resources", e);
        throw e;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Unexpected", e);
        throw e;
    }
    logger.fine(String.format("Resources merged in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
}
Also used : Path(java.nio.file.Path) AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions) MergingException(com.android.ide.common.res2.MergingException) VariantType(com.android.builder.core.VariantType) Stopwatch(com.google.common.base.Stopwatch) OptionsParser(com.google.devtools.common.options.OptionsParser) MergingException(com.android.ide.common.res2.MergingException) IOException(java.io.IOException) PngException(com.android.ide.common.internal.PngException) AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)

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