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

Commit 6e27180d authored by Iurii Makhno's avatar Iurii Makhno Committed by Android (Google) Code Review
Browse files

Merge changes Ibc63d826,I3c228767,I040224f4,If07e77b9

* changes:
  Implementation of 'aapt2 apkinfo' command.
  Extend DumpManifest unit to export badging data into proto.
  Split ManifestExtractor to Extract and Print methods.
  Tests for 'aapt2 dump badging' command.
parents 266d6496 f248074f
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ package {
}

toolSources = [
    "cmd/ApkInfo.cpp",
    "cmd/Command.cpp",
    "cmd/Compile.cpp",
    "cmd/Convert.cpp",
@@ -159,6 +160,7 @@ cc_library_host_static {
        "xml/XmlDom.cpp",
        "xml/XmlPullParser.cpp",
        "xml/XmlUtil.cpp",
        "ApkInfo.proto",
        "Configuration.proto",
        "Resources.proto",
        "ResourcesInternal.proto",
@@ -191,6 +193,7 @@ cc_test_host {
        "integration-tests/CompileTest/**/*",
        "integration-tests/CommandTests/**/*",
        "integration-tests/ConvertTest/**/*",
        "integration-tests/DumpTest/**/*",
    ],
}

+338 −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";

import "frameworks/base/tools/aapt2/Resources.proto";

package aapt.pb;

option java_package = "com.android.aapt";

// Top level message representing data extracted from the APK for 'apkinfo'
// command.
message ApkInfo {
  message XmlFile {
    string path = 1;
    XmlNode root = 2;
  }

  Badging badging = 1;
  ResourceTable resource_table = 2;
  repeated XmlFile xml_files = 3;
}

// Data extracted from the manifest of the APK.
message Badging {
  PackageInfo package = 1;
  Application application = 2;
  UsesSdk uses_sdk = 3;
  UsesConfiguration uses_configuration = 4;
  SupportsScreen supports_screen = 5;
  SupportsInput supports_input = 6;
  LaunchableActivity launchable_activity = 7;
  LeanbackLaunchableActivity leanback_launchable_activity = 8;
  StaticLibrary static_library = 9;
  SdkLibrary sdk_library = 10;
  Overlay overlay = 11;
  PackageVerifier package_verifier = 12;
  CompatibleScreens compatible_screens = 13;
  Architectures architectures = 14;
  SupportsGlTexture supports_gl_texture = 15;
  Components components = 16;

  repeated string locales = 17;
  repeated int32 densities = 18;

  repeated FeatureGroup feature_groups = 53;
  repeated UsesPermission uses_permissions = 54;
  repeated Permission permissions = 55;
  repeated UsesLibrary uses_libraries = 56;
  repeated UsesStaticLibrary uses_static_libraries = 57;
  repeated UsesSdkLibrary uses_sdk_libraries = 58;
  repeated UsesNativeLibrary uses_native_libraries = 59;
  repeated UsesPackage uses_packages = 51;

  repeated Metadata metadata = 62;
  repeated Property properties = 63;
}

// Information extracted about package from <manifest> and
// <original-package> tags.
message PackageInfo {
  enum InstallLocation {
    DEFAULT_INSTALL_LOCATION = 0;
    AUTO = 1;
    INTERNAL_ONLY = 2;
    PREFER_EXTERNAL = 3;
  }

  string package = 1;
  int32 version_code = 2;
  string version_name = 3;

  string split = 4;

  string platform_version_name = 5;
  string platform_version_code = 6;

  int32 compile_sdk_version = 7;
  string compile_sdk_version_codename = 8;

  InstallLocation install_location = 9;

  string original_package = 10;
}

// Information extracted from <application> element.
message Application {
  string label = 1;
  string icon = 2;
  string banner = 3;

  bool test_only = 4;
  bool game = 5;
  bool debuggable = 6;

  map<string, string> locale_labels = 8;
  map<int32, string> density_icons = 9;
}

// Components defined in the APK.
message Components {
  bool main = 1;
  bool other_activities = 2;
  bool other_receivers = 3;
  bool other_services = 4;

  repeated string provided_components = 5;
}

// Application's min and target SDKs.
message UsesSdk {
  oneof min_sdk {
    int32 min_sdk_version = 2;
    string min_sdk_version_name = 3;
  }
  int32 max_sdk_version = 4;
  oneof target_sdk {
    int32 target_sdk_version = 5;
    string target_sdk_version_name = 6;
  }
}

message UsesConfiguration {
  int32 req_touch_screen = 1;
  int32 req_keyboard_type = 2;
  int32 req_hard_keyboard = 3;
  int32 req_navigation = 4;
  int32 req_five_way_nav = 5;
}

// Screens supported by this application.
message SupportsScreen {
  enum ScreenType {
    UNSPECIFIED_SCREEN_TYPE = 0;
    SMALL = 1;
    NORMAL = 2;
    LARGE = 3;
    XLARGE = 4;
  }
  repeated ScreenType screens = 1;
  bool supports_any_densities = 2;
  int32 requires_smallest_width_dp = 3;
  int32 compatible_width_limit_dp = 4;
  int32 largest_width_limit_dp = 5;
}

// Inputs supported by this application.
message SupportsInput {
  repeated string inputs = 1;
}

// Information about used features which is extracted from <uses-permission>
// elements or implied from permissions.
message Feature {
  message ImpliedData {
      bool from_sdk_23_permission = 1;
      repeated string reasons = 2;
  }

  string name = 1;
  bool required = 2;
  int32 version = 3;

  ImpliedData implied_data = 4;
}

message FeatureGroup {
  string label = 1;
  int32 open_gles_version = 2;
  repeated Feature features = 3;
}

// Information about permission requested by the application.
message UsesPermission {
  message PermissionFlags {
    bool never_for_location = 1;
  }

  string name = 1;
  int32 max_sdk_version = 2;
  bool required = 3;
  bool implied = 4;
  bool sdk23_and_above = 5;

  repeated string required_features = 6;
  repeated string required_not_features = 7;

  PermissionFlags permission_flags = 8;
}

// Permission defined by the application.
message Permission {
  string name = 1;
}

// Data extracted about launchable activity. Launchable activity is an entry
// point on phone and tablet devices.
message LaunchableActivity {
  string name = 1;
  string icon = 2;
  string label = 3;
}

// Data extracted about leanback launchable activity. Leanback launchable
// activity is an entry point on TV devices.
message LeanbackLaunchableActivity {
  string name = 1;
  string icon = 2;
  string label = 3;
  string banner = 4;
}

// Library used by the application.
message UsesLibrary {
  string name = 1;
  bool required = 2;
}

// Static library this APK declares.
message StaticLibrary {
  string name = 1;
  int32 version = 2;
  int32 version_major = 3;
}

// Static library used by the application.
message UsesStaticLibrary {
  string name = 1;
  int32 version = 2;
  int32 version_major = 3;
  repeated string certificates = 4;
}

// SDK library this APK declares.
message SdkLibrary {
  string name = 1;
  int32 version_major = 2;
}

// SDK library used by the application.
message UsesSdkLibrary {
  string name = 1;
  int32 version_major = 2;
  repeated string certificates = 3;
}

// Native library used by the application.
message UsesNativeLibrary {
  string name = 1;
  bool required = 2;
}

// Information extracted from <meta-data> elements defined across
// AndroidManifest.xml.
message Metadata {
  string name = 1;
  oneof value {
    string value_string = 2;
    int32 value_int = 3;
  }
  oneof resource {
    string resource_string = 4;
    int32 resource_int = 5;
  }
}

// Information about overlay that is declared in the APK.
message Overlay {
  string target_package = 1;
  int32 priority = 2;
  bool static = 3;
  string required_property_name = 4;
  string required_property_value = 5;
}

// Data extracted from <package-verifier> element.
message PackageVerifier {
  string name = 1;
  string public_key = 2;
}

// External packages used by the application
message UsesPackage {
  string name = 1;
  string package_type = 2;
  int32 version = 3;
  int32 version_major = 4;
  repeated string certificates = 5;
}

// Open GL textures format supported by the current application.
message SupportsGlTexture {
  repeated string name = 1;
}

// Screens compatible with the application.
message CompatibleScreens {
  message Screen {
    int32 size = 1;
    int32 density = 2;
  }

  repeated Screen screens = 1;
}

// Architectures supported by the application.
message Architectures {
  repeated string architectures = 1;
  repeated string alt_architectures = 2;
}

// Information extracted from <property> elements defined across
// AndroidManifest.xml.
message Property {
  string name = 1;
  oneof value {
    string value_string = 2;
    int32 value_int = 3;
  }
  oneof resource {
    string resource_string = 4;
    int32 resource_int = 5;
  }
}
 No newline at end of file
+3 −2
Original line number Diff line number Diff line
@@ -24,11 +24,11 @@
#include <iostream>
#include <vector>

#include "Diagnostics.h"
#include "android-base/stringprintf.h"
#include "android-base/utf8.h"
#include "androidfw/StringPiece.h"

#include "Diagnostics.h"
#include "cmd/ApkInfo.h"
#include "cmd/Command.h"
#include "cmd/Compile.h"
#include "cmd/Convert.h"
@@ -72,6 +72,7 @@ class MainCommand : public Command {
    AddOptionalSubcommand(util::make_unique<OptimizeCommand>());
    AddOptionalSubcommand(util::make_unique<ConvertCommand>());
    AddOptionalSubcommand(util::make_unique<VersionCommand>());
    AddOptionalSubcommand(util::make_unique<ApkInfoCommand>(diagnostics));
  }

  int Action(const std::vector<std::string>& args) override {
+94 −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.
 */

#include "ApkInfo.h"

#include <fcntl.h>

#include <iostream>
#include <memory>

#include "Diagnostics.h"
#include "LoadedApk.h"
#include "android-base/file.h"  // for O_BINARY
#include "android-base/utf8.h"
#include "androidfw/StringPiece.h"
#include "dump/DumpManifest.h"
#include "format/proto/ProtoSerialize.h"

using ::android::StringPiece;

namespace aapt {

int ExportApkInfo(LoadedApk* apk, bool include_resource_table,
                  const std::unordered_set<std::string>& xml_resources, pb::ApkInfo* out_apk_info,
                  IDiagnostics* diag) {
  auto result = DumpBadgingProto(apk, out_apk_info->mutable_badging(), diag);
  if (result != 0) {
    return result;
  }

  if (include_resource_table) {
    SerializeTableToPb(*apk->GetResourceTable(), out_apk_info->mutable_resource_table(), diag);
  }

  for (auto& xml_resource : xml_resources) {
    auto xml = apk->LoadXml(xml_resource, diag);
    if (xml) {
      auto out_xml = out_apk_info->add_xml_files();
      out_xml->set_path(xml_resource);
      SerializeXmlResourceToPb(*xml, out_xml->mutable_root(),
                               {/* remove_empty_text_nodes= */ true});
    }
  }

  return 0;
}

int ApkInfoCommand::Action(const std::vector<std::string>& args) {
  if (args.size() != 1) {
    std::cerr << "must supply a single APK\n";
    Usage(&std::cerr);
    return 1;
  }
  const StringPiece& path = args[0];
  std::unique_ptr<LoadedApk> apk = LoadedApk::LoadApkFromPath(path, diag_);
  if (!apk) {
    return 1;
  }

  pb::ApkInfo out_apk_info;
  int result =
      ExportApkInfo(apk.get(), include_resource_table_, xml_resources_, &out_apk_info, diag_);
  if (result != 0) {
    diag_->Error(DiagMessage() << "Failed to serialize ApkInfo into proto.");
    return result;
  }

  int mode = O_WRONLY | O_CREAT | O_TRUNC | O_BINARY;
  int outfd = ::android::base::utf8::open(output_path_.c_str(), mode, 0666);
  if (outfd == -1) {
    diag_->Error(DiagMessage() << "Failed to open output file.");
    return 1;
  }

  bool is_serialized = out_apk_info.SerializeToFileDescriptor(outfd);
  close(outfd);

  return is_serialized ? 0 : 1;
}

}  // namespace aapt
 No newline at end of file
+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.
 */

#ifndef AAPT2_APKINFO_H
#define AAPT2_APKINFO_H

#include "Command.h"
#include "Diagnostics.h"

namespace aapt {

class ApkInfoCommand : public Command {
 public:
  explicit ApkInfoCommand(IDiagnostics* diag) : Command("apkinfo"), diag_(diag) {
    SetDescription("Dump information about an APK in binary proto format.");
    AddRequiredFlag("-o", "Output path", &output_path_, Command::kPath);
    AddOptionalSwitch("--include-resource-table", "Include the resource table data into output.",
                      &include_resource_table_);
    AddOptionalFlagList("--include-xml",
                        "Include an XML file content into output. Multiple XML files might be "
                        "requested during single invocation.",
                        &xml_resources_);
  }

  int Action(const std::vector<std::string>& args) override;

 private:
  IDiagnostics* diag_;
  std::string output_path_;
  bool include_resource_table_ = false;
  std::unordered_set<std::string> xml_resources_;
};

}  // namespace aapt

#endif  // AAPT2_APKINFO_H
Loading