From 1bdf2f49edf67c699b0ffe4a35dd89673c7d9dc8 Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 15 Apr 2016 20:35:59 +0000 Subject: Add a crunch_png attribute to android_binary This allows a user to turn off png crunching during the final merge (with crunch_png = 0), but it does not skip nine-patch processing. RELNOTES: adds crunch_png attribute to android_binary -- MOS_MIGRATED_REVID=119986498 --- .../android/AndroidResourceProcessingAction.java | 21 +++++++-- .../build/android/NinePatchOnlyCruncher.java | 50 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 src/tools/android/java/com/google/devtools/build/android/NinePatchOnlyCruncher.java (limited to 'src/tools/android/java/com/google/devtools') diff --git a/src/tools/android/java/com/google/devtools/build/android/AndroidResourceProcessingAction.java b/src/tools/android/java/com/google/devtools/build/android/AndroidResourceProcessingAction.java index 0e233743d6..ba8e557f62 100644 --- a/src/tools/android/java/com/google/devtools/build/android/AndroidResourceProcessingAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/AndroidResourceProcessingAction.java @@ -33,6 +33,7 @@ import com.android.builder.core.VariantConfiguration; import com.android.ide.common.internal.AaptCruncher; import com.android.ide.common.internal.CommandLineRunner; import com.android.ide.common.internal.LoggedErrorException; +import com.android.ide.common.internal.PngCruncher; import com.android.ide.common.res2.MergingException; import com.android.utils.StdLogger; @@ -249,8 +250,7 @@ public class AndroidResourceProcessingAction { mergedResources, mergedAssets, modifiers, - useAaptCruncher() ? new AaptCruncher(aaptConfigOptions.aapt.toString(), - new CommandLineRunner(STD_LOGGER)) : null, + selectPngCruncher(), true); LOGGER.fine(String.format("Merging finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); @@ -288,7 +288,7 @@ public class AndroidResourceProcessingAction { options.resourcesOutput != null ? processedManifestData.getResourceDir().resolve("values").resolve("public.xml") : null); - LOGGER.fine(String.format("appt finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); + LOGGER.fine(String.format("aapt finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS))); if (options.manifestOutput != null) { resourceProcessor.copyManifestToOutput(processedManifestData, options.manifestOutput); @@ -326,7 +326,7 @@ public class AndroidResourceProcessingAction { LOGGER.fine(String.format("Resources processed in %sms", timer.elapsed(TimeUnit.MILLISECONDS))); } - private static boolean useAaptCruncher() { + private static boolean usePngCruncher() { // If the value was set, use that. if (aaptConfigOptions.useAaptCruncher != TriState.AUTO) { return aaptConfigOptions.useAaptCruncher == TriState.YES; @@ -334,4 +334,17 @@ public class AndroidResourceProcessingAction { // By default png cruncher shouldn't be invoked on a library -- the work is just thrown away. return options.packageType != VariantConfiguration.Type.LIBRARY; } + + private static PngCruncher selectPngCruncher() { + // Use the full cruncher if asked to do so. + if (usePngCruncher()) { + return new AaptCruncher(aaptConfigOptions.aapt.toString(), new CommandLineRunner(STD_LOGGER)); + } + // Otherwise, if this is a binary, we need to at least process nine-patch PNGs. + if (options.packageType != VariantConfiguration.Type.LIBRARY) { + return new NinePatchOnlyCruncher( + aaptConfigOptions.aapt.toString(), new CommandLineRunner(STD_LOGGER)); + } + return null; + } } diff --git a/src/tools/android/java/com/google/devtools/build/android/NinePatchOnlyCruncher.java b/src/tools/android/java/com/google/devtools/build/android/NinePatchOnlyCruncher.java new file mode 100644 index 0000000000..3957ab8c4a --- /dev/null +++ b/src/tools/android/java/com/google/devtools/build/android/NinePatchOnlyCruncher.java @@ -0,0 +1,50 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.android; + +import com.google.common.io.Files; + +import com.android.SdkConstants; +import com.android.ide.common.internal.AaptCruncher; +import com.android.ide.common.internal.CommandLineRunner; +import com.android.ide.common.internal.LoggedErrorException; + +import java.io.File; +import java.io.IOException; + +/** + * A wrapper around a PNG cruncher that only processes nine-patch PNGs. + */ +public class NinePatchOnlyCruncher extends AaptCruncher { + + public NinePatchOnlyCruncher(String aaptLocation, CommandLineRunner commandLineRunner) { + super(aaptLocation, commandLineRunner); + } + + /** + * Runs the cruncher on a single file (or copies the file if no crunching is needed). + * + * @param from the file to process + * @param to the output file + */ + @Override + public void crunchPng(File from, File to) + throws InterruptedException, LoggedErrorException, IOException { + if (from.getPath().endsWith(SdkConstants.DOT_9PNG)) { + super.crunchPng(from, to); + } else { + Files.copy(from, to); + } + } +} -- cgit v1.2.3