Search in sources :

Example 1 with SymbolFileProvider

use of com.android.builder.dependency.SymbolFileProvider 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 2 with SymbolFileProvider

use of com.android.builder.dependency.SymbolFileProvider 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 3 with SymbolFileProvider

use of com.android.builder.dependency.SymbolFileProvider 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

SymbolFileProvider (com.android.builder.dependency.SymbolFileProvider)3 SymbolLoader (com.android.builder.internal.SymbolLoader)3 ArrayList (java.util.ArrayList)3 Path (java.nio.file.Path)2 Nullable (com.android.annotations.Nullable)1 Stopwatch (com.google.common.base.Stopwatch)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 ListeningExecutorService (com.google.common.util.concurrent.ListeningExecutorService)1 OptionsParser (com.google.devtools.common.options.OptionsParser)1 Closeable (java.io.Closeable)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionException (java.util.concurrent.ExecutionException)1