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

Commit 0a5b2011 authored by Shane Farmer's avatar Shane Farmer
Browse files

AAPT2: Add a APK filtering.

Allow resource files to be removed from the final artifact based on the
density and locale configuration in the config file. The APK is split
along the density, locale and ABI axis. Each split is generated from the
original APK without modifying the original. The new resource table is
written back to the file system with unneeded assets etc removed.

Test: Unit tests
Test: Manually run optimize command against an APK and inspect results
Test: Installed split searchlite APK (after resigning) and ran on N6

Change-Id: If73597dcfd88c02d2616518585d0e25a5c6a84d1
parent 8dbbdf0c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ cc_library_host_static {
        "link/XmlCompatVersioner.cpp",
        "link/XmlNamespaceRemover.cpp",
        "link/XmlReferenceLinker.cpp",
        "optimize/MultiApkGenerator.cpp",
        "optimize/ResourceDeduper.cpp",
        "optimize/VersionCollapser.cpp",
        "process/SymbolTable.cpp",
+6 −5
Original line number Diff line number Diff line
@@ -58,14 +58,15 @@ std::unique_ptr<LoadedApk> LoadedApk::LoadApkFromPath(IAaptContext* context,
bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
                               IArchiveWriter* writer) {
  FilterChain empty;
  return WriteToArchive(context, options, &empty, writer);
  return WriteToArchive(context, table_.get(), options, &empty, writer);
}

bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
                               FilterChain* filters, IArchiveWriter* writer) {
bool LoadedApk::WriteToArchive(IAaptContext* context, ResourceTable* split_table,
                               const TableFlattenerOptions& options, FilterChain* filters,
                               IArchiveWriter* writer) {
  std::set<std::string> referenced_resources;
  // List the files being referenced in the resource table.
  for (auto& pkg : table_->packages) {
  for (auto& pkg : split_table->packages) {
    for (auto& type : pkg->types) {
      for (auto& entry : type->entries) {
        for (auto& config_value : entry->values) {
@@ -108,7 +109,7 @@ bool LoadedApk::WriteToArchive(IAaptContext* context, const TableFlattenerOption
      // TODO(adamlesinski): How to determine if there were sparse entries (and if to encode
      // with sparse entries) b/35389232.
      TableFlattener flattener(options, &buffer);
      if (!flattener.Consume(context, table_.get())) {
      if (!flattener.Consume(context, split_table)) {
        return false;
      }

+5 −4
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ class LoadedApk {
   * Writes the APK on disk at the given path, while also removing the resource
   * files that are not referenced in the resource table.
   */
  bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
  virtual bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
                              IArchiveWriter* writer);

  /**
@@ -55,8 +55,9 @@ class LoadedApk {
   * files that are not referenced in the resource table. The provided filter
   * chain is applied to each entry in the APK file.
   */
  bool WriteToArchive(IAaptContext* context, const TableFlattenerOptions& options,
                      FilterChain* filters, IArchiveWriter* writer);
  virtual bool WriteToArchive(IAaptContext* context, ResourceTable* split_table,
                              const TableFlattenerOptions& options, FilterChain* filters,
                              IArchiveWriter* writer);

  static std::unique_ptr<LoadedApk> LoadApkFromPath(IAaptContext* context,
                                                    const android::StringPiece& path);
+30 −0
Original line number Diff line number Diff line
@@ -546,4 +546,34 @@ Maybe<ResourceTable::SearchResult> ResourceTable::FindResource(const ResourceNam
  return SearchResult{package, type, entry};
}

std::unique_ptr<ResourceTable> ResourceTable::Clone() const {
  std::unique_ptr<ResourceTable> new_table = util::make_unique<ResourceTable>();
  for (const auto& pkg : packages) {
    ResourceTablePackage* new_pkg = new_table->CreatePackage(pkg->name, pkg->id);
    for (const auto& type : pkg->types) {
      ResourceTableType* new_type = new_pkg->FindOrCreateType(type->type);
      if (!new_type->id) {
        new_type->id = type->id;
        new_type->symbol_status = type->symbol_status;
      }

      for (const auto& entry : type->entries) {
        ResourceEntry* new_entry = new_type->FindOrCreateEntry(entry->name);
        if (!new_entry->id) {
          new_entry->id = entry->id;
          new_entry->symbol_status = entry->symbol_status;
        }

        for (const auto& config_value : entry->values) {
          ResourceConfigValue* new_value =
              new_entry->FindOrCreateValue(config_value->config, config_value->product);
          Value* value = config_value->value->Clone(&new_table->string_pool);
          new_value->value = std::unique_ptr<Value>(value);
        }
      }
    }
  }
  return new_table;
}

}  // namespace aapt
+2 −0
Original line number Diff line number Diff line
@@ -251,6 +251,8 @@ class ResourceTable {

  ResourceTablePackage* CreatePackage(const android::StringPiece& name, Maybe<uint8_t> id = {});

  std::unique_ptr<ResourceTable> Clone() const;

  /**
   * The string pool used by this resource table. Values that reference strings
   * must use
Loading