use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.
the class RClassGeneratorTest method corruptIntArraysTrailingComma.
@Test
public void corruptIntArraysTrailingComma() throws Exception {
boolean finalFields = true;
// Test a few cases of what happens if the R.txt is corrupted. It shouldn't happen unless there
// is a bug in aapt, or R.txt is manually written the wrong way.
SymbolLoader symbolValues = createSymbolFile("R.txt", "int[] styleable ActionMenuView { 1, }");
SymbolLoader symbolsInLibrary = symbolValues;
Path out = temp.resolve("classes");
Files.createDirectories(out);
thrown.expect(NumberFormatException.class);
RClassGenerator writer = RClassGenerator.fromSymbols(out, "com.foo", symbolValues, ImmutableList.of(symbolsInLibrary), finalFields);
writer.write();
}
use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.
the class RClassGeneratorTest method emptyIntArrays.
@Test
public void emptyIntArrays() throws Exception {
boolean finalFields = true;
// Make sure we parse an empty array the way the R.txt writes it.
SymbolLoader symbolValues = createSymbolFile("R.txt", "int[] styleable ActionMenuView { }");
SymbolLoader symbolsInLibrary = symbolValues;
Path out = temp.resolve("classes");
Files.createDirectories(out);
RClassGenerator writer = RClassGenerator.fromSymbols(out, "com.testEmptyIntArray", symbolValues, ImmutableList.of(symbolsInLibrary), finalFields);
writer.write();
Path packageDir = out.resolve("com/testEmptyIntArray");
checkFilesInPackage(packageDir, "R.class", "R$styleable.class");
Class<?> outerClass = checkTopLevelClass(out, "com.testEmptyIntArray.R", "com.testEmptyIntArray.R$styleable");
checkInnerClass(out, "com.testEmptyIntArray.R$styleable", outerClass, ImmutableMap.<String, Integer>of(), ImmutableMap.<String, List<Integer>>of("ActionMenuView", ImmutableList.<Integer>of()), finalFields);
}
use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.
the class RClassGeneratorTest method emptyPackage.
@Test
public void emptyPackage() throws Exception {
boolean finalFields = true;
// Make sure we handle an empty package string.
SymbolLoader symbolValues = createSymbolFile("R.txt", "int string some_string 0x7f200000");
SymbolLoader symbolsInLibrary = symbolValues;
Path out = temp.resolve("classes");
Files.createDirectories(out);
RClassGenerator writer = RClassGenerator.fromSymbols(out, "", symbolValues, ImmutableList.of(symbolsInLibrary), finalFields);
writer.write();
Path packageDir = out.resolve("");
checkFilesInPackage(packageDir, "R.class", "R$string.class");
Class<?> outerClass = checkTopLevelClass(out, "R", "R$string");
checkInnerClass(out, "R$string", outerClass, ImmutableMap.of("some_string", 0x7f200000), ImmutableMap.<String, List<Integer>>of(), finalFields);
}
use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.
the class RClassGeneratorTest method binaryDropsLibraryFields.
@Test
public void binaryDropsLibraryFields() throws Exception {
boolean finalFields = true;
// Test what happens if the binary R.txt is not a strict superset of the
// library R.txt (overrides that drop elements).
SymbolLoader symbolValues = createSymbolFile("R.txt", "int layout stubbable_activity 0x7f020000");
SymbolLoader symbolsInLibrary = createSymbolFile("lib.R.txt", "int id debug_text_field 0x1", "int id debug_text_field2 0x1", "int layout stubbable_activity 0x1");
Path out = temp.resolve("classes");
Files.createDirectories(out);
RClassGenerator writer = RClassGenerator.fromSymbols(out, "com.foo", symbolValues, ImmutableList.of(symbolsInLibrary), finalFields);
writer.write();
Path packageDir = out.resolve("com/foo");
checkFilesInPackage(packageDir, "R.class", "R$id.class", "R$layout.class");
Class<?> outerClass = checkTopLevelClass(out, "com.foo.R", "com.foo.R$id", "com.foo.R$layout");
checkInnerClass(out, "com.foo.R$id", outerClass, ImmutableMap.<String, Integer>of(), ImmutableMap.<String, List<Integer>>of(), finalFields);
checkInnerClass(out, "com.foo.R$layout", outerClass, ImmutableMap.of("stubbable_activity", 0x7f020000), ImmutableMap.<String, List<Integer>>of(), finalFields);
}
use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.
the class AndroidResourceProcessor method loadResourceSymbolTable.
@Nullable
public SymbolLoader loadResourceSymbolTable(List<SymbolFileProvider> libraries, String appPackageName, Path primaryRTxt, Multimap<String, SymbolLoader> libMap) throws IOException {
// The reported availableProcessors may be higher than the actual resources
// (on a shared system). On the other hand, a lot of the work is I/O, so it's not completely
// CPU bound. As a compromise, divide by 2 the reported availableProcessors.
int numThreads = Math.max(1, Runtime.getRuntime().availableProcessors() / 2);
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(numThreads));
try (Closeable closeable = ExecutorServiceCloser.createWith(executorService)) {
// Load the package names from the manifest files.
Map<SymbolFileProvider, ListenableFuture<String>> packageJobs = new HashMap<>();
for (final SymbolFileProvider lib : libraries) {
packageJobs.put(lib, executorService.submit(new PackageParsingTask(lib.getManifest())));
}
Map<SymbolFileProvider, String> packageNames = new HashMap<>();
try {
for (Map.Entry<SymbolFileProvider, ListenableFuture<String>> entry : packageJobs.entrySet()) {
packageNames.put(entry.getKey(), entry.getValue().get());
}
} catch (InterruptedException | ExecutionException e) {
throw new IOException("Failed to load package name: ", e);
}
// Associate the packages with symbol files.
for (SymbolFileProvider lib : libraries) {
String packageName = packageNames.get(lib);
// stored in the primaryRTxt file.
if (appPackageName.equals(packageName)) {
continue;
}
File rFile = lib.getSymbolFile();
// If the library has no resource, this file won't exist.
if (rFile.isFile()) {
SymbolLoader libSymbols = new SymbolLoader(rFile, stdLogger);
libMap.put(packageName, libSymbols);
}
}
// Even if there are no libraries, load fullSymbolValues, in case we only have resources
// defined for the binary.
File primaryRTxtFile = primaryRTxt.toFile();
SymbolLoader fullSymbolValues = null;
if (primaryRTxtFile.isFile()) {
fullSymbolValues = new SymbolLoader(primaryRTxtFile, stdLogger);
}
// Now load the symbol files in parallel.
List<ListenableFuture<?>> loadJobs = new ArrayList<>();
Iterable<SymbolLoader> toLoad = fullSymbolValues != null ? Iterables.concat(libMap.values(), ImmutableList.of(fullSymbolValues)) : libMap.values();
for (final SymbolLoader loader : toLoad) {
loadJobs.add(executorService.submit(new SymbolLoadingTask(loader)));
}
try {
Futures.allAsList(loadJobs).get();
} catch (InterruptedException | ExecutionException e) {
throw new IOException("Failed to load SymbolFile: ", e);
}
return fullSymbolValues;
}
}
Aggregations