Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fb6312fe authored by Adam Lesinski's avatar Adam Lesinski
Browse files

AAPT2: Add version collapsing

When an app specifies (or imports) resources with various
configurations for different SDK versions, specifying
a minSdk will make many of those resources unreachable.

Version collapsing will prune out the resources specified
for SDK versions less than the minSdk.

If, however, there is no exact matching resource for the
minSdk version, the next smallest SDK version is kept.

Change-Id: Ic7bcab6c59d65c97c67c8767358abb57cdec60a4
parent 4c62cbda
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -39,6 +39,7 @@ sources := \
	link/PrivateAttributeMover.cpp \
	link/ReferenceLinker.cpp \
	link/TableMerger.cpp \
	link/VersionCollapser.cpp \
	link/XmlReferenceLinker.cpp \
	process/SymbolTable.cpp \
	proto/ProtoHelpers.cpp \
@@ -87,6 +88,7 @@ testSources := \
	link/ProductFilter_test.cpp \
	link/ReferenceLinker_test.cpp \
	link/TableMerger_test.cpp \
	link/VersionCollapser_test.cpp \
	link/XmlReferenceLinker_test.cpp \
	process/SymbolTable_test.cpp \
	proto/TableProtoSerializer_test.cpp \
+7 −0
Original line number Diff line number Diff line
@@ -17,6 +17,8 @@
#ifndef AAPT_APP_INFO_H
#define AAPT_APP_INFO_H

#include "util/Maybe.h"

#include <string>

namespace aapt {
@@ -30,6 +32,11 @@ struct AppInfo {
     * App's package name.
     */
    std::u16string package;

    /**
     * The App's minimum SDK version.
     */
    Maybe<std::u16string> minSdkVersion;
};

} // namespace aapt
+16 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

#include "NameMangler.h"
#include "ResourceUtils.h"
#include "SdkConstants.h"
#include "flatten/ResourceTypeExtensions.h"
#include "util/Files.h"
#include "util/Util.h"
@@ -402,6 +403,21 @@ bool tryParseBool(const StringPiece16& str, bool* outValue) {
    return false;
}

Maybe<int> tryParseSdkVersion(const StringPiece16& str) {
    StringPiece16 trimmedStr(util::trimWhitespace(str));
    android::Res_value value;
    if (android::ResTable::stringToInt(trimmedStr.data(), trimmedStr.size(), &value)) {
        return static_cast<int>(value.data);
    }

    // Try parsing the code name.
    std::pair<StringPiece16, int> entry = getDevelopmentSdkCodeNameAndVersion();
    if (entry.first == trimmedStr) {
        return entry.second;
    }
    return {};
}

std::unique_ptr<BinaryPrimitive> tryParseBool(const StringPiece16& str) {
    bool result = false;
    if (tryParseBool(str, &result)) {
+5 −0
Original line number Diff line number Diff line
@@ -79,6 +79,11 @@ bool isAttributeReference(const StringPiece16& str);
 */
bool tryParseBool(const StringPiece16& str, bool* outValue);

/**
 * Parses an SDK version, which can be an integer, or a letter from A-Z.
 */
Maybe<int> tryParseSdkVersion(const StringPiece16& str);

/*
 * Returns a Reference, or None Maybe instance if the string `str` was parsed as a
 * valid reference to a style.
+7 −0
Original line number Diff line number Diff line
@@ -23,6 +23,9 @@

namespace aapt {

static const char16_t* sDevelopmentSdkCodeName = u"O";
static int sDevelopmentSdkLevel = 26;

static const std::vector<std::pair<uint16_t, size_t>> sAttrIdMap = {
    { 0x021c, 1 },
    { 0x021d, 2 },
@@ -735,4 +738,8 @@ size_t findAttributeSdkLevel(const ResourceName& name) {
    return SDK_LOLLIPOP_MR1;
}

std::pair<StringPiece16, int> getDevelopmentSdkCodeNameAndVersion() {
    return std::make_pair(StringPiece16(sDevelopmentSdkCodeName), sDevelopmentSdkLevel);
}

} // namespace aapt
Loading