Search in sources :

Example 6 with SymbolLoader

use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.

the class RClassGeneratorAction method main.

public static void main(String[] args) throws Exception {
    final Stopwatch timer = Stopwatch.createStarted();
    OptionsParser optionsParser = OptionsParser.newOptionsParser(Options.class);
    optionsParser.enableParamsFileSupport(FileSystems.getDefault());
    if (args.length == 1 && args[0].startsWith("@")) {
        args = Files.readAllLines(Paths.get(args[0].substring(1)), StandardCharsets.UTF_8).toArray(new String[0]);
    }
    optionsParser.parseAndExitUponError(args);
    Options options = optionsParser.getOptions(Options.class);
    Preconditions.checkNotNull(options.classJarOutput);
    final AndroidResourceProcessor resourceProcessor = new AndroidResourceProcessor(STD_LOGGER);
    try (ScopedTemporaryDirectory scopedTmp = new ScopedTemporaryDirectory("android_res_compile_tmp")) {
        Path tmp = scopedTmp.getPath();
        Path classOutPath = tmp.resolve("compiled_classes");
        logger.fine(String.format("Setup finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
        List<SymbolFileProvider> libraries = new ArrayList<>();
        for (DependencySymbolFileProvider library : options.libraries) {
            libraries.add(library);
        }
        // are no libraries).
        if (options.primaryRTxt != null) {
            String appPackageName = options.packageForR;
            if (appPackageName == null) {
                appPackageName = VariantConfiguration.getManifestPackage(options.primaryManifest.toFile());
            }
            Multimap<String, SymbolLoader> libSymbolMap = ArrayListMultimap.create();
            SymbolLoader fullSymbolValues = resourceProcessor.loadResourceSymbolTable(libraries, appPackageName, options.primaryRTxt, libSymbolMap);
            logger.fine(String.format("Load symbols finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
            // For now, assuming not used for libraries and setting final access for fields.
            if (fullSymbolValues != null) {
                resourceProcessor.writePackageRClasses(libSymbolMap, fullSymbolValues, appPackageName, classOutPath, true);
                logger.fine(String.format("Finished R.class at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
            }
        } else {
            Files.createDirectories(classOutPath);
        }
        // We write .class files to temp, then jar them up after (we create a dummy jar, even if
        // there are no class files).
        AndroidResourceOutputs.createClassJar(classOutPath, options.classJarOutput);
        logger.fine(String.format("createClassJar finished at %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
    } finally {
        resourceProcessor.shutdown();
    }
    logger.fine(String.format("Compile action done in %sms", timer.elapsed(TimeUnit.MILLISECONDS)));
}
Also used : Path(java.nio.file.Path) Stopwatch(com.google.common.base.Stopwatch) ArrayList(java.util.ArrayList) OptionsParser(com.google.devtools.common.options.OptionsParser) SymbolLoader(com.android.builder.internal.SymbolLoader) SymbolFileProvider(com.android.builder.dependency.SymbolFileProvider)

Example 7 with SymbolLoader

use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.

the class AndroidResourceProcessor method writePackageRClasses.

void writePackageRClasses(Multimap<String, SymbolLoader> libMap, SymbolLoader fullSymbolValues, String appPackageName, Path classesOut, boolean finalFields) throws IOException {
    for (String packageName : libMap.keySet()) {
        Collection<SymbolLoader> symbols = libMap.get(packageName);
        RClassGenerator classWriter = RClassGenerator.fromSymbols(classesOut, packageName, fullSymbolValues, symbols, finalFields);
        classWriter.write();
    }
    // Unlike the R.java generation, we also write the app's R.class file so that the class
    // jar file can be complete (aapt doesn't generate it for us).
    RClassGenerator classWriter = RClassGenerator.fromSymbols(classesOut, appPackageName, fullSymbolValues, ImmutableList.of(fullSymbolValues), finalFields);
    classWriter.write();
}
Also used : RClassGenerator(com.google.devtools.build.android.resources.RClassGenerator) SymbolLoader(com.android.builder.internal.SymbolLoader)

Example 8 with SymbolLoader

use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.

the class AndroidResourceProcessor method writePackageRJavaFiles.

private void writePackageRJavaFiles(Multimap<String, SymbolLoader> libMap, SymbolLoader fullSymbolValues, Path sourceOut) throws IOException {
    // Loop on all the package name, merge all the symbols to write, and write.
    for (String packageName : libMap.keySet()) {
        Collection<SymbolLoader> symbols = libMap.get(packageName);
        SymbolWriter writer = new SymbolWriter(sourceOut.toString(), packageName, fullSymbolValues);
        for (SymbolLoader symbolLoader : symbols) {
            writer.addSymbolsToWrite(symbolLoader);
        }
        writer.write();
    }
}
Also used : SymbolWriter(com.android.builder.internal.SymbolWriter) SymbolLoader(com.android.builder.internal.SymbolLoader)

Example 9 with SymbolLoader

use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.

the class AndroidResourceProcessor method writeDependencyPackageRJavaFiles.

void writeDependencyPackageRJavaFiles(List<DependencyAndroidData> dependencyData, String customPackageForR, Path androidManifest, Path sourceOut) throws IOException {
    List<SymbolFileProvider> libraries = new ArrayList<>();
    for (DependencyAndroidData dataDep : dependencyData) {
        SymbolFileProvider library = dataDep.asSymbolFileProvider();
        libraries.add(library);
    }
    String appPackageName = customPackageForR;
    if (appPackageName == null) {
        appPackageName = VariantConfiguration.getManifestPackage(androidManifest.toFile());
    }
    Multimap<String, SymbolLoader> libSymbolMap = ArrayListMultimap.create();
    Path primaryRTxt = sourceOut != null ? sourceOut.resolve("R.txt") : null;
    if (primaryRTxt != null && !libraries.isEmpty()) {
        SymbolLoader fullSymbolValues = loadResourceSymbolTable(libraries, appPackageName, primaryRTxt, libSymbolMap);
        if (fullSymbolValues != null) {
            writePackageRJavaFiles(libSymbolMap, fullSymbolValues, sourceOut);
        }
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) SymbolFileProvider(com.android.builder.dependency.SymbolFileProvider) SymbolLoader(com.android.builder.internal.SymbolLoader)

Example 10 with SymbolLoader

use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.

the class RClassGeneratorTest method corruptIntArraysOmittedMiddle.

@Test
public void corruptIntArraysOmittedMiddle() throws Exception {
    boolean finalFields = true;
    SymbolLoader symbolValues = createSymbolFile("R.txt", "int[] styleable ActionMenuView { 1, , 2 }");
    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)

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