/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "link/Linkers.h" #include #include "android-base/logging.h" #include "ResourceTable.h" #include "SdkConstants.h" #include "ValueVisitor.h" #include "trace/TraceBuffer.h" using android::ConfigDescription; namespace aapt { bool ShouldGenerateVersionedResource(const ResourceEntry* entry, const ConfigDescription& config, const ApiVersion sdk_version_to_generate) { // We assume the caller is trying to generate a version greater than the current configuration. CHECK(sdk_version_to_generate > config.sdkVersion); return sdk_version_to_generate < FindNextApiVersionForConfig(entry, config); } ApiVersion FindNextApiVersionForConfig(const ResourceEntry* entry, const ConfigDescription& config) { const auto end_iter = entry->values.end(); auto iter = entry->values.begin(); for (; iter != end_iter; ++iter) { if ((*iter)->config == config) { break; } } // The source config came from this list, so it should be here. CHECK(iter != entry->values.end()); ++iter; // The next configuration either only varies in sdkVersion, or it is completely different // and therefore incompatible. If it is incompatible, we must generate the versioned resource. // NOTE: The ordering of configurations takes sdkVersion as higher precedence than other // qualifiers, so we need to iterate through the entire list to be sure there // are no higher sdk level versions of this resource. ConfigDescription temp_config(config); for (; iter != end_iter; ++iter) { temp_config.sdkVersion = (*iter)->config.sdkVersion; if (temp_config == (*iter)->config) { // The two configs are the same, return the sdkVersion. return (*iter)->config.sdkVersion; } } // Didn't find another config with a different sdk version, so return the highest possible value. return std::numeric_limits::max(); } bool AutoVersioner::Consume(IAaptContext* context, ResourceTable* table) { TRACE_NAME("AutoVersioner::Consume"); CloningValueTransformer cloner(&table->string_pool); for (auto& package : table->packages) { for (auto& type : package->types) { if (type->type != ResourceType::kStyle) { continue; } for (auto& entry : type->entries) { for (size_t i = 0; i < entry->values.size(); i++) { ResourceConfigValue* config_value = entry->values[i].get(); if (config_value->config.sdkVersion >= SDK_LOLLIPOP_MR1) { // If this configuration is only used on L-MR1 then we don't need // to do anything since we use private attributes since that // version. continue; } if (Style* style = ValueCast