diff options
author | 2017-06-28 21:22:28 +0200 | |
---|---|---|
committer | 2017-06-29 09:33:15 +0200 | |
commit | 36a1f0e1210a920c6850d786fd5321b4ac562867 (patch) | |
tree | 2c5a6c44753a1cefbe907cb46b954356e28fed8a /src/tools/android/java | |
parent | 5b91934474a5c245f82135274b51ec4a65ef6bab (diff) |
Fix extra bytes and missing resources for roboelectric tests:
*) Changed the GenerateRobolectricResourceSymbolsAction to merge duplicate namespaces from the dependency list input.
*) RClassGenerator throws if an existing R.class file exists for that package.
*) New test for duplicate package dependencies
RELNOTES: none
PiperOrigin-RevId: 160436937
Diffstat (limited to 'src/tools/android/java')
2 files changed, 17 insertions, 8 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/GenerateRobolectricResourceSymbolsAction.java b/src/tools/android/java/com/google/devtools/build/android/GenerateRobolectricResourceSymbolsAction.java index 0857fe37fd..4f33dd490d 100644 --- a/src/tools/android/java/com/google/devtools/build/android/GenerateRobolectricResourceSymbolsAction.java +++ b/src/tools/android/java/com/google/devtools/build/android/GenerateRobolectricResourceSymbolsAction.java @@ -33,6 +33,7 @@ import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -43,7 +44,7 @@ import java.util.logging.Level; import java.util.logging.Logger; /** - * This action generates consistant ids R.class files for use in robolectric tests. + * This action generates consistent ids R.class files for use in robolectric tests. */ public class GenerateRobolectricResourceSymbolsAction { @@ -51,11 +52,11 @@ public class GenerateRobolectricResourceSymbolsAction { Logger.getLogger(GenerateRobolectricResourceSymbolsAction.class.getName()); private static final class WriteLibraryRClass implements Callable<Boolean> { - private final Entry<String, ListenableFuture<ResourceSymbols>> librarySymbolEntry; + private final Entry<String, Collection<ListenableFuture<ResourceSymbols>>> librarySymbolEntry; private final RClassGenerator generator; private WriteLibraryRClass( - Entry<String, ListenableFuture<ResourceSymbols>> librarySymbolEntry, + Entry<String, Collection<ListenableFuture<ResourceSymbols>>> librarySymbolEntry, RClassGenerator generator) { this.librarySymbolEntry = librarySymbolEntry; this.generator = generator; @@ -63,8 +64,14 @@ public class GenerateRobolectricResourceSymbolsAction { @Override public Boolean call() throws Exception { + List<ResourceSymbols> resourceSymbolsList = new ArrayList<>(); + for (final ListenableFuture<ResourceSymbols> resourceSymbolsReader : + librarySymbolEntry.getValue()) { + resourceSymbolsList.add(resourceSymbolsReader.get()); + } + generator.write( - librarySymbolEntry.getKey(), librarySymbolEntry.getValue().get().asInitializers()); + librarySymbolEntry.getKey(), ResourceSymbols.merge(resourceSymbolsList).asInitializers()); return true; } } @@ -152,8 +159,8 @@ public class GenerateRobolectricResourceSymbolsAction { libraries.add(library); } List<ListenableFuture<Boolean>> writeSymbolsTask = new ArrayList<>(); - for (final Entry<String, ListenableFuture<ResourceSymbols>> librarySymbolEntry : - ResourceSymbols.loadFrom(libraries, executorService, null).entries()) { + for (final Entry<String, Collection<ListenableFuture<ResourceSymbols>>> librarySymbolEntry : + ResourceSymbols.loadFrom(libraries, executorService, null).asMap().entrySet()) { writeSymbolsTask.add( executorService.submit(new WriteLibraryRClass(librarySymbolEntry, generator))); } 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 ea09b5bbd3..81a55e09de 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 @@ -13,6 +13,8 @@ // limitations under the License. package com.google.devtools.build.android.resources; +import static java.nio.file.StandardOpenOption.CREATE_NEW; + import com.android.SdkConstants; import com.android.resources.ResourceType; import com.google.common.base.Splitter; @@ -116,7 +118,7 @@ public class RClassGenerator { Opcodes.ACC_PUBLIC | Opcodes.ACC_FINAL | Opcodes.ACC_STATIC); } classWriter.visitEnd(); - Files.write(rClassFile, classWriter.toByteArray()); + Files.write(rClassFile, classWriter.toByteArray(), CREATE_NEW); // Now generate the R$inner.class files. for (Map.Entry<ResourceType, Map<String, FieldInitializer>> entry : initializersToWrite) { writeInnerClass(entry.getValue(), packageDir, rClassName, entry.getKey().toString()); @@ -151,7 +153,7 @@ public class RClassGenerator { innerClassWriter.visitEnd(); Path innerFile = packageDir.resolve("R$" + innerClass + ".class"); - Files.write(innerFile, innerClassWriter.toByteArray()); + Files.write(innerFile, innerClassWriter.toByteArray(), CREATE_NEW); } private String writeInnerClassHeader( |