Search in sources :

Example 1 with SymbolWriter

use of com.android.builder.internal.SymbolWriter in project atlas by alibaba.

the class AtlasBuilder method processResources.

/**
     * 对主bundle的资源进行处理
     *
     * @param aaptCommand
     * @param enforceUniquePackageName
     * @param processOutputHandler
     * @throws IOException
     * @throws InterruptedException
     * @throws ProcessException
     */
public void processResources(AaptPackageProcessBuilder aaptCommand, boolean enforceUniquePackageName, ProcessOutputHandler processOutputHandler) throws IOException, InterruptedException, ProcessException {
    checkState(getTargetInfo() != null, "Cannot call processResources() before setTargetInfo() is called.");
    BuildToolInfo buildToolInfo = getTargetInfo().getBuildTools();
    // launch aapt: create the command line
    ProcessInfo processInfo = aaptCommand.build(buildToolInfo, getTargetInfo().getTarget(), getLogger());
    processInfo = new TProcessInfo(processInfo);
    // 打印日志
    //        if (null != getLogger()) {
    //            getLogger().info("[Aapt]" + processInfo.getExecutable() + " "
    //                    + StringUtils.join(processInfo.getArgs(), " "));
    //        }
    ProcessResult result = getProcessExecutor().execute(processInfo, processOutputHandler);
    result.rethrowFailure().assertNormalExitValue();
    // If the project has libraries, R needs to be created for each library.
    if (aaptCommand.getSourceOutputDir() != null && !aaptCommand.getLibraries().isEmpty()) {
        SymbolLoader fullSymbolValues = null;
        // First pass processing the libraries, collecting them by packageName,
        // and ignoring the ones that have the same package name as the application
        // (since that R class was already created).
        String appPackageName = aaptCommand.getPackageForR();
        if (appPackageName == null) {
            appPackageName = ManifestFileUtils.getPackage(aaptCommand.getManifestFile());
        }
        // list of all the symbol loaders per package names.
        Multimap<String, SymbolLoader> libMap = ArrayListMultimap.create();
        for (AndroidLibrary lib : aaptCommand.getLibraries()) {
            if (lib.isOptional()) {
                continue;
            }
            String packageName = ManifestFileUtils.getPackage(lib.getManifest());
            if (appPackageName == null) {
                continue;
            }
            if (appPackageName.equals(packageName)) {
                if (enforceUniquePackageName) {
                    String msg = String.format("Error: A library uses the same package as this project: %s", packageName);
                    throw new RuntimeException(msg);
                }
                // ignore libraries that have the same package name as the app
                continue;
            }
            File rFile = lib.getSymbolFile();
            // if the library has no resource, this file won't exist.
            if (rFile.isFile()) {
                // resources anywhere.
                if (fullSymbolValues == null) {
                    fullSymbolValues = new SymbolLoader(new File(aaptCommand.getSymbolOutputDir(), "R.txt"), getLogger());
                    fullSymbolValues.load();
                }
                SymbolLoader libSymbols = new SymbolLoader(rFile, getLogger());
                libSymbols.load();
                // store these symbols by associating them with the package name.
                libMap.put(packageName, libSymbols);
            }
        }
        // now loop on all the package name, merge all the symbols to write, and write them
        for (String packageName : libMap.keySet()) {
            Collection<SymbolLoader> symbols = libMap.get(packageName);
            if (enforceUniquePackageName && symbols.size() > 1) {
                String msg = String.format("Error: more than one library with package name '%s'", packageName);
                throw new RuntimeException(msg);
            }
            SymbolWriter writer = new SymbolWriter(aaptCommand.getSourceOutputDir(), packageName, fullSymbolValues, false);
            for (SymbolLoader symbolLoader : symbols) {
                writer.addSymbolsToWrite(symbolLoader);
            }
            writer.write();
        }
    }
}
Also used : SymbolWriter(com.android.builder.internal.SymbolWriter) BuildToolInfo(com.android.sdklib.BuildToolInfo) AndroidLibrary(com.android.builder.model.AndroidLibrary) ProcessResult(com.android.ide.common.process.ProcessResult) ProcessInfo(com.android.ide.common.process.ProcessInfo) SymbolLoader(com.android.builder.internal.SymbolLoader) File(java.io.File)

Example 2 with SymbolWriter

use of com.android.builder.internal.SymbolWriter in project atlas by alibaba.

the class AtlasBuilder method processAwbSymbols.

/**
     * 处理Awb的资源文件
     *
     * @param aaptCommand
     * @throws IOException
     */
public void processAwbSymbols(AaptPackageProcessBuilder aaptCommand, File mainSymbolFile, boolean enforceUniquePackageName) throws IOException {
    //1. 首先将主的R.txt和awb生成的R.txt进行merge操作
    File awbSymbolFile = new File(aaptCommand.getSymbolOutputDir(), "R.txt");
    File mergedSymbolFile = new File(aaptCommand.getSymbolOutputDir(), "R-all.txt");
    //合并2个R.txt文件
    try {
        getLogger().info("mainSymbolFile:" + mainSymbolFile);
        if (null != mainSymbolFile && mainSymbolFile.exists()) {
            FileUtils.copyFile(mainSymbolFile, mergedSymbolFile);
        }
        FileUtils.writeLines(mergedSymbolFile, FileUtils.readLines(awbSymbolFile), true);
    } catch (IOException e) {
        throw new RuntimeException("Could not load file ", e);
    }
    //生成awb的java文件
    SymbolLoader awbSymbols = null;
    // First pass processing the libraries, collecting them by packageName,
    // and ignoring the ones that have the same package name as the application
    // (since that R class was already created).
    String appPackageName = aaptCommand.getPackageForR();
    if (appPackageName == null) {
        appPackageName = ManifestFileUtils.getPackage(aaptCommand.getManifestFile());
    }
    awbSymbols = new SymbolLoader(mergedSymbolFile, getLogger());
    awbSymbols.load();
    SymbolWriter writer = new SymbolWriter(aaptCommand.getSourceOutputDir(), appPackageName, awbSymbols, false);
    writer.addSymbolsToWrite(awbSymbols);
    getLogger().info("SymbolWriter Package:" + appPackageName + " to dir:" + aaptCommand.getSourceOutputDir());
    writer.write();
    //再写入各自awb依赖的aar的资源
    if (!aaptCommand.getLibraries().isEmpty()) {
        // list of all the symbol loaders per package names.
        Multimap<String, SymbolLoader> libMap = ArrayListMultimap.create();
        for (AndroidLibrary lib : aaptCommand.getLibraries()) {
            if (lib.isOptional()) {
                continue;
            }
            String packageName = ManifestFileUtils.getPackage(lib.getManifest());
            if (appPackageName == null) {
                continue;
            }
            if (appPackageName.equals(packageName)) {
                if (enforceUniquePackageName) {
                    String msg = String.format("Error: A library uses the same package as this project: %s\n" + "You can temporarily disable this error with android.enforceUniquePackageName=false\n" + "However, this is temporary and will be enforced in 1.0", packageName);
                    throw new RuntimeException(msg);
                }
                // ignore libraries that have the same package name as the app
                continue;
            }
            File rFile = lib.getSymbolFile();
            // if the library has no resource, this file won't exist.
            if (rFile.isFile()) {
                SymbolLoader libSymbols = new SymbolLoader(rFile, getLogger());
                libSymbols.load();
                // store these symbols by associating them with the package name.
                libMap.put(packageName, libSymbols);
            }
        }
        // now loop on all the package name, merge all the symbols to write, and write them
        for (String packageName : libMap.keySet()) {
            Collection<SymbolLoader> symbols = libMap.get(packageName);
            if (enforceUniquePackageName && symbols.size() > 1) {
                String msg = String.format("Error: more than one library with package name '%s'\n" + "You can temporarily disable this error with android.enforceUniquePackageName=false\n" + "However, this is temporary and will be enforced in 1.0", packageName);
                throw new RuntimeException(msg);
            }
            SymbolWriter libWriter = new SymbolWriter(aaptCommand.getSourceOutputDir(), packageName, awbSymbols, false);
            for (SymbolLoader symbolLoader : symbols) {
                libWriter.addSymbolsToWrite(symbolLoader);
            }
            getLogger().info("SymbolWriter Package:" + packageName + " to dir:" + aaptCommand.getSourceOutputDir());
            libWriter.write();
        }
    }
}
Also used : SymbolWriter(com.android.builder.internal.SymbolWriter) AndroidLibrary(com.android.builder.model.AndroidLibrary) IOException(java.io.IOException) File(java.io.File) SymbolLoader(com.android.builder.internal.SymbolLoader)

Example 3 with SymbolWriter

use of com.android.builder.internal.SymbolWriter 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)

Aggregations

SymbolLoader (com.android.builder.internal.SymbolLoader)3 SymbolWriter (com.android.builder.internal.SymbolWriter)3 AndroidLibrary (com.android.builder.model.AndroidLibrary)2 File (java.io.File)2 ProcessInfo (com.android.ide.common.process.ProcessInfo)1 ProcessResult (com.android.ide.common.process.ProcessResult)1 BuildToolInfo (com.android.sdklib.BuildToolInfo)1 IOException (java.io.IOException)1