Revert "Merge libdexfile_external into libdexfile."

Revert "libdexfile_external is replaced by libdexfile."

Revert "Rename libdexfile_external_static to libdexfile_static."

Revert "Rename libdexfile_external_static to libdexfile_static."

Revert "Allow dependencies from platform variants to APEX modules."

Revert submission 1658000

Reason for revert: Breaks full-eng build: b/184239856
Reverted Changes:
I4f8ead785:Avoid internal APEX stubs for libsigchain and clea...
I68affdf69:Allow dependencies from platform variants to APEX ...
I54b33784e:Rename libdexfile_external_static to libdexfile_st...
Id68ae9438:libdexfile_external is being replaced by libdexfil...
I12ac84eb4:libdexfile_external is replaced by libdexfile.
If05dbffc8:Rename libdexfile_external_static to libdexfile_st...
Ia011fa3a8:Merge libdexfile_external into libdexfile.

Change-Id: I9064dd8debe2f385782c8cc6c1830e4cc5cb7221
diff --git a/CleanSpec.mk b/CleanSpec.mk
index 3e1f5be..2e6bf1e 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -111,10 +111,6 @@
 # Remove symbols/apex/com.android.art symlink (b/171406631)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/symbols/apex/com.android.art)
 
-# art/tools/build_linux_bionic_tests.sh uses find here and can encounter
-# libdexfile_external.so in incremental builds.
-$(call add-clean-step, rm -rf $(HOST_OUT))
-
 # ************************************************
 # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
 # ************************************************
diff --git a/build/apex/Android.bp b/build/apex/Android.bp
index 0b07395..4e9dca8 100644
--- a/build/apex/Android.bp
+++ b/build/apex/Android.bp
@@ -50,7 +50,7 @@
 // the ART APEX.
 art_runtime_base_native_shared_libs = [
     // External API (having APEX stubs).
-    "libdexfile",
+    "libdexfile_external",
     "libnativebridge",
     "libnativehelper",
     "libnativeloader",
@@ -104,7 +104,7 @@
     "libadbconnectiond",
     "libartd",
     "libartd-compiler",
-    "libdexfiled",
+    "libdexfiled_external",
     "libopenjdkjvmd",
     "libopenjdkjvmtid",
 ]
diff --git a/build/apex/art_apex_test.py b/build/apex/art_apex_test.py
index 30f8e1f..91af3db 100755
--- a/build/apex/art_apex_test.py
+++ b/build/apex/art_apex_test.py
@@ -527,7 +527,7 @@
     self._checker.check_symlinked_multilib_executable('dalvikvm')
 
     # Check exported libraries for ART.
-    self._checker.check_native_library('libdexfile')
+    self._checker.check_native_library('libdexfile_external')
     self._checker.check_native_library('libnativebridge')
     self._checker.check_native_library('libnativehelper')
     self._checker.check_native_library('libnativeloader')
@@ -542,6 +542,7 @@
     self._checker.check_native_library('libartpalette')
     self._checker.check_native_library('libartservice')
     self._checker.check_native_library('libarttools')
+    self._checker.check_native_library('libdexfile')
     self._checker.check_native_library('libdexfile_support')
     self._checker.check_native_library('libdt_fd_forward')
     self._checker.check_native_library('libopenjdkjvm')
@@ -676,9 +677,6 @@
     self._checker.check_symlinked_multilib_executable('imgdiagd')
     self._checker.check_executable('profmand')
 
-    # Check exported libraries for ART.
-    self._checker.check_native_library('libdexfiled')
-
     # Check internal libraries for ART.
     self._checker.check_native_library('libadbconnectiond')
     self._checker.check_native_library('libartbased')
@@ -686,6 +684,7 @@
     self._checker.check_native_library('libartd-compiler')
     self._checker.check_native_library('libartd-dexlayout')
     self._checker.check_native_library('libartd-disassembler')
+    self._checker.check_native_library('libdexfiled')
     self._checker.check_native_library('libopenjdkjvmd')
     self._checker.check_native_library('libopenjdkjvmtid')
     self._checker.check_native_library('libprofiled')
@@ -708,6 +707,7 @@
     self._checker.check_executable('oatdumpd')
 
     # Check ART internal libraries.
+    self._checker.check_native_library('libdexfiled_external')
     self._checker.check_native_library('libperfetto_hprofd')
 
     # Check internal native library dependencies.
diff --git a/build/sdk/Android.bp b/build/sdk/Android.bp
index 2dadee0..386663f 100644
--- a/build/sdk/Android.bp
+++ b/build/sdk/Android.bp
@@ -65,7 +65,7 @@
 
             native_shared_libs: [
                 "libandroidio",
-                "libdexfile",
+                "libdexfile_external",
                 "libnativebridge",
                 "libnativehelper",
                 "libnativeloader",
@@ -77,7 +77,7 @@
             ],
 
             native_static_libs: [
-                "libdexfile_static",
+                "libdexfile_external_static",
                 "libnativehelper_lazy",
             ],
         },
diff --git a/libdexfile/Android.bp b/libdexfile/Android.bp
index 8c7114e..8aa287f 100644
--- a/libdexfile/Android.bp
+++ b/libdexfile/Android.bp
@@ -45,16 +45,9 @@
         "dex/standard_dex_file.cc",
         "dex/type_lookup_table.cc",
         "dex/utf.cc",
-        "external/dex_file_ext.cc",
     ],
-    header_libs: [
-        "jni_headers",
-        "libdexfile_external_headers",
-    ],
-    export_header_lib_headers: [
-        "jni_headers",
-        "libdexfile_external_headers",
-    ],
+    header_libs: ["jni_headers"],
+    export_header_lib_headers: ["jni_headers"],
     target: {
         android: {
             static_libs: [
@@ -195,11 +188,6 @@
         "com.android.art",
         "com.android.art.debug",
     ],
-    stubs: {
-        // TODO(b/143978909): Rename and move this file to this directory.
-        symbol_file: "external/libdexfile_external.map.txt",
-        versions: ["1"],
-    },
 }
 
 art_cc_library {
@@ -241,11 +229,6 @@
     apex_available: [
         "com.android.art.debug",
     ],
-    stubs: {
-        // TODO(b/143978909): Rename and move this file to this directory.
-        symbol_file: "external/libdexfile_external.map.txt",
-        versions: ["1"],
-    },
 }
 
 art_cc_test {
@@ -321,6 +304,69 @@
     min_sdk_version: "S",
 }
 
+cc_defaults {
+    name: "libdexfile_external-defaults",
+    host_supported: true,
+    srcs: [
+        "external/dex_file_ext.cc",
+    ],
+    header_libs: [
+        "jni_headers",
+        "libdexfile_external_headers",
+    ],
+    shared_libs: [
+        "libbase",
+    ],
+    stubs: {
+        symbol_file: "external/libdexfile_external.map.txt",
+        versions: ["1"],
+    },
+    export_header_lib_headers: [
+        "jni_headers",
+        "libdexfile_external_headers",
+    ],
+}
+
+art_cc_library {
+    name: "libdexfile_external",
+    defaults: [
+        "art_defaults",
+        "libdexfile_external-defaults",
+    ],
+    visibility: ["//visibility:public"],
+    target: {
+        darwin: {
+            enabled: true,
+        },
+    },
+    shared_libs: [
+        "libdexfile",
+    ],
+    apex_available: [
+        "com.android.art",
+        "com.android.art.debug",
+    ],
+}
+
+art_cc_library {
+    name: "libdexfiled_external",
+    defaults: [
+        "art_debug_defaults",
+        "libdexfile_external-defaults",
+    ],
+    target: {
+        darwin: {
+            enabled: true,
+        },
+    },
+    shared_libs: [
+        "libdexfiled",
+    ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
+}
+
 art_cc_test {
     name: "art_libdexfile_external_tests",
     host_supported: true,
@@ -348,7 +394,7 @@
     srcs: [
         "external/dex_file_supp.cc",
     ],
-    runtime_libs: ["libdexfile"],
+    runtime_libs: ["libdexfile_external"],
     shared_libs: [
         "liblog",
         "libbase",
@@ -380,7 +426,7 @@
     shared_libs: [
         "libartbase",
         "libbase",
-        "libdexfile",
+        "libdexfile_external",
         "libdexfile_support",
     ],
 }
@@ -402,7 +448,7 @@
         "libdexfile_static_defaults",
     ],
     whole_static_libs: [
-        "libdexfile",
+        "libdexfile_external",
         "libdexfile_support_static",
     ],
 }
@@ -413,8 +459,8 @@
         "libdexfiled_static_defaults",
     ],
     whole_static_libs: [
-        "libdexfiled",
         "libdexfile_support_static",
+        "libdexfiled_external",
     ],
 }
 
@@ -432,7 +478,7 @@
 // This is not allowed in any module that may end up in an APEX or platform
 // image, so visibility is restrictive.
 cc_library_static {
-    name: "libdexfile_static",
+    name: "libdexfile_external_static",
     host_supported: true,
     visibility: [
         // Required for simpleperf, libsimpleperf_record, and libsimpleperf_report
@@ -451,14 +497,14 @@
 }
 
 art_cc_test {
-    name: "art_libdexfile_static_tests",
+    name: "art_libdexfile_external_static_tests",
     host_supported: true,
     test_suites: ["general-tests"],
     srcs: [
         "external/dex_file_supp_test.cc",
     ],
     static_libs: [
-        "libdexfile_static",
+        "libdexfile_external_static",
     ],
     enabled: false,
     target: {
@@ -468,7 +514,7 @@
     },
     // TODO(b/181740144): Enable a ubsan check to create a dependency on
     // ubsan_minimal. It's needed to be able to link with the prebuilt
-    // libdexfile_static.a, which contains libziparchive.a, which is
+    // libdexfile_external_static.a, which contains libziparchive.a, which is
     // built with some ubsan checks
     // (https://cs.android.com/android/platform/superproject/+/master:system/libziparchive/Android.bp;l=47-59;drc=c7b498fdf2002194709e40ea58ce39f43684fc14)
     // that the SDK snapshots currently don't propagate properly for static
diff --git a/libdexfile/external/dex_file_supp.cc b/libdexfile/external/dex_file_supp.cc
index 6313afe..63dec1c 100644
--- a/libdexfile/external/dex_file_supp.cc
+++ b/libdexfile/external/dex_file_supp.cc
@@ -65,10 +65,10 @@
     // Check which version is already loaded to avoid loading both debug and
     // release builds. We might also be backtracing from separate process, in
     // which case neither is loaded.
-    const char* so_name = "libdexfiled.so";
+    const char* so_name = "libdexfiled_external.so";
     void* handle = dlopen(so_name, RTLD_NOLOAD | RTLD_NOW | RTLD_NODELETE);
     if (handle == nullptr) {
-      so_name = "libdexfile.so";
+      so_name = "libdexfile_external.so";
       handle = dlopen(so_name, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
     }
     if (handle == nullptr) {
diff --git a/libdexfile/external/libdexfile_external.map.txt b/libdexfile/external/libdexfile_external.map.txt
index 174a546..e9d12fb 100644
--- a/libdexfile/external/libdexfile_external.map.txt
+++ b/libdexfile/external/libdexfile_external.map.txt
@@ -1,4 +1,4 @@
-LIBDEXFILE_1 {
+LIBDEXFILE_EXTERNAL_1 {
   global:
     ExtDexFileClose;
     ExtDexFileGetAllMethodInfos;
diff --git a/runtime/Android.bp b/runtime/Android.bp
index ca2fb36..3de88d7 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -547,6 +547,8 @@
     shared_libs: [
         "libartbase",
         "libdexfile",
+        // We need to eagerly load it so libdexfile_support used from libunwindstack can find it.
+        "libdexfile_external",
         "libprofile",
     ],
     export_shared_lib_headers: [
@@ -579,6 +581,9 @@
     shared_libs: [
         "libartbased",
         "libdexfiled",
+        // We need to eagerly preload it, so that libunwindstack can find it.
+        // Otherwise, it would try to load the non-debug version with dlopen.
+        "libdexfiled_external",
         "libprofiled",
     ],
     export_shared_lib_headers: [
diff --git a/test/Android.bp b/test/Android.bp
index 33fa5d8..7df6ead 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -29,12 +29,6 @@
     name: "art_test_defaults",
     host_supported: true,
     target: {
-        android: {
-            test_for: [
-                "com.android.art",
-                "com.android.art.debug",
-            ],
-        },
         android_arm: {
             relative_install_path: "art/arm",
         },
@@ -1041,10 +1035,6 @@
             suffix: "64",
         },
     },
-    test_for: [
-        "com.android.art",
-        "com.android.art.debug",
-    ],
 }
 
 art_cc_test {
@@ -1061,10 +1051,6 @@
     ],
     test_suites: ["general-tests"],
     test_config: "art-gtests-target-chroot.xml",
-    test_for: [
-        "com.android.art",
-        "com.android.art.debug",
-    ],
 }
 
 filegroup {
diff --git a/tools/pylibdexfile.py b/tools/pylibdexfile.py
index 953cf0d..30c0b26 100644
--- a/tools/pylibdexfile.py
+++ b/tools/pylibdexfile.py
@@ -15,7 +15,7 @@
 # limitations under the License.
 
 #
-# This script can get info out of dexfiles using libdexfile.so external API
+# This script can get info out of dexfiles using libdexfile_external
 #
 
 from abc import ABC
@@ -24,8 +24,8 @@
 import functools
 import zipfile
 
-libdexfile = CDLL(
-    os.path.expandvars("$ANDROID_HOST_OUT/lib64/libdexfile.so"))
+libdexfile_external = CDLL(
+    os.path.expandvars("$ANDROID_HOST_OUT/lib64/libdexfile_external.so"))
 
 DexFileStr = c_void_p
 ExtDexFile = c_void_p
@@ -37,28 +37,28 @@
 
 
 AllMethodsCallback = CFUNCTYPE(c_int, POINTER(ExtMethodInfo), c_void_p)
-libdexfile.ExtDexFileOpenFromFd.argtypes = [
+libdexfile_external.ExtDexFileOpenFromFd.argtypes = [
     c_int, c_size_t, c_char_p,
     POINTER(DexFileStr),
     POINTER(ExtDexFile)
 ]
-libdexfile.ExtDexFileOpenFromFd.restype = c_int
-libdexfile.ExtDexFileOpenFromMemory.argtypes = [
+libdexfile_external.ExtDexFileOpenFromFd.restype = c_int
+libdexfile_external.ExtDexFileOpenFromMemory.argtypes = [
     c_void_p,
     POINTER(c_size_t), c_char_p,
     POINTER(DexFileStr),
     POINTER(ExtDexFile)
 ]
-libdexfile.ExtDexFileOpenFromMemory.restype = c_int
-libdexfile.ExtDexFileFree.argtypes = [ExtDexFile]
-libdexfile.ExtDexFileGetAllMethodInfos.argtypes = [
+libdexfile_external.ExtDexFileOpenFromMemory.restype = c_int
+libdexfile_external.ExtDexFileFree.argtypes = [ExtDexFile]
+libdexfile_external.ExtDexFileGetAllMethodInfos.argtypes = [
     ExtDexFile, c_int, AllMethodsCallback, c_void_p
 ]
-libdexfile.ExtDexFileGetString.argtypes = [
+libdexfile_external.ExtDexFileGetString.argtypes = [
     DexFileStr, POINTER(c_size_t)
 ]
-libdexfile.ExtDexFileGetString.restype = c_char_p
-libdexfile.ExtDexFileFreeString.argtypes = [DexFileStr]
+libdexfile_external.ExtDexFileGetString.restype = c_char_p
+libdexfile_external.ExtDexFileFreeString.argtypes = [DexFileStr]
 
 
 class DexClass(object):
@@ -105,9 +105,9 @@
   def __init__(self, mi):
     self.offset = mi.offset
     self.len = mi.len
-    self.name = libdexfile.ExtDexFileGetString(
+    self.name = libdexfile_external.ExtDexFileGetString(
         mi.name, byref(c_size_t(0))).decode("utf-8")
-    libdexfile.ExtDexFileFreeString(mi.name)
+    libdexfile_external.ExtDexFileFreeString(mi.name)
 
   def __repr__(self):
     return "(" + self.name + ")"
@@ -164,8 +164,8 @@
       meths.append(Method(info[0]))
       return 0
 
-    libdexfile.ExtDexFileGetAllMethodInfos(self.ext_dex_file_,
-                                           c_int(1), my_cb, c_void_p())
+    libdexfile_external.ExtDexFileGetAllMethodInfos(self.ext_dex_file_,
+                                                    c_int(1), my_cb, c_void_p())
     return meths
 
 
@@ -176,14 +176,14 @@
     super().__init__()
     res_fle_ptr = pointer(c_void_p())
     err_ptr = pointer(c_void_p())
-    res = libdexfile.ExtDexFileOpenFromFd(
+    res = libdexfile_external.ExtDexFileOpenFromFd(
         c_int(fd), 0, create_string_buffer(bytes(loc, "utf-8")), err_ptr,
         res_fle_ptr)
     if res == 0:
-      err = libdexfile.ExtDexFileGetString(err_ptr.contents,
-                                           byref(c_size_t()))
+      err = libdexfile_external.ExtDexFileGetString(err_ptr.contents,
+                                                    byref(c_size_t()))
       out = Exception("Failed to open file: {}. Error was: {}".format(loc, err))
-      libdexfile.ExtDexFileFreeString(err_ptr.contents)
+      libdexfile_external.ExtDexFileFreeString(err_ptr.contents)
       raise out
     self.ext_dex_file_ = res_fle_ptr.contents
 
@@ -209,14 +209,14 @@
     self.mem_ref = (c_byte * len(dat)).from_buffer_copy(dat)
     res_fle_ptr = pointer(c_void_p())
     err_ptr = pointer(c_void_p())
-    res = libdexfile.ExtDexFileOpenFromMemory(
+    res = libdexfile_external.ExtDexFileOpenFromMemory(
         self.mem_ref, byref(c_size_t(len(dat))),
         create_string_buffer(bytes(loc, "utf-8")), err_ptr, res_fle_ptr)
     if res == 0:
-      err = libdexfile.ExtDexFileGetString(err_ptr.contents,
-                                           byref(c_size_t()))
+      err = libdexfile_external.ExtDexFileGetString(err_ptr.contents,
+                                                    byref(c_size_t()))
       out = Exception("Failed to open file: {}. Error was: {}".format(loc, err))
-      libdexfile.ExtDexFileFreeString(err_ptr.contents)
+      libdexfile_external.ExtDexFileFreeString(err_ptr.contents)
       raise out
     self.ext_dex_file_ = res_fle_ptr.contents