Merge "Make sure ninja keeps going after errors" into main
diff --git a/core/Makefile b/core/Makefile
index 93f88c0..9d77ec1 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -3448,14 +3448,9 @@
 .PHONY: installed-file-list
 installed-file-list: $(INSTALLED_FILES_FILE)
 
-systemimage_intermediates :=$= $(call intermediates-dir-for,PACKAGING,systemimage)
+systemimage_intermediates :=$= $(call intermediates-dir-for,PACKAGING,system)
 BUILT_SYSTEMIMAGE :=$= $(systemimage_intermediates)/system.img
 
-
-# Used by the bazel sandwich to request the staging dir be built
-$(systemimage_intermediates)/staging_dir.stamp: $(FULL_SYSTEMIMAGE_DEPS)
-	touch $@
-
 # $(1): output file
 define build-systemimage-target
   @echo "Target system fs image: $(1)"
@@ -3472,6 +3467,9 @@
 endef
 
 $(eval $(call write-file-lines,$(systemimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT)/,,$(filter $(TARGET_OUT)/%,$(FULL_SYSTEMIMAGE_DEPS)))))
+# Used by soong sandwich to request the staging dir be built
+$(systemimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT)/%,$(FULL_SYSTEMIMAGE_DEPS))
+	touch $@
 
 ifeq ($(BOARD_AVB_ENABLE),true)
 $(BUILT_SYSTEMIMAGE): $(BOARD_AVB_SYSTEM_KEY_PATH)
@@ -3586,6 +3584,9 @@
     $(INTERNAL_USERDATAIMAGE_FILES)
 
 $(eval $(call write-file-lines,$(userdataimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_DATA)/,,$(filter $(TARGET_OUT_DATA)/%,$(INSTALLED_USERDATAIMAGE_TARGET_DEPS)))))
+# Used by soong sandwich to request the staging dir be built
+$(userdataimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_DATA)/%,$(INSTALLED_USERDATAIMAGE_TARGET_DEPS))
+	touch $@
 
 $(INSTALLED_USERDATAIMAGE_TARGET): $(INSTALLED_USERDATAIMAGE_TARGET_DEPS) $(userdataimage_intermediates)/file_list.txt
 	$(build-userdataimage-target)
@@ -3639,6 +3640,9 @@
 endef
 
 $(eval $(call write-file-lines,$(cacheimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_CACHE)/,,$(filter $(TARGET_OUT_CACHE)/%,$(INTERNAL_CACHEIMAGE_FILES)))))
+# Used by soong sandwich to request the staging dir be built
+$(cacheimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_CACHE)/%,$(INTERNAL_CACHEIMAGE_FILES))
+	touch $@
 
 # We just build this directly to the install location.
 INSTALLED_CACHEIMAGE_TARGET := $(BUILT_CACHEIMAGE_TARGET)
@@ -3723,6 +3727,9 @@
 endef
 
 $(eval $(call write-file-lines,$(systemotherimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_SYSTEM_OTHER)/,,$(filter $(TARGET_OUT_SYSTEM_OTHER)/%,$(INTERNAL_SYSTEMOTHERIMAGE_FILES)))))
+# Used by soong sandwich to request the staging dir be built
+$(systemotherimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_SYSTEM_OTHER)/%,$(INTERNAL_SYSTEMOTHERIMAGE_FILES))
+	touch $@
 
 # We just build this directly to the install location.
 INSTALLED_SYSTEMOTHERIMAGE_TARGET := $(BUILT_SYSTEMOTHERIMAGE_TARGET)
@@ -3826,6 +3833,9 @@
 endef
 
 $(eval $(call write-file-lines,$(vendorimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_VENDOR)/,,$(filter $(TARGET_OUT_VENDOR)/%,$(INTERNAL_VENDORIMAGE_FILES)))))
+# Used by soong sandwich to request the staging dir be built
+$(vendorimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_VENDOR)/%,$(INTERNAL_VENDORIMAGE_FILES))
+	touch $@
 
 # We just build this directly to the install location.
 INSTALLED_VENDORIMAGE_TARGET := $(BUILT_VENDORIMAGE_TARGET)
@@ -3896,6 +3906,9 @@
 endef
 
 $(eval $(call write-file-lines,$(productimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_PRODUCT)/,,$(filter $(TARGET_OUT_PRODUCT)/%,$(INTERNAL_PRODUCTIMAGE_FILES)))))
+# Used by soong sandwich to request the staging dir be built
+$(productimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_PRODUCT)/%,$(INTERNAL_PRODUCTIMAGE_FILES))
+	touch $@
 
 # We just build this directly to the install location.
 INSTALLED_PRODUCTIMAGE_TARGET := $(BUILT_PRODUCTIMAGE_TARGET)
@@ -3963,6 +3976,9 @@
 endef
 
 $(eval $(call write-file-lines,$(system_extimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_SYSTEM_EXT)/,,$(filter $(TARGET_OUT_SYSTEM_EXT)/%,$(INTERNAL_SYSTEM_EXTIMAGE_FILES)))))
+# Used by soong sandwich to request the staging dir be built
+$(system_extimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_SYSTEM_EXT)/%,$(INTERNAL_SYSTEM_EXTIMAGE_FILES))
+	touch $@
 
 # We just build this directly to the install location.
 INSTALLED_SYSTEM_EXTIMAGE_TARGET := $(BUILT_SYSTEM_EXTIMAGE_TARGET)
@@ -4049,6 +4065,9 @@
 endef
 
 $(eval $(call write-file-lines,$(odmimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_ODM)/,,$(filter $(TARGET_OUT_ODM)/%,$(INTERNAL_ODMIMAGE_FILES)))))
+# Used by soong sandwich to request the staging dir be built
+$(odmimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_ODM)/%,$(INTERNAL_ODMIMAGE_FILES))
+	touch $@
 
 # We just build this directly to the install location.
 INSTALLED_ODMIMAGE_TARGET := $(BUILT_ODMIMAGE_TARGET)
@@ -4115,6 +4134,9 @@
 endef
 
 $(eval $(call write-file-lines,$(vendor_dlkmimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_VENDOR_DLKM)/,,$(filter $(TARGET_OUT_VENDOR_DLKM)/%,$(INTERNAL_VENDOR_DLKMIMAGE_FILES)))))
+# Used by soong sandwich to request the staging dir be built
+$(vendor_dlkmimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_VENDOR_DLKM)/%,$(INTERNAL_VENDOR_DLKMIMAGE_FILES))
+	touch $@
 
 # We just build this directly to the install location.
 INSTALLED_VENDOR_DLKMIMAGE_TARGET := $(BUILT_VENDOR_DLKMIMAGE_TARGET)
@@ -4181,6 +4203,9 @@
 endef
 
 $(eval $(call write-file-lines,$(odm_dlkmimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_ODM_DLKM)/,,$(filter $(TARGET_OUT_ODM_DLKM)/%,$(INTERNAL_ODM_DLKMIMAGE_FILES)))))
+# Used by soong sandwich to request the staging dir be built
+$(odm_dlkmimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_ODM_DLKM)/%,$(INTERNAL_ODM_DLKMIMAGE_FILES))
+	touch $@
 
 # We just build this directly to the install location.
 INSTALLED_ODM_DLKMIMAGE_TARGET := $(BUILT_ODM_DLKMIMAGE_TARGET)
@@ -4249,6 +4274,9 @@
 endef
 
 $(eval $(call write-file-lines,$(system_dlkmimage_intermediates)/file_list.txt,$(subst $(TARGET_OUT_SYSTEM_DLKM)/,,$(filter $(TARGET_OUT_SYSTEM_DLKM)/%,$(INTERNAL_SYSTEM_DLKMIMAGE_FILES)))))
+# Used by soong sandwich to request the staging dir be built
+$(system_dlkmimage_intermediates)/staging_dir.stamp: $(filter $(TARGET_OUT_SYSTEM_DLKM)/%,$(INTERNAL_SYSTEM_DLKMIMAGE_FILES))
+	touch $@
 
 # We just build this directly to the install location.
 INSTALLED_SYSTEM_DLKMIMAGE_TARGET := $(BUILT_SYSTEM_DLKMIMAGE_TARGET)
@@ -7574,6 +7602,24 @@
 sdk_atree_files += $(atree_dir)/sdk.atree
 endif
 
+SDK_METADATA_DIR :=$= $(call intermediates-dir-for,PACKAGING,framework-doc-stubs-metadata,,COMMON)
+SDK_METADATA_FILES :=$= $(addprefix $(SDK_METADATA_DIR)/,\
+    activity_actions.txt \
+    broadcast_actions.txt \
+    categories.txt \
+    features.txt \
+    service_actions.txt \
+    widgets.txt)
+SDK_METADATA :=$= $(firstword $(SDK_METADATA_FILES))
+$(SDK_METADATA): .KATI_IMPLICIT_OUTPUTS := $(filter-out $(SDK_METADATA),$(SDK_METADATA_FILES))
+$(SDK_METADATA): $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/framework-doc-stubs-metadata.zip
+	rm -rf $(SDK_METADATA_DIR)
+	mkdir -p $(SDK_METADATA_DIR)
+	unzip -DDqo $< -d $(SDK_METADATA_DIR)
+
+.PHONY: framework-doc-stubs
+framework-doc-stubs: $(SDK_METADATA)
+
 deps := \
 	$(OUT_DOCS)/offline-sdk-timestamp \
 	$(SDK_METADATA_FILES) \
diff --git a/core/tasks/meta-lic.mk b/core/tasks/meta-lic.mk
index 0079714..dea4892 100644
--- a/core/tasks/meta-lic.mk
+++ b/core/tasks/meta-lic.mk
@@ -21,3 +21,12 @@
 
 # Moved here from frameworks/av/services/Android.mk
 $(eval $(call declare-1p-copy-files,frameworks/av/services/audiopolicy,))
+
+# Moved here from frameworks/base/Android.mk
+$(eval $(call declare-1p-copy-files,frameworks/base,.ogg))
+$(eval $(call declare-1p-copy-files,frameworks/base,.kl))
+$(eval $(call declare-1p-copy-files,frameworks/base,.kcm))
+$(eval $(call declare-1p-copy-files,frameworks/base,.idc))
+$(eval $(call declare-1p-copy-files,frameworks/base,dirty-image-objects))
+$(eval $(call declare-1p-copy-files,frameworks/base/config,))
+$(eval $(call declare-1p-copy-files,frameworks/native/data,))
diff --git a/core/tasks/offline-sdk-docs.mk b/core/tasks/offline-sdk-docs.mk
new file mode 100644
index 0000000..d9e8006
--- /dev/null
+++ b/core/tasks/offline-sdk-docs.mk
@@ -0,0 +1,26 @@
+#
+# Copyright (C) 2008 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.
+#
+
+# sdk.atree needs to copy the whole dir: $(OUT_DOCS)/offline-sdk to the final zip.
+# So keep offline-sdk-timestamp target here, and unzip offline-sdk-docs.zip to
+# $(OUT_DOCS)/offline-sdk.
+$(OUT_DOCS)/offline-sdk-timestamp: $(OUT_DOCS)/offline-sdk-docs-docs.zip
+	$(hide) rm -rf $(OUT_DOCS)/offline-sdk
+	$(hide) mkdir -p $(OUT_DOCS)/offline-sdk
+	( unzip -qo $< -d $(OUT_DOCS)/offline-sdk && touch -f $@ ) || exit 1
+
+.PHONY: docs offline-sdk-docs
+docs offline-sdk-docs: $(OUT_DOCS)/offline-sdk-timestamp
diff --git a/target/product/base_system.mk b/target/product/base_system.mk
index 8eef791..cd45e2a 100644
--- a/target/product/base_system.mk
+++ b/target/product/base_system.mk
@@ -420,13 +420,12 @@
     tz_version_host \
     tz_version_host_tzdata_apex \
 
+PRODUCT_PACKAGES += init.usb.rc init.usb.configfs.rc
 
 PRODUCT_COPY_FILES += \
-    system/core/rootdir/init.usb.rc:system/etc/init/hw/init.usb.rc \
-    system/core/rootdir/init.usb.configfs.rc:system/etc/init/hw/init.usb.configfs.rc \
     system/core/rootdir/etc/hosts:system/etc/hosts
 
-PRODUCT_COPY_FILES += system/core/rootdir/init.zygote32.rc:system/etc/init/hw/init.zygote32.rc
+PRODUCT_PACKAGES += init.zygote32.rc
 PRODUCT_VENDOR_PROPERTIES += ro.zygote?=zygote32
 
 PRODUCT_SYSTEM_PROPERTIES += debug.atrace.tags.enableflags=0
diff --git a/target/product/core_64_bit.mk b/target/product/core_64_bit.mk
index e0c4d53..790f57b 100644
--- a/target/product/core_64_bit.mk
+++ b/target/product/core_64_bit.mk
@@ -23,9 +23,7 @@
 # for 32-bit only.
 
 # Copy the 64-bit primary, 32-bit secondary zygote startup script
-PRODUCT_COPY_FILES += \
-    system/core/rootdir/init.zygote64.rc:system/etc/init/hw/init.zygote64.rc \
-    system/core/rootdir/init.zygote64_32.rc:system/etc/init/hw/init.zygote64_32.rc \
+PRODUCT_PACKAGES += init.zygote64.rc init.zygote64_32.rc
 
 # Set the zygote property to select the 64-bit primary, 32-bit secondary script
 # This line must be parsed before the one in core_minimal.mk
diff --git a/target/product/core_64_bit_only.mk b/target/product/core_64_bit_only.mk
index fc2b8e5..ffa5567 100644
--- a/target/product/core_64_bit_only.mk
+++ b/target/product/core_64_bit_only.mk
@@ -20,7 +20,7 @@
 # to core_minimal.mk.
 
 # Copy the 64-bit zygote startup script
-PRODUCT_COPY_FILES += system/core/rootdir/init.zygote64.rc:system/etc/init/hw/init.zygote64.rc
+PRODUCT_PACKAGES += init.zygote64.rc
 
 # Set the zygote property to select the 64-bit script.
 # This line must be parsed before the one in core_minimal.mk
diff --git a/target/product/generic_system.mk b/target/product/generic_system.mk
index 19ec86d..fa31e04 100644
--- a/target/product/generic_system.mk
+++ b/target/product/generic_system.mk
@@ -111,10 +111,10 @@
     $(PRODUCT_PACKAGES_SHIPPING_API_LEVEL_34)
 
 # Include all zygote init scripts. "ro.zygote" will select one of them.
-PRODUCT_COPY_FILES += \
-    system/core/rootdir/init.zygote32.rc:system/etc/init/hw/init.zygote32.rc \
-    system/core/rootdir/init.zygote64.rc:system/etc/init/hw/init.zygote64.rc \
-    system/core/rootdir/init.zygote64_32.rc:system/etc/init/hw/init.zygote64_32.rc \
+PRODUCT_PACKAGES += \
+    init.zygote32.rc \
+    init.zygote64.rc \
+    init.zygote64_32.rc
 
 # Enable dynamic partition size
 PRODUCT_USE_DYNAMIC_PARTITION_SIZE := true
diff --git a/target/product/sdk.mk b/target/product/sdk.mk
index 650f8e9..009a9d4 100644
--- a/target/product/sdk.mk
+++ b/target/product/sdk.mk
@@ -29,4 +29,8 @@
 PRODUCT_BRAND := Android
 PRODUCT_DEVICE := mainline_x86
 
-PRODUCT_BUILD_FROM_SOURCE_STUB := true
\ No newline at end of file
+PRODUCT_BUILD_FROM_SOURCE_STUB := true
+
+ifeq ($(WITHOUT_CHECK_API),true)
+  $(error WITHOUT_CHECK_API cannot be set to true for SDK product builds)
+endif
diff --git a/tools/aconfig/aconfig_storage_read_api/aconfig_storage_read_api.cpp b/tools/aconfig/aconfig_storage_read_api/aconfig_storage_read_api.cpp
index ceb5a96..131dc9d 100644
--- a/tools/aconfig/aconfig_storage_read_api/aconfig_storage_read_api.cpp
+++ b/tools/aconfig/aconfig_storage_read_api/aconfig_storage_read_api.cpp
@@ -1,6 +1,5 @@
 #include <android-base/file.h>
 #include <android-base/logging.h>
-#include <android-base/result.h>
 #include <protos/aconfig_storage_metadata.pb.h>
 
 #include <sys/mman.h>
@@ -97,40 +96,21 @@
 namespace private_internal_api {
 
 /// Get mapped file implementation.
-MappedStorageFileQuery get_mapped_file_impl(
+Result<MappedStorageFile> get_mapped_file_impl(
     std::string const& pb_file,
     std::string const& container,
     StorageFileType file_type) {
-  auto query =  MappedStorageFileQuery();
-
   auto file_result = find_storage_file(pb_file, container, file_type);
   if (!file_result.ok()) {
-    query.query_success = false;
-    query.error_message = file_result.error().message();
-    query.mapped_file.file_ptr = nullptr;
-    query.mapped_file.file_size = 0;
-    return query;
+    return Error() << file_result.error();
   }
-
-  auto mapped_file_result = map_storage_file(*file_result);
-  if (!mapped_file_result.ok()) {
-    query.query_success = false;
-    query.error_message = mapped_file_result.error().message();
-    query.mapped_file.file_ptr = nullptr;
-    query.mapped_file.file_size = 0;
-  } else {
-    query.query_success = true;
-    query.error_message = "";
-    query.mapped_file = *mapped_file_result;
-  }
-
-  return query;
+  return map_storage_file(*file_result);
 }
 
 } // namespace private internal api
 
 /// Get mapped storage file
-MappedStorageFileQuery get_mapped_file(
+Result<MappedStorageFile> get_mapped_file(
     std::string const& container,
     StorageFileType file_type) {
   return private_internal_api::get_mapped_file_impl(
@@ -138,61 +118,65 @@
 }
 
 /// Get storage file version number
-VersionNumberQuery get_storage_file_version(
+Result<uint32_t> get_storage_file_version(
     std::string const& file_path) {
   auto version_cxx = get_storage_file_version_cxx(
       rust::Str(file_path.c_str()));
-  auto version = VersionNumberQuery();
-  version.query_success = version_cxx.query_success;
-  version.error_message = std::string(version_cxx.error_message.c_str());
-  version.version_number = version_cxx.version_number;
-  return version;
+  if (version_cxx.query_success) {
+    return version_cxx.version_number;
+  } else {
+    return Error() << version_cxx.error_message;
+  }
 }
 
 /// Get package offset
-PackageOffsetQuery get_package_offset(
+Result<PackageOffset> get_package_offset(
     MappedStorageFile const& file,
     std::string const& package) {
   auto content = rust::Slice<const uint8_t>(
       static_cast<uint8_t*>(file.file_ptr), file.file_size);
   auto offset_cxx = get_package_offset_cxx(content, rust::Str(package.c_str()));
-  auto offset = PackageOffsetQuery();
-  offset.query_success = offset_cxx.query_success;
-  offset.error_message = std::string(offset_cxx.error_message.c_str());
-  offset.package_exists = offset_cxx.package_exists;
-  offset.package_id = offset_cxx.package_id;
-  offset.boolean_offset = offset_cxx.boolean_offset;
-  return offset;
+  if (offset_cxx.query_success) {
+    auto offset = PackageOffset();
+    offset.package_exists = offset_cxx.package_exists;
+    offset.package_id = offset_cxx.package_id;
+    offset.boolean_offset = offset_cxx.boolean_offset;
+    return offset;
+  } else {
+    return Error() << offset_cxx.error_message;
+  }
 }
 
 /// Get flag offset
-FlagOffsetQuery get_flag_offset(
+Result<FlagOffset> get_flag_offset(
     MappedStorageFile const& file,
     uint32_t package_id,
     std::string const& flag_name){
   auto content = rust::Slice<const uint8_t>(
       static_cast<uint8_t*>(file.file_ptr), file.file_size);
   auto offset_cxx = get_flag_offset_cxx(content, package_id, rust::Str(flag_name.c_str()));
-  auto offset = FlagOffsetQuery();
-  offset.query_success = offset_cxx.query_success;
-  offset.error_message = std::string(offset_cxx.error_message.c_str());
-  offset.flag_exists = offset_cxx.flag_exists;
-  offset.flag_offset = offset_cxx.flag_offset;
-  return offset;
+  if (offset_cxx.query_success) {
+    auto offset = FlagOffset();
+    offset.flag_exists = offset_cxx.flag_exists;
+    offset.flag_offset = offset_cxx.flag_offset;
+    return offset;
+  } else {
+   return Error() << offset_cxx.error_message;
+  }
 }
 
 /// Get boolean flag value
-BooleanFlagValueQuery get_boolean_flag_value(
+Result<bool> get_boolean_flag_value(
     MappedStorageFile const& file,
     uint32_t offset) {
   auto content = rust::Slice<const uint8_t>(
       static_cast<uint8_t*>(file.file_ptr), file.file_size);
   auto value_cxx = get_boolean_flag_value_cxx(content, offset);
-  auto value = BooleanFlagValueQuery();
-  value.query_success = value_cxx.query_success;
-  value.error_message = std::string(value_cxx.error_message.c_str());
-  value.flag_value = value_cxx.flag_value;
-  return value;
+  if (value_cxx.query_success) {
+    return value_cxx.flag_value;
+  } else {
+    return Error() << value_cxx.error_message;
+  }
 }
 
 } // namespace aconfig_storage
diff --git a/tools/aconfig/aconfig_storage_read_api/include/aconfig_storage/aconfig_storage_read_api.hpp b/tools/aconfig/aconfig_storage_read_api/include/aconfig_storage/aconfig_storage_read_api.hpp
index 92c03e0..aa90f47 100644
--- a/tools/aconfig/aconfig_storage_read_api/include/aconfig_storage/aconfig_storage_read_api.hpp
+++ b/tools/aconfig/aconfig_storage_read_api/include/aconfig_storage/aconfig_storage_read_api.hpp
@@ -2,6 +2,7 @@
 
 #include <stdint.h>
 #include <string>
+#include <android-base/result.h>
 
 namespace aconfig_storage {
 
@@ -18,73 +19,48 @@
   size_t file_size;
 };
 
-/// Mapped storage file query
-struct MappedStorageFileQuery {
-  bool query_success;
-  std::string error_message;
-  MappedStorageFile mapped_file;
-};
-
-/// DO NOT USE APIS IN THE FOLLOWING NAMESPACE DIRECTLY
-namespace private_internal_api {
-
-MappedStorageFileQuery get_mapped_file_impl(
-    std::string const& pb_file,
-    std::string const& container,
-    StorageFileType file_type);
-
-} // namespace private_internal_api
-
-/// Storage version number query result
-struct VersionNumberQuery {
-  bool query_success;
-  std::string error_message;
-  uint32_t version_number;
-};
-
 /// Package offset query result
-struct PackageOffsetQuery {
-  bool query_success;
-  std::string error_message;
+struct PackageOffset {
   bool package_exists;
   uint32_t package_id;
   uint32_t boolean_offset;
 };
 
 /// Flag offset query result
-struct FlagOffsetQuery {
-  bool query_success;
-  std::string error_message;
+struct FlagOffset {
   bool flag_exists;
   uint16_t flag_offset;
 };
 
-/// Boolean flag value query result
-struct BooleanFlagValueQuery {
-  bool query_success;
-  std::string error_message;
-  bool flag_value;
-};
+/// DO NOT USE APIS IN THE FOLLOWING NAMESPACE DIRECTLY
+namespace private_internal_api {
+
+android::base::Result<MappedStorageFile> get_mapped_file_impl(
+    std::string const& pb_file,
+    std::string const& container,
+    StorageFileType file_type);
+
+} // namespace private_internal_api
 
 /// Get mapped storage file
 /// \input container: stoarge container name
 /// \input file_type: storage file type enum
 /// \returns a MappedStorageFileQuery
-MappedStorageFileQuery get_mapped_file(
+android::base::Result<MappedStorageFile> get_mapped_file(
     std::string const& container,
     StorageFileType file_type);
 
 /// Get storage file version number
 /// \input file_path: the path to the storage file
-/// \returns a VersionNumberQuery
-VersionNumberQuery get_storage_file_version(
+/// \returns the storage file version
+android::base::Result<uint32_t> get_storage_file_version(
     std::string const& file_path);
 
 /// Get package offset
 /// \input file: mapped storage file
 /// \input package: the flag package name
-/// \returns a PackageOffsetQuery
-PackageOffsetQuery get_package_offset(
+/// \returns a package offset
+android::base::Result<PackageOffset> get_package_offset(
     MappedStorageFile const& file,
     std::string const& package);
 
@@ -92,8 +68,8 @@
 /// \input file: mapped storage file
 /// \input package_id: the flag package id obtained from package offset query
 /// \input flag_name: flag name
-/// \returns a FlagOffsetQuery
-FlagOffsetQuery get_flag_offset(
+/// \returns the flag offset
+android::base::Result<FlagOffset> get_flag_offset(
     MappedStorageFile const& file,
     uint32_t package_id,
     std::string const& flag_name);
@@ -101,8 +77,8 @@
 /// Get boolean flag value
 /// \input file: mapped storage file
 /// \input offset: the boolean flag value byte offset in the file
-/// \returns a BooleanFlagValueQuery
-BooleanFlagValueQuery get_boolean_flag_value(
+/// \returns the boolean flag value
+android::base::Result<bool> get_boolean_flag_value(
     MappedStorageFile const& file,
     uint32_t offset);
 
diff --git a/tools/aconfig/aconfig_storage_read_api/src/flag_table_query.rs b/tools/aconfig/aconfig_storage_read_api/src/flag_table_query.rs
index 403badb..43977ee 100644
--- a/tools/aconfig/aconfig_storage_read_api/src/flag_table_query.rs
+++ b/tools/aconfig/aconfig_storage_read_api/src/flag_table_query.rs
@@ -65,7 +65,7 @@
 #[cfg(test)]
 mod tests {
     use super::*;
-    use aconfig_storage_file::{StorageFileType, FlagTable};
+    use aconfig_storage_file::{FlagTable, StorageFileType};
 
     // create test baseline, syntactic sugar
     fn new_expected_node(
diff --git a/tools/aconfig/aconfig_storage_read_api/src/flag_value_query.rs b/tools/aconfig/aconfig_storage_read_api/src/flag_value_query.rs
index 4b7a65e..88d2397 100644
--- a/tools/aconfig/aconfig_storage_read_api/src/flag_value_query.rs
+++ b/tools/aconfig/aconfig_storage_read_api/src/flag_value_query.rs
@@ -48,7 +48,7 @@
 #[cfg(test)]
 mod tests {
     use super::*;
-    use aconfig_storage_file::{StorageFileType, FlagValueList};
+    use aconfig_storage_file::{FlagValueList, StorageFileType};
 
     pub fn create_test_flag_value_list() -> FlagValueList {
         let header = FlagValueHeader {
diff --git a/tools/aconfig/aconfig_storage_read_api/src/lib.rs b/tools/aconfig/aconfig_storage_read_api/src/lib.rs
index 87372c6..647ca55 100644
--- a/tools/aconfig/aconfig_storage_read_api/src/lib.rs
+++ b/tools/aconfig/aconfig_storage_read_api/src/lib.rs
@@ -183,14 +183,9 @@
 
         pub fn get_package_offset_cxx(file: &[u8], package: &str) -> PackageOffsetQueryCXX;
 
-        pub fn get_flag_offset_cxx(
-            file: &[u8],
-            package_id: u32,
-            flag: &str,
-        ) -> FlagOffsetQueryCXX;
+        pub fn get_flag_offset_cxx(file: &[u8], package_id: u32, flag: &str) -> FlagOffsetQueryCXX;
 
-        pub fn get_boolean_flag_value_cxx(file: &[u8], offset: u32)
-            -> BooleanFlagValueQueryCXX;
+        pub fn get_boolean_flag_value_cxx(file: &[u8], offset: u32) -> BooleanFlagValueQueryCXX;
     }
 }
 
@@ -294,11 +289,7 @@
 }
 
 /// Get flag start offset cc interlop
-pub fn get_flag_offset_cxx(
-    file: &[u8],
-    package_id: u32,
-    flag: &str,
-) -> ffi::FlagOffsetQueryCXX {
+pub fn get_flag_offset_cxx(file: &[u8], package_id: u32, flag: &str) -> ffi::FlagOffsetQueryCXX {
     ffi::FlagOffsetQueryCXX::new(find_flag_offset(file, package_id, flag))
 }
 
diff --git a/tools/aconfig/aconfig_storage_read_api/src/package_table_query.rs b/tools/aconfig/aconfig_storage_read_api/src/package_table_query.rs
index 81feec6..3587e10 100644
--- a/tools/aconfig/aconfig_storage_read_api/src/package_table_query.rs
+++ b/tools/aconfig/aconfig_storage_read_api/src/package_table_query.rs
@@ -72,7 +72,7 @@
 #[cfg(test)]
 mod tests {
     use super::*;
-    use aconfig_storage_file::{StorageFileType, PackageTable};
+    use aconfig_storage_file::{PackageTable, StorageFileType};
 
     pub fn create_test_package_table() -> PackageTable {
         let header = PackageTableHeader {
diff --git a/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.cpp b/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.cpp
index ff2f849..377395a 100644
--- a/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.cpp
+++ b/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.cpp
@@ -93,105 +93,92 @@
 
 /// Test to lock down storage file version query api
 TEST_F(AconfigStorageTest, test_storage_version_query) {
-  auto query = api::get_storage_file_version(package_map);
-  ASSERT_EQ(query.error_message, std::string());
-  ASSERT_TRUE(query.query_success);
-  ASSERT_EQ(query.version_number, 1);
-  query = api::get_storage_file_version(flag_map);
-  ASSERT_EQ(query.error_message, std::string());
-  ASSERT_TRUE(query.query_success);
-  ASSERT_EQ(query.version_number, 1);
-  query = api::get_storage_file_version(flag_val);
-  ASSERT_EQ(query.error_message, std::string());
-  ASSERT_TRUE(query.query_success);
-  ASSERT_EQ(query.version_number, 1);
+  auto version = api::get_storage_file_version(package_map);
+  ASSERT_TRUE(version.ok());
+  ASSERT_EQ(*version, 1);
+  version = api::get_storage_file_version(flag_map);
+  ASSERT_TRUE(version.ok());
+  ASSERT_EQ(*version, 1);
+  version = api::get_storage_file_version(flag_val);
+  ASSERT_TRUE(version.ok());
+  ASSERT_EQ(*version, 1);
 }
 
 /// Negative test to lock down the error when mapping none exist storage files
 TEST_F(AconfigStorageTest, test_none_exist_storage_file_mapping) {
-  auto mapped_file_query = private_api::get_mapped_file_impl(
+  auto mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "vendor", api::StorageFileType::package_map);
-  ASSERT_FALSE(mapped_file_query.query_success);
-  ASSERT_EQ(mapped_file_query.error_message,
+  ASSERT_FALSE(mapped_file.ok());
+  ASSERT_EQ(mapped_file.error().message(),
             "Unable to find storage files for container vendor");
 }
 
 /// Negative test to lock down the error when mapping a writeable storage file
 TEST_F(AconfigStorageTest, test_writable_storage_file_mapping) {
-  ASSERT_TRUE(chmod(package_map.c_str(),
-                    S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH) != -1);
-  auto mapped_file_query = private_api::get_mapped_file_impl(
+  ASSERT_TRUE(chmod(package_map.c_str(), 0666) != -1);
+  auto mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "system", api::StorageFileType::package_map);
-  ASSERT_FALSE(mapped_file_query.query_success);
-  ASSERT_EQ(mapped_file_query.error_message, "cannot map writeable file");
+  ASSERT_FALSE(mapped_file.ok());
+  ASSERT_EQ(mapped_file.error().message(), "cannot map writeable file");
 
-  ASSERT_TRUE(chmod(flag_map.c_str(),
-                    S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH) != -1);
-  mapped_file_query = private_api::get_mapped_file_impl(
+  ASSERT_TRUE(chmod(flag_map.c_str(), 0666) != -1);
+  mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "system", api::StorageFileType::flag_map);
-  ASSERT_FALSE(mapped_file_query.query_success);
-  ASSERT_EQ(mapped_file_query.error_message, "cannot map writeable file");
+  ASSERT_FALSE(mapped_file.ok());
+  ASSERT_EQ(mapped_file.error().message(), "cannot map writeable file");
 
-  ASSERT_TRUE(chmod(flag_val.c_str(),
-                    S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH) != -1);
-  mapped_file_query = private_api::get_mapped_file_impl(
+  ASSERT_TRUE(chmod(flag_val.c_str(), 0666) != -1);
+  mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "system", api::StorageFileType::flag_val);
-  ASSERT_FALSE(mapped_file_query.query_success);
-  ASSERT_EQ(mapped_file_query.error_message, "cannot map writeable file");
+  ASSERT_FALSE(mapped_file.ok());
+  ASSERT_EQ(mapped_file.error().message(), "cannot map writeable file");
 }
 
 /// Test to lock down storage package offset query api
 TEST_F(AconfigStorageTest, test_package_offset_query) {
-  auto mapped_file_query = private_api::get_mapped_file_impl(
+  auto mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "system", api::StorageFileType::package_map);
-  ASSERT_TRUE(mapped_file_query.query_success);
-  auto mapped_file = mapped_file_query.mapped_file;
+  ASSERT_TRUE(mapped_file.ok());
 
-  auto query = api::get_package_offset(
-      mapped_file, "com.android.aconfig.storage.test_1");
-  ASSERT_EQ(query.error_message, std::string());
-  ASSERT_TRUE(query.query_success);
-  ASSERT_TRUE(query.package_exists);
-  ASSERT_EQ(query.package_id, 0);
-  ASSERT_EQ(query.boolean_offset, 0);
+  auto offset = api::get_package_offset(
+      *mapped_file, "com.android.aconfig.storage.test_1");
+  ASSERT_TRUE(offset.ok());
+  ASSERT_TRUE(offset->package_exists);
+  ASSERT_EQ(offset->package_id, 0);
+  ASSERT_EQ(offset->boolean_offset, 0);
 
-  query = api::get_package_offset(
-      mapped_file, "com.android.aconfig.storage.test_2");
-  ASSERT_EQ(query.error_message, std::string());
-  ASSERT_TRUE(query.query_success);
-  ASSERT_TRUE(query.package_exists);
-  ASSERT_EQ(query.package_id, 1);
-  ASSERT_EQ(query.boolean_offset, 3);
+  offset = api::get_package_offset(
+      *mapped_file, "com.android.aconfig.storage.test_2");
+  ASSERT_TRUE(offset.ok());
+  ASSERT_TRUE(offset->package_exists);
+  ASSERT_EQ(offset->package_id, 1);
+  ASSERT_EQ(offset->boolean_offset, 3);
 
-  query = api::get_package_offset(
-      mapped_file, "com.android.aconfig.storage.test_4");
-  ASSERT_EQ(query.error_message, std::string());
-  ASSERT_TRUE(query.query_success);
-  ASSERT_TRUE(query.package_exists);
-  ASSERT_EQ(query.package_id, 2);
-  ASSERT_EQ(query.boolean_offset, 6);
+  offset = api::get_package_offset(
+      *mapped_file, "com.android.aconfig.storage.test_4");
+  ASSERT_TRUE(offset.ok());
+  ASSERT_TRUE(offset->package_exists);
+  ASSERT_EQ(offset->package_id, 2);
+  ASSERT_EQ(offset->boolean_offset, 6);
 }
 
 /// Test to lock down when querying none exist package
 TEST_F(AconfigStorageTest, test_none_existent_package_offset_query) {
-  auto mapped_file_query = private_api::get_mapped_file_impl(
+  auto mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "system", api::StorageFileType::package_map);
-  ASSERT_TRUE(mapped_file_query.query_success);
-  auto mapped_file = mapped_file_query.mapped_file;
+  ASSERT_TRUE(mapped_file.ok());
 
-  auto query = api::get_package_offset(
-      mapped_file, "com.android.aconfig.storage.test_3");
-  ASSERT_EQ(query.error_message, std::string());
-  ASSERT_TRUE(query.query_success);
-  ASSERT_FALSE(query.package_exists);
+  auto offset = api::get_package_offset(
+      *mapped_file, "com.android.aconfig.storage.test_3");
+  ASSERT_TRUE(offset.ok());
+  ASSERT_FALSE(offset->package_exists);
 }
 
 /// Test to lock down storage flag offset query api
 TEST_F(AconfigStorageTest, test_flag_offset_query) {
-  auto mapped_file_query = private_api::get_mapped_file_impl(
+  auto mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "system", api::StorageFileType::flag_map);
-  ASSERT_TRUE(mapped_file_query.query_success);
-  auto mapped_file = mapped_file_query.mapped_file;
+  ASSERT_TRUE(mapped_file.ok());
 
   auto baseline = std::vector<std::tuple<int, std::string, int>>{
     {0, "enabled_ro", 1},
@@ -204,56 +191,49 @@
     {0, "disabled_rw", 0},
   };
   for (auto const&[package_id, flag_name, expected_offset] : baseline) {
-    auto query = api::get_flag_offset(mapped_file, package_id, flag_name);
-    ASSERT_EQ(query.error_message, std::string());
-    ASSERT_TRUE(query.query_success);
-    ASSERT_TRUE(query.flag_exists);
-    ASSERT_EQ(query.flag_offset, expected_offset);
+    auto offset = api::get_flag_offset(*mapped_file, package_id, flag_name);
+    ASSERT_TRUE(offset.ok());
+    ASSERT_TRUE(offset->flag_exists);
+    ASSERT_EQ(offset->flag_offset, expected_offset);
   }
 }
 
 /// Test to lock down when querying none exist flag
 TEST_F(AconfigStorageTest, test_none_existent_flag_offset_query) {
-  auto mapped_file_query = private_api::get_mapped_file_impl(
+  auto mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "system", api::StorageFileType::flag_map);
-  ASSERT_TRUE(mapped_file_query.query_success);
-  auto mapped_file = mapped_file_query.mapped_file;
+  ASSERT_TRUE(mapped_file.ok());
 
-  auto query = api::get_flag_offset(mapped_file, 0, "none_exist");
-  ASSERT_EQ(query.error_message, std::string());
-  ASSERT_TRUE(query.query_success);
-  ASSERT_FALSE(query.flag_exists);
+  auto offset = api::get_flag_offset(*mapped_file, 0, "none_exist");
+  ASSERT_TRUE(offset.ok());
+  ASSERT_FALSE(offset->flag_exists);
 
-  query = api::get_flag_offset(mapped_file, 3, "enabled_ro");
-  ASSERT_EQ(query.error_message, std::string());
-  ASSERT_TRUE(query.query_success);
-  ASSERT_FALSE(query.flag_exists);
+  offset = api::get_flag_offset(*mapped_file, 3, "enabled_ro");
+  ASSERT_TRUE(offset.ok());
+  ASSERT_FALSE(offset->flag_exists);
 }
 
 /// Test to lock down storage flag value query api
 TEST_F(AconfigStorageTest, test_boolean_flag_value_query) {
-  auto mapped_file_query = private_api::get_mapped_file_impl(
+  auto mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "system", api::StorageFileType::flag_val);
-  ASSERT_TRUE(mapped_file_query.query_success);
-  auto mapped_file = mapped_file_query.mapped_file;
+  ASSERT_TRUE(mapped_file.ok());
 
   for (int offset = 0; offset < 8; ++offset) {
-    auto query = api::get_boolean_flag_value(mapped_file, offset);
-    ASSERT_EQ(query.error_message, std::string());
-    ASSERT_TRUE(query.query_success);
-    ASSERT_FALSE(query.flag_value);
+    auto value = api::get_boolean_flag_value(*mapped_file, offset);
+    ASSERT_TRUE(value.ok());
+    ASSERT_FALSE(*value);
   }
 }
 
 /// Negative test to lock down the error when querying flag value out of range
 TEST_F(AconfigStorageTest, test_invalid_boolean_flag_value_query) {
-  auto mapped_file_query = private_api::get_mapped_file_impl(
+  auto mapped_file = private_api::get_mapped_file_impl(
       storage_record_pb, "system", api::StorageFileType::flag_val);
-  ASSERT_TRUE(mapped_file_query.query_success);
-  auto mapped_file = mapped_file_query.mapped_file;
+  ASSERT_TRUE(mapped_file.ok());
 
-  auto query = api::get_boolean_flag_value(mapped_file, 8);
-  ASSERT_EQ(query.error_message,
+  auto value = api::get_boolean_flag_value(*mapped_file, 8);
+  ASSERT_FALSE(value.ok());
+  ASSERT_EQ(value.error().message(),
             std::string("InvalidStorageFileOffset(Flag value offset goes beyond the end of the file.)"));
-  ASSERT_FALSE(query.query_success);
 }
diff --git a/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.rs b/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.rs
index 64239e5..eb4d54d 100644
--- a/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.rs
+++ b/tools/aconfig/aconfig_storage_read_api/tests/storage_read_api_test.rs
@@ -114,9 +114,7 @@
         ];
         for (package_id, flag_name, expected_offset) in baseline.into_iter() {
             let flag_offset =
-                get_flag_offset(&flag_mapped_file, package_id, flag_name)
-                    .unwrap()
-                    .unwrap();
+                get_flag_offset(&flag_mapped_file, package_id, flag_name).unwrap().unwrap();
             assert_eq!(flag_offset, expected_offset);
         }
     }
@@ -128,12 +126,10 @@
         let flag_mapped_file =
             get_mapped_file(&pb_file_path, "system", StorageFileType::FlagMap).unwrap();
 
-        let flag_offset_option =
-            get_flag_offset(&flag_mapped_file, 0, "none_exist").unwrap();
+        let flag_offset_option = get_flag_offset(&flag_mapped_file, 0, "none_exist").unwrap();
         assert_eq!(flag_offset_option, None);
 
-        let flag_offset_option =
-            get_flag_offset(&flag_mapped_file, 3, "enabled_ro").unwrap();
+        let flag_offset_option = get_flag_offset(&flag_mapped_file, 3, "enabled_ro").unwrap();
         assert_eq!(flag_offset_option, None);
     }
 
@@ -146,8 +142,7 @@
 
         let baseline: Vec<bool> = vec![false; 8];
         for (offset, expected_value) in baseline.into_iter().enumerate() {
-            let flag_value =
-                get_boolean_flag_value(&flag_value_file, offset as u32).unwrap();
+            let flag_value = get_boolean_flag_value(&flag_value_file, offset as u32).unwrap();
             assert_eq!(flag_value, expected_value);
         }
     }
diff --git a/tools/aconfig/aconfig_storage_write_api/tests/storage_write_api_test.cpp b/tools/aconfig/aconfig_storage_write_api/tests/storage_write_api_test.cpp
index 3035f9e..3a1c5de 100644
--- a/tools/aconfig/aconfig_storage_write_api/tests/storage_write_api_test.cpp
+++ b/tools/aconfig/aconfig_storage_write_api/tests/storage_write_api_test.cpp
@@ -115,9 +115,9 @@
     auto ro_mapped_file = api::MappedStorageFile();
     ro_mapped_file.file_ptr = mapped_file.file_ptr;
     ro_mapped_file.file_size = mapped_file.file_size;
-    auto value_query = api::get_boolean_flag_value(ro_mapped_file, offset);
-    ASSERT_TRUE(value_query.query_success);
-    ASSERT_TRUE(value_query.flag_value);
+    auto value = api::get_boolean_flag_value(ro_mapped_file, offset);
+    ASSERT_TRUE(value.ok());
+    ASSERT_TRUE(*value);
   }
 }