Merge changes from topic "b/160390776"

* changes:
  Unset ENABLE_PREOPT when DISABLE_PREOPT is set
  Introduce TARGET_BUILD_UNBUNDLED_IMAGE
diff --git a/core/android_manifest.mk b/core/android_manifest.mk
index 8fab9c6..254e09b 100644
--- a/core/android_manifest.mk
+++ b/core/android_manifest.mk
@@ -45,7 +45,7 @@
 my_min_sdk_version := $(call module-min-sdk-version)
 
 ifdef TARGET_BUILD_APPS
-  ifndef TARGET_BUILD_APPS_USE_PREBUILT_SDK
+  ifndef TARGET_BUILD_USE_PREBUILT_SDKS
     ifeq ($(my_target_sdk_version),$(PLATFORM_VERSION_CODENAME))
       ifdef UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT
         my_target_sdk_version := $(my_target_sdk_version).$$(cat $(API_FINGERPRINT))
diff --git a/core/config.mk b/core/config.mk
index a35b718..1a0a418 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -517,18 +517,27 @@
 ifeq ($(TARGET_BUILD_PDK),true)
 ALLOW_MISSING_DEPENDENCIES := true
 endif
+ifeq ($(TARGET_BUILD_UNBUNDLED_IMAGE),true)
+ALLOW_MISSING_DEPENDENCIES := true
+endif
 ifneq ($(filter true,$(SOONG_ALLOW_MISSING_DEPENDENCIES)),)
 ALLOW_MISSING_DEPENDENCIES := true
 endif
 .KATI_READONLY := ALLOW_MISSING_DEPENDENCIES
 
-TARGET_BUILD_APPS_USE_PREBUILT_SDK :=
-ifdef TARGET_BUILD_APPS
+TARGET_BUILD_USE_PREBUILT_SDKS :=
+DISABLE_PREOPT :=
+ifneq (,$(TARGET_BUILD_APPS)$(TARGET_BUILD_UNBUNDLED_IMAGE))
+  DISABLE_PREOPT := true
   ifndef UNBUNDLED_BUILD_SDKS_FROM_SOURCE
-    TARGET_BUILD_APPS_USE_PREBUILT_SDK := true
+    TARGET_BUILD_USE_PREBUILT_SDKS := true
   endif
 endif
 
+.KATI_READONLY := \
+  TARGET_BUILD_USE_PREBUILT_SDKS \
+  DISABLE_PREOPT \
+
 prebuilt_sdk_tools := prebuilts/sdk/tools
 prebuilt_sdk_tools_bin := $(prebuilt_sdk_tools)/$(HOST_OS)/bin
 
@@ -550,18 +559,18 @@
 .KATI_READONLY := USE_D8
 
 #
-# Tools that are prebuilts for TARGET_BUILD_APPS
+# Tools that are prebuilts for TARGET_BUILD_USE_PREBUILT_SDKS
 #
-ifeq (,$(TARGET_BUILD_APPS)$(filter true,$(TARGET_BUILD_PDK)))
+ifeq (,$(TARGET_BUILD_USE_PREBUILT_SDKS)$(filter true,$(TARGET_BUILD_PDK)))
   AAPT := $(HOST_OUT_EXECUTABLES)/aapt
   MAINDEXCLASSES := $(HOST_OUT_EXECUTABLES)/mainDexClasses
 
-else # TARGET_BUILD_APPS || TARGET_BUILD_PDK
+else # TARGET_BUILD_USE_PREBUILT_SDKS || TARGET_BUILD_PDK
   AAPT := $(prebuilt_sdk_tools_bin)/aapt
   MAINDEXCLASSES := $(prebuilt_sdk_tools)/mainDexClasses
-endif # TARGET_BUILD_APPS || TARGET_BUILD_PDK
+endif # TARGET_BUILD_USE_PREBUILT_SDKS || TARGET_BUILD_PDK
 
-ifeq (,$(TARGET_BUILD_APPS))
+ifeq (,$(TARGET_BUILD_USE_PREBUILT_SDKS))
   # Use RenderScript prebuilts for unbundled builds but not PDK builds
   LLVM_RS_CC := $(HOST_OUT_EXECUTABLES)/llvm-rs-cc
   BCC_COMPAT := $(HOST_OUT_EXECUTABLES)/bcc_compat
@@ -1109,7 +1118,7 @@
 HISTORICAL_NDK_VERSIONS_ROOT := $(TOPDIR)prebuilts/ndk
 
 # The path where app can reference the support library resources.
-ifdef TARGET_BUILD_APPS
+ifdef TARGET_BUILD_USE_PREBUILT_SDKS
 SUPPORT_LIBRARY_ROOT := $(HISTORICAL_SDK_VERSIONS_ROOT)/current/support
 else
 SUPPORT_LIBRARY_ROOT := frameworks/support
diff --git a/core/dex_preopt_config.mk b/core/dex_preopt_config.mk
index 55f6f0b..41a2be9 100644
--- a/core/dex_preopt_config.mk
+++ b/core/dex_preopt_config.mk
@@ -5,7 +5,7 @@
   ENABLE_PREOPT :=
 else ifneq (true,$(filter true,$(PRODUCT_USES_DEFAULT_ART_CONFIG)))
   ENABLE_PREOPT :=
-else ifneq (,$(TARGET_BUILD_APPS))
+else ifeq (true,$(DISABLE_PREOPT))
   ENABLE_PREOPT :=
 endif
 
diff --git a/core/dex_preopt_odex_install.mk b/core/dex_preopt_odex_install.mk
index 3d5f68a..799b623 100644
--- a/core/dex_preopt_odex_install.mk
+++ b/core/dex_preopt_odex_install.mk
@@ -40,8 +40,8 @@
   LOCAL_DEX_PREOPT :=
 endif
 
-# Disable preopt for TARGET_BUILD_APPS
-ifneq (,$(TARGET_BUILD_APPS))
+# Disable preopt for DISABLE_PREOPT
+ifeq (true,$(DISABLE_PREOPT))
   LOCAL_DEX_PREOPT :=
 endif
 
diff --git a/core/envsetup.mk b/core/envsetup.mk
index f78ecb4..167fed9 100644
--- a/core/envsetup.mk
+++ b/core/envsetup.mk
@@ -93,6 +93,7 @@
 endif
 
 TARGET_BUILD_APPS ?=
+TARGET_BUILD_UNBUNDLED_IMAGE ?=
 
 # Set to true for an unbundled build, i.e. a build without
 # support for platform targets like the system image. This also
@@ -107,11 +108,19 @@
   TARGET_BUILD_UNBUNDLED := true
 endif
 
+# TARGET_BUILD_UNBUNDLED_IMAGE also implies unbundled build.
+# (i.e. it targets to only unbundled image, such as the vendor image,
+# ,or the product image). 
+ifneq ($(TARGET_BUILD_UNBUNDLED_IMAGE),)
+  TARGET_BUILD_UNBUNDLED := true
+endif
+
 .KATI_READONLY := \
   TARGET_PRODUCT \
   TARGET_BUILD_VARIANT \
   TARGET_BUILD_APPS \
   TARGET_BUILD_UNBUNDLED \
+  TARGET_BUILD_UNBUNDLED_IMAGE \
 
 # ---------------------------------------------------------------
 # Set up configuration for host machine.  We don't do cross-
diff --git a/core/java.mk b/core/java.mk
index 2f18ad9..bb4593d 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -106,8 +106,8 @@
 
 aidl_preprocess_import :=
 ifdef LOCAL_SDK_VERSION
-ifneq ($(filter current system_current test_current core_current, $(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS_USE_PREBUILT_SDK)),)
-  # LOCAL_SDK_VERSION is current and no TARGET_BUILD_APPS
+ifneq ($(filter current system_current test_current core_current, $(LOCAL_SDK_VERSION)$(TARGET_BUILD_USE_PREBUILT_SDKS)),)
+  # LOCAL_SDK_VERSION is current and no TARGET_BUILD_USE_PREBUILT_SDKS
   aidl_preprocess_import := $(FRAMEWORK_AIDL)
 else
   aidl_preprocess_import := $(call resolve-prebuilt-sdk-aidl-path,$(LOCAL_SDK_VERSION))
diff --git a/core/java_common.mk b/core/java_common.mk
index b7f2883..1798ca8 100644
--- a/core/java_common.mk
+++ b/core/java_common.mk
@@ -25,7 +25,7 @@
     LOCAL_JAVA_LANGUAGE_VERSION := 1.7
   else ifneq (,$(filter $(LOCAL_SDK_VERSION), $(TARGET_SDK_VERSIONS_WITHOUT_JAVA_19_SUPPORT)))
     LOCAL_JAVA_LANGUAGE_VERSION := 1.8
-  else ifneq (,$(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS_USE_PREBUILT_SDK))
+  else ifneq (,$(LOCAL_SDK_VERSION)$(TARGET_BUILD_USE_PREBUILT_SDKS))
     # TODO(ccross): allow 1.9 for current and unbundled once we have SDK system modules
     LOCAL_JAVA_LANGUAGE_VERSION := 1.8
   else
@@ -268,7 +268,7 @@
       my_system_modules := $(LEGACY_CORE_PLATFORM_SYSTEM_MODULES)
     endif  # LOCAL_NO_STANDARD_LIBRARIES
 
-    ifneq (,$(TARGET_BUILD_APPS_USE_PREBUILT_SDK))
+    ifneq (,$(TARGET_BUILD_USE_PREBUILT_SDKS))
       sdk_libs := $(foreach lib_name,$(LOCAL_SDK_LIBRARIES),$(call resolve-prebuilt-sdk-module,system_current,$(lib_name)))
     else
       # When SDK libraries are referenced from modules built without SDK, provide the all APIs to them
@@ -283,8 +283,8 @@
              Choices are: $(TARGET_AVAILABLE_SDK_VERSIONS))
     endif
 
-    ifneq (,$(TARGET_BUILD_APPS_USE_PREBUILT_SDK)$(filter-out %current,$(LOCAL_SDK_VERSION)))
-      # TARGET_BUILD_APPS mode or numbered SDK. Use prebuilt modules.
+    ifneq (,$(TARGET_BUILD_USE_PREBUILT_SDKS)$(filter-out %current,$(LOCAL_SDK_VERSION)))
+      # TARGET_BUILD_USE_PREBUILT_SDKS mode or numbered SDK. Use prebuilt modules.
       sdk_module := $(call resolve-prebuilt-sdk-module,$(LOCAL_SDK_VERSION))
       sdk_libs := $(foreach lib_name,$(LOCAL_SDK_LIBRARIES),$(call resolve-prebuilt-sdk-module,$(LOCAL_SDK_VERSION),$(lib_name)))
     else
@@ -325,7 +325,7 @@
   # related classes to be present. This change adds stubs needed for
   # javac to compile lambdas.
   ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-    ifdef TARGET_BUILD_APPS_USE_PREBUILT_SDK
+    ifdef TARGET_BUILD_USE_PREBUILT_SDKS
       full_java_bootclasspath_libs += $(call java-lib-header-files,sdk-core-lambda-stubs)
     else
       full_java_bootclasspath_libs += $(call java-lib-header-files,core-lambda-stubs)
diff --git a/core/java_prebuilt_internal.mk b/core/java_prebuilt_internal.mk
index 6ee1ae1..279b0e4 100644
--- a/core/java_prebuilt_internal.mk
+++ b/core/java_prebuilt_internal.mk
@@ -173,7 +173,7 @@
 framework_res_package_export :=
 # Please refer to package.mk
 ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_APPS),$(filter current system_current test_current,$(LOCAL_SDK_RES_VERSION))),)
+ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_USE_PREBUILT_SDKS),$(filter current system_current test_current,$(LOCAL_SDK_RES_VERSION))),)
 framework_res_package_export := \
     $(call resolve-prebuilt-sdk-jar-path,$(LOCAL_SDK_RES_VERSION))
 else
diff --git a/core/java_renderscript.mk b/core/java_renderscript.mk
index bfcf59e..572d6e4 100644
--- a/core/java_renderscript.mk
+++ b/core/java_renderscript.mk
@@ -50,8 +50,8 @@
 renderscript_flags += $(LOCAL_RENDERSCRIPT_FLAGS)
 
 # prepend the RenderScript system include path
-ifneq ($(filter-out current system_current test_current core_current,$(LOCAL_SDK_VERSION))$(if $(TARGET_BUILD_APPS),$(filter current system_current test_current,$(LOCAL_SDK_VERSION))),)
-# if a numeric LOCAL_SDK_VERSION, or current LOCAL_SDK_VERSION with TARGET_BUILD_APPS
+ifneq ($(filter-out current system_current test_current core_current,$(LOCAL_SDK_VERSION))$(if $(TARGET_BUILD_USE_PREBUILT_SDKS),$(filter current system_current test_current,$(LOCAL_SDK_VERSION))),)
+# if a numeric LOCAL_SDK_VERSION, or current LOCAL_SDK_VERSION with TARGET_BUILD_USE_PREBUILT_SDKS
 LOCAL_RENDERSCRIPT_INCLUDES := \
     $(HISTORICAL_SDK_VERSIONS_ROOT)/renderscript/clang-include \
     $(HISTORICAL_SDK_VERSIONS_ROOT)/renderscript/include \
@@ -110,7 +110,7 @@
 rs_jni_lib := $(call intermediates-dir-for,SHARED_LIBRARIES,librsjni.so)/librsjni.so
 LOCAL_JNI_SHARED_LIBRARIES += librsjni
 
-ifneq (,$(TARGET_BUILD_APPS)$(FORCE_BUILD_RS_COMPAT))
+ifneq (,$(TARGET_BUILD_USE_PREBUILT_SDKS)$(FORCE_BUILD_RS_COMPAT))
 
 rs_compatibility_jni_libs := $(addprefix \
     $(renderscript_intermediate)/librs., \
diff --git a/core/package_internal.mk b/core/package_internal.mk
index 775ee48..b058891 100644
--- a/core/package_internal.mk
+++ b/core/package_internal.mk
@@ -401,7 +401,7 @@
 # resources.
 ifeq ($(LOCAL_SDK_RES_VERSION),core_current)
 # core_current doesn't contain any framework resources.
-else ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_APPS_USE_PREBUILT_SDK),$(filter current system_current test_current,$(LOCAL_SDK_RES_VERSION))),)
+else ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_USE_PREBUILT_SDKS),$(filter current system_current test_current,$(LOCAL_SDK_RES_VERSION))),)
 # for released sdk versions, the platform resources were built into android.jar.
 framework_res_package_export := \
     $(call resolve-prebuilt-sdk-jar-path,$(LOCAL_SDK_RES_VERSION))
diff --git a/core/soong_config.mk b/core/soong_config.mk
index 01b355e..9c4b509 100644
--- a/core/soong_config.mk
+++ b/core/soong_config.mk
@@ -41,7 +41,7 @@
 $(call add_json_bool, Allow_missing_dependencies,        $(ALLOW_MISSING_DEPENDENCIES))
 $(call add_json_bool, Unbundled_build,                   $(TARGET_BUILD_UNBUNDLED))
 $(call add_json_bool, Unbundled_build_apps,              $(TARGET_BUILD_APPS))
-$(call add_json_bool, Unbundled_build_sdks_from_source,  $(UNBUNDLED_BUILD_SDKS_FROM_SOURCE))
+$(call add_json_bool, Always_use_prebuilt_sdks,          $(TARGET_BUILD_USE_PREBUILT_SDKS))
 $(call add_json_bool, Pdk,                               $(filter true,$(TARGET_BUILD_PDK)))
 
 $(call add_json_bool, Debuggable,                        $(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
diff --git a/core/static_java_library.mk b/core/static_java_library.mk
index 81dc2df..7a87322 100644
--- a/core/static_java_library.mk
+++ b/core/static_java_library.mk
@@ -111,7 +111,7 @@
 framework_res_package_export :=
 # Please refer to package.mk
 ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_APPS_USE_PREBUILT_SDK),$(filter current system_current test_current,$(LOCAL_SDK_RES_VERSION))),)
+ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_USE_PREBUILT_SDKS),$(filter current system_current test_current,$(LOCAL_SDK_RES_VERSION))),)
 framework_res_package_export := \
     $(call resolve-prebuilt-sdk-jar-path,$(LOCAL_SDK_RES_VERSION))
 else