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

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

Merge changes I27b4b1af,Ie3829834

* changes:
  Rename ResourcePathShortener to Obfuscator
  Define a unified protobuf message for "aapt2 optimize" metadata.
parents bf856296 4bdd3ac4
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -130,7 +130,7 @@ cc_library_host_static {
        "optimize/MultiApkGenerator.cpp",
        "optimize/ResourceDeduper.cpp",
        "optimize/ResourceFilter.cpp",
        "optimize/ResourcePathShortener.cpp",
        "optimize/Obfuscator.cpp",
        "optimize/VersionCollapser.cpp",
        "process/SymbolTable.cpp",
        "split/TableSplitter.cpp",
@@ -161,6 +161,7 @@ cc_library_host_static {
        "ApkInfo.proto",
        "Configuration.proto",
        "Resources.proto",
        "ResourceMetadata.proto",
        "ResourcesInternal.proto",
        "ValueTransformer.cpp",
    ],
@@ -218,6 +219,7 @@ genrule {
    srcs: [
        "Configuration.proto",
        "ResourcesInternal.proto",
        "ResourceMetadata.proto",
        "Resources.proto",
    ],
    out: ["aapt2-protos.zip"],
+49 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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.
 */

syntax = "proto3";

package aapt.pb;

option java_package = "com.android.aapt";
option java_multiple_files = true;

message ResourceMappings {
  ShortenedPathsMap shortened_paths = 1;
  CollapsedNamesMap collapsed_names = 2;
}

// Metadata relating to "aapt2 optimize --shorten-resource-paths"
message ShortenedPathsMap {
  // Maps shorted paths (e.g. "res/foo.xml") to their original names (e.g.
  // "res/xml/file_with_long_name.xml").
  message ResourcePathMapping {
    string shortened_path = 1;
    string original_path = 2;
  }
  repeated ResourcePathMapping resource_paths = 1;
}

// Metadata relating to "aapt2 optimize --collapse-resource-names"
message CollapsedNamesMap {
  // Maps resource IDs (e.g. 0x7f123456) to their original names (e.g.
  // "package:type/entry").
  message ResourceNameMapping {
    uint32 id = 1;
    string name = 2;
  }
  repeated ResourceNameMapping resource_names = 1;
}
+9 −5
Original line number Diff line number Diff line
@@ -16,7 +16,11 @@

#include "Optimize.h"

#include <map>
#include <memory>
#include <set>
#include <string>
#include <utility>
#include <vector>

#include "Diagnostics.h"
@@ -38,9 +42,9 @@
#include "io/BigBufferStream.h"
#include "io/Util.h"
#include "optimize/MultiApkGenerator.h"
#include "optimize/Obfuscator.h"
#include "optimize/ResourceDeduper.h"
#include "optimize/ResourceFilter.h"
#include "optimize/ResourcePathShortener.h"
#include "optimize/VersionCollapser.h"
#include "split/TableSplitter.h"
#include "util/Files.h"
@@ -114,11 +118,11 @@ class OptimizeContext : public IAaptContext {
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(OptimizeContext);

  StdErrDiagnostics diagnostics_;
  bool verbose_ = false;
  int sdk_version_ = 0;

  DISALLOW_COPY_AND_ASSIGN(OptimizeContext);
};

class Optimizer {
@@ -151,8 +155,8 @@ class Optimizer {
    }

    if (options_.shorten_resource_paths) {
      ResourcePathShortener shortener(options_.table_flattener_options.shortened_path_map);
      if (!shortener.Consume(context_, apk->GetResourceTable())) {
      Obfuscator obfuscator(options_.table_flattener_options.shortened_path_map);
      if (!obfuscator.Consume(context_, apk->GetResourceTable())) {
        context_->GetDiagnostics()->Error(android::DiagMessage()
                                          << "failed shortening resource paths");
        return 1;
+14 −19
Original line number Diff line number Diff line
@@ -14,28 +14,25 @@
 * limitations under the License.
 */

#include "optimize/ResourcePathShortener.h"
#include "optimize/Obfuscator.h"

#include <set>
#include <string>
#include <unordered_set>

#include "androidfw/StringPiece.h"

#include "ResourceTable.h"
#include "ValueVisitor.h"
#include "androidfw/StringPiece.h"
#include "util/Util.h"


static const std::string base64_chars =
static const char base64_chars[] =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    "abcdefghijklmnopqrstuvwxyz"
    "0123456789-_";

namespace aapt {

ResourcePathShortener::ResourcePathShortener(
    std::map<std::string, std::string>& path_map_out)
    : path_map_(path_map_out) {
Obfuscator::Obfuscator(std::map<std::string, std::string>& path_map_out) : path_map_(path_map_out) {
}

std::string ShortenFileName(const android::StringPiece& file_path, int output_length) {
@@ -50,7 +47,6 @@ std::string ShortenFileName(const android::StringPiece& file_path, int output_le
  return result;
}


// Return the optimal hash length such that at most 10% of resources collide in
// their shortened path.
// Reference: http://matt.might.net/articles/counting-hash-collisions/
@@ -81,7 +77,7 @@ struct PathComparator {
  }
};

bool ResourcePathShortener::Consume(IAaptContext* context, ResourceTable* table) {
bool Obfuscator::Consume(IAaptContext* context, ResourceTable* table) {
  // used to detect collisions
  std::unordered_set<std::string> shortened_paths;
  std::set<FileReference*, PathComparator> file_refs;
@@ -103,8 +99,7 @@ bool ResourcePathShortener::Consume(IAaptContext* context, ResourceTable* table)
    util::ExtractResFilePathParts(*file_ref->path, &res_subdir, &actual_filename, &extension);

    // Android detects ColorStateLists via pathname, skip res/color*
    if (util::StartsWith(res_subdir, "res/color"))
      continue;
    if (util::StartsWith(res_subdir, "res/color")) continue;

    std::string shortened_filename = ShortenFileName(*file_ref->path, num_chars);
    int collision_count = 0;
+8 −8
Original line number Diff line number Diff line
@@ -14,13 +14,13 @@
 * limitations under the License.
 */

#ifndef AAPT_OPTIMIZE_RESOURCEPATHSHORTENER_H
#define AAPT_OPTIMIZE_RESOURCEPATHSHORTENER_H
#ifndef TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_
#define TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_

#include <map>
#include <string>

#include "android-base/macros.h"

#include "process/IResourceTableConsumer.h"

namespace aapt {
@@ -28,17 +28,17 @@ namespace aapt {
class ResourceTable;

// Maps resources in the apk to shortened paths.
class ResourcePathShortener : public IResourceTableConsumer {
class Obfuscator : public IResourceTableConsumer {
 public:
  explicit ResourcePathShortener(std::map<std::string, std::string>& path_map_out);
  explicit Obfuscator(std::map<std::string, std::string>& path_map_out);

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

 private:
  DISALLOW_COPY_AND_ASSIGN(ResourcePathShortener);
  std::map<std::string, std::string>& path_map_;
  DISALLOW_COPY_AND_ASSIGN(Obfuscator);
};

}  // namespace aapt

#endif  // AAPT_OPTIMIZE_RESOURCEPATHSHORTENER_H
#endif  // TOOLS_AAPT2_OPTIMIZE_OBFUSCATOR_H_
Loading