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

Commit d3c5fb64 authored by Mohamed Heikal's avatar Mohamed Heikal
Browse files

AAPT2: Adds config support for manipulating resources

aapt2 optimise command can now take a resources config file as an
argument. The config has the name of each resource and a list of
directives. Currently implemented is the "remove" directive which marks
the resource for deletion.

The obfuscation whitelist code and argument name was changed to prevent
confusion.

Test: make aapt2_tests
Bug: b/27523794

Change-Id: I2d8e1985e5ea2286131c25231e2c411f3d9610ce
parent f42a1080
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -110,6 +110,7 @@ cc_library_host_static {
        "link/XmlReferenceLinker.cpp",
        "optimize/MultiApkGenerator.cpp",
        "optimize/ResourceDeduper.cpp",
        "optimize/ResourceFilter.cpp",
        "optimize/VersionCollapser.cpp",
        "process/SymbolTable.cpp",
        "split/TableSplitter.cpp",
+1 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
#include <sys/stat.h>
#include <cinttypes>

#include <algorithm>
#include <queue>
#include <unordered_map>
#include <vector>
+76 −6
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@
#include "io/Util.h"
#include "optimize/MultiApkGenerator.h"
#include "optimize/ResourceDeduper.h"
#include "optimize/ResourceFilter.h"
#include "optimize/VersionCollapser.h"
#include "split/TableSplitter.h"
#include "util/Files.h"
@@ -62,6 +63,9 @@ struct OptimizeOptions {
  // Details of the app extracted from the AndroidManifest.xml
  AppInfo app_info;

  // Blacklist of unused resources that should be removed from the apk.
  std::unordered_set<ResourceName> resources_blacklist;

  // Split APK options.
  TableSplitterOptions table_splitter_options;

@@ -147,6 +151,13 @@ class OptimizeCommand {
    if (context_->IsVerbose()) {
      context_->GetDiagnostics()->Note(DiagMessage() << "Optimizing APK...");
    }
    if (!options_.resources_blacklist.empty()) {
      ResourceFilter filter(options_.resources_blacklist);
      if (!filter.Consume(context_, apk->GetResourceTable())) {
        context_->GetDiagnostics()->Error(DiagMessage() << "failed filtering resources");
        return 1;
      }
    }

    VersionCollapser collapser;
    if (!collapser.Consume(context_, apk->GetResourceTable())) {
@@ -284,7 +295,7 @@ class OptimizeCommand {
  OptimizeContext* context_;
};

bool ExtractWhitelistFromConfig(const std::string& path, OptimizeContext* context,
bool ExtractObfuscationWhitelistFromConfig(const std::string& path, OptimizeContext* context,
                                           OptimizeOptions* options) {
  std::string contents;
  if (!ReadFileToString(path, &contents, true)) {
@@ -292,8 +303,54 @@ bool ExtractWhitelistFromConfig(const std::string& path, OptimizeContext* contex
                                     << "failed to parse whitelist from config file: " << path);
    return false;
  }
  for (const StringPiece& resource_name : util::Tokenize(contents, ',')) {
    options->table_flattener_options.whitelisted_resources.insert(resource_name.to_string());
  for (StringPiece resource_name : util::Tokenize(contents, ',')) {
    options->table_flattener_options.whitelisted_resources.insert(
        resource_name.to_string());
  }
  return true;
}

bool ExtractConfig(const std::string& path, OptimizeContext* context,
                                    OptimizeOptions* options) {
  std::string content;
  if (!android::base::ReadFileToString(path, &content, true /*follow_symlinks*/)) {
    context->GetDiagnostics()->Error(DiagMessage(path) << "failed reading whitelist");
    return false;
  }

  size_t line_no = 0;
  for (StringPiece line : util::Tokenize(content, '\n')) {
    line_no++;
    line = util::TrimWhitespace(line);
    if (line.empty()) {
      continue;
    }

    auto split_line = util::Split(line, '#');
    if (split_line.size() < 2) {
      context->GetDiagnostics()->Error(DiagMessage(line) << "No # found in line");
      return false;
    }
    StringPiece resource_string = split_line[0];
    StringPiece directives = split_line[1];
    ResourceNameRef resource_name;
    if (!ResourceUtils::ParseResourceName(resource_string, &resource_name)) {
      context->GetDiagnostics()->Error(DiagMessage(line) << "Malformed resource name");
      return false;
    }
    if (!resource_name.package.empty()) {
      context->GetDiagnostics()->Error(DiagMessage(line)
                                       << "Package set for resource. Only use type/name");
      return false;
    }
    for (StringPiece directive : util::Tokenize(directives, ',')) {
      if (directive == "remove") {
        options->resources_blacklist.insert(resource_name.ToResourceName());
      } else if (directive == "no_obfuscate") {
        options->table_flattener_options.whitelisted_resources.insert(
            resource_name.entry.to_string());
      }
    }
  }
  return true;
}
@@ -322,6 +379,7 @@ int Optimize(const std::vector<StringPiece>& args) {
  OptimizeOptions options;
  Maybe<std::string> config_path;
  Maybe<std::string> whitelist_path;
  Maybe<std::string> resources_config_path;
  Maybe<std::string> target_densities;
  std::vector<std::string> configs;
  std::vector<std::string> split_args;
@@ -340,10 +398,15 @@ int Optimize(const std::vector<StringPiece>& args) {
              "All the resources that would be unused on devices of the given densities will be \n"
              "removed from the APK.",
              &target_densities)
          .OptionalFlag("--whitelist-config-path",
          .OptionalFlag("--whitelist-path",
                        "Path to the whitelist.cfg file containing whitelisted resources \n"
                        "whose names should not be altered in final resource tables.",
                        &whitelist_path)
          .OptionalFlag("--resources-config-path",
                        "Path to the resources.cfg file containing the list of resources and \n"
                        "directives to each resource. \n"
                        "Format: type/resource_name#[directive][,directive]",
                        &resources_config_path)
          .OptionalFlagList("-c",
                            "Comma separated list of configurations to include. The default\n"
                            "is all configurations.",
@@ -460,12 +523,19 @@ int Optimize(const std::vector<StringPiece>& args) {
  if (options.table_flattener_options.collapse_key_stringpool) {
    if (whitelist_path) {
      std::string& path = whitelist_path.value();
      if (!ExtractWhitelistFromConfig(path, &context, &options)) {
      if (!ExtractObfuscationWhitelistFromConfig(path, &context, &options)) {
        return 1;
      }
    }
  }

  if (resources_config_path) {
    std::string& path = resources_config_path.value();
    if (!ExtractConfig(path, &context, &options)) {
      return 1;
    }
  }

  if (!ExtractAppDataFromManifest(&context, apk.get(), &options)) {
    return 1;
  }
+43 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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 "optimize/ResourceFilter.h"

#include "ResourceTable.h"

namespace aapt {

ResourceFilter::ResourceFilter(const std::unordered_set<ResourceName>& blacklist)
    : blacklist_(blacklist) {
}

bool ResourceFilter::Consume(IAaptContext* context, ResourceTable* table) {
  for (auto& package : table->packages) {
    for (auto& type : package->types) {
      for (auto it = type->entries.begin(); it != type->entries.end(); ) {
        ResourceName resource = ResourceName({}, type->type, (*it)->name);
        if (blacklist_.find(resource) != blacklist_.end()) {
          it = type->entries.erase(it);
        } else {
          ++it;
        }
      }
    }
  }
  return true;
}

}  // namespace aapt
+42 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2018 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_OPTIMIZE_RESOURCEFILTER_H
#define AAPT_OPTIMIZE_RESOURCEFILTER_H

#include "android-base/macros.h"

#include "process/IResourceTableConsumer.h"

#include <unordered_set>

namespace aapt {

// Removes non-whitelisted entries from resource table.
class ResourceFilter : public IResourceTableConsumer {
 public:
  explicit ResourceFilter(const std::unordered_set<ResourceName>& blacklist);

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

 private:
  DISALLOW_COPY_AND_ASSIGN(ResourceFilter);
  std::unordered_set<ResourceName> blacklist_;
};

} // namespace aapt

#endif  // AAPT_OPTIMIZE_RESOURCEFILTER_H
Loading