blob: 2a5163f22e17ed04a0bb3f72f58b83163060f764 [file] [log] [blame]
Adam Lesinski34a16872018-02-23 16:18:10 -08001/*
2 * Copyright (C) 2018 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
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020017#include "androidfw/Locale.h"
18
Adam Lesinski34a16872018-02-23 16:18:10 -080019#include "link/NoDefaultResourceRemover.h"
20
21#include <algorithm>
22
23#include "ResourceTable.h"
24
Mårten Kongstad24c9aa62018-06-20 08:46:41 +020025using android::ConfigDescription;
26
Adam Lesinski34a16872018-02-23 16:18:10 -080027namespace aapt {
28
Ryan Mitchella5936052018-05-23 13:31:28 -070029static bool KeepResource(const std::unique_ptr<ResourceEntry>& entry, int minSdk) {
Adam Lesinski34a16872018-02-23 16:18:10 -080030 if (entry->visibility.level == Visibility::Level::kPublic) {
31 // Removing a public API without the developer knowing is bad, so just leave this here for now.
32 return true;
33 }
34
35 if (entry->HasDefaultValue()) {
36 // There is a default value, no removal needed.
37 return true;
38 }
39
40 // There is no default value defined, check if removal is required.
Ryan Mitchella5936052018-05-23 13:31:28 -070041 bool defaultRequired = false;
Adam Lesinski34a16872018-02-23 16:18:10 -080042 for (const auto& config_value : entry->values) {
Ryan Mitchella5936052018-05-23 13:31:28 -070043 const int config = ConfigDescription::DefaultConfig().diff(config_value->config);
44 // If a resource defines a value for a locale-only configuration, the default configuration is
45 // required.
46 if (config == ConfigDescription::CONFIG_LOCALE) {
47 defaultRequired = true;
48 }
49 // If a resource defines a version-only config, the config value can be used as a default if
50 // the version is at most the minimum sdk version
51 else if (config == ConfigDescription::CONFIG_VERSION
52 && config_value->config.sdkVersion <= minSdk) {
53 return true;
54 }
55 // If a resource defines a value for a density only configuration, then that value could be used
56 // as a default and the entry should not be removed
57 else if (config == ConfigDescription::CONFIG_DENSITY
58 || (config == (ConfigDescription::CONFIG_DENSITY | ConfigDescription::CONFIG_VERSION)
59 && config_value->config.sdkVersion <= minSdk)) {
60 return true;
Adam Lesinski34a16872018-02-23 16:18:10 -080061 }
62 }
Ryan Mitchella5936052018-05-23 13:31:28 -070063
64 return !defaultRequired;
Adam Lesinski34a16872018-02-23 16:18:10 -080065}
66
67bool NoDefaultResourceRemover::Consume(IAaptContext* context, ResourceTable* table) {
Adam Lesinski34a16872018-02-23 16:18:10 -080068 for (auto& pkg : table->packages) {
69 for (auto& type : pkg->types) {
Ryan Mitchella5936052018-05-23 13:31:28 -070070 // Gather the entries without defaults that must be removed
71 const int minSdk = context->GetMinSdkVersion();
Adam Lesinski34a16872018-02-23 16:18:10 -080072 const auto end_iter = type->entries.end();
Ryan Mitchella5936052018-05-23 13:31:28 -070073 const auto remove_iter = std::stable_partition(type->entries.begin(), end_iter,
74 [&minSdk](const std::unique_ptr<ResourceEntry>& entry) -> bool {
75 return KeepResource(entry, minSdk);
76 });
77
78 for (auto iter = remove_iter; iter != end_iter; ++iter) {
Iurii Makhnof0c5ff42022-02-22 13:31:02 +000079 const ResourceName name(pkg->name, type->named_type, (*iter)->name);
Jeremy Meyer56f36e82022-05-20 20:35:42 +000080 android::IDiagnostics* diag = context->GetDiagnostics();
81 diag->Warn(android::DiagMessage()
82 << "removing resource " << name << " without required default value");
Adam Lesinski34a16872018-02-23 16:18:10 -080083 if (context->IsVerbose()) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000084 diag->Note(android::DiagMessage() << " did you forget to remove all definitions?");
Adam Lesinski34a16872018-02-23 16:18:10 -080085 for (const auto& config_value : (*iter)->values) {
86 if (config_value->value != nullptr) {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000087 diag->Note(android::DiagMessage(config_value->value->GetSource()) << "defined here");
Adam Lesinski34a16872018-02-23 16:18:10 -080088 }
89 }
90 }
91 }
92
Ryan Mitchella5936052018-05-23 13:31:28 -070093 type->entries.erase(remove_iter, end_iter);
Adam Lesinski34a16872018-02-23 16:18:10 -080094 }
95 }
96 return true;
97}
98
99} // namespace aapt