23 Commits

Author SHA1 Message Date
Shane Farmer
11cdc1cf7a AAPT2: Add order attribute to groups
Require explicit ordering of groups in the configuration file to ensure
that the correct version code is set. Ordering based on a single ABI is
straight forward to ensure Play Store delivers the correct APK, but when
an APK needs more than one ABI things get messy quickly. This also goes
for screen density etc. The only thing that is easily sorted without
this attribute is android-sdk since an artifact can only reference a
single SDK.

Test: unit tests
Test: manually split an APK with update config.xml
Change-Id: I37a2b8b8a8409d6d6ff27c7142d4c8c8065a7a51
2018-02-02 12:11:18 -08:00
Shane Farmer
78c43d7bee AAPT2: Sort artifacts based on the Play Store rules.
Sort output artifacts so that the updated versionCode manifest entry
will allow correct handling of updates from Play Store. The most
important dimension is Android SDK version. It is important that a split
based on min SDK version will allow a user to get a new APK if they
upgrade the OS on their device to support a new split.

ABI splits need to also be taken into consideration as it is possible
for a device to run in ARM emulation mode and installing an ARM APK over
a x86 APK could cause performance regressions.

The XML file format was updated to give each of the configuration groups
have their own section of the XML file. This allows the sort order to be
determined by a groups ordering. Artifacts can now be added to the
configuration file in an arbitrary order. Since this will be the common
case for developers, it will help reduce errors from inserting a new
artifact in the wrong spot.

The implementation follows the rules outlined at:
https://developer.android.com/google/play/publishing/multiple-apks.html

Test: Unit tests
Test: Manual process XML configuration

Change-Id: I0face862c6d6b9d3cd2d99088afe5b9491be0120
2017-12-19 13:52:30 -08:00
Shane Farmer
39e474f4b4 AAPT2: Allow empty group definitions
With ABI, screen density, and locale, it is possible to use a shorthand
notation when the group only has a single entry. The shorthand is to
leave the group empty and use a valid configuration for the group name.

Test: manually ran optimize command
Test: unit tests

Change-Id: If2d091e587474847c6c9e9be1a29196b261cc82d
2017-12-19 13:52:30 -08:00
Shane Farmer
cb6c3f9b88 AAPT2: Push more configuration code into the parser
When parsing is complete, we now have a list of output artifacts that
have their referential integrity validated. This means that once the
configuration file is parsed, the only errors that can occur are related
to APK processing, and not the configuration itself.

This reduces the number of errors that could cause a partial output of
APK artifacts. It simplifies the public API and reduces the complexity of
the code to generate multiple APKs.

Test: Ran unit tests
Test: manually ran the optimize command to ensure it still works

Change-Id: I3f2d885b207a84c958f5348a4baa6718598184a4
2017-12-13 12:28:39 -08:00
Shane Farmer
67e8a3074d AAPT2: Add validation for SDK version strings.
Ensure that the configured min max and target SDK versions of the
android-sdk configuration item are correct. This will prevent AAPT2
crashing when it tries to dereference the Android SDK version to update
the manifest.

The test for the latest development SDK version has also been made
future proof by using the SDK constants.

Test: unit tests
Test: manually split an APK
Change-Id: I1ffa90ba2d96cab0cbfa4bd75ef37a50d986852d
2017-12-12 16:25:26 -08:00
Adam Lesinski
178e67835a AAPT2: Update tests to reflect new development API 'P'
Bug: 68346913
Test: make aapt2_tests
Change-Id: Ic1435c529b6c80206d2326ac0da076777dffcc9a
2017-11-02 14:12:38 -07:00
Adam Lesinski
0045116606 AAPT2: Define and Implement AAPT Container Format
AAPT Container Format (.apc) is a simple container that
enumerates the various intermediate files that AAPT2 generates
during the compile phase.

The format is defined in formats.md.

For now, continue using the .flat extension for the container file,
and keep making use of the .flata zip for storing multiple files.
This will allow easier integration with existing build systems and allow
the evolution of the APC format to better handle arbitrarily large
files.

Test: make aapt2_tests
Change-Id: Id7216e5b76316bdd683f0fa4eaf2d2da273ba815
2017-10-19 12:50:53 -07:00
Shane Farmer
810fd18435 AAPT2: Update versionCode in multi-APKs.
Set versionCode in the AndroidManifest.xml so each artifact has a unique
version code. This will allow the play store to serve up the correct
APKs.

The versionCode is determined by taking the versionCode from the input
APK and adding the configured version to that. If an artifact does not
have a version, the next version is used based on the order in the
configuration file. If the firs artifact does not have a version, it
starts at 1, so that all artifacts have a different version code to the
base APK.

Bug: 64972562

Test: unit tests
Test: manually optimized an APK into multiple APKs and confirmed versions:
  aapt dump badging ${apk} | grep versionCode

Change-Id: Ie12b3e42bbd8ce9a2819712c2c56b24c0a901e6b
2017-10-03 16:13:04 +00:00
Shane Farmer
3edd472643 AAPT2: Set the minSdkVersion when generating multiple APKs.
When generating multiple APKs from a configuration file, check to see if
we have filtered resource by minSdkVersion and update the manifest to
reflect this. We only want to inflate and modify the manifest file if
there is an update to be applied.

Bug: 37944703
Bug: 67005138

Test: Ran unit tests
Test: Manually split an APK and verified the manifest by dumping with
      AAPT (both xmltree and badging).

Change-Id: I64a0e4889d7d9e57373369b044a091287b06cc35
2017-10-03 16:02:38 +00:00
Shane Farmer
efe45392c3 AAPT2: Multi APK generator by version
- Added an additional axis for generating a multi-apk split by minimum
Android SDK version. This removes any resources that will not be used
for the desired minimum SDK version. If there are multiple resources
that would be valid for any version newer than the requested minimum,
then all would be kept so that the best match can be found.

- Added a context wrapper to set the appropriate Android SDK version for
each generated artifact.

- Split out the FilterTable method to allow it to be directly tested
without the need to mock the APK writing steps.

Test: Unit tests
Test: manually run optimize command

Change-Id: I7e6018df081af9ed5d9e8aaf40ed216c1275f138
2017-08-31 16:30:38 -07:00
Shane Farmer
9ecc0751e3 AAPT2: Add flag to print multi APK artifact names.
- Added new flag that exits after printing the list of artifact names
  that would be generated from the combination of the configuration file
  and input APK.

- Cleaned up the code to generate the artifact names which also involved
  adding some more test cases for corner cases.

Test: Unit tests
Test: Manually ran new command
Test: Manually ran old command
Change-Id: I8d30e7a4a070af26945b8f544a13f23bdf1ba169
2017-08-31 10:17:39 -07:00
Adam Lesinski
2def7dcf22 Merge "AAPT2: Change XmlDom to exclude Namespace as a node" 2017-08-17 17:43:53 +00:00
Adam Lesinski
6b37299129 AAPT2: Change XmlDom to exclude Namespace as a node
In preparation for exporting an XML proto format for UAM to consume,
this change brings the XML DOM API more in line with other APIs that
do not make the Namespace a separate node.

Treating Namespace declarations as just properties of an Element
node makes the implementation of algorithms much simpler, as
the constraints that Namespace nodes have only one child
are now built in and traversing to find Element nodes
is much simpler.

Also made a bunch of quality of life improvements, like formatting and
comment style.

Test: make aapt2_tests
Change-Id: Ib97ff1c4252b7907e2cc1f13a448dc4ca3b809a4
2017-08-17 17:43:08 +00:00
Shane Farmer
0a5b201156 AAPT2: Add a APK filtering.
Allow resource files to be removed from the final artifact based on the
density and locale configuration in the config file. The APK is split
along the density, locale and ABI axis. Each split is generated from the
original APK without modifying the original. The new resource table is
written back to the file system with unneeded assets etc removed.

Test: Unit tests
Test: Manually run optimize command against an APK and inspect results
Test: Installed split searchlite APK (after resigning) and ran on N6

Change-Id: If73597dcfd88c02d2616518585d0e25a5c6a84d1
2017-08-16 19:19:54 +00:00
Adam Lesinski
efeb7af13b AAPT2: Fix windows unicode path issues
Mingw64 was being difficult, so instead of defining a wmain entrypoint,
the command line parameters are parsed manually using built-in Windows
methods that support Unicode. The results are converted to UTF8 and
handled just like the rest of the linux/mac version of the code.

This also removes dependencies on std::istream in favour of a
FileInputStream which calls the appropriate unicode version of
open to read a file.

No speed regressions found on Linux or MacOS.

Bug: 62336414
Bug: 63830502
Test: manual
Change-Id: I597da51e33729ed1b98bf246e7e773337fd3fee8
2017-08-03 17:33:52 -07:00
Shane Farmer
1a21b8c041 AAPT2: Update ReplacePlaceholder for artifact name parser
Update the artifact name parser to ensure that duplicate placeholders
are treated as an error.

Also applied suggested changes from ag/2447777.

Test: ran unit tests
Change-Id: Iab8fd9d9b81aa3008177141256ecd16ef04b0c34
2017-07-25 00:01:18 +00:00
Shane Farmer
9f0e7f1dba AAPT2: Parse artifact names from template.
Add a helper method to convert a templated artifact name to file name
based on the values present in an Artifact struct. The Artifact
validates that all required template paramters are present.

Test: Unit tests
Change-Id: Id97ff606bb41c72a31c2d769104966be9cbca1a0
2017-07-18 20:32:54 +00:00
Shane Farmer
280be34fa6 AAPT2: Update the name of the configuration struct.
Make the Configuration struct name less ambiguous. Also ran clang-format
on the change since some lines were too long and this resulted in a
bigger diff than expected.

Test: built manually
Test: ran existing unit tests
Change-Id: I85e78041fa78d913d47a26c85ad48b05bf935a6a
2017-06-21 16:13:02 -07:00
Shane Farmer
5766943f55 AAPT2: Split APK by ABI.
Added a FilterChain that can apply multiple filter steps to an APK file
as it is being written to disk. The first filter applied is by ABI. If
a library in the APK does not match the filter it is skipped.

Added an AbiFilter that keeps files that are either not native libs or
are for the set of wanted ABIs

Test: ran unit tests locally
Test: ran against an APK with ARM and x68 libs and diffed the results

Change-Id: I3fb901d3de3513e85f2a2763a8e4487a28ed4881
2017-06-21 14:10:23 -07:00
Shane Farmer
b102727771 AAPT2: Read config from disk
Implement the todo left from last change to read the contents of the
configuration file from disk. Since this is an operation that may fail
the API was changed to take return a Maybe to indicate errors reading
the file.

Test: unit test for error condition
Test: ran aapt2 optimize with the new code path wired in

Change-Id: I93d532b4a57af9520231225eee4fc5f2b1a046b9
2017-06-19 12:49:48 -07:00
TreeHugger Robot
95cad98ad5 Merge "AAPT2: XML configuration file parser." 2017-06-01 07:20:59 +00:00
Shane Farmer
74cdea34cd AAPT2: XML configuration file parser.
The XML file is parsed with the aapt::xml::XmlDom parser and actions are
used for the elements we are interested in. This means that there are
cases where a user could add aditional tags/elements to the document
that are technically invalid (acording to the schema) but are ignored by
the parser. This allows us to be more lenient which probably isn't a bad
thing.

Documents have the namespace stripped before processing as the
XmlActionExecutor ignores any elements with a namespace. The namespace
is validated before being removed.

The test cases are all based off the previous example XML file.

Test: Unit tests

Change-Id: I86d7e0dc6347ace3eaa60c1842d59f3cd0d4f749
2017-05-31 16:18:31 -07:00
Shane Farmer
7eded65454 AAPT2: XML schema and an example configuraiton
The current iteration of the schema allows for both Split and Optimize
commands running off the same file. A split command is multiple optimize
command, and an optimize is a split with only a single artifact. To
combine the two, a single file with one or more artifacts can be passed
to either command. If multiple artifiacts are used for an optimize
command, the required artifact can be specified on the command line. A
configuration with only a single artifact can be used with either
command to get the same result.

Change-Id: Ie48eda6e2f9f8bf25d75ef7bcc3b3401a2d3e8eb
Bugs: 37847908
Test: xmllint --noout --schema aapt2.xsd example/config.xml
2017-05-31 16:05:07 -07:00