Search in sources :

Example 1 with AaptConfigOptions

use of com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions 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 AaptConfigOptions

use of com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions 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)

Example 3 with AaptConfigOptions

use of com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions 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 4 with AaptConfigOptions

use of com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions in project bazel by bazelbuild.

the class AndroidResourceValidatorAction 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);
    final AndroidResourceProcessor resourceProcessor = new AndroidResourceProcessor(stdLogger);
    VariantType packageType = VariantType.LIBRARY;
    Preconditions.checkNotNull(options.rOutput);
    Preconditions.checkNotNull(options.srcJarOutput);
    try (ScopedTemporaryDirectory scopedTmp = new ScopedTemporaryDirectory("resource_validator_tmp")) {
        Path tmp = scopedTmp.getPath();
        Path expandedOut = tmp.resolve("tmp-expanded");
        Path resources = expandedOut.resolve("res");
        Path assets = expandedOut.resolve("assets");
        Path generatedSources = tmp.resolve("generated_resources");
        Path dummyManifest = tmp.resolve("manifest-aapt-dummy/AndroidManifest.xml");
        unpackZip(options.mergedResources, expandedOut);
        logger.fine(String.format("unpacked zip at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        // We need to make the manifest aapt safe (w.r.t., placeholders). For now, just stub it out.
        resourceProcessor.writeDummyManifestForAapt(dummyManifest, options.packageForR);
        resourceProcessor.runAapt(aaptConfigOptions.aapt, aaptConfigOptions.androidJar, aaptConfigOptions.buildToolsVersion, packageType, aaptConfigOptions.debug, options.packageForR, new FlagAaptOptions(aaptConfigOptions), aaptConfigOptions.resourceConfigs, ImmutableList.<String>of(), dummyManifest, resources, assets, generatedSources, null, /* packageOut */
        null, /* proguardOut */
        null, /* mainDexProguardOut */
        null);
        logger.fine(String.format("aapt finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        AndroidResourceOutputs.copyRToOutput(generatedSources, options.rOutput, VariantType.LIBRARY == packageType);
        AndroidResourceOutputs.createSrcJar(generatedSources, options.srcJarOutput, VariantType.LIBRARY == packageType);
    } catch (Exception e) {
        logger.log(java.util.logging.Level.SEVERE, "Unexpected", e);
        throw e;
    } finally {
        resourceProcessor.shutdown();
    }
    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) FlagAaptOptions(com.google.devtools.build.android.AndroidResourceProcessor.FlagAaptOptions) FlagAaptOptions(com.google.devtools.build.android.AndroidResourceProcessor.FlagAaptOptions) VariantType(com.android.builder.core.VariantType) Stopwatch(com.google.common.base.Stopwatch) OptionsParser(com.google.devtools.common.options.OptionsParser) IOException(java.io.IOException) AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)

Example 5 with AaptConfigOptions

use of com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions in project bazel by bazelbuild.

the class ResourceShrinkerAction method main.

public static void main(String[] args) throws Exception {
    final Stopwatch timer = Stopwatch.createStarted();
    // Parse arguments.
    OptionsParser optionsParser = OptionsParser.newOptionsParser(Options.class, AaptConfigOptions.class);
    optionsParser.parseAndExitUponError(args);
    aaptConfigOptions = optionsParser.getOptions(AaptConfigOptions.class);
    options = optionsParser.getOptions(Options.class);
    AndroidResourceProcessor resourceProcessor = new AndroidResourceProcessor(stdLogger);
    // Setup temporary working directories.
    try (ScopedTemporaryDirectory scopedTmp = new ScopedTemporaryDirectory("resource_shrinker_tmp")) {
        Path working = scopedTmp.getPath();
        final Path resourceFiles = working.resolve("resource_files");
        final Path shrunkResources = working.resolve("shrunk_resources");
        // Gather package list from manifests.
        Set<String> resourcePackages = getManifestPackages(options.primaryManifest, options.dependencyManifests);
        resourcePackages.addAll(options.resourcePackages);
        // Expand resource files zip into working directory.
        try (ZipInputStream zin = new ZipInputStream(new FileInputStream(options.resourcesZip.toFile()))) {
            ZipEntry entry;
            while ((entry = zin.getNextEntry()) != null) {
                if (!entry.isDirectory()) {
                    Path output = resourceFiles.resolve(entry.getName());
                    Files.createDirectories(output.getParent());
                    try (FileOutputStream fos = new FileOutputStream(output.toFile())) {
                        ByteStreams.copy(zin, fos);
                    }
                }
            }
        }
        // Shrink resources.
        ResourceUsageAnalyzer resourceShrinker = new ResourceUsageAnalyzer(resourcePackages, options.rTxt, options.shrunkJar, options.primaryManifest, options.proguardMapping, resourceFiles.resolve("res"), options.log);
        resourceShrinker.shrink(shrunkResources);
        logger.fine(String.format("Shrinking resources finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        Path generatedSources = null;
        if (options.rTxtOutput != null) {
            generatedSources = working.resolve("generated_resources");
        }
        // Build ap_ with shrunk resources.
        resourceProcessor.processResources(aaptConfigOptions.aapt, aaptConfigOptions.androidJar, aaptConfigOptions.buildToolsVersion, VariantType.DEFAULT, aaptConfigOptions.debug, null, /* packageForR */
        new FlagAaptOptions(aaptConfigOptions), aaptConfigOptions.resourceConfigs, aaptConfigOptions.splits, new MergedAndroidData(shrunkResources, resourceFiles.resolve("assets"), options.primaryManifest), ImmutableList.<DependencyAndroidData>of(), /* libraries */
        generatedSources, options.shrunkApk, null, /* proguardOutput */
        null, /* mainDexProguardOutput */
        null, /* publicResourcesOut */
        null);
        if (options.shrunkResources != null) {
            AndroidResourceOutputs.createResourcesZip(shrunkResources, resourceFiles.resolve("assets"), options.shrunkResources, false);
        }
        if (options.rTxtOutput != null) {
            AndroidResourceOutputs.copyRToOutput(generatedSources, options.rTxtOutput, options.packageType == VariantType.LIBRARY);
        }
        logger.fine(String.format("Packing resources finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Error shrinking resources", e);
        throw e;
    } finally {
        resourceProcessor.shutdown();
    }
}
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) ZipEntry(java.util.zip.ZipEntry) Stopwatch(com.google.common.base.Stopwatch) OptionsParser(com.google.devtools.common.options.OptionsParser) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) StreamException(com.android.io.StreamException) ZipInputStream(java.util.zip.ZipInputStream) FileOutputStream(java.io.FileOutputStream) ResourceUsageAnalyzer(com.android.build.gradle.tasks.ResourceUsageAnalyzer) AaptConfigOptions(com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)5 AaptConfigOptions (com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)5 OptionsParser (com.google.devtools.common.options.OptionsParser)5 IOException (java.io.IOException)5 Path (java.nio.file.Path)4 MergingException (com.android.ide.common.res2.MergingException)3 FlagAaptOptions (com.google.devtools.build.android.AndroidResourceProcessor.FlagAaptOptions)3 VariantType (com.android.builder.core.VariantType)2 ResourceUsageAnalyzer (com.android.build.gradle.tasks.ResourceUsageAnalyzer)1 LoggedErrorException (com.android.ide.common.internal.LoggedErrorException)1 PngException (com.android.ide.common.internal.PngException)1 StreamException (com.android.io.StreamException)1 UnrecognizedSplitsException (com.google.devtools.build.android.SplitConfigurationFilter.UnrecognizedSplitsException)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 SAXException (org.xml.sax.SAXException)1