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

Commit 2738abbd authored by Inseob Kim's avatar Inseob Kim
Browse files

Implement filter-product

filter-product option is added to aapt2 compile, which filters values
with a given product and removes all other values (including default).
The motivation of this change is to generate product-specific RRO
automatically, making a main resource APK (e.g. framework-res.apk)
identical in all targets.

Bug: 294799593
Test: build
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:5fe521e5b3bd5e7ef17dc50425060680cadb4e0e)

Merged-In: I42eb3e134c7aa120f6bbe2d26d311bd46b586595
Change-Id: I42eb3e134c7aa120f6bbe2d26d311bd46b586595
parent f9668c39
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -121,7 +121,6 @@ cc_library_host_static {
        "link/AutoVersioner.cpp",
        "link/ManifestFixer.cpp",
        "link/NoDefaultResourceRemover.cpp",
        "link/ProductFilter.cpp",
        "link/PrivateAttributeMover.cpp",
        "link/ReferenceLinker.cpp",
        "link/ResourceExcluder.cpp",
@@ -134,6 +133,7 @@ cc_library_host_static {
        "optimize/ResourceFilter.cpp",
        "optimize/Obfuscator.cpp",
        "optimize/VersionCollapser.cpp",
        "process/ProductFilter.cpp",
        "process/SymbolTable.cpp",
        "split/TableSplitter.cpp",
        "text/Printer.cpp",
+10 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@
#include "io/StringStream.h"
#include "io/Util.h"
#include "io/ZipArchive.h"
#include "process/ProductFilter.h"
#include "trace/TraceBuffer.h"
#include "util/Files.h"
#include "util/Util.h"
@@ -179,6 +180,15 @@ static bool CompileTable(IAaptContext* context, const CompileOptions& options,
    if (!res_parser.Parse(&xml_parser)) {
      return false;
    }

    if (options.product_.has_value()) {
      if (!ProductFilter({*options.product_}, /* remove_default_config_values = */ true)
               .Consume(context, &table)) {
        context->GetDiagnostics()->Error(android::DiagMessage(path_data.source)
                                         << "failed to filter product");
        return false;
      }
    }
  }

  if (options.pseudolocalize && translatable_file) {
+5 −0
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ struct CompileOptions {
  // See comments on aapt::ResourceParserOptions.
  bool preserve_visibility_of_styleables = false;
  bool verbose = false;
  std::optional<std::string> product_;
};

/** Parses flags and compiles resources to be used in linking.  */
@@ -76,6 +77,10 @@ class CompileCommand : public Command {
    AddOptionalFlag("--source-path",
                      "Sets the compiled resource file source file path to the given string.",
                      &options_.source_path);
    AddOptionalFlag("--filter-product",
                    "Leave only resources specific to the given product. All "
                    "other resources (including defaults) are removed.",
                    &options_.product_);
  }

  int Action(const std::vector<std::string>& args) override;
+2 −1
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@
#include "optimize/ResourceDeduper.h"
#include "optimize/VersionCollapser.h"
#include "process/IResourceTableConsumer.h"
#include "process/ProductFilter.h"
#include "process/SymbolTable.h"
#include "split/TableSplitter.h"
#include "trace/TraceBuffer.h"
@@ -2127,7 +2128,7 @@ class Linker {
                                         << "can't select products when building static library");
      }
    } else {
      ProductFilter product_filter(options_.products);
      ProductFilter product_filter(options_.products, /* remove_default_config_values = */ false);
      if (!product_filter.Consume(context_, &final_table_)) {
        context_->GetDiagnostics()->Error(android::DiagMessage() << "failed stripping products");
        return 1;
+3 −25
Original line number Diff line number Diff line
@@ -20,12 +20,12 @@
#include <set>
#include <unordered_set>

#include "Resource.h"
#include "SdkConstants.h"
#include "android-base/macros.h"
#include "android-base/result.h"
#include "androidfw/ConfigDescription.h"
#include "androidfw/StringPiece.h"

#include "Resource.h"
#include "SdkConstants.h"
#include "process/IResourceTableConsumer.h"
#include "xml/XmlDom.h"

@@ -92,28 +92,6 @@ class PrivateAttributeMover : public IResourceTableConsumer {
  DISALLOW_COPY_AND_ASSIGN(PrivateAttributeMover);
};

class ResourceConfigValue;

class ProductFilter : public IResourceTableConsumer {
 public:
  using ResourceConfigValueIter = std::vector<std::unique_ptr<ResourceConfigValue>>::iterator;

  explicit ProductFilter(std::unordered_set<std::string> products) : products_(products) {
  }

  ResourceConfigValueIter SelectProductToKeep(const ResourceNameRef& name,
                                              const ResourceConfigValueIter begin,
                                              const ResourceConfigValueIter end,
                                              android::IDiagnostics* diag);

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

 private:
  DISALLOW_COPY_AND_ASSIGN(ProductFilter);

  std::unordered_set<std::string> products_;
};

// Removes namespace nodes and URI information from the XmlResource.
//
// Once an XmlResource is processed by this consumer, it is no longer able to have its attributes
Loading