1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "optimize/VersionCollapser.h"
18
19 #include <algorithm>
20 #include <vector>
21
22 #include "ResourceTable.h"
23 #include "trace/TraceBuffer.h"
24
25 using android::ConfigDescription;
26
27 namespace aapt {
28
29 template <typename Iterator, typename Pred>
30 class FilterIterator {
31 public:
FilterIterator(Iterator begin,Iterator end,Pred pred=Pred ())32 FilterIterator(Iterator begin, Iterator end, Pred pred = Pred())
33 : current_(begin), end_(end), pred_(pred) {
34 Advance();
35 }
36
HasNext()37 bool HasNext() { return current_ != end_; }
38
NextIter()39 Iterator NextIter() {
40 Iterator iter = current_;
41 ++current_;
42 Advance();
43 return iter;
44 }
45
Next()46 typename Iterator::reference Next() { return *NextIter(); }
47
48 private:
Advance()49 void Advance() {
50 for (; current_ != end_; ++current_) {
51 if (pred_(*current_)) {
52 return;
53 }
54 }
55 }
56
57 Iterator current_, end_;
58 Pred pred_;
59 };
60
61 template <typename Iterator, typename Pred>
make_filter_iterator(Iterator begin,Iterator end=Iterator (),Pred pred=Pred ())62 FilterIterator<Iterator, Pred> make_filter_iterator(Iterator begin,
63 Iterator end = Iterator(),
64 Pred pred = Pred()) {
65 return FilterIterator<Iterator, Pred>(begin, end, pred);
66 }
67
68 /**
69 * Every Configuration with an SDK version specified that is less than minSdk will be removed. The
70 * exception is when there is no exact matching resource for the minSdk. The next smallest one will
71 * be kept.
72 */
CollapseVersions(IAaptContext * context,int min_sdk,ResourceEntry * entry)73 static void CollapseVersions(IAaptContext* context, int min_sdk, ResourceEntry* entry) {
74 // First look for all sdks less than minSdk.
75 for (auto iter = entry->values.rbegin(); iter != entry->values.rend();
76 ++iter) {
77 // Check if the item was already marked for removal.
78 if (!(*iter)) {
79 continue;
80 }
81
82 const ConfigDescription& config = (*iter)->config;
83 if (config.sdkVersion <= min_sdk) {
84 // This is the first configuration we've found with a smaller or equal SDK level to the
85 // minimum. We MUST keep this one, but remove all others we find, which get overridden by this
86 // one.
87
88 ConfigDescription config_without_sdk = config.CopyWithoutSdkVersion();
89 auto pred = [&](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
90 // Check that the value hasn't already been marked for removal.
91 if (!val) {
92 return false;
93 }
94
95 // Only return Configs that differ in SDK version.
96 config_without_sdk.sdkVersion = val->config.sdkVersion;
97 return config_without_sdk == val->config &&
98 val->config.sdkVersion <= min_sdk;
99 };
100
101 // Remove the rest that match.
102 auto filter_iter =
103 make_filter_iterator(iter + 1, entry->values.rend(), pred);
104 while (filter_iter.HasNext()) {
105 auto& next = filter_iter.Next();
106 if (context->IsVerbose()) {
107 context->GetDiagnostics()->Note(android::DiagMessage()
108 << "removing configuration " << next->config.to_string()
109 << " for entry: " << entry->name
110 << ", because its SDK version is smaller than minSdk");
111 }
112 next = {};
113 }
114 }
115 }
116
117 // Now erase the nullptr values.
118 entry->values.erase(
119 std::remove_if(entry->values.begin(), entry->values.end(),
120 [](const std::unique_ptr<ResourceConfigValue>& val)
121 -> bool { return val == nullptr; }),
122 entry->values.end());
123
124 // Strip the version qualifiers for every resource with version <= minSdk. This will ensure that
125 // the resource entries are all packed together in the same ResTable_type struct and take up less
126 // space in the resources.arsc table.
127 bool modified = false;
128 for (std::unique_ptr<ResourceConfigValue>& config_value : entry->values) {
129 if (config_value->config.sdkVersion != 0 &&
130 config_value->config.sdkVersion <= min_sdk) {
131 // Override the resource with a Configuration without an SDK.
132 std::unique_ptr<ResourceConfigValue> new_value =
133 util::make_unique<ResourceConfigValue>(
134 config_value->config.CopyWithoutSdkVersion(),
135 config_value->product);
136 if (context->IsVerbose()) {
137 context->GetDiagnostics()->Note(android::DiagMessage()
138 << "overriding resource: " << entry->name
139 << ", removing SDK version from configuration "
140 << config_value->config.to_string());
141 }
142 new_value->value = std::move(config_value->value);
143 config_value = std::move(new_value);
144
145 modified = true;
146 }
147 }
148
149 if (modified) {
150 // We've modified the keys (ConfigDescription) by changing the sdkVersion to 0. We MUST re-sort
151 // to ensure ordering guarantees hold.
152 std::sort(entry->values.begin(), entry->values.end(),
153 [](const std::unique_ptr<ResourceConfigValue>& a,
154 const std::unique_ptr<ResourceConfigValue>& b) -> bool {
155 return a->config.compare(b->config) < 0;
156 });
157 }
158 }
159
Consume(IAaptContext * context,ResourceTable * table)160 bool VersionCollapser::Consume(IAaptContext* context, ResourceTable* table) {
161 TRACE_NAME("VersionCollapser::Consume");
162 const int min_sdk = context->GetMinSdkVersion();
163 if (context->IsVerbose()) {
164 context->GetDiagnostics()->Note(android::DiagMessage()
165 << "Running VersionCollapser with minSdk = " << min_sdk);
166 }
167 for (auto& package : table->packages) {
168 for (auto& type : package->types) {
169 for (auto& entry : type->entries) {
170 CollapseVersions(context, min_sdk, entry.get());
171 }
172 }
173 }
174 return true;
175 }
176
177 } // namespace aapt
178