Use apex_available property

Use apex_available property to prevent modules that are only for the ART
apex from being used outside of the APEX.

This change also fixes the reference to
$(TARGET_OUT_SHARED_LIBRARIES)/$(lib).so to $(lib).com.android.art.debug
phony module since the former is never installed to the system partition
due to their 'apex_available' settings.

Bug: 128708192
Bug: 133140750
Test: m
Change-Id: I990e0a67e066d9b1dc221748422a2f44449258da
diff --git a/Android.mk b/Android.mk
index 36e5b90..467490f 100644
--- a/Android.mk
+++ b/Android.mk
@@ -407,16 +407,16 @@
 
 # Base requirements.
 LOCAL_REQUIRED_MODULES := \
-    dalvikvm \
-    dex2oat \
-    dexoptanalyzer \
-    libart \
-    libart-compiler \
-    libopenjdkjvm \
-    libopenjdkjvmti \
-    profman \
-    libadbconnection \
-    libperfetto_hprof \
+    dalvikvm.com.android.art.release \
+    dex2oat.com.android.art.release \
+    dexoptanalyzer.com.android.art.release \
+    libart.com.android.art.release \
+    libart-compiler.com.android.art.release \
+    libopenjdkjvm.com.android.art.release \
+    libopenjdkjvmti.com.android.art.release \
+    profman.com.android.art.release \
+    libadbconnection.com.android.art.release \
+    libperfetto_hprof.com.android.art.release \
 
 # Potentially add in debug variants:
 #
@@ -430,16 +430,16 @@
 endif
 ifeq (true,$(art_target_include_debug_build))
 LOCAL_REQUIRED_MODULES += \
-    dex2oatd \
-    dexoptanalyzerd \
-    libartd \
-    libartd-compiler \
-    libopenjdkd \
-    libopenjdkjvmd \
-    libopenjdkjvmtid \
-    profmand \
-    libadbconnectiond \
-    libperfetto_hprofd \
+    dex2oatd.com.android.art.debug \
+    dexoptanalyzerd.com.android.art.debug \
+    libartd.com.android.art.debug \
+    libartd-compiler.com.android.art.debug \
+    libopenjdkd.com.android.art.debug \
+    libopenjdkjvmd.com.android.art.debug \
+    libopenjdkjvmtid.com.android.art.debug \
+    profmand.com.android.art.debug \
+    libadbconnectiond.com.android.art.debug \
+    libperfetto_hprofd.com.android.art.debug \
 
 endif
 endif
@@ -625,7 +625,7 @@
                         $(TARGET_OUT_EXECUTABLES)/art \
                         $(TARGET_OUT)/etc/public.libraries.txt \
                         $(ART_TARGET_DEX_DEPENDENCIES) \
-                        $(ART_TARGET_SHARED_LIBRARY_DEPENDENCIES) \
+                        $(ART_DEBUG_TARGET_SHARED_LIBRARY_DEPENDENCIES) \
                         $(ART_TARGET_SHARED_LIBRARY_BENCHMARK) \
                         $(TARGET_CORE_IMG_OUT_BASE).art \
                         $(TARGET_CORE_IMG_OUT_BASE)-interpreter.art \
diff --git a/adbconnection/Android.bp b/adbconnection/Android.bp
index 239cecb..b2503e1 100644
--- a/adbconnection/Android.bp
+++ b/adbconnection/Android.bp
@@ -53,6 +53,10 @@
         "libart",
         "libartbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 // We export a library to do the server-side socket handling that gets loaded
@@ -91,4 +95,7 @@
         "libartd",
         "libartbased",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
diff --git a/build/Android.common_path.mk b/build/Android.common_path.mk
index df35bd8..4cc408e 100644
--- a/build/Android.common_path.mk
+++ b/build/Android.common_path.mk
@@ -111,10 +111,11 @@
 ART_HOST_SHARED_LIBRARY_DEBUG_DEPENDENCIES += $(foreach lib,$(ART_CORE_SHARED_DEBUG_LIBRARIES), $(2ND_HOST_OUT_SHARED_LIBRARIES)/$(lib).so)
 endif
 
-ART_TARGET_SHARED_LIBRARY_DEPENDENCIES := $(foreach lib,$(ART_CORE_SHARED_LIBRARIES), $(TARGET_OUT_SHARED_LIBRARIES)/$(lib).so)
+# Both the primary and the secondary arches of the libs are built by depending
+# on the module name.
+ART_DEBUG_TARGET_SHARED_LIBRARY_DEPENDENCIES := $(foreach lib,$(ART_CORE_SHARED_LIBRARIES), $(lib).com.android.art.debug)
 ART_TARGET_SHARED_LIBRARY_DEBUG_DEPENDENCIES := $(foreach lib,$(ART_CORE_SHARED_DEBUG_LIBRARIES), $(TARGET_OUT_SHARED_LIBRARIES)/$(lib).so)
 ifdef TARGET_2ND_ARCH
-ART_TARGET_SHARED_LIBRARY_DEPENDENCIES += $(foreach lib,$(ART_CORE_SHARED_LIBRARIES), $(2ND_TARGET_OUT_SHARED_LIBRARIES)/$(lib).so)
 ART_TARGET_SHARED_LIBRARY_DEBUG_DEPENDENCIES += $(foreach lib,$(ART_CORE_SHARED_DEBUG_LIBRARIES), $(2ND_TARGET_OUT_SHARED_LIBRARIES)/$(lib).so)
 endif
 
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index c9f05b5..abc8f6f 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -500,13 +500,15 @@
 
   # Add the test dependencies to test-art-target-sync, which will be a prerequisite for the test
   # to ensure files are pushed to the device.
+  # Note that, both the primary and the secondary arches of the libs are built by depending
+  # on the module name.
   gtest_deps := \
     $$(ART_GTEST_$(1)_TARGET_DEPS) \
     $(foreach file,$(ART_GTEST_$(1)_DEX_DEPS),$(ART_TEST_TARGET_GTEST_$(file)_DEX)) \
     $$(gtest_exe) \
-    $$($(3)TARGET_OUT_SHARED_LIBRARIES)/libicu_jni.so \
-    $$($(3)TARGET_OUT_SHARED_LIBRARIES)/libjavacore.so \
-    $$($(3)TARGET_OUT_SHARED_LIBRARIES)/libopenjdkd.so \
+    libicu_jni.com.android.art.debug \
+    libjavacore.com.android.art.debug \
+    libopenjdkd.com.android.art.debug \
     $$(foreach jar,$$(TARGET_TEST_CORE_JARS),$$(TARGET_OUT_JAVA_LIBRARIES)/$$(jar).jar)
 
   ART_TEST_TARGET_GTEST_DEPENDENCIES += $$(gtest_deps)
diff --git a/compiler/Android.bp b/compiler/Android.bp
index dfd71f1..532f7d6 100644
--- a/compiler/Android.bp
+++ b/compiler/Android.bp
@@ -266,6 +266,10 @@
             },
         },
     },
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 cc_defaults {
@@ -330,6 +334,9 @@
         "libdexfiled",
     ],
     whole_static_libs: ["libelffiled"],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 cc_defaults {
diff --git a/dalvikvm/Android.bp b/dalvikvm/Android.bp
index 0688318..8cd8819 100644
--- a/dalvikvm/Android.bp
+++ b/dalvikvm/Android.bp
@@ -54,4 +54,8 @@
 
     // Create symlink for the primary version target.
     symlink_preferred_arch: true,
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
diff --git a/dex2oat/Android.bp b/dex2oat/Android.bp
index 14bca00..635a3d3 100644
--- a/dex2oat/Android.bp
+++ b/dex2oat/Android.bp
@@ -118,6 +118,9 @@
         "libartpalette",
         "libprofile",
     ],
+    apex_available: [
+        "com.android.art.release",
+    ],
 }
 
 cc_defaults {
@@ -147,6 +150,9 @@
         "libartpalette",
         "libprofiled",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 cc_defaults {
@@ -283,6 +289,10 @@
             ],
         },
     },
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_binary {
@@ -330,6 +340,9 @@
             suffix: "64",
         },
     },
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 cc_defaults {
diff --git a/dexdump/Android.bp b/dexdump/Android.bp
index f46b4de..0bbb001 100644
--- a/dexdump/Android.bp
+++ b/dexdump/Android.bp
@@ -52,6 +52,11 @@
             enabled: true,
         },
     },
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+        "//apex_available:platform", // for SDK
+    ],
 }
 
 art_cc_test {
diff --git a/dexlayout/Android.bp b/dexlayout/Android.bp
index 65bb92c..cd40620 100644
--- a/dexlayout/Android.bp
+++ b/dexlayout/Android.bp
@@ -94,6 +94,10 @@
             ],
         },
     },
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 cc_defaults {
@@ -136,6 +140,9 @@
             ],
         },
     },
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 cc_defaults {
@@ -168,6 +175,10 @@
         "libartbase",
         "libart-dexlayout",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_binary {
@@ -206,6 +217,9 @@
         "libartbased",
         "libartd-dexlayout",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_test {
@@ -238,6 +252,10 @@
             ],
         },
     },
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_test {
diff --git a/dexlist/Android.bp b/dexlist/Android.bp
index e1d53a2..860a5fd 100644
--- a/dexlist/Android.bp
+++ b/dexlist/Android.bp
@@ -22,6 +22,10 @@
         "libartbase",
         "libbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_binary {
diff --git a/dexoptanalyzer/Android.bp b/dexoptanalyzer/Android.bp
index 72896c8..7875dbb 100644
--- a/dexoptanalyzer/Android.bp
+++ b/dexoptanalyzer/Android.bp
@@ -41,6 +41,10 @@
         "libart",
         "libartbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_binary {
@@ -53,6 +57,9 @@
         "libartd",
         "libartbased",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_test {
diff --git a/disassembler/Android.bp b/disassembler/Android.bp
index 892d060..e841953 100644
--- a/disassembler/Android.bp
+++ b/disassembler/Android.bp
@@ -58,6 +58,10 @@
         // For disassembler_arm*.
         "libvixl",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_library {
diff --git a/dt_fd_forward/Android.bp b/dt_fd_forward/Android.bp
index 75e5c0d..e722a4d 100644
--- a/dt_fd_forward/Android.bp
+++ b/dt_fd_forward/Android.bp
@@ -48,6 +48,10 @@
 art_cc_library {
     name: "libdt_fd_forward",
     defaults: ["dt_fd_forward-defaults"],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_library {
diff --git a/imgdiag/Android.bp b/imgdiag/Android.bp
index 39720a0..7c2f515 100644
--- a/imgdiag/Android.bp
+++ b/imgdiag/Android.bp
@@ -57,6 +57,10 @@
         "libartbase",
         "libart-compiler",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_binary {
@@ -70,6 +74,9 @@
         "libartbased",
         "libartd-compiler",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_test {
diff --git a/libartbase/Android.bp b/libartbase/Android.bp
index 0728669..6802041 100644
--- a/libartbase/Android.bp
+++ b/libartbase/Android.bp
@@ -173,6 +173,13 @@
         // TODO(b/133140750): Clean this up.
         "//packages/modules/NetworkStack/tests:__subpackages__",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+        // TODO(b/142944931) Clean this up. This is due to the dependency to libdexfile_support_static
+        // from /system/core, etc.
+        "//apex_available:platform",
+    ],
 
     shared_libs: [
         "libbase",
@@ -195,6 +202,9 @@
         "art_debug_defaults",
         "libartbase_defaults",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
     shared_libs: [
         "libbase",
         "libziparchive",
diff --git a/libartpalette/Android.bp b/libartpalette/Android.bp
index f25501d..2fe4cc6 100644
--- a/libartpalette/Android.bp
+++ b/libartpalette/Android.bp
@@ -92,6 +92,13 @@
             ],
         },
     },
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+        // TODO(b/142944931) Clean this up. This is due to the dependency to libdexfile_support_static
+        // from /system/core, etc.
+        "//apex_available:platform",
+    ],
 }
 
 art_cc_test {
diff --git a/libdexfile/Android.bp b/libdexfile/Android.bp
index 2249d0a..e9ae603 100644
--- a/libdexfile/Android.bp
+++ b/libdexfile/Android.bp
@@ -174,6 +174,13 @@
             ],
         },
     },
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+        // TODO(b/142944931) Clean this up. This is due to the dependency to libdexfile_support_static
+        // from /system/core, etc.
+        "//apex_available:platform",
+    ],
 }
 
 art_cc_library {
@@ -212,6 +219,9 @@
             ],
         },
     },
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_test {
@@ -298,6 +308,13 @@
     shared_libs: [
         "libdexfile",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+        // TODO(b/142944931) Clean this up. This is due to the dependency to libdexfile_support_static
+        // from /system/core, etc.
+        "//apex_available:platform",
+    ],
 }
 
 cc_library {
@@ -314,6 +331,9 @@
     shared_libs: [
         "libdexfiled",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_test {
diff --git a/libelffile/Android.bp b/libelffile/Android.bp
index 092a429..3e6cbf0 100644
--- a/libelffile/Android.bp
+++ b/libelffile/Android.bp
@@ -39,6 +39,9 @@
     shared_libs: [
         "libartbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+    ],
 }
 
 art_cc_library_static {
@@ -50,4 +53,7 @@
     shared_libs: [
         "libartbased",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
diff --git a/libnativebridge/Android.bp b/libnativebridge/Android.bp
index f2db117b..b5dc324 100644
--- a/libnativebridge/Android.bp
+++ b/libnativebridge/Android.bp
@@ -23,6 +23,10 @@
         // TODO(b/133140750): Clean this up.
         "//frameworks/base/native/webview/loader/libwebviewchromium_loader",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 
     host_supported: true,
     srcs: ["native_bridge.cc"],
diff --git a/libnativeloader/Android.bp b/libnativeloader/Android.bp
index beb2c8c..12bba44 100644
--- a/libnativeloader/Android.bp
+++ b/libnativeloader/Android.bp
@@ -18,6 +18,10 @@
         // TODO(b/133140750): Clean this up.
         "//frameworks/base/native/webview/loader",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
     host_supported: true,
     srcs: [
         "native_loader.cpp",
diff --git a/libprofile/Android.bp b/libprofile/Android.bp
index 1742a97..367eefc 100644
--- a/libprofile/Android.bp
+++ b/libprofile/Android.bp
@@ -130,6 +130,10 @@
             ],
         },
     },
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_library {
@@ -163,6 +167,9 @@
         },
     },
     export_shared_lib_headers: ["libbase"],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 // For now many of these tests still use CommonRuntimeTest, almost universally because of
diff --git a/oatdump/Android.bp b/oatdump/Android.bp
index 64d49b4..e36d9d7 100644
--- a/oatdump/Android.bp
+++ b/oatdump/Android.bp
@@ -37,6 +37,10 @@
         "libprofile",
         "libbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_binary {
@@ -55,6 +59,9 @@
         "libprofiled",
         "libbase",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 cc_defaults {
diff --git a/openjdkjvm/Android.bp b/openjdkjvm/Android.bp
index a5350f9..ace8d0b 100644
--- a/openjdkjvm/Android.bp
+++ b/openjdkjvm/Android.bp
@@ -34,6 +34,10 @@
         "libart",
         "libartbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_library {
@@ -46,4 +50,7 @@
         "libartd",
         "libartbased",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
diff --git a/openjdkjvmti/Android.bp b/openjdkjvmti/Android.bp
index 37ae951..9ec9dea 100644
--- a/openjdkjvmti/Android.bp
+++ b/openjdkjvmti/Android.bp
@@ -76,6 +76,10 @@
         "libdexfile",
         "libartbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_library {
@@ -91,4 +95,7 @@
         "libdexfiled",
         "libartbased",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
diff --git a/perfetto_hprof/Android.bp b/perfetto_hprof/Android.bp
index a40e7d3..7627bcd 100644
--- a/perfetto_hprof/Android.bp
+++ b/perfetto_hprof/Android.bp
@@ -79,6 +79,10 @@
         "libart",
         "libartbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_library {
@@ -91,4 +95,7 @@
         "libartd",
         "libartbased",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
diff --git a/profman/Android.bp b/profman/Android.bp
index 099d555..c574fde 100644
--- a/profman/Android.bp
+++ b/profman/Android.bp
@@ -47,6 +47,10 @@
         "libdexfile",
         "libartbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_binary {
@@ -60,6 +64,9 @@
         "libdexfiled",
         "libartbased",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_binary {
diff --git a/runtime/Android.bp b/runtime/Android.bp
index e09d828..2aff77d 100644
--- a/runtime/Android.bp
+++ b/runtime/Android.bp
@@ -585,6 +585,10 @@
             },
         },
     },
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_library {
@@ -609,6 +613,9 @@
     export_shared_lib_headers: [
         "libdexfiled",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_library {
diff --git a/sigchainlib/Android.bp b/sigchainlib/Android.bp
index a94daf3..ac909a6 100644
--- a/sigchainlib/Android.bp
+++ b/sigchainlib/Android.bp
@@ -43,6 +43,13 @@
     },
 
     export_include_dirs: ["."],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+        // TODO(b/142944931) Clean this up. This is due to the dependency from
+        // app_process
+        "//apex_available:platform",
+    ],
 }
 
 // Create a dummy version of libsigchain which expose the necessary symbols
diff --git a/simulator/Android.bp b/simulator/Android.bp
index 223c891..1410444 100644
--- a/simulator/Android.bp
+++ b/simulator/Android.bp
@@ -97,4 +97,7 @@
         "libartbased",
         "libartd",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
diff --git a/test/Android.bp b/test/Android.bp
index 0051853..1f763b1 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -45,6 +45,9 @@
     cflags: [
         "-Wno-frame-larger-than=",
     ],
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_defaults {
@@ -143,6 +146,9 @@
             enabled: false,
         },
     },
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_library {
@@ -168,6 +174,9 @@
             enabled: false,
         },
     },
+    apex_available: [
+        "com.android.art.debug",
+    ],
 }
 
 // ART run-tests.
diff --git a/tools/cpp-define-generator/Android.bp b/tools/cpp-define-generator/Android.bp
index ca0c7a6..20634fe 100644
--- a/tools/cpp-define-generator/Android.bp
+++ b/tools/cpp-define-generator/Android.bp
@@ -31,6 +31,7 @@
     // Produce text file rather than binary.
     cflags: ["-S"],
     srcs: ["asm_defines.cc"],
+    apex_available: ["//apex_available:platform"],
 }
 
 // This extracts the compile-time constants from asm_defines.s and creates the header.
diff --git a/tools/dexanalyze/Android.bp b/tools/dexanalyze/Android.bp
index a232a1b..e7ae113 100644
--- a/tools/dexanalyze/Android.bp
+++ b/tools/dexanalyze/Android.bp
@@ -37,6 +37,10 @@
         "libartbase",
         "libbase",
     ],
+    apex_available: [
+        "com.android.art.release",
+        "com.android.art.debug",
+    ],
 }
 
 art_cc_test {