Search in sources :

Example 11 with SymbolLoader

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();
}
Also used : Path(java.nio.file.Path) SymbolLoader(com.android.builder.internal.SymbolLoader) Test(org.junit.Test)

Example 12 with SymbolLoader

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);
}
Also used : Path(java.nio.file.Path) SymbolLoader(com.android.builder.internal.SymbolLoader) Test(org.junit.Test)

Example 13 with SymbolLoader

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);
}
Also used : Path(java.nio.file.Path) SymbolLoader(com.android.builder.internal.SymbolLoader) Test(org.junit.Test)

Example 14 with SymbolLoader

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);
}
Also used : Path(java.nio.file.Path) SymbolLoader(com.android.builder.internal.SymbolLoader) Test(org.junit.Test)

Example 15 with SymbolLoader

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;
    }
}
Also used : HashMap(java.util.HashMap) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SymbolLoader(com.android.builder.internal.SymbolLoader) SymbolFileProvider(com.android.builder.dependency.SymbolFileProvider) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) ExecutionException(java.util.concurrent.ExecutionException) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) Nullable(com.android.annotations.Nullable)

Aggregations

SymbolLoader (com.android.builder.internal.SymbolLoader)15 Path (java.nio.file.Path)10 Test (org.junit.Test)5 SymbolFileProvider (com.android.builder.dependency.SymbolFileProvider)3 SymbolWriter (com.android.builder.internal.SymbolWriter)3 File (java.io.File)3 ArrayList (java.util.ArrayList)3 AndroidLibrary (com.android.builder.model.AndroidLibrary)2 IOException (java.io.IOException)2 Nullable (com.android.annotations.Nullable)1 ProcessInfo (com.android.ide.common.process.ProcessInfo)1 ProcessResult (com.android.ide.common.process.ProcessResult)1 BuildToolInfo (com.android.sdklib.BuildToolInfo)1 Stopwatch (com.google.common.base.Stopwatch)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 RClassGenerator (com.google.devtools.build.android.resources.RClassGenerator)1 OptionsParser (com.google.devtools.common.options.OptionsParser)1 Closeable (java.io.Closeable)1 HashMap (java.util.HashMap)1