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

Commit 275c3ec6 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Option to exclude configs from AAPT2 Link"

parents 439c73b1 3c918b8c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -108,6 +108,7 @@ cc_library_host_static {
        "link/ProductFilter.cpp",
        "link/PrivateAttributeMover.cpp",
        "link/ReferenceLinker.cpp",
        "link/ResourceExcluder.cpp",
        "link/TableMerger.cpp",
        "link/XmlCompatVersioner.cpp",
        "link/XmlNamespaceRemover.cpp",
+24 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@
#include "link/ManifestFixer.h"
#include "link/NoDefaultResourceRemover.h"
#include "link/ReferenceLinker.h"
#include "link/ResourceExcluder.h"
#include "link/TableMerger.h"
#include "link/XmlCompatVersioner.h"
#include "optimize/ResourceDeduper.h"
@@ -1828,6 +1829,29 @@ class Linker {
      }
    }

    if (!options_.exclude_configs_.empty()) {
      std::vector<ConfigDescription> excluded_configs;

      for (auto& config_string : options_.exclude_configs_) {
        ConfigDescription config_description;

        if (!ConfigDescription::Parse(config_string, &config_description)) {
          context_->GetDiagnostics()->Error(DiagMessage()
                                                << "failed to parse --excluded-configs "
                                                << config_string);
          return 1;
        }

        excluded_configs.push_back(config_description);
      }

      ResourceExcluder excluder(excluded_configs);
      if (!excluder.Consume(context_, &final_table_)) {
        context_->GetDiagnostics()->Error(DiagMessage() << "failed excluding configurations");
        return 1;
      }
    }

    if (!options_.no_resource_deduping) {
      ResourceDeduper deduper;
      if (!deduper.Consume(context_, &final_table_)) {
+6 −0
Original line number Diff line number Diff line
@@ -82,6 +82,9 @@ struct LinkOptions {
  std::vector<SplitConstraints> split_constraints;
  std::vector<std::string> split_paths;

  // Configurations to exclude
  std::vector<std::string> exclude_configs_;

  // Stable ID options.
  std::unordered_map<ResourceName, ResourceId> stable_id_map;
  Maybe<std::string> resource_id_map_path;
@@ -255,6 +258,9 @@ class LinkCommand : public Command {
            "Syntax: path/to/output.apk:<config>[,<config>[...]].\n"
            "On Windows, use a semicolon ';' separator instead.",
        &split_args_);
    AddOptionalFlagList("--exclude-configs",
        "Excludes values of resources whose configs contain the specified qualifiers.",
        &options_.exclude_configs_);
    AddOptionalSwitch("--debug-mode",
        "Inserts android:debuggable=\"true\" in to the application node of the\n"
            "manifest, making the application debuggable even on production devices.",
+89 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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/ResourceExcluder.h"

#include <algorithm>

#include "DominatorTree.h"
#include "ResourceTable.h"

using android::ConfigDescription;

namespace aapt {

namespace {

void RemoveIfExcluded(std::set<std::pair<ConfigDescription, int>>& excluded_configs_,
                      IAaptContext* context,
                      ResourceEntry* entry,
                      ResourceConfigValue* value) {
  const ConfigDescription& config = value->config;

  // If this entry is a default, ignore
  if (config == ConfigDescription::DefaultConfig()) {
    return;
  }

  for (auto& excluded_pair : excluded_configs_) {

    const ConfigDescription& excluded_config = excluded_pair.first;
    const int& excluded_diff = excluded_pair.second;

    // Check whether config contains all flags in excluded config
    int node_diff = config.diff(excluded_config);
    int masked_diff = excluded_diff & node_diff;

    if (masked_diff == 0) {
      if (context->IsVerbose()) {
        context->GetDiagnostics()->Note(
            DiagMessage(value->value->GetSource())
                << "excluded resource \""
                << entry->name
                << "\" with config "
                << config.toString());
      }
      value->value = {};
      return;
    }
  }
}

}  // namespace

bool ResourceExcluder::Consume(IAaptContext* context, ResourceTable* table) {
  for (auto& package : table->packages) {
    for (auto& type : package->types) {
      for (auto& entry : type->entries) {
        for (auto& value : entry->values) {
          RemoveIfExcluded(excluded_configs_, context, entry.get(), value.get());
        }

        // Erase the values that were removed.
        entry->values.erase(
            std::remove_if(
                entry->values.begin(), entry->values.end(),
                [](const std::unique_ptr<ResourceConfigValue>& val) -> bool {
                  return val == nullptr || val->value == nullptr;
                }),
            entry->values.end());
      }
    }
  }
  return true;
}

}  // namespace aapt
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 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.
 */

#ifndef AAPT_LINK_RESOURCEEXCLUDER_H
#define AAPT_LINK_RESOURCEEXCLUDER_H

#include "android-base/macros.h"

#include "process/IResourceTableConsumer.h"

using android::ConfigDescription;

namespace aapt {

// Removes excluded configs from resources.
class ResourceExcluder : public IResourceTableConsumer {
 public:
  explicit ResourceExcluder(std::vector<ConfigDescription>& excluded_configs) {
    for (auto& config: excluded_configs) {
      int diff_from_default = config.diff(ConfigDescription::DefaultConfig());
      excluded_configs_.insert(std::pair(config, diff_from_default));
    }
  }

  bool Consume(IAaptContext* context, ResourceTable* table) override;

 private:
  DISALLOW_COPY_AND_ASSIGN(ResourceExcluder);

  std::set<std::pair<ConfigDescription, int>> excluded_configs_;
};

} // namespace aapt

#endif  // AAPT_LINK_RESOURCEEXCLUDER_H
Loading