Symbols
package contains classes used for parsing and processing android resources
and generating the R.java file.See: Description
Interface | Description |
---|---|
IdProvider |
Provides IDs for resource assignment.
|
Class | Description |
---|---|
ResourceDirectoryParser |
Parser that scans a resource directory for all resources and builds a
SymbolTable . |
ResourceExtraXmlParser |
A parser used for finding all inline declaration of android resources in XML files.
|
ResourceValuesXmlParser |
Parser that can load a
SymbolTable from a resource XML file. |
RGeneration |
Utility class to generate
R.java files. |
SymbolIo |
Reads and writes symbol tables to files.
|
SymbolUtils |
Helper methods related to Symbols and resource processing.
|
Enum | Description |
---|---|
SymbolJavaType | |
SymbolUtils.SymbolTableGenerationMode |
Exception | Description |
---|---|
ResourceDirectoryParseException |
Exception thrown when failed to parse a resource directory.
|
ResourceValuesXmlParseException |
Exception thrown when failed to parse the resource values XML.
|
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:
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
.
drawable-v21/a.png
we would create a new Symbol with name "a"
and type "drawable"
.
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:
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:
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.