AAPT2: Implement attribute compat versioning
This change defines some hardcoded rules to degrade
attributes in newer SDKs to specific older attributes.
An attribute with a degrade rule will generate a new XML for the API
in which the attribute resulting from the degradation was introduced.
Since API 22 (Lollipop MR1), attributes are correctly ignored and do
not need to be versioned. In XML files defined for APIs 22+, the
original and degraded attributes coexist in the same XML file.
One such example is paddingHorizontal, introduced in API 26.
paddingHorizontal degrades to paddingLeft and paddingRight, which
were both introduced in API 1.
Bug: 35763493
Test: make aapt2_tests
Change-Id: I4aa8755a9ee2c0cc5afdc55c3d30093fd3a47f3d
diff --git a/tools/aapt2/link/Linkers.h b/tools/aapt2/link/Linkers.h
index d00fa73..5527f90 100644
--- a/tools/aapt2/link/Linkers.h
+++ b/tools/aapt2/link/Linkers.h
@@ -23,6 +23,7 @@
#include "android-base/macros.h"
#include "Resource.h"
+#include "SdkConstants.h"
#include "process/IResourceTableConsumer.h"
#include "xml/XmlDom.h"
@@ -44,9 +45,12 @@
* Determines whether a versioned resource should be created. If a versioned
* resource already exists, it takes precedence.
*/
-bool ShouldGenerateVersionedResource(const ResourceEntry* entry,
- const ConfigDescription& config,
- const int sdk_version_to_generate);
+bool ShouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config,
+ const ApiVersion sdk_version_to_generate);
+
+// Finds the next largest ApiVersion of the config which is identical to the given config except
+// for sdkVersion.
+ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry, const ConfigDescription& config);
class AutoVersioner : public IResourceTableConsumer {
public:
@@ -105,11 +109,10 @@
class ProductFilter : public IResourceTableConsumer {
public:
- using ResourceConfigValueIter =
- std::vector<std::unique_ptr<ResourceConfigValue>>::iterator;
+ using ResourceConfigValueIter = std::vector<std::unique_ptr<ResourceConfigValue>>::iterator;
- explicit ProductFilter(std::unordered_set<std::string> products)
- : products_(products) {}
+ explicit ProductFilter(std::unordered_set<std::string> products) : products_(products) {
+ }
ResourceConfigValueIter SelectProductToKeep(
const ResourceNameRef& name, const ResourceConfigValueIter begin,
@@ -118,19 +121,9 @@
bool Consume(IAaptContext* context, ResourceTable* table) override;
private:
- std::unordered_set<std::string> products_;
-
DISALLOW_COPY_AND_ASSIGN(ProductFilter);
-};
-class XmlAutoVersioner : public IXmlResourceConsumer {
- public:
- XmlAutoVersioner() = default;
-
- bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
-
- private:
- DISALLOW_COPY_AND_ASSIGN(XmlAutoVersioner);
+ std::unordered_set<std::string> products_;
};
/**
@@ -143,8 +136,7 @@
*/
class XmlNamespaceRemover : public IXmlResourceConsumer {
public:
- explicit XmlNamespaceRemover(bool keep_uris = false)
- : keep_uris_(keep_uris){};
+ explicit XmlNamespaceRemover(bool keep_uris = false) : keep_uris_(keep_uris){};
bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
@@ -165,17 +157,8 @@
bool Consume(IAaptContext* context, xml::XmlResource* resource) override;
- /**
- * Once the XmlResource has been consumed, this returns the various SDK levels
- * in which
- * framework attributes used within the XML document were defined.
- */
- inline const std::set<int>& sdk_levels() const { return sdk_levels_found_; }
-
private:
DISALLOW_COPY_AND_ASSIGN(XmlReferenceLinker);
-
- std::set<int> sdk_levels_found_;
};
} // namespace aapt