Skip to content

Commit

Permalink
Automated rollback of commit 8fe0f45.
Browse files Browse the repository at this point in the history
*** 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: 206774364
  • Loading branch information
corbinrsmith-work authored and Copybara-Service committed Jul 31, 2018
1 parent a0f77be commit 0a635c5
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 465 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ public static void main(String[] args) throws Exception {
options.densities, filteredResources, mergedResources),
new DensitySpecificManifestProcessor(options.densities, densityManifest));

profiler.recordEndOf("merging").startTask("compile");
profiler.recordEndOf("merging");


profiler.startTask("compile");
final ResourceCompiler compiler =
ResourceCompiler.create(
executorService,
Expand All @@ -128,37 +131,37 @@ public static void main(String[] args) throws Exception {
aaptConfigOptions.buildToolsVersion,
aaptConfigOptions.generatePseudoLocale);

CompiledResources compiled =
options
.primaryData
.processDataBindings(
options.dataBindingInfoOut, options.packageForR, databindingResourcesRoot)
.compile(compiler, compiledResources)
.processManifest(
manifest ->
AndroidManifestProcessor.with(STD_LOGGER)
.processManifest(
options.applicationId,
options.versionCode,
options.versionName,
manifest,
processedManifest))
.processManifest(
manifest ->
new DensitySpecificManifestProcessor(options.densities, densityManifest)
.process(manifest));
profiler.recordEndOf("compile").startTask("link");
// Write manifestOutput now before the dummy manifest is created.
if (options.manifestOutput != null) {
AndroidResourceOutputs.copyManifestToOutput(compiled, options.manifestOutput);
}

List<CompiledResources> compiledResourceDeps =
// Last defined dependencies will overwrite previous one, so always place direct
// after transitive.
concat(options.transitiveData.stream(), options.directData.stream())
.map(DependencyAndroidData::getCompiledSymbols)
.collect(toList());
CompiledResources compiled =
options
.primaryData
.processDataBindings(
options.dataBindingInfoOut, options.packageForR, databindingResourcesRoot)
.compile(compiler, compiledResources)
.processManifest(
manifest ->
AndroidManifestProcessor.with(STD_LOGGER)
.processManifest(
options.applicationId,
options.versionCode,
options.versionName,
manifest,
processedManifest))
.processManifest(
manifest ->
new DensitySpecificManifestProcessor(options.densities, densityManifest)
.process(manifest));
profiler.recordEndOf("compile").startTask("link");
// Write manifestOutput now before the dummy manifest is created.
if (options.manifestOutput != null) {
AndroidResourceOutputs.copyManifestToOutput(compiled, options.manifestOutput);
}

List<CompiledResources> compiledResourceDeps =
// Last defined dependencies will overwrite previous one, so always place direct
// after transitive.
concat(options.transitiveData.stream(), options.directData.stream())
.map(DependencyAndroidData::getCompiledSymbols)
.collect(toList());

List<Path> assetDirs =
concat(
Expand All @@ -168,7 +171,7 @@ public static void main(String[] args) throws Exception {
options.directAssets.stream())
.flatMap(dep -> dep.assetDirs.stream())
.collect(toList());
assetDirs.addAll(options.primaryData.assetDirs);
assetDirs.addAll(options.primaryData.assetDirs);

final PackagedResources packagedResources =
ResourceLinker.create(aaptConfigOptions.aapt2, executorService, linkedOut)
Expand All @@ -190,14 +193,18 @@ public static void main(String[] args) throws Exception {
.copyMainDexProguardTo(options.mainDexProguardOutput)
.createSourceJar(options.srcJarOutput)
.copyRTxtTo(options.rOutput);
profiler.recordEndOf("link");
if (options.resourcesOutput != null) {
profiler.recordEndOf("link");
if (options.resourcesOutput != null) {
profiler.startTask("package");
// The compiled resources and the merged resources should be the same.
// TODO(corysmith): Decompile or otherwise provide the exact resources in the apk.
packagedResources
.packageWith(mergedAndroidData.getResourceDir())
.writeTo(options.resourcesOutput, false);
ResourcesZip.fromApk(
mergedAndroidData.getResourceDir(),
packagedResources.getApk(),
packagedResources.getResourceIds())
.writeTo(options.resourcesOutput, /* compress= */ false);
profiler.recordEndOf("package");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,12 @@
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;

Expand All @@ -57,101 +53,68 @@
*/
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(
ImmutableList.of(Options.class, Aapt2ConfigOptions.class, Aapt2ShrinkOptions.class));
OptionsParser.newOptionsParser(Options.class, Aapt2ConfigOptions.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");
ExecutorServiceCloser executorService = ExecutorServiceCloser.createWithFixedPoolOf(15)) {
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");

final ResourcesZip resourcesZip =
ResourcesZip.createFrom(
options.resourcesZip, scopedTmp.subDirectoryOf("merged-resources"));
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());
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");

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);
}
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);
profiler.recordEndOf("shrink");
}
}
Expand Down
Loading

0 comments on commit 0a635c5

Please sign in to comment.