diff options
author | corysmith <corysmith@google.com> | 2018-07-31 10:39:15 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-07-31 10:41:13 -0700 |
commit | 3852edd46cb777191728c42e62873e39fd9fe4c4 (patch) | |
tree | 8acda6f07d4ff6d0e65337c0bbffd5040220fec0 /src/tools/android/java/com/google/devtools/build/android/Aapt2ResourceShrinkingAction.java | |
parent | ab8a844240cf6ccb4ee46e6bb8795fcc2b3fe6be (diff) |
Automated rollback of commit 0a635c5236ce30ea84b765ce752267992733a649.
*** Reason for rollback ***
Rolling forward with the correct attribute handling.
*** Original change description ***
Automated rollback of commit 8fe0f45852a620a078013310989396caed273342.
*** Reason for rollback ***
Breaks a couple of builds due to a bad merge.
*** Original change description ***
Add apk converted to proto and all attributes from CompiledResources to ResourcesZip.
Add new proto format for tool attributes stored in the AndroidDataXml for storing them in the resources.zip.
RELNOTES:None
PiperOrigin-RevId: 206786645
Diffstat (limited to 'src/tools/android/java/com/google/devtools/build/android/Aapt2ResourceShrinkingAction.java')
-rw-r--r-- | src/tools/android/java/com/google/devtools/build/android/Aapt2ResourceShrinkingAction.java | 127 |
1 files changed, 82 insertions, 45 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourceShrinkingAction.java b/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourceShrinkingAction.java index af2a98ab2f..8d1be26dab 100644 --- a/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourceShrinkingAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/Aapt2ResourceShrinkingAction.java @@ -25,12 +25,16 @@ import com.google.devtools.build.android.aapt2.CompiledResources; import com.google.devtools.build.android.aapt2.ResourceCompiler; import com.google.devtools.build.android.aapt2.ResourceLinker; import com.google.devtools.build.android.aapt2.StaticLibrary; +import com.google.devtools.common.options.Option; +import com.google.devtools.common.options.OptionDocumentationCategory; +import com.google.devtools.common.options.OptionEffectTag; +import com.google.devtools.common.options.OptionsBase; import com.google.devtools.common.options.OptionsParser; import com.google.devtools.common.options.ShellQuotedParamsFilePreProcessor; -import java.io.Closeable; import java.io.File; import java.nio.file.FileSystems; import java.nio.file.Path; +import java.util.Set; import java.util.concurrent.ExecutionException; import java.util.function.Function; @@ -53,68 +57,101 @@ import java.util.function.Function; */ public class Aapt2ResourceShrinkingAction { + /** Aapt2 shrinking specific options */ + public static final class Aapt2ShrinkOptions extends OptionsBase { + @Option( + name = "useProtoApk", + defaultValue = "false", + category = "config", + documentationCategory = OptionDocumentationCategory.UNCATEGORIZED, + effectTags = {OptionEffectTag.UNKNOWN}, + help = "Path to the shrunk jar from a Proguard run with shrinking enabled.") + public boolean useProtoApk; + } + public static void main(String[] args) throws Exception { final Profiler profiler = LoggingProfiler.createAndStart("shrink").startTask("flags"); // Parse arguments. OptionsParser optionsParser = - OptionsParser.newOptionsParser(Options.class, Aapt2ConfigOptions.class); + OptionsParser.newOptionsParser( + ImmutableList.of(Options.class, Aapt2ConfigOptions.class, Aapt2ShrinkOptions.class)); optionsParser.enableParamsFileSupport( new ShellQuotedParamsFilePreProcessor(FileSystems.getDefault())); optionsParser.parseAndExitUponError(args); Aapt2ConfigOptions aapt2ConfigOptions = optionsParser.getOptions(Aapt2ConfigOptions.class); Options options = optionsParser.getOptions(Options.class); + Aapt2ShrinkOptions aapt2ShrinkOptions = optionsParser.getOptions(Aapt2ShrinkOptions.class); profiler.recordEndOf("flags").startTask("setup"); - final ListeningExecutorService executorService = ExecutorServiceCloser.createDefaultService(); try (ScopedTemporaryDirectory scopedTmp = new ScopedTemporaryDirectory("android_resources_tmp"); - Closeable closer = ExecutorServiceCloser.createWith(executorService)) { - - Path workingResourcesDirectory = scopedTmp.subDirectoryOf("resources"); - final ResourceCompiler resourceCompiler = - ResourceCompiler.create( - executorService, - workingResourcesDirectory, - aapt2ConfigOptions.aapt2, - aapt2ConfigOptions.buildToolsVersion, - aapt2ConfigOptions.generatePseudoLocale); - profiler.recordEndOf("setup").startTask("compile"); + ExecutorServiceCloser executorService = ExecutorServiceCloser.createWithFixedPoolOf(15)) { final ResourcesZip resourcesZip = ResourcesZip.createFrom( options.resourcesZip, scopedTmp.subDirectoryOf("merged-resources")); - final CompiledResources compiled = - resourcesZip - .shrink( - options - .dependencyManifests - .stream() - .map(Path::toFile) - .map(manifestToPackageUsing(executorService)) - .map(futureToString()) - .collect(toSet()), - options.rTxt, - options.shrunkJar, - options.primaryManifest, - options.proguardMapping, - options.log, - scopedTmp.subDirectoryOf("shrunk-resources")) - .writeArchiveTo(options.shrunkResources, false) - .compile(resourceCompiler, workingResourcesDirectory); - profiler.recordEndOf("compile"); + Path workingResourcesDirectory = scopedTmp.subDirectoryOf("resources"); + final ResourceLinker linker = + ResourceLinker.create( + aapt2ConfigOptions.aapt2, executorService, scopedTmp.subDirectoryOf("linking")) + .profileUsing(profiler); + + final Set<String> packages = + options + .dependencyManifests + .stream() + .map(Path::toFile) + .map(manifestToPackageUsing(executorService)) + .map(futureToString()) + .collect(toSet()); - ResourceLinker.create( - aapt2ConfigOptions.aapt2, executorService, scopedTmp.subDirectoryOf("linking")) - .profileUsing(profiler) - .dependencies(ImmutableList.of(StaticLibrary.from(aapt2ConfigOptions.androidJar))) - .profileUsing(profiler) - .outputAsProto(aapt2ConfigOptions.resourceTableAsProto) - .buildVersion(aapt2ConfigOptions.buildToolsVersion) - .includeOnlyConfigs(aapt2ConfigOptions.resourceConfigs) - .debug(aapt2ConfigOptions.debug) - .link(compiled) - .copyPackageTo(options.shrunkApk) - .copyRTxtTo(options.rTxtOutput); + if (aapt2ShrinkOptions.useProtoApk) { + resourcesZip + .shrinkUsingProto( + packages, + options.rTxt, + options.shrunkJar, + options.primaryManifest, + options.proguardMapping, + options.log, + scopedTmp.subDirectoryOf("shrunk-resources")) + .writeBinaryTo(linker, options.shrunkApk) + .writeReportTo(options.log) + .writeResourceToZip(options.shrunkResources); + } else { + final ResourceCompiler resourceCompiler = + ResourceCompiler.create( + executorService, + workingResourcesDirectory, + aapt2ConfigOptions.aapt2, + aapt2ConfigOptions.buildToolsVersion, + aapt2ConfigOptions.generatePseudoLocale); + profiler.recordEndOf("setup").startTask("compile"); + + final CompiledResources compiled = + resourcesZip + .shrink( + packages, + options.rTxt, + options.shrunkJar, + options.primaryManifest, + options.proguardMapping, + options.log, + scopedTmp.subDirectoryOf("shrunk-resources")) + .writeArchiveTo(options.shrunkResources, false) + .compile(resourceCompiler, workingResourcesDirectory); + profiler.recordEndOf("compile"); + linker + .dependencies(ImmutableList.of(StaticLibrary.from(aapt2ConfigOptions.androidJar))) + .profileUsing(profiler) + .outputAsProto(aapt2ConfigOptions.resourceTableAsProto) + .buildVersion(aapt2ConfigOptions.buildToolsVersion) + .includeOnlyConfigs(aapt2ConfigOptions.resourceConfigs) + .debug(aapt2ConfigOptions.debug) + .link(compiled) + .copyPackageTo(options.shrunkApk) + .copyRTxtTo(options.rTxtOutput); + } profiler.recordEndOf("shrink"); } } |