summaryrefslogtreecommitdiff
path: root/tools/aapt2/cmd/Compile.cpp
diff options
context:
space:
mode:
authorIzabela Orlowska <imorlowska@google.com>2017-12-05 12:07:28 +0000
committerIzabela Orlowska <imorlowska@google.com>2017-12-20 12:04:23 +0000
commitc81d9f36ec2372b0a9424186f5fee5b189769d4a (patch)
tree2e018e81365882200ab7ace6783c5651e11ad7bb /tools/aapt2/cmd/Compile.cpp
parent8226fd9e7d9bbf2b9d3f2293e788628ef229b136 (diff)
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
Diffstat (limited to 'tools/aapt2/cmd/Compile.cpp')
-rw-r--r--tools/aapt2/cmd/Compile.cpp83
1 files changed, 83 insertions, 0 deletions
diff --git a/tools/aapt2/cmd/Compile.cpp b/tools/aapt2/cmd/Compile.cpp
index 83512b9126da..7c1e96e88fee 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 @@ static Maybe<ResourcePathData> ExtractResourcePathData(const std::string& path,
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 @@ static bool CompileTable(IAaptContext* context, const CompileOptions& options,
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 @@ static bool CompileXml(IAaptContext* context, const CompileOptions& options,
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 @@ int Compile(const std::vector<StringPiece>& args, IDiagnostics* diagnostics) {
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)",