aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2016-07-19 23:35:54 +0000
committerGravatar Dmitry Lomov <dslomov@google.com>2016-07-20 09:10:18 +0000
commita5851ac47f5ba7ef3e38373fea22feb4885738b1 (patch)
treed959ea7a8c76dfc55b56488791dc7e9b24d2c0b3
parent0c6cccdcb423e155031d8f4930778a49bd2b061e (diff)
Plumb customPackage to RClassGenerator and skip empty R classes
Forgot to do this, so we get compiler errors since it can't find the R class imported under the requested custom java package. Also, avoid writing out the top-level R.class file if there are no symbols. There is a test that happens to check for this. -- MOS_MIGRATED_REVID=127888913
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java6
-rw-r--r--src/test/java/com/google/devtools/build/android/RClassGeneratorActionTest.java118
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/resources/RClassGenerator.java9
3 files changed, 109 insertions, 24 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java
index 973966943b..933790a9c0 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/android/RClassGeneratorActionBuilder.java
@@ -14,6 +14,7 @@
package com.google.devtools.build.lib.rules.android;
import com.google.common.base.Function;
+import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
@@ -27,10 +28,8 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.rules.android.AndroidResourcesProvider.ResourceContainer;
-
import java.util.ArrayList;
import java.util.List;
-
import javax.annotation.Nullable;
/**
@@ -114,6 +113,9 @@ public class RClassGeneratorActionBuilder {
builder.addExecPath("--primaryManifest", primary.getManifest());
inputs.add(primary.getManifest());
}
+ if (!Strings.isNullOrEmpty(primary.getJavaPackage())) {
+ builder.add("--packageForR").add(primary.getJavaPackage());
+ }
if (dependencies != null) {
Iterable<ResourceContainer> depResources = dependencies.getResources();
if (depResources.iterator().hasNext()) {
diff --git a/src/test/java/com/google/devtools/build/android/RClassGeneratorActionTest.java b/src/test/java/com/google/devtools/build/android/RClassGeneratorActionTest.java
index dd28b3ab54..3b8f72c5cd 100644
--- a/src/test/java/com/google/devtools/build/android/RClassGeneratorActionTest.java
+++ b/src/test/java/com/google/devtools/build/android/RClassGeneratorActionTest.java
@@ -19,12 +19,6 @@ import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
@@ -36,6 +30,10 @@ import java.util.Collections;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
/**
* Tests for {@link RClassGeneratorAction}.
@@ -114,20 +112,20 @@ public class RClassGeneratorActionTest {
RClassGeneratorAction.main(
ImmutableList.<String>of(
- "--primaryRTxt",
- binarySymbols.toString(),
- "--primaryManifest",
- binaryManifest.toString(),
- "--libraries",
- libFooSymbols
- + File.pathSeparator
- + libFooManifest
- + ","
- + libBarSymbols
- + File.pathSeparator
- + libBarManifest,
- "--classJarOutput",
- jarPath.toString())
+ "--primaryRTxt",
+ binarySymbols.toString(),
+ "--primaryManifest",
+ binaryManifest.toString(),
+ "--libraries",
+ libFooSymbols
+ + File.pathSeparator
+ + libFooManifest
+ + ","
+ + libBarSymbols
+ + File.pathSeparator
+ + libBarManifest,
+ "--classJarOutput",
+ jarPath.toString())
.toArray(new String[0]));
assertThat(Files.exists(jarPath)).isTrue();
@@ -215,6 +213,86 @@ public class RClassGeneratorActionTest {
}
}
+ @Test
+ public void customPackageForR() throws Exception {
+ Path binaryManifest = ManifestBuilder.of(tempDir.resolve("binary"))
+ .createManifest("AndroidManifest.xml", "com.google.app",
+ "<application android:name=\"com.google.app\">",
+ "<activity android:name=\"com.google.foo.activityFoo\" />",
+ "</application>");
+ Path libFooManifest = ManifestBuilder.of(tempDir.resolve("libFoo"))
+ .createManifest("AndroidManifest.xml", "com.google.foo", "");
+
+ Path binarySymbols = createFile("R.txt",
+ "int attr agility 0x7f010000",
+ "int integer maxNotifications 0x7f090000",
+ "int string ok 0x7f100001");
+ Path libFooSymbols = createFile("libFoo.R.txt",
+ "int string ok 0x1");
+ Path jarPath = tempDir.resolve("app_resources.jar");
+ RClassGeneratorAction.main(
+ ImmutableList.<String>of(
+ "--primaryRTxt",
+ binarySymbols.toString(),
+ "--primaryManifest",
+ binaryManifest.toString(),
+ "--packageForR", "com.custom.er",
+ "--libraries",
+ libFooSymbols + File.pathSeparator + libFooManifest,
+ "--classJarOutput",
+ jarPath.toString())
+ .toArray(new String[0]));
+
+ assertThat(Files.exists(jarPath)).isTrue();
+ assertThat(Files.getLastModifiedTime(jarPath)).isEqualTo(FileTime.fromMillis(0));
+
+ try (ZipFile zip = new ZipFile(jarPath.toFile())) {
+ List<? extends ZipEntry> zipEntries = Collections.list(zip.entries());
+ Iterable<String> entries = getZipFilenames(zipEntries);
+ assertThat(entries)
+ .containsExactly(
+ Paths.get("com/google/foo/R$string.class").toString(),
+ Paths.get("com/google/foo/R.class").toString(),
+ Paths.get("com/custom/er/R$attr.class").toString(),
+ Paths.get("com/custom/er/R$integer.class").toString(),
+ Paths.get("com/custom/er/R$string.class").toString(),
+ Paths.get("com/custom/er/R.class").toString(),
+ Paths.get("META-INF/MANIFEST.MF").toString());
+ }
+ }
+
+ @Test
+ public void noSymbolsNoRClass() throws Exception {
+ Path binaryManifest = ManifestBuilder.of(tempDir.resolve("binary"))
+ .createManifest("AndroidManifest.xml", "com.google.app",
+ "<application android:name=\"com.google.app\">",
+ "<activity android:name=\"com.google.foo.activityFoo\" />",
+ "</application>");
+
+ Path binarySymbols = createFile("R.txt", "");
+ Path jarPath = tempDir.resolve("app_resources.jar");
+ RClassGeneratorAction.main(
+ ImmutableList.<String>of(
+ "--primaryRTxt",
+ binarySymbols.toString(),
+ "--primaryManifest",
+ binaryManifest.toString(),
+ "--classJarOutput",
+ jarPath.toString())
+ .toArray(new String[0]));
+
+ assertThat(Files.exists(jarPath)).isTrue();
+ assertThat(Files.getLastModifiedTime(jarPath)).isEqualTo(FileTime.fromMillis(0));
+
+ try (ZipFile zip = new ZipFile(jarPath.toFile())) {
+ List<? extends ZipEntry> zipEntries = Collections.list(zip.entries());
+ Iterable<String> entries = getZipFilenames(zipEntries);
+ assertThat(entries)
+ .containsExactly(
+ Paths.get("META-INF/MANIFEST.MF").toString());
+ }
+ }
+
private Path createFile(String name, String... contents) throws IOException {
Path path = tempDir.resolve(name);
Files.createDirectories(path.getParent());
diff --git a/src/tools/android/java/com/google/devtools/build/android/resources/RClassGenerator.java b/src/tools/android/java/com/google/devtools/build/android/resources/RClassGenerator.java
index be25c0f6fa..8735e9ba8c 100644
--- a/src/tools/android/java/com/google/devtools/build/android/resources/RClassGenerator.java
+++ b/src/tools/android/java/com/google/devtools/build/android/resources/RClassGenerator.java
@@ -109,7 +109,14 @@ public class RClassGenerator {
packageDir = new File(packageDir, folder);
}
File rClassFile = new File(packageDir, SdkConstants.FN_COMPILED_RESOURCE_CLASS);
+ // At least create the outFolder that was requested. However, if there are no symbols, don't
+ // create the R.class and inner class files (no need to have an empty class).
Files.createParentDirs(rClassFile);
+ Table<String, String, SymbolEntry> symbols = getAllSymbols();
+ if (symbols.isEmpty()) {
+ return;
+ }
+
String packageWithSlashes = packageName.replaceAll("\\.", "/");
String rClassName = packageWithSlashes + "/R";
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
@@ -119,9 +126,7 @@ public class RClassGenerator {
classWriter.visitSource(SdkConstants.FN_RESOURCE_CLASS, null);
writeConstructor(classWriter);
- Table<String, String, SymbolEntry> symbols = getAllSymbols();
Table<String, String, SymbolEntry> values = getSymbols(symbolValues);
-
Set<String> rowSet = symbols.rowKeySet();
List<String> rowList = new ArrayList<>(rowSet);
Collections.sort(rowList);