Search in sources :

Example 1 with FlagAaptOptions

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

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

use of com.google.devtools.build.android.AndroidResourceProcessor.FlagAaptOptions 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)3 AaptConfigOptions (com.google.devtools.build.android.AndroidResourceProcessor.AaptConfigOptions)3 FlagAaptOptions (com.google.devtools.build.android.AndroidResourceProcessor.FlagAaptOptions)3 OptionsParser (com.google.devtools.common.options.OptionsParser)3 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 ResourceUsageAnalyzer (com.android.build.gradle.tasks.ResourceUsageAnalyzer)1 VariantType (com.android.builder.core.VariantType)1 LoggedErrorException (com.android.ide.common.internal.LoggedErrorException)1 MergingException (com.android.ide.common.res2.MergingException)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