AAPT2: Add flag to compile command for outputting symbols
Only XML files can define resources inside of them, so the fragment
R.txt will only be created for XML files. The fragment R.txt will
contain files defined inside the XML files and the file itself.
For example for res/layout/my_layout.xml that defines "@+id/myView" the
fragment R.txt will contain "default int id myView" and "default int
layout my_layout".
Resources defined with the "public" keyword will have the word "public"
in the partial R.txt, resources defined with the "java-symbol" keyword
will have the word "private, and all other resources will have the word
"default".
If a string is declared in values/strings.xml as:
'<string name="foo">text</string>'
then the partial R.txt will contain "default int string foo". If the
same string is also marked as public in the values/public.xml as:
'<public type="string" name="foo" id="0x7f000001"/>
then the partial R.txt for that file will cointain:
"public int string foo".
Also, the resource IDs will be skipped as this is only for compilation,
proper IDs will be generated at linking phase.
Test: manual
Change-Id: I37d07d5ee4a9f2e5a60a54e48579eba86ae7dd60
diff --git a/tools/aapt2/cmd/Compile.cpp b/tools/aapt2/cmd/Compile.cpp
index 83512b9..7c1e96e 100644
--- a/tools/aapt2/cmd/Compile.cpp
+++ b/tools/aapt2/cmd/Compile.cpp
@@ -49,6 +49,7 @@
#include "xml/XmlPullParser.h"
using ::aapt::io::FileInputStream;
+using ::aapt::text::Printer;
using ::android::StringPiece;
using ::android::base::SystemErrorCodeToString;
using ::google::protobuf::io::CopyingOutputStreamAdaptor;
@@ -112,6 +113,7 @@
struct CompileOptions {
std::string output_path;
Maybe<std::string> res_dir;
+ Maybe<std::string> generate_text_symbols_path;
bool pseudolocalize = false;
bool no_png_crunch = false;
bool legacy_mode = false;
@@ -261,6 +263,58 @@
context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish entry");
return false;
}
+
+ if (options.generate_text_symbols_path) {
+ io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
+
+ if (fout_text.HadError()) {
+ context->GetDiagnostics()->Error(DiagMessage()
+ << "failed writing to'"
+ << options.generate_text_symbols_path.value()
+ << "': " << fout_text.GetError());
+ return false;
+ }
+
+ Printer r_txt_printer(&fout_text);
+ for (const auto& package : table.packages) {
+ for (const auto& type : package->types) {
+ for (const auto& entry : type->entries) {
+ // Check access modifiers.
+ switch(entry->visibility.level) {
+ case Visibility::Level::kUndefined :
+ r_txt_printer.Print("default ");
+ break;
+ case Visibility::Level::kPublic :
+ r_txt_printer.Print("public ");
+ break;
+ case Visibility::Level::kPrivate :
+ r_txt_printer.Print("private ");
+ }
+
+ if (type->type != ResourceType::kStyleable) {
+ r_txt_printer.Print("int ");
+ r_txt_printer.Print(to_string(type->type));
+ r_txt_printer.Print(" ");
+ r_txt_printer.Println(entry->name);
+ } else {
+ r_txt_printer.Print("int[] styleable ");
+ r_txt_printer.Println(entry->name);
+
+ if (!entry->values.empty()) {
+ auto styleable = static_cast<const Styleable*>(entry->values.front()->value.get());
+ for (const auto& attr : styleable->entries) {
+ r_txt_printer.Print("default int styleable ");
+ r_txt_printer.Print(entry->name);
+ r_txt_printer.Print("_");
+ r_txt_printer.Println(attr.name.value().entry);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
return true;
}
@@ -402,6 +456,31 @@
context->GetDiagnostics()->Error(DiagMessage(output_path) << "failed to finish writing data");
return false;
}
+
+ if (options.generate_text_symbols_path) {
+ io::FileOutputStream fout_text(options.generate_text_symbols_path.value());
+
+ if (fout_text.HadError()) {
+ context->GetDiagnostics()->Error(DiagMessage()
+ << "failed writing to'"
+ << options.generate_text_symbols_path.value()
+ << "': " << fout_text.GetError());
+ return false;
+ }
+
+ Printer r_txt_printer(&fout_text);
+ for (const auto res : xmlres->file.exported_symbols) {
+ r_txt_printer.Print("default int id ");
+ r_txt_printer.Println(res.name.entry);
+ }
+
+ // And print ourselves.
+ r_txt_printer.Print("default int ");
+ r_txt_printer.Print(path_data.resource_dir);
+ r_txt_printer.Print(" ");
+ r_txt_printer.Println(path_data.name);
+ }
+
return true;
}
@@ -609,6 +688,10 @@
Flags()
.RequiredFlag("-o", "Output path", &options.output_path)
.OptionalFlag("--dir", "Directory to scan for resources", &options.res_dir)
+ .OptionalFlag("--output-text-symbols",
+ "Generates a text file containing the resource symbols in the\n"
+ "specified file",
+ &options.generate_text_symbols_path)
.OptionalSwitch("--pseudo-localize",
"Generate resources for pseudo-locales "
"(en-XA and ar-XB)",