Skip to content

Commit

Permalink
Create proguard.txt in android_library AAR output.
Browse files Browse the repository at this point in the history
The proguard.txt is the concatenation of the proguard_specs on the android_library rule itself. Note that it does not include transitively defined proguard_specs.

Fixes #4467

RELNOTES: android_library AAR output now contains proguard.txt
PiperOrigin-RevId: 191302610
  • Loading branch information
aj-michael authored and Copybara-Service committed Apr 2, 2018
1 parent 1592238 commit cb16c50
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class AarGeneratorBuilder {
private Artifact manifest;
private Artifact rTxt;
private Artifact classes;
private ImmutableList<Artifact> proguardSpecs = ImmutableList.of();

private Artifact aarOut;
private boolean throwOnResourceConflict;
Expand Down Expand Up @@ -81,6 +82,11 @@ public AarGeneratorBuilder setAAROut(Artifact aarOut) {
return this;
}

public AarGeneratorBuilder setProguardSpecs(ImmutableList<Artifact> proguardSpecs) {
this.proguardSpecs = proguardSpecs;
return this;
}

public AarGeneratorBuilder setThrowOnResourceConflict(boolean throwOnResourceConflict) {
this.throwOnResourceConflict = throwOnResourceConflict;
return this;
Expand Down Expand Up @@ -118,6 +124,12 @@ public void build(ActionConstructionContext context) {
ins.add(classes);
}

for (Artifact proguardSpec : proguardSpecs) {
args.add("--proguardSpec");
args.add(proguardSpec.getExecPathString());
ins.add(proguardSpec);
}

args.add("--aarOutput");
args.add(aarOut.getExecPathString());
outs.add(aarOut);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public ConfiguredTarget create(RuleContext ruleContext)
collectTransitiveAars(ruleContext, transitiveAars, transitiveAarArtifacts);

NestedSetBuilder<Artifact> proguardConfigsbuilder = NestedSetBuilder.stableOrder();
proguardConfigsbuilder.addTransitive(new ProguardLibrary(ruleContext).collectProguardSpecs());
ProguardLibrary proguardLibrary = new ProguardLibrary(ruleContext);
proguardConfigsbuilder.addTransitive(proguardLibrary.collectProguardSpecs());
AndroidIdlHelper.maybeAddSupportLibProguardConfigs(ruleContext, proguardConfigsbuilder);
NestedSet<Artifact> transitiveProguardConfigs = proguardConfigsbuilder.build();

Expand Down Expand Up @@ -237,6 +238,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
.withRtxt(primaryResources.getRTxt())
.withClasses(classesJar)
.setAAROut(aarOut)
.setProguardSpecs(proguardLibrary.collectLocalProguardSpecs())
.setThrowOnResourceConflict(
ruleContext.getFragment(AndroidConfiguration.class).throwOnResourceConflict())
.build(ruleContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public NestedSet<Artifact> collectProguardSpecs(Multimap<Mode, String> attribute
/**
* Collects the unvalidated proguard specs exported by this rule.
*/
private Collection<Artifact> collectLocalProguardSpecs() {
public ImmutableList<Artifact> collectLocalProguardSpecs() {
if (!ruleContext.attributes().has(LOCAL_SPEC_ATTRIBUTE, BuildType.LABEL_LIST)) {
return ImmutableList.of();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.android.AarGeneratorAction.AarGeneratorOptions;
import com.google.devtools.build.zip.ZipReader;
import com.google.devtools.common.options.OptionsParser;
import com.google.devtools.common.options.OptionsParsingException;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
Expand Down Expand Up @@ -87,6 +92,7 @@ private static class Builder {
private Path classes;
private Map<Path, String> filesToWrite = new HashMap<>();
private Map<String, String> classesToWrite = new HashMap<>();
private ImmutableList.Builder<Path> proguardSpecs = ImmutableList.builder();
private boolean withEmptyRes = false;
private boolean withEmptyAssets = false;

Expand Down Expand Up @@ -149,9 +155,16 @@ public Builder addClassesFile(String filePackage, String filename, String... lin
return this;
}

public Builder addProguardSpec(String path, String... lines) {
Path proguardSpecPath = root.resolve(path);
proguardSpecs.add(proguardSpecPath);
filesToWrite.put(proguardSpecPath, String.format("%s", Joiner.on("\n").join(lines)));
return this;
}

public AarData build() throws IOException {
writeFiles();
return new AarData(buildMerged(), manifest, rtxt, classes);
return new AarData(buildMerged(), manifest, rtxt, classes, proguardSpecs.build());
}

private MergedAndroidData buildMerged() {
Expand Down Expand Up @@ -206,12 +219,19 @@ private void writeClassesJar() throws IOException {
final Path manifest;
final Path rtxt;
final Path classes;

private AarData(MergedAndroidData data, Path manifest, Path rtxt, Path classes) {
final ImmutableList<Path> proguardSpecs;

private AarData(
MergedAndroidData data,
Path manifest,
Path rtxt,
Path classes,
ImmutableList<Path> proguardSpecs) {
this.data = data;
this.manifest = manifest;
this.rtxt = rtxt;
this.classes = classes;
this.proguardSpecs = proguardSpecs;
}
}

Expand Down Expand Up @@ -343,7 +363,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);
}

@Test public void testWriteAar_DefaultTimestamps() throws Exception {
Expand All @@ -365,7 +386,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);

assertThat(getZipEntryTimestamps(aar)).containsExactly(AarGeneratorAction.DEFAULT_TIMESTAMP);
assertThat(aar.toFile().lastModified()).isEqualTo(AarGeneratorAction.DEFAULT_TIMESTAMP);
Expand All @@ -390,7 +412,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);

// verify aar archive
Set<String> zipEntries = getZipEntries(aar);
Expand Down Expand Up @@ -418,7 +441,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);
}

@Test public void testMissingRtxt() throws Exception {
Expand All @@ -439,7 +463,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);
}

@Test public void testMissingClasses() throws Exception {
Expand All @@ -461,7 +486,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);
}

@Test public void testMissingResources() throws Exception {
Expand All @@ -482,7 +508,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);
}

@Test public void testEmptyResources() throws Exception {
Expand All @@ -505,7 +532,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);
}

@Test public void testMissingAssets() throws Exception {
Expand All @@ -526,7 +554,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);
}

@Test public void testEmptyAssets() throws Exception {
Expand All @@ -548,7 +577,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes);
aarData.classes,
aarData.proguardSpecs);
}

@Test public void testFullIntegration() throws Exception {
Expand Down Expand Up @@ -650,7 +680,8 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
/* filteredResources= */ ImmutableList.of(),
true);

AarGeneratorAction.writeAar(aar, mergedData, aarData.manifest, aarData.rtxt, aarData.classes);
AarGeneratorAction.writeAar(
aar, mergedData, aarData.manifest, aarData.rtxt, aarData.classes, aarData.proguardSpecs);

// verify aar archive
Set<String> zipEntries = getZipEntries(aar);
Expand All @@ -677,4 +708,37 @@ private Set<Long> getZipEntryTimestamps(Path zip) throws IOException {
"assets/some/other/ft/data1.txt",
"assets/some/other/ft/data2.txt");
}

@Test public void testProguardSpecs() throws Exception {
Path aar = tempDir.resolve("foo.aar");
AarData aarData =
new AarData.Builder(tempDir.resolve("data"))
.createManifest("AndroidManifest.xml", "com.google.android.apps.foo.d1", "")
.createRtxt("R.txt", "")
.withEmptyResources(true)
.withEmptyAssets(true)
.createClassesJar("classes.jar")
.addProguardSpec("spec1", "foo", "bar")
.addProguardSpec("spec2", "baz")
.build();

AarGeneratorAction.writeAar(
aar,
aarData.data,
aarData.manifest,
aarData.rtxt,
aarData.classes,
aarData.proguardSpecs);
Set<String> zipEntries = getZipEntries(aar);
assertThat(zipEntries).contains("proguard.txt");
ZipReader aarReader = new ZipReader(aar.toFile());
List<String> proguardTxtContents =
new BufferedReader(
new InputStreamReader(
aarReader.getInputStream(aarReader.getEntry("proguard.txt")),
StandardCharsets.UTF_8))
.lines()
.collect(Collectors.toList());
assertThat(proguardTxtContents).containsExactly("foo", "bar", "baz").inOrder();
}
}
1 change: 1 addition & 0 deletions src/test/java/com/google/devtools/build/android/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ java_test(
srcs = ["AarGeneratorActionTest.java"],
deps = [
":test_utils",
"//src/java_tools/singlejar/java/com/google/devtools/build/zip",
"//src/main/java/com/google/devtools/common/options",
"//src/tools/android/java/com/google/devtools/build/android:android_builder_lib",
"//third_party:android_common_25_0_0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// limitations under the License.
package com.google.devtools.build.android;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.android.builder.core.VariantType;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
Expand Down Expand Up @@ -124,6 +126,18 @@ public static final class AarGeneratorOptions extends OptionsBase {
)
public Path classes;

@Option(
name = "proguardSpec",
defaultValue = "",
converter = ExistingPathConverter.class,
allowMultiple = true,
category = "input",
documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
effectTags = {OptionEffectTag.UNKNOWN},
help = "Path to proguard spec file."
)
public List<Path> proguardSpecs;

@Option(
name = "aarOutput",
defaultValue = "null",
Expand Down Expand Up @@ -179,7 +193,13 @@ public static void main(String[] args) {
options.throwOnResourceConflict);
logger.fine(String.format("Merging finished at %dms", timer.elapsed(TimeUnit.MILLISECONDS)));

writeAar(options.aarOutput, mergedData, options.manifest, options.rtxt, options.classes);
writeAar(
options.aarOutput,
mergedData,
options.manifest,
options.rtxt,
options.classes,
options.proguardSpecs);
logger.fine(
String.format("Packaging finished at %dms", timer.elapsed(TimeUnit.MILLISECONDS)));
} catch (MergeConflictException e) {
Expand Down Expand Up @@ -214,7 +234,12 @@ static void checkFlags(AarGeneratorOptions options) {

@VisibleForTesting
static void writeAar(
Path aar, final MergedAndroidData data, Path manifest, Path rtxt, Path classes)
Path aar,
final MergedAndroidData data,
Path manifest,
Path rtxt,
Path classes,
List<Path> proguardSpecs)
throws IOException {
try (final ZipOutputStream zipOut =
new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(aar)))) {
Expand All @@ -240,6 +265,17 @@ static void writeAar(
zipOut.write(Files.readAllBytes(rtxt));
zipOut.closeEntry();

if (!proguardSpecs.isEmpty()) {
ZipEntry proguardTxt = new ZipEntry("proguard.txt");
proguardTxt.setTime(DEFAULT_TIMESTAMP);
zipOut.putNextEntry(proguardTxt);
for (Path proguardSpec : proguardSpecs) {
zipOut.write(Files.readAllBytes(proguardSpec));
zipOut.write("\r\n".getBytes(UTF_8));
}
zipOut.closeEntry();
}

if (Files.exists(data.getAssetDir()) && data.getAssetDir().toFile().list().length > 0) {
ZipDirectoryWriter assetWriter =
new ZipDirectoryWriter(zipOut, data.getAssetDir(), "assets");
Expand Down
4 changes: 2 additions & 2 deletions tools/cpp/CROSSTOOL.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default_toolchain {
}

default_toolchain {
cpu: "local"
cpu: "armeabi-v7a"
toolchain_identifier: "stub_armeabi-v7a"
}

Expand All @@ -47,7 +47,7 @@ default_toolchain {
}

default_toolchain {
cpu: "local"
cpu: "ios_x86_64"
toolchain_identifier: "ios_x86_64"
}

Expand Down

0 comments on commit cb16c50

Please sign in to comment.