Merge Android 14 QPR2 to AOSP main

Bug: 319669529
Merged-In: Ia9088f0f663dc8d4458f0c5d6996e43b77bf84cd
Change-Id: Iac78fd3ee1ba1201b35d0705f6b360bda60f0156
diff --git a/Android.bp b/Android.bp
index 95746d2..ccbd262 100644
--- a/Android.bp
+++ b/Android.bp
@@ -67,6 +67,7 @@
         "com.android.neuralnetworks",
         "test_com.android.neuralnetworks",
         "com.android.adservices",
+        "com.android.ondevicepersonalization",
     ],
 }
 
@@ -133,10 +134,14 @@
 }
 
 cc_library_static {
-    name: "libtensorflow_core_mobile_srcs_only_runtime",
+    name: "libtflite_flex_delegate",
     srcs: [
+        ":tflite_src",
+        ":tflite_flex_delegate",
         ":tensorflow_c_srcs",
         ":tensorflow_c_eager_srcs",
+        ":tensorflow_core_data_mobile_srcs_no_runtime",
+        ":tensorflow_core_kernels_mobile_srcs",
         ":tensorflow_core_common_runtime_mobile_srcs_only_runtime",
         ":tensorflow_core_framework_mobile_srcs_only_runtime",
         ":tensorflow_core_framework_mobile_srcs_no_runtime",
@@ -154,24 +159,43 @@
         ":tensorflow_core_lib_mobile_src_no_runtime",
         "tensorflow/core/nccl/collective_communicator.cc",
         "tensorflow/core/example/feature_util.cc",
+        "tensorflow/core/ops/no_op.cc",
+        "tensorflow/core/ops/array_ops.cc",
+        "tensorflow/core/ops/parsing_ops.cc",
+        "tensorflow/core/ops/dataset_ops.cc",
+        "tensorflow/core/ops/function_ops.cc",
+        "tensorflow/core/ops/io_ops.cc",
+        "tensorflow/core/ops/nn_ops.cc",
+        "tensorflow/core/ops/string_ops.cc",
         "tensorflow/core/lib/wav/wav_io.cc",
         "tensorflow/compiler/jit/defs.cc",
     ],
     export_include_dirs: ["."],
-    static_libs: [
-        "tensorflow_abseil",
-    ],
     whole_static_libs: [
         "libdoubleconversion",
+        "tensorflow_abseil",
+        "libfft2d",
+        "libruy_static",
+        "libtextclassifier_hash_static",
         "tensorflow_core_proto_cpp_lite",
     ],
+    header_libs: [
+        "fp16_headers",
+        "jni_headers",
+        "liblog_headers",
+    ],
+    generated_headers: ["libtflite_mutable_schema"],
     shared_libs: [
         "libz",
     ],
-    header_libs: [
-        "libtextclassifier_hash_headers",
-        "libeigen",
-        "tensorflow_headers",
+    include_dirs: [
+        "external/FP16",
+        "external/eigen",
+        "external/flatbuffers/include",
+        "external/gemmlowp",
+        "external/libtextclassifier/native/",
+        "external/tensorflow",
+        "external/neon_2_sse",
     ],
     cflags: [
         "-DNAMESPACE_FOR_HASH_FUNCTIONS=farmhash",
@@ -180,6 +204,9 @@
         "-DENABLE_ABSL_IN_TENSORFLOW",
         "-DIS_MOBILE_PLATFORM",
         "-DTF_ANDROID_ENABLE_LOG_EVERY_N_SECONDS",
+        // Used to support int64, string type in //tensorflow/core/framework/register_types.h.
+        "-D__ANDROID_TYPES_FULL__",
+        "-Wno-defaulted-function-deleted",
         "-Wno-deprecated-builtins",
         "-Wno-deprecated-declarations",
         "-Wno-expansion-to-defined",
@@ -189,8 +216,14 @@
         "-Wno-unused-const-variable",
         "-Wno-unused-but-set-variable",
         "-Wno-unused-parameter",
+        "-Wno-user-defined-warnings",
+        "-Wno-gnu-inline-cpp-without-extern",
     ],
+    cpp_std: "gnu++17",
     stl: "libc++_static",
+    min_sdk_version: "33",
+    sdk_version: "current",
+    apex_available: ["com.android.ondevicepersonalization"],
 }
 
 cc_library_static {
@@ -250,7 +283,7 @@
     ],
 )
 
-cc_library {
+cc_library_static {
     name: "tensorflow_core_proto_cpp_lite",
     proto: {
         export_proto_headers: true,
@@ -271,9 +304,16 @@
         ":libprotobuf-internal-protos",
     ],
     shared_libs: [
-        "libprotobuf-cpp-lite",
+        "liblog",
     ],
+    static_libs: [
+        "libprotobuf-cpp-lite-ndk",
+    ],
+    sdk_version: "current",
     export_include_dirs: ["."],
+    stl: "libc++_static",
+    min_sdk_version: "33",
+    apex_available: ["com.android.ondevicepersonalization"],
 }
 
 java_library_static {
diff --git a/tensorflow/core/data/Android.bp b/tensorflow/core/data/Android.bp
new file mode 100644
index 0000000..c10e64f
--- /dev/null
+++ b/tensorflow/core/data/Android.bp
@@ -0,0 +1,39 @@
+// Copyright (C) 2023 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.
+
+package {
+    // See: http://go/android-license-faq
+    // A large-scale-change added 'default_applicable_licenses' to import
+    // all of the 'license_kinds' from "external_tensorflow_license"
+    // to get the below license kinds:
+    //   SPDX-license-identifier-Apache-2.0
+    default_applicable_licenses: ["external_tensorflow_license"],
+}
+
+filegroup(
+    name = "tensorflow_core_data_mobile_srcs_no_runtime",
+    srcs = [
+        "name_utils.cc",
+        "captured_function.cc",
+        "dataset_utils.cc",
+        "stats_utils.cc",
+        "metric_utils.cc",
+        "finalization_utils.cc",
+        "serialization_utils.cc",
+        "unbounded_thread_pool.cc",
+        "root_dataset.cc",
+        "rewrite_utils.cc",
+        "utils.cc",
+    ],
+)
\ No newline at end of file
diff --git a/tensorflow/core/data/dataset_utils.cc b/tensorflow/core/data/dataset_utils.cc
index 4ed4d4e..1d36192 100644
--- a/tensorflow/core/data/dataset_utils.cc
+++ b/tensorflow/core/data/dataset_utils.cc
@@ -42,7 +42,6 @@
 #include "tensorflow/core/lib/hash/hash.h"
 #include "tensorflow/core/lib/strings/proto_serialization.h"
 #include "tensorflow/core/platform/host_info.h"
-#include "tensorflow/core/platform/regexp.h"
 #include "tensorflow/core/util/determinism.h"
 #include "tensorflow/core/util/work_sharder.h"
 
diff --git a/tensorflow/core/kernels/Android.bp b/tensorflow/core/kernels/Android.bp
index 886cd39..062f03f 100644
--- a/tensorflow/core/kernels/Android.bp
+++ b/tensorflow/core/kernels/Android.bp
@@ -25,7 +25,35 @@
     name = "tensorflow_core_kernels_mobile_srcs",
     srcs = [
         "initializable_lookup_table.cc",
+        "checkpoint_callback_manager.cc",
+        "constant_op.cc",
         "lookup_util.cc",
+        "function_ops.cc",
+        "fill_functor.cc",
+        "example_parsing_ops.cc",
+        "no_op.cc",
         "pooling_ops_common.cc",
+        "save_restore_v2_ops.cc",
+        "save_restore_tensor.cc",
+        "save_op.cc",
+        "split_op.cc",
+        "split_lib_cpu.cc",
+        "string_to_hash_bucket_fast_op.cc",
+        "as_string_op.cc",
+        "tensor_to_hash_bucket_op.cc",
+        "bcast_ops.cc",
+        "shape_ops.cc",
+        "concat_op.cc",
+        "concat_lib_cpu.cc",
+        "relu_op.cc",
+        "data/take_dataset_op.cc",
+        "data/map_dataset_op.cc",
+        "data/reduce_dataset_op.cc",
+        "data/iterator_ops.cc",
+        "data/prefetch_dataset_op.cc",
+        "data/prefetch_autotuner.cc",
+        "data/optional_ops.cc",
+        "data/optional_ops_util.cc",
+        "data/batch_dataset_op.cc",
     ],
 )
diff --git a/tensorflow/core/kernels/checkpoint_callback_manager.cc b/tensorflow/core/kernels/checkpoint_callback_manager.cc
index fb94c19..0e0fae0 100644
--- a/tensorflow/core/kernels/checkpoint_callback_manager.cc
+++ b/tensorflow/core/kernels/checkpoint_callback_manager.cc
@@ -14,6 +14,7 @@
 ==============================================================================*/
 #include "tensorflow/core/kernels/checkpoint_callback_manager.h"
 
+#include <regex>
 #include <string>
 #include <utility>
 
@@ -24,7 +25,8 @@
 #include "tensorflow/core/platform/errors.h"
 #include "tensorflow/core/platform/mutex.h"
 #include "tensorflow/core/platform/path.h"
-#include "tensorflow/core/platform/regexp.h"
+// Remove RE2 usage
+// #include "tensorflow/core/platform/regexp.h"
 #include "tensorflow/core/platform/status.h"
 #include "tensorflow/core/platform/statusor.h"
 #include "tensorflow/core/platform/stringpiece.h"
@@ -38,9 +40,9 @@
 
 namespace {
 
-const absl::string_view kCheckpointFileRegex = "^part-[0-9]*-of-[0-9]*$";
-const absl::string_view kCheckpointTempDirRegex = "-[0-9]*_temp$";
-const absl::string_view kCheckpointDirRegex = "-[0-9]*$";
+const char* kCheckpointFileRegex = "^part-[0-9]*-of-[0-9]*$";
+const char* kCheckpointTempDirRegex = "-[0-9]*_temp$";
+const char* kCheckpointDirRegex = "-[0-9]*$";
 const absl::string_view kCheckpointTempDirSuffix = "_temp";
 
 void TriggerSaveCallbackIfFileNotExist(absl::string_view checkpoint_id,
@@ -115,17 +117,26 @@
 CheckpointCallbackManager::GetCheckpointIdAndPathFromPrefix(
     absl::string_view prefix) {
   for (absl::string_view path = prefix;; path = io::Dirname(path)) {
-    absl::string_view basename = io::Basename(path);
+    std::string basename = std::string(io::Basename(path));
 
     // Failed to find checkpoint_id
     if (basename.empty()) break;
 
     // Skip known checkpoint file: e.g., part-00000-of-00001
-    if (RE2::PartialMatch(basename, kCheckpointFileRegex)) continue;
+    // if (RE2::PartialMatch(basename, kCheckpointFileRegex)) continue;
+    std::regex checkpoint_file_regex(kCheckpointFileRegex);
+    if (std::regex_search(basename, checkpoint_file_regex)) continue;
 
     // With _temp suffix: e.g., checkpoint-1_temp
-    if (RE2::PartialMatch(basename, kCheckpointTempDirRegex)) {
-      // Trim suffix, "_temp".
+    // if (RE2::PartialMatch(basename, kCheckpointTempDirRegex)) {
+    //   // Trim suffix, "_temp".
+    //   return std::make_pair(
+    //       std::string(basename.substr(
+    //           0, basename.length() - kCheckpointTempDirSuffix.length())),
+    //       std::string(io::Dirname(path)));
+    // }
+    std::regex checkpoint_temp_dir_regex(kCheckpointTempDirRegex);
+    if (std::regex_search(basename, checkpoint_temp_dir_regex)) {
       return std::make_pair(
           std::string(basename.substr(
               0, basename.length() - kCheckpointTempDirSuffix.length())),
@@ -133,7 +144,12 @@
     }
 
     // Without _temp suffix: e.g., checkpoint-1
-    if (RE2::PartialMatch(basename, kCheckpointDirRegex)) {
+    // if (RE2::PartialMatch(basename, kCheckpointDirRegex)) {
+    //   return std::make_pair(std::string(basename),
+    //                         std::string(io::Dirname(path)));
+    // }
+    std::regex checkpoint_dir_regex(kCheckpointDirRegex);
+    if (std::regex_search(basename, checkpoint_dir_regex)) {
       return std::make_pair(std::string(basename),
                             std::string(io::Dirname(path)));
     }
diff --git a/tensorflow/core/kernels/constant_op.cc b/tensorflow/core/kernels/constant_op.cc
index 74fe21d..8917f92 100644
--- a/tensorflow/core/kernels/constant_op.cc
+++ b/tensorflow/core/kernels/constant_op.cc
@@ -24,7 +24,6 @@
 
 #include "tensorflow/core/kernels/constant_op.h"
 
-#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
 #include "tensorflow/core/framework/allocator.h"
 #include "tensorflow/core/framework/bounds_check.h"
 #include "tensorflow/core/framework/node_def.pb.h"
diff --git a/tensorflow/core/platform/env.cc b/tensorflow/core/platform/env.cc
index 9961fff..93b9037 100644
--- a/tensorflow/core/platform/env.cc
+++ b/tensorflow/core/platform/env.cc
@@ -327,6 +327,12 @@
   return fs->HasAtomicMove(path, has_atomic_move);
 }
 
+Status Env::CanCreateTempFile(const string& fname, bool* can_create_temp_file) {
+  FileSystem* fs;
+  TF_RETURN_IF_ERROR(GetFileSystemForFile(fname, &fs));
+  return fs->CanCreateTempFile(fname, can_create_temp_file);
+}
+
 Status Env::DeleteRecursively(const string& dirname, int64_t* undeleted_files,
                               int64_t* undeleted_dirs) {
   FileSystem* fs;
diff --git a/tensorflow/core/platform/env.h b/tensorflow/core/platform/env.h
index 86b1077..67b45c6 100644
--- a/tensorflow/core/platform/env.h
+++ b/tensorflow/core/platform/env.h
@@ -333,6 +333,15 @@
   ///  TF
   Status HasAtomicMove(const std::string& path, bool* has_atomic_move);
 
+  /// Returns whether the give path is on a file system
+  /// that has ability to create a new temp file. This can be used
+  /// to determine if there needs to be a temp location to safely write objects.
+  /// If this returns false, TensorFlow will write directly to output files
+  /// instead of creating a temporary file and swapping it in. This may mean
+  /// that incomplete writes are visible to consumers.
+  Status CanCreateTempFile(const std::string& fname,
+                           bool* can_create_temp_file);
+
   /// Stores the size of `fname` in `*file_size`.
   Status GetFileSize(const std::string& fname, uint64* file_size);
 
diff --git a/tensorflow/core/platform/file_system.cc b/tensorflow/core/platform/file_system.cc
index e170b09..5847e76 100644
--- a/tensorflow/core/platform/file_system.cc
+++ b/tensorflow/core/platform/file_system.cc
@@ -87,6 +87,12 @@
   return OkStatus();
 }
 
+Status FileSystem::CanCreateTempFile(const std::string& fname,
+                                     bool* can_create_temp_file) {
+  *can_create_temp_file = true;
+  return OkStatus();
+}
+
 void FileSystem::FlushCaches(TransactionToken* token) {}
 
 bool FileSystem::FilesExist(const std::vector<string>& files,
diff --git a/tensorflow/core/platform/file_system.h b/tensorflow/core/platform/file_system.h
index 3b74a47..be0266d 100644
--- a/tensorflow/core/platform/file_system.h
+++ b/tensorflow/core/platform/file_system.h
@@ -386,6 +386,15 @@
   ///  TF
   virtual Status HasAtomicMove(const std::string& path, bool* has_atomic_move);
 
+  /// Returns whether the give path is on a file system
+  /// that has ability to create a new temp file. This can be used
+  /// to determine if there needs to be a temp location to safely write objects.
+  /// If this returns false, TensorFlow will write directly to output files
+  /// instead of creating a temporary file and swapping it in. This may mean
+  /// that incomplete writes are visible to consumers.
+  virtual Status CanCreateTempFile(const std::string& fname,
+                                   bool* can_create_temp_file);
+
   /// \brief Flushes any cached filesystem objects from memory.
   virtual void FlushCaches() { FlushCaches(nullptr); }
 
diff --git a/tensorflow/core/platform/file_system_test.cc b/tensorflow/core/platform/file_system_test.cc
index f5ca57b..d49a252 100644
--- a/tensorflow/core/platform/file_system_test.cc
+++ b/tensorflow/core/platform/file_system_test.cc
@@ -273,6 +273,14 @@
   EXPECT_EQ(has_atomic_move, true);
 }
 
+TEST(InterPlanetaryFileSystemTest, CanCreateTempFile) {
+  InterPlanetaryFileSystem ipfs;
+  const string dirname = io::JoinPath(kPrefix, "match-00/abc/00");
+  bool can_create_temp_file;
+  TF_EXPECT_OK(ipfs.CanCreateTempFile(dirname, &can_create_temp_file));
+  EXPECT_EQ(can_create_temp_file, true);
+}
+
 // A simple file system with a root directory and a single file underneath it.
 class TestFileSystem : public NullFileSystem {
  public:
diff --git a/tensorflow/core/platform/retrying_file_system.h b/tensorflow/core/platform/retrying_file_system.h
index 1543345..ec739fb 100644
--- a/tensorflow/core/platform/retrying_file_system.h
+++ b/tensorflow/core/platform/retrying_file_system.h
@@ -144,6 +144,12 @@
     return base_file_system_->HasAtomicMove(path, has_atomic_move);
   }
 
+  Status CanCreateTempFile(const std::string& fname,
+                           bool* can_create_temp_file) override {
+    // this method does not need to be retried
+    return base_file_system_->CanCreateTempFile(fname, can_create_temp_file);
+  }
+
   Status DeleteRecursively(const string& dirname, TransactionToken* token,
                            int64_t* undeleted_files,
                            int64_t* undeleted_dirs) override {
diff --git a/tensorflow/core/platform/test.h b/tensorflow/core/platform/test.h
index b598b6e..e49f479 100644
--- a/tensorflow/core/platform/test.h
+++ b/tensorflow/core/platform/test.h
@@ -16,10 +16,11 @@
 #ifndef TENSORFLOW_CORE_PLATFORM_TEST_H_
 #define TENSORFLOW_CORE_PLATFORM_TEST_H_
 
+#include <gtest/gtest.h>  // IWYU pragma: export
+
 #include <memory>
 #include <vector>
 
-#include <gtest/gtest.h>  // IWYU pragma: export
 #include "tensorflow/core/platform/macros.h"
 #include "tensorflow/core/platform/platform.h"
 #include "tensorflow/core/platform/types.h"
@@ -39,7 +40,8 @@
 // The advantages of using gmock matchers instead of self defined matchers are
 // better error messages, more maintainable tests and more test coverage.
 #if !defined(PLATFORM_GOOGLE) && !defined(PLATFORM_GOOGLE_ANDROID) && \
-    !defined(PLATFORM_CHROMIUMOS)
+    !defined(PLATFORM_CHROMIUMOS) &&                                  \
+    !defined(IS_MOBILE_PLATFORM)  // We have gmock.h in AOSP.
 #include <gmock/gmock-generated-matchers.h>
 #include <gmock/gmock-matchers.h>
 #include <gmock/gmock-more-matchers.h>
diff --git a/tensorflow/core/util/Android.bp b/tensorflow/core/util/Android.bp
index 87395ef..e9466a4 100644
--- a/tensorflow/core/util/Android.bp
+++ b/tensorflow/core/util/Android.bp
@@ -63,6 +63,6 @@
         "sparse/sparse_tensor.cc",
         "tensor_bundle/naming.cc",
         "tensor_bundle/tensor_bundle.cc",
-
+        "tensor_bundle/byte_swap.cc",
     ],
 )
diff --git a/tensorflow/core/util/tensor_slice_writer.cc b/tensorflow/core/util/tensor_slice_writer.cc
index 75197a5..0880b65 100644
--- a/tensorflow/core/util/tensor_slice_writer.cc
+++ b/tensorflow/core/util/tensor_slice_writer.cc
@@ -85,8 +85,17 @@
                                      CreateBuilderFunction create_builder)
     : filename_(filename),
       create_builder_(std::move(create_builder)),
-      tmpname_(strings::StrCat(filename, ".tempstate", random::New64())),
       slices_(0) {
+  Env* env = Env::Default();
+  Status status = env->CanCreateTempFile(filename_, &use_temp_file_);
+  if (!status.ok()) {
+    LOG(ERROR) << "Failed to get CanCreateTempFile attribute: " << filename_;
+    use_temp_file_ = true;
+  }
+  data_filename_ = filename_;
+  if (use_temp_file_) {
+    data_filename_ = strings::StrCat(filename_, ".tempstate", random::New64());
+  }
   VersionDef* versions = sts_.mutable_meta()->mutable_versions();
   versions->set_producer(TF_CHECKPOINT_VERSION);
   versions->set_min_consumer(TF_CHECKPOINT_VERSION_MIN_CONSUMER);
@@ -94,7 +103,7 @@
 
 Status TensorSliceWriter::Finish() {
   Builder* b;
-  Status s = create_builder_(tmpname_, &b);
+  Status s = create_builder_(data_filename_, &b);
   if (!s.ok()) {
     delete b;
     return s;
@@ -113,18 +122,21 @@
 
   int64_t file_size;
   s = builder->Finish(&file_size);
-  // We need to rename the file to the proper name
-  if (s.ok()) {
-    s = Env::Default()->RenameFile(tmpname_, filename_);
+  // If use temp file, we need to rename the file to the proper name.
+  if (use_temp_file_) {
     if (s.ok()) {
-      VLOG(1) << "Written " << slices_ << " slices for "
-              << sts_.meta().tensor_size() << " tensors (" << file_size
-              << " bytes) to " << filename_;
+      s = Env::Default()->RenameFile(data_filename_, filename_);
+      if (s.ok()) {
+        VLOG(1) << "Written " << slices_ << " slices for "
+                << sts_.meta().tensor_size() << " tensors (" << file_size
+                << " bytes) to " << filename_;
+      } else {
+        LOG(ERROR) << "Failed to rename file " << data_filename_ << " to "
+                   << filename_;
+      }
     } else {
-      LOG(ERROR) << "Failed to rename file " << tmpname_ << " to " << filename_;
+      Env::Default()->DeleteFile(data_filename_).IgnoreError();
     }
-  } else {
-    Env::Default()->DeleteFile(tmpname_).IgnoreError();
   }
   return s;
 }
diff --git a/tensorflow/core/util/tensor_slice_writer.h b/tensorflow/core/util/tensor_slice_writer.h
index 34aa7c0..887db9d 100644
--- a/tensorflow/core/util/tensor_slice_writer.h
+++ b/tensorflow/core/util/tensor_slice_writer.h
@@ -83,7 +83,8 @@
 
   const string filename_;
   const CreateBuilderFunction create_builder_;
-  const string tmpname_;
+  string data_filename_;
+  bool use_temp_file_;
 
   // A mapping from the tensor names to their index in meta_.saved_slice_meta()
   std::unordered_map<string, int> name_to_index_;
diff --git a/tensorflow/lite/Android.bp b/tensorflow/lite/Android.bp
index 759fede..f7a28b1 100644
--- a/tensorflow/lite/Android.bp
+++ b/tensorflow/lite/Android.bp
@@ -155,6 +155,47 @@
     stl: "libc++_static",
 }
 
+cc_library_shared {
+    name: "libtensorflowlite_jni",
+    sdk_version: "current",
+    min_sdk_version: "30",
+    export_include_dirs: [
+        "delegates/nnapi/java/src/main/native/",
+        "java/src/main/native/",
+    ],
+    srcs: [
+        "delegates/nnapi/java/src/main/native/*.cc",
+        "java/src/main/native/*.cc",
+    ],
+    header_libs: [
+        "flatbuffer_headers",
+        "jni_headers",
+        "tensorflow_headers",
+    ],
+    static_libs: [
+        "libruy_static",
+        "libtflite_static",
+    ],
+    shared_libs: [
+        "liblog",
+    ],
+    cflags: [
+        "-Wno-unused-function",
+        "-Wno-unused-parameter",
+    ],
+    ldflags: [
+        // Allow (benign) missing symbols referenced in the version script.
+        "-Wl,--undefined-version",
+    ],
+    version_script: "java/tflite_version_script.lds",
+    stl: "libc++_static",
+    apex_available: [
+        "//apex_available:platform",
+        "com.android.adservices",
+        "com.android.extservices",
+    ],
+}
+
 cc_library_static {
     name: "tflite_mutable_op_resolver",
     defaults: ["tflite_defaults"],
diff --git a/tensorflow/lite/delegates/Android.bp b/tensorflow/lite/delegates/Android.bp
index 7e3465f..8409b67 100644
--- a/tensorflow/lite/delegates/Android.bp
+++ b/tensorflow/lite/delegates/Android.bp
@@ -21,37 +21,16 @@
     default_applicable_licenses: ["external_tensorflow_license"],
 }
 
-cc_library_static {
-    name: "libtflite_flex_delegate_only_runtime",
-    //defaults: ["tflite_defaults"],
-    srcs: [
+filegroup(
+    name = "tflite_flex_delegate",
+    srcs = [
         "flex/buffer_map.cc",
         "flex/buffer_map_util.cc",
         "flex/delegate.cc",
         "flex/delegate_data.cc",
         "flex/kernel.cc",
         "flex/util.cc",
+        "flex/tflite_subgraph_execute.cc",
         "utils/simple_delegate.cc",
     ],
-    static_libs: [
-        "tensorflow_abseil",
-        "tensorflow_core_proto_cpp_lite",
-        "libtensorflow_core_mobile_srcs_only_runtime",
-    ],
-    header_libs: [
-        "flatbuffer_headers",
-        "libeigen",
-        "libtextclassifier_hash_headers",
-        "tensorflow_headers",
-    ],
-    cflags: [
-        "-DNAMESPACE_FOR_HASH_FUNCTIONS=farmhash",
-        "-Wno-defaulted-function-deleted",
-        "-Wno-unused-const-variable",
-        "-Wno-unused-but-set-variable",
-        "-Wno-unused-parameter",
-        "-Wno-missing-field-initializers",
-        "-Wno-ignored-qualifiers",
-        "-Wno-mismatched-tags",
-    ],
-}
\ No newline at end of file
+)
diff --git a/tensorflow/lite/java/Android.bp b/tensorflow/lite/java/Android.bp
index 33a7c37..5be051b 100644
--- a/tensorflow/lite/java/Android.bp
+++ b/tensorflow/lite/java/Android.bp
@@ -41,42 +41,3 @@
         "com.android.extservices",
     ],
 }
-
-cc_library_shared {
-    name: "libtensorflowlite_jni",
-    sdk_version: "current",
-    min_sdk_version: "30",
-    export_include_dirs: [
-        "src/main/native/",
-    ],
-    srcs: [
-        "src/main/native/*.cc",
-    ],
-    header_libs: [
-        "flatbuffer_headers",
-        "jni_headers",
-        "tensorflow_headers",
-    ],
-    static_libs: [
-        "libruy_static",
-        "libtflite_static",
-    ],
-    shared_libs: [
-        "liblog",
-    ],
-    cflags: [
-        "-Wno-unused-function",
-        "-Wno-unused-parameter",
-    ],
-    ldflags: [
-        // Allow (benign) missing symbols referenced in the version script.
-        "-Wl,--undefined-version",
-    ],
-    version_script: "tflite_version_script.lds",
-    stl: "libc++_static",
-    apex_available: [
-        "//apex_available:platform",
-        "com.android.adservices",
-        "com.android.extservices",
-    ],
-}
diff --git a/tensorflow/lite/tflite_static.bp b/tensorflow/lite/tflite_static.bp
index 959273c..f70104f 100644
--- a/tensorflow/lite/tflite_static.bp
+++ b/tensorflow/lite/tflite_static.bp
@@ -13,13 +13,9 @@
 // limitations under the License.
 
 // Create a static version for apps to use.
-cc_library_static {
-    name: "libtflite_static",
-    host_supported: true,
-    vendor_available: true,
-    sdk_version: "current",
-    min_sdk_version: "30",
-    srcs: [
+filegroup(
+    name = "tflite_src",
+    srcs = [
         ":tensorflow_tsl_logging_srcs",
         "allocation.cc",
         "arena_planner.cc",
@@ -200,6 +196,17 @@
         "tools/versioning/op_version.cc",
         "util.cc",
     ],
+)
+
+cc_library_static {
+    name: "libtflite_static",
+    host_supported: true,
+    vendor_available: true,
+    sdk_version: "current",
+    min_sdk_version: "30",
+    srcs: [
+        ":tflite_src",
+    ],
     header_libs: [
         "fp16_headers",
         "jni_headers",
diff --git a/tensorflow/tsl/platform/default/Android.bp b/tensorflow/tsl/platform/default/Android.bp
index 4ca9fca..9faf6ae 100644
--- a/tensorflow/tsl/platform/default/Android.bp
+++ b/tensorflow/tsl/platform/default/Android.bp
@@ -25,12 +25,10 @@
     name = "tensorflow_tsl_platform_default_mobile_srcs_no_runtime",
     srcs = [
         "env.cc",
-        "env_time.cc",
         "load_library.cc",
         "port.cc",
         "posix_file_system.cc",
         "resource.cc",
-        "logging.cc",
     ],
 )
 
diff --git a/third_party/absl/abseil-cpp/Android.bp b/third_party/absl/abseil-cpp/Android.bp
index 8c474af..c45a840 100644
--- a/third_party/absl/abseil-cpp/Android.bp
+++ b/third_party/absl/abseil-cpp/Android.bp
@@ -30,25 +30,53 @@
     ],
 }
 
-cc_library_static {
-    name: "tensorflow_abseil",
-    export_include_dirs: ["."],
-    visibility: [
-        "//external/tensorflow:__subpackages__",
-    ],
+filegroup {
+    name: "absl_srcs",
     srcs: [
         "absl/**/*.cc",
     ],
-    sdk_version: "current",
-    min_sdk_version: "30",
-    stl: "libc++_static",
     exclude_srcs: [
+        "absl/random/**/*.cc",
         "**/*_test.cc",
         "**/*_test_common.cc",
         "**/*test_util.cc",
         "**/*test_utils.cc",
         "**/*benchmark.cc",
-        "absl/random/benchmarks.cc",
         "absl/hash/internal/print_hash_of.cc",
     ],
 }
+
+// Only include necessary random files here. If include "absl/random/*.cc", we
+// will get "duplicate symbol: main" error because both
+// gaussian_distribution_gentables.cc and randen_benchmarks.cc have main
+// function.
+filegroup {
+    name: "absl_random",
+    srcs: [
+        "absl/random/discrete_distribution.cc",
+        "absl/random/gaussian_distribution.cc",
+        "absl/random/seed_sequences.cc",
+        "absl/random/internal/pool_urbg.cc",
+        "absl/random/internal/seed_material.cc",
+    ],
+}
+
+cc_library_static {
+    name: "tensorflow_abseil",
+    export_include_dirs: ["."],
+    visibility: [
+        "//external/tensorflow:__subpackages__",
+        "//external/federated-compute:__subpackages__",
+        "//packages/modules/OnDevicePersonalization:__subpackages__",
+    ],
+    apex_available: [
+        "com.android.ondevicepersonalization",
+    ],
+    srcs: [
+        ":absl_srcs",
+        ":absl_random",
+    ],
+    sdk_version: "current",
+    min_sdk_version: "30",
+    stl: "libc++_static",
+}