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();
}
}
}
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();
}
}
}
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();
}
}
Aggregations