use of com.android.builder.internal.SymbolLoader 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.SymbolLoader 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.SymbolLoader in project bazel by bazelbuild.
the class RClassGeneratorTest method checkIntArrays.
public void checkIntArrays(boolean finalFields) throws Exception {
SymbolLoader symbolValues = createSymbolFile("R.txt", "int attr android_layout 0x010100f2", "int attr bar 0x7f010001", "int attr baz 0x7f010002", "int attr fox 0x7f010003", "int attr attr 0x7f010004", "int attr another_attr 0x7f010005", "int attr zoo 0x7f010006", // Test several > 5 elements, so that clinit must use bytecodes other than iconst_0 to 5.
"int[] styleable ActionButton { 0x010100f2, 0x7f010001, 0x7f010002, 0x7f010003, " + "0x7f010004, 0x7f010005, 0x7f010006 }", // The array indices of each attribute.
"int styleable ActionButton_android_layout 0", "int styleable ActionButton_another_attr 5", "int styleable ActionButton_attr 4", "int styleable ActionButton_bar 1", "int styleable ActionButton_baz 2", "int styleable ActionButton_fox 3", "int styleable ActionButton_zoo 6");
SymbolLoader symbolsInLibrary = symbolValues;
Path out = temp.resolve("classes");
Files.createDirectories(out);
RClassGenerator writer = RClassGenerator.fromSymbols(out, "com.intArray", symbolValues, ImmutableList.of(symbolsInLibrary), finalFields);
writer.write();
Path packageDir = out.resolve("com/intArray");
checkFilesInPackage(packageDir, "R.class", "R$attr.class", "R$styleable.class");
Class<?> outerClass = checkTopLevelClass(out, "com.intArray.R", "com.intArray.R$attr", "com.intArray.R$styleable");
checkInnerClass(out, "com.intArray.R$attr", outerClass, ImmutableMap.<String, Integer>builder().put("android_layout", 0x010100f2).put("bar", 0x7f010001).put("baz", 0x7f010002).put("fox", 0x7f010003).put("attr", 0x7f010004).put("another_attr", 0x7f010005).put("zoo", 0x7f010006).build(), ImmutableMap.<String, List<Integer>>of(), finalFields);
checkInnerClass(out, "com.intArray.R$styleable", outerClass, ImmutableMap.<String, Integer>builder().put("ActionButton_android_layout", 0).put("ActionButton_bar", 1).put("ActionButton_baz", 2).put("ActionButton_fox", 3).put("ActionButton_attr", 4).put("ActionButton_another_attr", 5).put("ActionButton_zoo", 6).build(), ImmutableMap.<String, List<Integer>>of("ActionButton", ImmutableList.of(0x010100f2, 0x7f010001, 0x7f010002, 0x7f010003, 0x7f010004, 0x7f010005, 0x7f010006)), finalFields);
}
use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.
the class RClassGeneratorTest method createSymbolFile.
private SymbolLoader createSymbolFile(String name, String... contents) throws IOException {
Path path = createFile(name, contents);
SymbolLoader symbolFile = new SymbolLoader(path.toFile(), stdLogger);
symbolFile.load();
return symbolFile;
}
use of com.android.builder.internal.SymbolLoader in project bazel by bazelbuild.
the class RClassGeneratorTest method checkSimpleInts.
private void checkSimpleInts(boolean finalFields) throws Exception {
// R.txt with the real IDs after linking together libraries.
SymbolLoader symbolValues = createSymbolFile("R.txt", "int attr agility 0x7f010000", "int attr dexterity 0x7f010001", "int drawable heart 0x7f020000", "int id someTextView 0x7f080000", "int integer maxNotifications 0x7f090000", "int string alphabet 0x7f100000", "int string ok 0x7f100001");
// R.txt for the library, where the values are not the final ones (so ignore them). We only use
// this to keep the # of inner classes small (exactly the set needed by the library).
SymbolLoader symbolsInLibrary = createSymbolFile("lib.R.txt", "int attr agility 0x1", "int id someTextView 0x1", "int string ok 0x1");
Path out = temp.resolve("classes");
Files.createDirectories(out);
RClassGenerator writer = RClassGenerator.fromSymbols(out, "com.bar", symbolValues, ImmutableList.of(symbolsInLibrary), finalFields);
writer.write();
Path packageDir = out.resolve("com/bar");
checkFilesInPackage(packageDir, "R.class", "R$attr.class", "R$id.class", "R$string.class");
Class<?> outerClass = checkTopLevelClass(out, "com.bar.R", "com.bar.R$attr", "com.bar.R$id", "com.bar.R$string");
checkInnerClass(out, "com.bar.R$attr", outerClass, ImmutableMap.of("agility", 0x7f010000), ImmutableMap.<String, List<Integer>>of(), finalFields);
checkInnerClass(out, "com.bar.R$id", outerClass, ImmutableMap.of("someTextView", 0x7f080000), ImmutableMap.<String, List<Integer>>of(), finalFields);
checkInnerClass(out, "com.bar.R$string", outerClass, ImmutableMap.of("ok", 0x7f100001), ImmutableMap.<String, List<Integer>>of(), finalFields);
}
Aggregations