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

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

Merge changes I2f594c22,I37a2b8b8

* changes:
  AAPT2: Fix compatible-screens element in AndroidManifest.xml
  AAPT2: Add order attribute to groups
parents 6467e982 d05b9130
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -222,7 +222,9 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table

    } else if (manifest != nullptr && path == "AndroidManifest.xml") {
      BigBuffer buffer(8192);
      XmlFlattener xml_flattener(&buffer, {});
      XmlFlattenerOptions xml_flattener_options;
      xml_flattener_options.use_utf16 = true;
      XmlFlattener xml_flattener(&buffer, xml_flattener_options);
      if (!xml_flattener.Consume(context, manifest)) {
        context->GetDiagnostics()->Error(DiagMessage(path) << "flattening failed");
        return false;
+4 −0
Original line number Diff line number Diff line
@@ -520,6 +520,10 @@ std::unique_ptr<BinaryPrimitive> TryParseInt(const StringPiece& str) {
  return util::make_unique<BinaryPrimitive>(value);
}

std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t val) {
  return util::make_unique<BinaryPrimitive>(android::Res_value::TYPE_INT_DEC, val);
}

std::unique_ptr<BinaryPrimitive> TryParseFloat(const StringPiece& str) {
  std::u16string str16 = util::Utf8ToUtf16(util::TrimWhitespace(str));
  android::Res_value value;
+3 −0
Original line number Diff line number Diff line
@@ -165,6 +165,9 @@ std::unique_ptr<BinaryPrimitive> MakeBool(bool val);
 */
std::unique_ptr<BinaryPrimitive> TryParseInt(const android::StringPiece& str);

// Returns an integer BinaryPrimitive.
std::unique_ptr<BinaryPrimitive> MakeInt(uint32_t value);

/*
 * Returns a BinaryPrimitve object representing a floating point number
 * (float, dimension, etc) if the string was parsed as one.
+70 −10
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <utility>

#include "android-base/file.h"
@@ -93,6 +94,7 @@ class NoopDiagnostics : public IDiagnostics {
};
NoopDiagnostics noop_;

/** Returns the value of the label attribute for a given element. */
std::string GetLabel(const Element* element, IDiagnostics* diag) {
  std::string label;
  for (const auto& attr : element->attributes) {
@@ -108,6 +110,18 @@ std::string GetLabel(const Element* element, IDiagnostics* diag) {
  return label;
}

/** Returns the value of the version-code-order attribute for a given element. */
Maybe<int32_t> GetVersionCodeOrder(const Element* element, IDiagnostics* diag) {
  const xml::Attribute* version = element->FindAttribute("", "version-code-order");
  if (version == nullptr) {
    std::string label = GetLabel(element, diag);
    diag->Error(DiagMessage() << "No version-code-order found for element '" << element->name
                              << "' with label '" << label << "'");
    return {};
  }
  return std::stoi(version->value);
}

/** XML node visitor that removes all of the namespace URIs from the node and all children. */
class NamespaceVisitor : public xml::Visitor {
 public:
@@ -437,26 +451,37 @@ Maybe<std::vector<OutputArtifact>> ConfigurationParser::Parse(
  // Convert from a parsed configuration to a list of artifacts for processing.
  const std::string& apk_name = file::GetFilename(apk_path).to_string();
  std::vector<OutputArtifact> output_artifacts;
  bool has_errors = false;

  PostProcessingConfiguration& config = maybe_config.value();
  config.SortArtifacts();

  bool valid = true;
  int version = 1;

  for (const ConfiguredArtifact& artifact : config.artifacts) {
    Maybe<OutputArtifact> output_artifact = ToOutputArtifact(artifact, apk_name, config, diag_);
    if (!output_artifact) {
      // Defer return an error condition so that all errors are reported.
      has_errors = true;
      valid = false;
    } else {
      output_artifact.value().version = version++;
      output_artifacts.push_back(std::move(output_artifact.value()));
    }
  }

  if (has_errors) {
  if (!config.ValidateVersionCodeOrdering(diag_)) {
    diag_->Error(DiagMessage() << "could not validate post processing configuration");
    valid = false;
  }

  if (valid) {
    // Sorting artifacts requires that all references are valid as it uses them to determine order.
    config.SortArtifacts();
  }

  if (!valid) {
    return {};
  }

  return {output_artifacts};
}

@@ -509,8 +534,15 @@ bool AbiGroupTagHandler(PostProcessingConfiguration* config, Element* root_eleme
    return false;
  }

  auto& group = GetOrCreateGroup(label, &config->abi_groups);
  bool valid = true;
  OrderedEntry<Abi>& entry = config->abi_groups[label];
  Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
  if (!order) {
    valid = false;
  } else {
    entry.order = order.value();
  }
  auto& group = entry.entry;

  // Special case for empty abi-group tag. Label will be used as the ABI.
  if (root_element->GetChildElements().empty()) {
@@ -519,7 +551,7 @@ bool AbiGroupTagHandler(PostProcessingConfiguration* config, Element* root_eleme
      return false;
    }
    group.push_back(abi->second);
    return true;
    return valid;
  }

  for (auto* child : root_element->GetChildElements()) {
@@ -553,8 +585,15 @@ bool ScreenDensityGroupTagHandler(PostProcessingConfiguration* config, Element*
    return false;
  }

  auto& group = GetOrCreateGroup(label, &config->screen_density_groups);
  bool valid = true;
  OrderedEntry<ConfigDescription>& entry = config->screen_density_groups[label];
  Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
  if (!order) {
    valid = false;
  } else {
    entry.order = order.value();
  }
  auto& group = entry.entry;

  // Special case for empty screen-density-group tag. Label will be used as the screen density.
  if (root_element->GetChildElements().empty()) {
@@ -613,8 +652,15 @@ bool LocaleGroupTagHandler(PostProcessingConfiguration* config, Element* root_el
    return false;
  }

  auto& group = GetOrCreateGroup(label, &config->locale_groups);
  bool valid = true;
  OrderedEntry<ConfigDescription>& entry = config->locale_groups[label];
  Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
  if (!order) {
    valid = false;
  } else {
    entry.order = order.value();
  }
  auto& group = entry.entry;

  // Special case to auto insert a locale for an empty group. Label will be used for locale.
  if (root_element->GetChildElements().empty()) {
@@ -728,8 +774,15 @@ bool GlTextureGroupTagHandler(PostProcessingConfiguration* config, Element* root
    return false;
  }

  auto& group = GetOrCreateGroup(label, &config->gl_texture_groups);
  bool valid = true;
  OrderedEntry<GlTexture>& entry = config->gl_texture_groups[label];
  Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
  if (!order) {
    valid = false;
  } else {
    entry.order = order.value();
  }
  auto& group = entry.entry;

  GlTexture result;
  for (auto* child : root_element->GetChildElements()) {
@@ -771,8 +824,15 @@ bool DeviceFeatureGroupTagHandler(PostProcessingConfiguration* config, Element*
    return false;
  }

  auto& group = GetOrCreateGroup(label, &config->device_feature_groups);
  bool valid = true;
  OrderedEntry<DeviceFeature>& entry = config->device_feature_groups[label];
  Maybe<int32_t> order = GetVersionCodeOrder(root_element, diag);
  if (!order) {
    valid = false;
  } else {
    entry.order = order.value();
  }
  auto& group = entry.entry;

  for (auto* child : root_element->GetChildElements()) {
    if (child->name != "supports-feature") {
+28 −6
Original line number Diff line number Diff line
@@ -33,18 +33,31 @@ namespace configuration {

template <typename T>
struct OrderedEntry {
  size_t order;
  int32_t order;
  std::vector<T> entry;
};

/** A mapping of group labels to group of configuration items. */
template <class T>
using Group = std::unordered_map<std::string, OrderedEntry<T>>;

/** A mapping of group label to a single configuration item. */
template <class T>
using Entry = std::unordered_map<std::string, T>;

/** A mapping of group labels to group of configuration items. */
template <class T>
using Group = Entry<OrderedEntry<T>>;

template<typename T>
bool IsGroupValid(const Group<T>& group, const std::string& name, IDiagnostics* diag) {
  std::set<int32_t> orders;
  for (const auto& p : group) {
    orders.insert(p.second.order);
  }
  bool valid = orders.size() == group.size();
  if (!valid) {
    diag->Error(DiagMessage() << name << " have overlapping version-code-order attributes");
  }
  return valid;
}

/** Retrieves an entry from the provided Group, creating a new instance if one does not exist. */
template <typename T>
std::vector<T>& GetOrCreateGroup(std::string label, Group<T>* group) {
@@ -93,7 +106,7 @@ class ComparisonChain {

 private:
  template <typename T>
  inline size_t GetGroupOrder(const Group<T>& groups, const Maybe<std::string>& label) {
  inline size_t GetGroupOrder(const Entry<T>& groups, const Maybe<std::string>& label) {
    if (!label) {
      return std::numeric_limits<size_t>::max();
    }
@@ -141,6 +154,15 @@ struct PostProcessingConfiguration {
  Group<GlTexture> gl_texture_groups;
  Entry<AndroidSdk> android_sdks;

  bool ValidateVersionCodeOrdering(IDiagnostics* diag) {
    bool valid = IsGroupValid(abi_groups, "abi-groups", diag);
    valid &= IsGroupValid(screen_density_groups, "screen-density-groups", diag);
    valid &= IsGroupValid(locale_groups, "locale-groups", diag);
    valid &= IsGroupValid(device_feature_groups, "device-feature-groups", diag);
    valid &= IsGroupValid(gl_texture_groups, "gl-texture-groups", diag);
    return valid;
  }

  /**
   * Sorts the configured artifacts based on the ordering of the groups in the configuration file.
   * The only exception to this rule is Android SDK versions. Larger SDK versions will have a larger
Loading