Skip navigation links

Package com.android.builder.symbols

The Symbols package contains classes used for parsing and processing android resources and generating the R.java file.

See: Description

Package com.android.builder.symbols Description

The Symbols package contains classes used for parsing and processing android resources and generating the R.java file.

The Symbol class is used to represent a single android resource by a resource type, a name, a java type and a value. A set of Symbols with unique type/name pairs can be represented by a SymbolTable.

Various parsers in this package were introduced to enable resource parsing without the use of AAPT for libraries. They provide means to scan through the resource directory, parse XML files in search for declared resources and find non-XML files, such as drawables.

The parsers' flow is as follows:

Library resources are passed to a ResourceDirectoryParser. There the parser goes through each of the directories and takes different paths depending on the directories' names and their files' types:

  1. If we are in a values directory (directory name starts with a "values" prefix and is followed by optional qualifiers, like "-v21" or "-w820dp"), all files inside are XML files with declared values inside of them (for example values/strings.xml). Parse each file with a ResourceValuesXmlParser.
  2. If we are in a non-values directory, create a Symbol for each file inside the directory, with the Symbol's name as the filename without the optional extension and the Symbol's type as the directory's name without extra qualifiers. For example for file drawable-v21/a.png we would create a new Symbol with name "a" and type "drawable".
  3. Additionally, if we are in a non-values directory and are parsing a file that is an XML file, we will parse the contents of the file in search of inline declared values. For example, a file layout/activity_main.xml could contain an inline declaration of an id such as {code android:id="@+id/activity_main"}. From such a line a new Symbol should be created with a name "activity_main" and type "id". Such inline declarations are identified by the "@+" prefix and follow a "@+type/name" pattern. This is done by calling the parse method in ResourceExtraXmlParser

The ResourceDirectoryParser collects all Symbols from aforementioned cases and collects them in a SymbolTable which is later used to create the R.txt and R.java files for the library as well as R.java files for all the libraries it depends on.

It is worth mentioning that with this new flow, the new pipeline needs to also create minify rules in the aapt_rules.txt file since we are not calling AAPT anymore. It is done by parsing the library's android manifest, creating keep rules and writing the file in method SymbolUtils.generateMinifyKeepRules(com.android.ide.common.xml.ManifestData, java.io.File).

com.android.builder.symbols.SymbolUtils#generateMainDexKeepRules method is used when AAPT2 is enabled and we need to create the {@code manifest_keep.txt} file with keep rules for Dex. In this case, we keep only nodes with shared processes and filter out remaining ones: if their {@code process} is null, empty or starts with a colon symbol (private process).

Naming conventions:

  1. Resource names declared in XML files inside the {@code values} directories are allowed to contain lower- and upper-case letters, numbers and the underscore character. Dots and colons are allowed to accommodate AAPT's old behaviour, but are deprecated and the support for them might end in the near future.
  2. 2. File names are allowed to contain lower- and upper-case letters, numbers and the underscore character. A dot is only allowed to separate the name from the extension (for example {@code "a.png"}), the usage of two dots is only allowed for 9-patch image extension (for example {@code "a.9.png"}). It is also worth noting that some resources can be declared with a prefix like {@code aapt:} or {@code android:}. Following aapt's original behaviour, we strip the type names from those prefixes. This behaviour is deprecated and might be the support for it might end in the near future.

Example:

Assume in the resources directory we have the following sub-directories and files:

 +---.drawable
 |   +---.a.png
 +---.layout
 |   +---.activity_main.xml
 +---.values
 |   +---.colors.xml
 

Contents of {@code activity_main,xml} include a {@code FloatingActionButton}:

     (...)
     
     (...)
 

And {@code colors.xml} contains:

 
     #3F51B5
     #303F9F
     #FF4081
 
 

Then the parsers would create a following SymbolTable:

Symbol table
Java type Resource type Resource name ID
int drawable a 1
int layout activity_main 2
int id fab 3
int color colorPrimary 4
int color colorPrimaryDark 5
int color colorAccent 6

See {@code ResourceValuesXmlParserTest} and {@code ResourceDirectoryParserTest} for more examples of the parsers' behaviour.

Skip navigation links