Add support for multiple LOCAL_COMPATIBILITY_SUITES & undefined testcase directories. am: 0dd1fb9112 am: a749d6bdb5 am: 5a8f82d4a1 am: 7b26faa5ea am: acaa4cc623  -s ours am: 203a1f407b  -s ours am: f1be8db559  -s ours
am: fd5769448d  -s ours

Change-Id: I01dcd7af15ae5b9a144c7615ddbcb722437d681d
diff --git a/Android.mk b/Android.mk
index a1455d4..5053e7d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -1,8 +1 @@
-LOCAL_PATH := $(call my-dir)
-
-# We're relocating the build project to a subdirectory, then using symlinks
-# to expose the subdirectories where they used to be. If the manifest hasn't
-# been updated, we need to include all the subdirectories.
-ifeq ($(LOCAL_PATH),build)
-include $(call first-makefiles-under,$(LOCAL_PATH))
-endif
+include $(call all-subdir-makefiles)
diff --git a/Changes.md b/Changes.md
new file mode 100644
index 0000000..53ff007
--- /dev/null
+++ b/Changes.md
@@ -0,0 +1,106 @@
+# Build System Changes for Android.mk Writers
+
+## Deprecating / obsoleting envsetup.sh variables in Makefiles
+
+It is not required to source envsetup.sh before running a build. Many scripts,
+including a majority of our automated build systems, do not do so. Make will
+transparently make every environment variable available as a make variable.
+This means that relying on environment variables only set up in envsetup.sh will
+produce different output for local users and scripted users.
+
+Many of these variables also include absolute path names, which we'd like to
+keep out of the generated files, so that you don't need to do a full rebuild if
+you move the source tree.
+
+To fix this, we're marking the variables that are set in envsetup.sh as
+deprecated in the makefiles. This will trigger a warning every time one is read
+(or written) inside Kati. Once all the warnings have been removed for a
+particular variable, we'll switch it to obsolete, and any references will become
+errors.
+
+### envsetup.sh variables with make equivalents
+
+| instead of                                                   | use                  |
+|--------------------------------------------------------------|----------------------|
+| OUT {#OUT}                                                   | OUT_DIR              |
+| ANDROID_HOST_OUT {#ANDROID_HOST_OUT}                         | HOST_OUT             |
+| ANDROID_PRODUCT_OUT {#ANDROID_PRODUCT_OUT}                   | PRODUCT_OUT          |
+| ANDROID_HOST_OUT_TESTCASES {#ANDROID_HOST_OUT_TESTCASES}     | HOST_OUT_TESTCASES   |
+| ANDROID_TARGET_OUT_TESTCASES {#ANDROID_TARGET_OUT_TESTCASES} | TARGET_OUT_TESTCASES |
+
+All of the make variables may be relative paths from the current directory, or
+absolute paths if the output directory was specified as an absolute path. If you
+need an absolute variable, convert it to absolute during a rule, so that it's
+not expanded into the generated ninja file:
+
+``` make
+$(PRODUCT_OUT)/gen.img: my/src/path/gen.sh
+	export PRODUCT_OUT=$$(cd $(PRODUCT_OUT); pwd); cd my/src/path; ./gen.sh -o $${PRODUCT_OUT}/gen.img
+```
+
+### ANDROID_BUILD_TOP  {#ANDROID_BUILD_TOP}
+
+In Android.mk files, you can always assume that the current directory is the
+root of the source tree, so this can just be replaced with '.' (which is what
+$TOP is hardcoded to), or removed entirely. If you need an absolute path, see
+the instructions above.
+
+### Stop using PATH directly  {#PATH}
+
+This isn't only set by envsetup.sh, but it is modified by it. Due to that it's
+rather easy for this to change between different shells, and it's not ideal to
+reread the makefiles every time this changes.
+
+In most cases, you shouldn't need to touch PATH at all. When you need to have a
+rule reference a particular binary that's part of the source tree or outputs,
+it's preferrable to just use the path to the file itself (since you should
+already be adding that as a dependency).
+
+Depending on the rule, passing the file path itself may not be feasible due to
+layers of unchangable scripts/binaries. In that case, be sure to add the
+dependency, but modify the PATH within the rule itself:
+
+``` make
+$(TARGET): myscript my/path/binary
+	PATH=my/path:$$PATH myscript -o $@
+```
+
+### Stop using PYTHONPATH directly  {#PYTHONPATH}
+
+Like PATH, this isn't only set by envsetup.sh, but it is modified by it. Due to
+that it's rather easy for this to change between different shells, and it's not
+ideal to reread the makefiles every time.
+
+The best solution here is to start switching to Soong's python building support,
+which packages the python interpreter, libraries, and script all into one file
+that no longer needs PYTHONPATH. See fontchain_lint for examples of this:
+
+* [external/fonttools/Lib/fontTools/Android.bp] for python_library_host
+* [frameworks/base/Android.bp] for python_binary_host
+* [frameworks/base/data/fonts/Android.mk] to execute the python binary
+
+If you still need to use PYTHONPATH, do so within the rule itself, just like
+path:
+
+``` make
+$(TARGET): myscript.py $(sort $(shell find my/python/lib -name '*.py'))
+	PYTHONPATH=my/python/lib:$$PYTHONPATH myscript.py -o $@
+```
+
+### Other envsetup.sh variables  {#other_envsetup_variables}
+
+* ANDROID_TOOLCHAIN
+* ANDROID_TOOLCHAIN_2ND_ARCH
+* ANDROID_DEV_SCRIPTS
+* ANDROID_EMULATOR_PREBUILTS
+* ANDROID_PRE_BUILD_PATHS
+
+These are all exported from envsetup.sh, but don't have clear equivalents within
+the makefile system. If you need one of them, you'll have to set up your own
+version.
+
+
+[build/soong/Changes.md]: https://android.googlesource.com/platform/build/soong/+/master/Changes.md
+[external/fonttools/Lib/fontTools/Android.bp]: https://android.googlesource.com/platform/external/fonttools/+/master/Lib/fontTools/Android.bp
+[frameworks/base/Android.bp]: https://android.googlesource.com/platform/frameworks/base/+/master/Android.bp
+[frameworks/base/data/fonts/Android.mk]: https://android.googlesource.com/platform/frameworks/base/+/master/data/fonts/Android.mk
diff --git a/CleanSpec.mk b/CleanSpec.mk
index fff622f..3ba1f45 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -424,10 +424,26 @@
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/vendor/nativetest*)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/vendor/nativetest*)
 
+# Jack is no longer the default compiler, remove the intermediates
+$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/*/*/classes*.jack)
+$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/*/*/jack*)
+
 # Move adbd from $(PRODUCT_OUT)/root/sbin to $(PRODUCT_OUT)/system/bin
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/root/sbin/adbd)
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/symbols/sbin/adbd)
 
+# Soong linux -> linux_glibc rename
+$(call add-clean-step, find $(SOONG_OUT_DIR)/.intermediates -name 'linux_x86*' | xargs rm -rf)
+$(call add-clean-step, find $(SOONG_OUT_DIR)/.intermediates -name 'linux_common*' | xargs rm -rf)
+
+# Remove old aidl/logtags files that may be in the generated source directory
+$(call add-clean-step, rm -rf $(OUT_DIR)/target/common/obj/*/*_intermediates/src)
+$(call add-clean-step, rm -f $(OUT_DIR)/target/common/obj/*/*_intermediates/java-source-list)
+$(call add-clean-step, rm -rf $(OUT_DIR)/host/common/obj/*/*_intermediates/src)
+$(call add-clean-step, rm -f $(OUT_DIR)/host/common/obj/*/*_intermediates/java-source-list)
+
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/APPS/*/flat-res)
+
 # ************************************************
 # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
 # ************************************************
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..47809a9
--- /dev/null
+++ b/README.md
@@ -0,0 +1,23 @@
+# Android Make Build System
+
+This is the Makefile-based portion of the Android Build System.
+
+For documentation on how to run a build, see [Usage.txt](Usage.txt)
+
+For a list of behavioral changes useful for Android.mk writers see
+[Changes.md](Changes.md)
+
+For an outdated reference on Android.mk files, see
+[build-system.html](/core/build-system.html). Our Android.mk files look similar,
+but are entirely different from the Android.mk files used by the NDK build
+system. When searching for documentation elsewhere, ensure that it is for the
+platform build system -- most are not.
+
+This Makefile-based system is in the process of being replaced with [Soong], a
+new build system written in Go. During the transition, all of these makefiles
+are read by [Kati], and generate a ninja file instead of being executed
+directly. That's combined with a ninja file read by Soong so that the build
+graph of the two systems can be combined and run as one.
+
+[Kati]: https://github.com/google/kati
+[Soong]: https://android.googlesource.com/platform/build/soong/+/master
diff --git a/Usage.txt b/Usage.txt
new file mode 100644
index 0000000..004210b
--- /dev/null
+++ b/Usage.txt
@@ -0,0 +1,85 @@
+Android build system usage:
+
+m [-j] [<targets>] [<variable>=<value>...]
+
+
+Ways to specify what to build:
+  The common way to specify what to build is to set that information in the
+  environment via:
+
+    # Set up the shell environment.
+    source build/envsetup.sh # Run "hmm" after sourcing for more info
+    # Select the device and variant to target. If no argument is given, it
+    # will list choices and prompt.
+    lunch [<product>-<variant>] # Selects the device and variant to target.
+    # Invoke the configured build.
+    m [<options>] [<targets>] [<variable>=<value>...]
+
+      <product> is the device that the created image is intended to be run on.
+        This is saved in the shell environment as $TARGET_PRODUCT by `lunch`.
+      <variant> is one of "user", "userdebug", or "eng", and controls the
+        amount of debugging to be added into the generated image.
+        This gets saved in the shell environment as $TARGET_BUILD_VARIANT by
+          `lunch`.
+
+    Each of <options>, <targets>, and <variable>=<value> is optional.
+      If no targets are specified, the build system will build the images
+      for the configured product and variant.
+
+  An alternative to setting $TARGET_PRODUCT and $TARGET_BUILD_VARIANT,
+  which you may see in build servers, is to execute:
+
+    make PRODUCT-<product>-<variant>
+
+
+  A target may be a file path. For example, out/host/linux-x86/bin/adb .
+    Note that when giving a relative file path as a target, that path is
+    interpreted relative to the root of the source tree (rather than relative
+    to the current working directory).
+
+  A target may also be any other target defined within a Makefile. Run
+    `m help` to view the names of some common targets.
+
+  To view the modules and targets defined in a particular directory, look for:
+    files named *.mk (most commonly Android.mk)
+      these files are defined in Make syntax
+    files named Android.bp
+      these files are defined in Blueprint syntax
+
+  For now, the full (extremely large) compiled list of targets can be found
+    (after running the build once), split among these two files:
+
+    ${OUT}/build-<product>*.ninja
+    ${OUT}/soong/build.ninja
+
+    If you find yourself interacting with these files, you are encouraged to
+    provide a more convenient tool for browsing targets, and to mention the
+    tool here.
+
+Targets that adjust an existing build:
+  showcommands              Display the individual commands run to implement
+                            the build
+  dist                      Copy into ${DIST_DIR} the portion of the build
+                            that must be distributed
+
+Flags
+  -j <N>                    Run <N> processes at once
+  -j                        Autodetect the number of processes to run at once,
+                            and run that many
+
+Variables
+  Variables can either be set in the surrounding shell environment or can be
+    passed as command-line arguments. For example:
+      export I_AM_A_SHELL_VAR=1
+      I_AM_ANOTHER_SHELL_VAR=2 make droid I_AM_A_MAKE_VAR=3
+  Here are some common variables and their meanings:
+    TARGET_PRODUCT          The <product> to build # as described above
+    TARGET_BUILD_VARIANT    The <variant> to build # as described above
+    DIST_DIR                The directory in which to place the distribution
+                            artifacts.
+    OUT_DIR                 The directory in which to place non-distribution
+                            artifacts.
+
+  There is not yet known a convenient method by which to discover the full
+  list of supported variables. Please mention it here when there is.
+
diff --git a/core/Makefile b/core/Makefile
index 618e983..521d78d 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -31,22 +31,46 @@
     $(if $(filter $(unique_product_copy_files_pairs),$(cf)),,\
         $(eval unique_product_copy_files_pairs += $(cf))))
 unique_product_copy_files_destinations :=
+product_copy_files_ignored :=
 $(foreach cf,$(unique_product_copy_files_pairs), \
     $(eval _src := $(call word-colon,1,$(cf))) \
     $(eval _dest := $(call word-colon,2,$(cf))) \
     $(call check-product-copy-files,$(cf)) \
     $(if $(filter $(unique_product_copy_files_destinations),$(_dest)), \
-        $(info PRODUCT_COPY_FILES $(cf) ignored.), \
+        $(eval product_copy_files_ignored += $(cf)), \
         $(eval _fulldest := $(call append-path,$(PRODUCT_OUT),$(_dest))) \
         $(if $(filter %.xml,$(_dest)),\
             $(eval $(call copy-xml-file-checked,$(_src),$(_fulldest))),\
-            $(eval $(call copy-one-file,$(_src),$(_fulldest)))) \
+            $(if $(and $(filter %.jar,$(_dest)),$(filter $(basename $(notdir $(_dest))),$(PRODUCT_LOADED_BY_PRIVILEGED_MODULES))),\
+                $(eval $(call copy-and-uncompress-dexs,$(_src),$(_fulldest))), \
+                $(eval $(call copy-one-file,$(_src),$(_fulldest))))) \
         $(eval ALL_DEFAULT_INSTALLED_MODULES += $(_fulldest)) \
         $(eval unique_product_copy_files_destinations += $(_dest))))
+
+# Dump a list of overriden (and ignored PRODUCT_COPY_FILES entries)
+$(file >$(PRODUCT_OUT)/product_copy_files_ignored.txt,$(subst $(space),$(newline),$(strip $(product_copy_files_ignored))))
+ifdef dist_goal
+$(file >$(DIST_DIR)/logs/product_copy_files_ignored.txt,$(subst $(space),$(newline),$(strip $(product_copy_files_ignored))))
+endif
+
+product_copy_files_ignored :=
 unique_product_copy_files_pairs :=
 unique_product_copy_files_destinations :=
 
 # -----------------------------------------------------------------
+# Returns the max allowed size for an image suitable for hash verification
+# (e.g., boot.img, recovery.img, etc).
+# The value 69632 derives from MAX_VBMETA_SIZE + MAX_FOOTER_SIZE in $(AVBTOOL).
+# $(1): partition size to flash the image
+define get-hash-image-max-size
+$(if $(1), \
+  $(if $(filter true,$(BOARD_AVB_ENABLE)), \
+    $(eval _hash_meta_size := 69632), \
+    $(eval _hash_meta_size := 0)) \
+  $(1)-$(_hash_meta_size))
+endef
+
+# -----------------------------------------------------------------
 # Define rules to copy headers defined in copy_headers.mk
 # If more than one makefile declared a header, print a warning,
 # then copy the last one defined. This matches the previous make
@@ -70,6 +94,27 @@
 	@cp -f $< $@
 endif
 
+ndk_doxygen_out := $(OUT_NDK_DOCS)
+ndk_headers := $(SOONG_OUT_DIR)/ndk/sysroot/usr/include
+ndk_docs_src_dir := frameworks/native/docs
+ndk_doxyfile := $(ndk_docs_src_dir)/Doxyfile
+ifneq ($(wildcard $(ndk_docs_src_dir)),)
+ndk_docs_srcs := $(addprefix $(ndk_docs_src_dir)/,\
+    $(call find-files-in-subdirs,$(ndk_docs_src_dir),"*",.))
+$(ndk_doxygen_out)/index.html: $(ndk_docs_srcs) $(SOONG_OUT_DIR)/ndk.timestamp
+	@mkdir -p $(ndk_doxygen_out)
+	@echo "Generating NDK docs to $(ndk_doxygen_out)"
+	@( cat $(ndk_doxyfile); \
+	    echo "INPUT=$(ndk_headers)"; \
+	    echo "HTML_OUTPUT=$(ndk_doxygen_out)" \
+	) | doxygen -
+
+# Note: Not a part of the docs target because we don't have doxygen available.
+# You can run this target locally if you have doxygen installed.
+ndk-docs: $(ndk_doxygen_out)/index.html
+.PHONY: ndk-docs
+endif
+
 # -----------------------------------------------------------------
 # property_overrides_split_enabled
 property_overrides_split_enabled :=
@@ -90,7 +135,8 @@
 endif
 ALL_DEFAULT_INSTALLED_MODULES += $(INSTALLED_DEFAULT_PROP_TARGET)
 FINAL_DEFAULT_PROPERTIES := \
-    $(call collapse-pairs, $(ADDITIONAL_DEFAULT_PROPERTIES))
+    $(call collapse-pairs, $(ADDITIONAL_DEFAULT_PROPERTIES)) \
+    $(call collapse-pairs, $(PRODUCT_SYSTEM_DEFAULT_PROPERTIES))
 ifndef property_overrides_split_enabled
   FINAL_DEFAULT_PROPERTIES += \
       $(call collapse-pairs, $(PRODUCT_DEFAULT_PROPERTY_OVERRIDES))
@@ -128,6 +174,11 @@
 INSTALLED_VENDOR_DEFAULT_PROP_TARGET := $(TARGET_OUT_VENDOR)/default.prop
 ALL_DEFAULT_INSTALLED_MODULES += $(INSTALLED_VENDOR_DEFAULT_PROP_TARGET)
 
+ifdef BOARD_VNDK_VERSION
+FINAL_VENDOR_DEFAULT_PROPERTIES := ro.vndk.version=${BOARD_VNDK_VERSION}
+else
+FINAL_VENDOR_DEFAULT_PROPERTIES :=
+endif
 FINAL_VENDOR_DEFAULT_PROPERTIES += \
     $(call collapse-pairs, $(PRODUCT_DEFAULT_PROPERTY_OVERRIDES))
 FINAL_VENDOR_DEFAULT_PROPERTIES := $(call uniq-pairs-by-first-component, \
@@ -180,9 +231,7 @@
 # The string used to uniquely identify the combined build and product; used by the OTA server.
 ifeq (,$(strip $(BUILD_FINGERPRINT)))
   ifneq ($(filter eng.%,$(BUILD_NUMBER)),)
-    # Trim down BUILD_FINGERPRINT: the default BUILD_NUMBER makes it easily exceed
-    # the Android system property length limit (PROPERTY_VALUE_MAX=92).
-    BF_BUILD_NUMBER := $(shell echo $${USER:0:6})$(shell $(DATE) +%m%d%H%M)
+    BF_BUILD_NUMBER := $(USER)$(shell $(DATE) +%m%d%H%M)
   else
     BF_BUILD_NUMBER := $(BUILD_NUMBER)
   endif
@@ -243,7 +292,7 @@
 # if it isn't already a part of the flavor (via a dedicated lunch
 # config for example).
 TARGET_BUILD_FLAVOR := $(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)
-ifdef SANITIZE_TARGET
+ifneq (, $(filter address, $(SANITIZE_TARGET)))
 ifeq (,$(findstring _asan,$(TARGET_BUILD_FLAVOR)))
 TARGET_BUILD_FLAVOR := $(TARGET_BUILD_FLAVOR)_asan
 endif
@@ -254,7 +303,7 @@
 else
 system_prop_file := $(wildcard $(TARGET_DEVICE_DIR)/system.prop)
 endif
-$(intermediate_system_build_prop): $(VENDOR_BUILDINFO_SH) $(BUILDINFO_SH) $(INTERNAL_BUILD_ID_MAKEFILE) $(BUILD_SYSTEM)/version_defaults.mk $(system_prop_file) $(INSTALLED_ANDROID_INFO_TXT_TARGET)
+$(intermediate_system_build_prop): $(BUILDINFO_SH) $(INTERNAL_BUILD_ID_MAKEFILE) $(BUILD_SYSTEM)/version_defaults.mk $(system_prop_file) $(INSTALLED_ANDROID_INFO_TXT_TARGET)
 	@echo Target buildinfo: $@
 	@mkdir -p $(dir $@)
 	$(hide) echo > $@
@@ -298,16 +347,6 @@
 			TARGET_CPU_ABI2="$(TARGET_CPU_ABI2)" \
 			TARGET_AAPT_CHARACTERISTICS="$(TARGET_AAPT_CHARACTERISTICS)" \
 	        bash $(BUILDINFO_SH) >> $@
-ifndef property_overrides_split_enabled
-	$(hide) TARGET_DEVICE="$(TARGET_DEVICE)" \
-			PRODUCT_NAME="$(TARGET_PRODUCT)" \
-			PRODUCT_BRAND="$(PRODUCT_BRAND)" \
-			PRODUCT_MODEL="$(PRODUCT_MODEL)" \
-			PRODUCT_MANUFACTURER="$(PRODUCT_MANUFACTURER)" \
-			TARGET_BOOTLOADER_BOARD_NAME="$(TARGET_BOOTLOADER_BOARD_NAME)" \
-			TARGET_BOARD_PLATFORM="$(TARGET_BOARD_PLATFORM)" \
-	        bash $(VENDOR_BUILDINFO_SH) >> $@
-endif
 	$(hide) $(foreach file,$(system_prop_file), \
 		if [ -f "$(file)" ]; then \
 			echo "#" >> $@; \
@@ -336,11 +375,7 @@
 
 $(INSTALLED_BUILD_PROP_TARGET): $(intermediate_system_build_prop) $(INSTALLED_RECOVERYIMAGE_TARGET)
 	@echo "Target build info: $@"
-ifdef BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE
 	$(hide) grep -v 'ro.product.first_api_level' $(intermediate_system_build_prop) > $@
-else
-	$(hide) cat $(intermediate_system_build_prop) > $@
-endif
 ifdef INSTALLED_RECOVERYIMAGE_TARGET
 	$(hide) echo ro.expect.recovery_id=`cat $(RECOVERYIMAGE_ID_FILE)` >> $@
 endif
@@ -349,7 +384,6 @@
 # vendor build.prop
 #
 # For verifying that the vendor build is what we think it is
-ifdef BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE
 INSTALLED_VENDOR_BUILD_PROP_TARGET := $(TARGET_OUT_VENDOR)/build.prop
 ALL_DEFAULT_INSTALLED_MODULES += $(INSTALLED_VENDOR_BUILD_PROP_TARGET)
 
@@ -368,7 +402,6 @@
 	$(hide) echo ro.vendor.build.date=`$(DATE_FROM_FILE)`>>$@
 	$(hide) echo ro.vendor.build.date.utc=`$(DATE_FROM_FILE) +%s`>>$@
 	$(hide) echo ro.vendor.build.fingerprint="$(BUILD_FINGERPRINT_FROM_FILE)">>$@
-ifdef property_overrides_split_enabled
 	$(hide) TARGET_DEVICE="$(TARGET_DEVICE)" \
 			PRODUCT_NAME="$(TARGET_PRODUCT)" \
 			PRODUCT_BRAND="$(PRODUCT_BRAND)" \
@@ -377,6 +410,7 @@
 			TARGET_BOOTLOADER_BOARD_NAME="$(TARGET_BOOTLOADER_BOARD_NAME)" \
 			TARGET_BOARD_PLATFORM="$(TARGET_BOARD_PLATFORM)" \
 	        bash $(VENDOR_BUILDINFO_SH) >> $@
+ifdef property_overrides_split_enabled
 	$(hide) echo "#" >> $@; \
 	        echo "# ADDITIONAL VENDOR BUILD PROPERTIES" >> $@; \
 	        echo "#" >> $@;
@@ -384,7 +418,6 @@
 		echo "$(line)" >> $@;)
 	$(hide) build/tools/post_process_props.py $@
 endif  # property_overrides_split_enabled
-endif  # BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE
 
 # ----------------------------------------------------------------
 
@@ -488,7 +521,7 @@
 SOONG_CONV_DATA := $(call intermediates-dir-for,PACKAGING,soong_conversion)/soong_conv_data
 $(SOONG_CONV_DATA):
 	@rm -f $@
-	@$(foreach s,$(SOONG_CONV),echo "$(s),$(sort $(SOONG_CONV.$(s).PROBLEMS)),$(sort $(filter-out $(SOONG_ALREADY_CONV),$(SOONG_CONV.$(s).DEPS)))" >>$@;)
+	@$(foreach s,$(SOONG_CONV),echo "$(s),$(SOONG_CONV.$(s).TYPE),$(sort $(SOONG_CONV.$(s).PROBLEMS)),$(sort $(filter-out $(SOONG_ALREADY_CONV),$(SOONG_CONV.$(s).DEPS)))" >>$@;)
 
 SOONG_TO_CONVERT_SCRIPT := build/tools/soong_to_convert.py
 SOONG_TO_CONVERT := $(PRODUCT_OUT)/soong_to_convert.txt
@@ -498,6 +531,18 @@
 $(call dist-for-goals,droidcore,$(SOONG_TO_CONVERT))
 
 # -----------------------------------------------------------------
+# Modules use -Wno-error, or added default -Wall -Werror
+WALL_WERROR := $(PRODUCT_OUT)/wall_werror.txt
+$(WALL_WERROR):
+	@rm -f $@
+	echo "# Modules using -Wno-error" >> $@
+	for m in $(sort $(SOONG_MODULES_USING_WNO_ERROR) $(MODULES_USING_WNO_ERROR)); do echo $$m >> $@; done
+	echo "# Modules added default -Wall" >> $@
+	for m in $(sort $(SOONG_MODULES_ADDED_WALL) $(MODULES_ADDED_WALL)); do echo $$m >> $@; done
+
+$(call dist-for-goals,droidcore,$(WALL_WERROR))
+
+# -----------------------------------------------------------------
 # The dev key is used to sign this package, and as the key required
 # for future OTA packages installed by this system.  Actual product
 # deliverables will be re-signed by hand.  We expect this file to
@@ -595,6 +640,8 @@
 	@echo "make $@: ignoring dependencies"
 	$(hide) $(MKBOOTFS) -d $(TARGET_OUT) $(TARGET_ROOT_OUT) | $(MINIGZIP) > $(INSTALLED_RAMDISK_TARGET)
 
+INSTALLED_BOOTIMAGE_TARGET := $(PRODUCT_OUT)/boot.img
+
 ifneq ($(strip $(TARGET_NO_KERNEL)),true)
 
 # -----------------------------------------------------------------
@@ -633,8 +680,6 @@
     --os_version $(PLATFORM_VERSION) \
     --os_patch_level $(PLATFORM_SECURITY_PATCH)
 
-INSTALLED_BOOTIMAGE_TARGET := $(PRODUCT_OUT)/boot.img
-
 # BOARD_USES_RECOVERY_AS_BOOT = true must have BOARD_BUILD_SYSTEM_ROOT_IMAGE = true.
 ifeq ($(BOARD_USES_RECOVERY_AS_BOOT),true)
 ifneq ($(BOARD_BUILD_SYSTEM_ROOT_IMAGE),true)
@@ -652,7 +697,7 @@
 $(INSTALLED_BOOTIMAGE_TARGET): $(MKBOOTIMG) $(AVBTOOL) $(INTERNAL_BOOTIMAGE_FILES) $(BOARD_AVB_BOOT_KEY_PATH)
 	$(call pretty,"Target boot image: $@")
 	$(hide) $(MKBOOTIMG) $(INTERNAL_BOOTIMAGE_ARGS) $(INTERNAL_MKBOOTIMG_VERSION_ARGS) $(BOARD_MKBOOTIMG_ARGS) --output $@
-	$(hide) $(call assert-max-image-size,$@,$(BOARD_BOOTIMAGE_PARTITION_SIZE))
+	$(hide) $(call assert-max-image-size,$@,$(call get-hash-image-max-size,$(BOARD_BOOTIMAGE_PARTITION_SIZE)))
 	$(hide) $(AVBTOOL) add_hash_footer \
 	  --image $@ \
 	  --partition_size $(BOARD_BOOTIMAGE_PARTITION_SIZE) \
@@ -663,9 +708,9 @@
 bootimage-nodeps: $(MKBOOTIMG) $(AVBTOOL) $(BOARD_AVB_BOOT_KEY_PATH)
 	@echo "make $@: ignoring dependencies"
 	$(hide) $(MKBOOTIMG) $(INTERNAL_BOOTIMAGE_ARGS) $(INTERNAL_MKBOOTIMG_VERSION_ARGS) $(BOARD_MKBOOTIMG_ARGS) --output $(INSTALLED_BOOTIMAGE_TARGET)
-	$(hide) $(call assert-max-image-size,$(INSTALLED_BOOTIMAGE_TARGET),$(BOARD_BOOTIMAGE_PARTITION_SIZE))
+	$(hide) $(call assert-max-image-size,$(INSTALLED_BOOTIMAGE_TARGET),$(call get-hash-image-max-size,$(BOARD_BOOTIMAGE_PARTITION_SIZE)))
 	$(hide) $(AVBTOOL) add_hash_footer \
-	  --image $@ \
+	  --image $(INSTALLED_BOOTIMAGE_TARGET) \
 	  --partition_size $(BOARD_BOOTIMAGE_PARTITION_SIZE) \
 	  --partition_name boot $(INTERNAL_AVB_BOOT_SIGNING_ARGS) \
 	  $(BOARD_AVB_BOOT_ADD_HASH_FOOTER_ARGS)
@@ -717,6 +762,13 @@
 endif # BOARD_USES_RECOVERY_AS_BOOT
 
 else	# TARGET_NO_KERNEL
+ifdef BOARD_PREBUILT_BOOTIMAGE
+ifneq ($(BOARD_BUILD_SYSTEM_ROOT_IMAGE),true)
+# Remove when b/63676296 is resolved.
+$(error Prebuilt bootimage is only supported for AB targets)
+endif
+$(eval $(call copy-one-file,$(BOARD_PREBUILT_BOOTIMAGE),$(INSTALLED_BOOTIMAGE_TARGET)))
+else
 INTERNAL_KERNEL_CMDLINE := $(strip $(BOARD_KERNEL_CMDLINE))
 # HACK: The top-level targets depend on the bootimage.  Not all targets
 # can produce a bootimage, though, and emulator targets need the ramdisk
@@ -725,6 +777,7 @@
 #       kernel-less inputs.
 INSTALLED_BOOTIMAGE_TARGET := $(INSTALLED_RAMDISK_TARGET)
 endif
+endif
 
 # -----------------------------------------------------------------
 # NOTICE files
@@ -1019,6 +1072,7 @@
 $(if $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VERITY),$(hide) echo "verity_key=$(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_VERITY_SIGNING_KEY)" >> $(1))
 $(if $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VERITY),$(hide) echo "verity_signer_cmd=$(notdir $(VERITY_SIGNER))" >> $(1))
 $(if $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VERITY_FEC),$(hide) echo "verity_fec=$(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VERITY_FEC)" >> $(1))
+$(if $(filter eng, $(TARGET_BUILD_VARIANT)),$(hide) echo "verity_disable=true" >> $(1))
 $(if $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SYSTEM_VERITY_PARTITION),$(hide) echo "system_verity_block_device=$(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SYSTEM_VERITY_PARTITION)" >> $(1))
 $(if $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_VENDOR_VERITY_PARTITION),$(hide) echo "vendor_verity_block_device=$(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_VENDOR_VERITY_PARTITION)" >> $(1))
 $(if $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VBOOT),$(hide) echo "vboot=$(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VBOOT)" >> $(1))
@@ -1049,6 +1103,23 @@
 $(if $(2),$(hide) $(foreach kv,$(2),echo "$(kv)" >> $(1);))
 endef
 
+# $(1): modules list
+# $(2): output dir
+# $(3): mount point
+# $(4): staging dir
+# Depmod requires a well-formed kernel version so 0.0 is used as a placeholder.
+define build-image-kernel-modules
+    $(hide) rm -rf $(2)/lib/modules
+    $(hide) mkdir -p $(2)/lib/modules
+    $(hide) cp $(1) $(2)/lib/modules/
+    $(hide) rm -rf $(4)
+    $(hide) mkdir -p $(4)/lib/modules/0.0/$(3)lib/modules
+    $(hide) cp $(1) $(4)/lib/modules/0.0/$(3)lib/modules
+    $(hide) $(DEPMOD) -b $(4) 0.0
+    $(hide) sed -e 's/\(.*modules.*\):/\/\1:/g' -e 's/ \([^ ]*modules[^ ]*\)/ \/\1/g' $(4)/lib/modules/0.0/modules.dep > $(2)/lib/modules/modules.dep
+    $(hide) cp $(4)/lib/modules/0.0/modules.alias $(2)/lib/modules
+endef
+
 # -----------------------------------------------------------------
 # Recovery image
 
@@ -1139,14 +1210,6 @@
 RECOVERY_RESOURCE_ZIP :=
 endif
 
-ifeq ($(TARGET_PRIVATE_RES_DIRS),)
-  $(info No private recovery resources for TARGET_DEVICE $(TARGET_DEVICE))
-endif
-
-ifeq ($(recovery_fstab),)
-  $(info No recovery.fstab for TARGET_DEVICE $(TARGET_DEVICE))
-endif
-
 INTERNAL_RECOVERYIMAGE_ARGS := \
 	$(addprefix --second ,$(INSTALLED_2NDBOOTLOADER_TARGET)) \
 	--kernel $(recovery_kernel) \
@@ -1183,37 +1246,21 @@
 
 RECOVERYIMAGE_ID_FILE := $(PRODUCT_OUT)/recovery.id
 
-# $(1): modules list
-# $(2): output dir
-# $(3): mount point
-# $(4): staging dir
-# Depmod requires a well-formed kernel version so 0.0 is used as a placeholder.
-define build-image-kernel-modules
-    $(hide) rm -rf $(2)/lib/modules
-    $(hide) mkdir -p $(2)/lib/modules
-    $(hide) cp $(1) $(2)/lib/modules/
-    $(hide) rm -rf $(4)
-    $(hide) mkdir -p $(4)/lib/modules/0.0/$(3)lib/modules
-    $(hide) cp $(1) $(4)/lib/modules/0.0/$(3)lib/modules
-    $(hide) $(DEPMOD) -b $(4) 0.0
-    $(hide) sed -e 's/\(.*modules.*\):/\/\1:/g' -e 's/ \([^ ]*modules[^ ]*\)/ \/\1/g' $(4)/lib/modules/0.0/modules.dep > $(2)/lib/modules/modules.dep
-    $(hide) cp $(4)/lib/modules/0.0/modules.alias $(2)/lib/modules
-endef
-
 # $(1): output file
 define build-recoveryimage-target
-  @echo ----- Making recovery image ------
+  # Making recovery image
   $(hide) mkdir -p $(TARGET_RECOVERY_OUT)
   $(hide) mkdir -p $(TARGET_RECOVERY_ROOT_OUT)/etc $(TARGET_RECOVERY_ROOT_OUT)/sdcard $(TARGET_RECOVERY_ROOT_OUT)/tmp
-  @echo Copying baseline ramdisk...
+  # Copying baseline ramdisk...
   # Use rsync because "cp -Rf" fails to overwrite broken symlinks on Mac.
   $(hide) rsync -a --exclude=etc --exclude=sdcard $(IGNORE_RECOVERY_SEPOLICY) $(IGNORE_CACHE_LINK) $(TARGET_ROOT_OUT) $(TARGET_RECOVERY_OUT)
   # Copy adbd from system/bin to recovery/root/sbin
   $(hide) cp -f $(TARGET_OUT_EXECUTABLES)/adbd $(TARGET_RECOVERY_ROOT_OUT)/sbin/adbd
-  @echo Modifying ramdisk contents...
+  # Modifying ramdisk contents...
   $(if $(BOARD_RECOVERY_KERNEL_MODULES), \
     $(call build-image-kernel-modules,$(BOARD_RECOVERY_KERNEL_MODULES),$(TARGET_RECOVERY_ROOT_OUT),,$(call intermediates-dir-for,PACKAGING,depmod_recovery)))
-  $(hide) rm -f $(TARGET_RECOVERY_ROOT_OUT)/init*.rc
+  # Removes $(TARGET_RECOVERY_ROOT_OUT)/init*.rc EXCEPT init.recovery*.rc.
+  $(hide) find $(TARGET_RECOVERY_ROOT_OUT) -maxdepth 1 -name 'init*.rc' -type f -not -name "init.recovery.*.rc" | xargs rm -f
   $(hide) cp -f $(recovery_initrc) $(TARGET_RECOVERY_ROOT_OUT)/
   $(hide) cp $(TARGET_ROOT_OUT)/init.recovery.*.rc $(TARGET_RECOVERY_ROOT_OUT)/ || true # Ignore error when the src file doesn't exist.
   $(hide) mkdir -p $(TARGET_RECOVERY_ROOT_OUT)/res
@@ -1252,16 +1299,13 @@
   )
   $(if $(filter true,$(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VBOOT)), \
     $(VBOOT_SIGNER) $(FUTILITY) $(1).unsigned $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_VBOOT_SIGNING_KEY).vbpubk $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_VBOOT_SIGNING_KEY).vbprivk $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_VBOOT_SIGNING_SUBKEY).vbprivk $(1).keyblock $(1))
-  $(if $(and $(filter true,$(BOARD_USES_RECOVERY_AS_BOOT)),$(filter true,$(BOARD_AVB_ENABLE))), \
-      $(hide) $(AVBTOOL) add_hash_footer \
-        --image $(1) \
-        --partition_size $(BOARD_BOOTIMAGE_PARTITION_SIZE) \
-        --partition_name boot $(INTERNAL_AVB_BOOT_SIGNING_ARGS) \
-        $(BOARD_AVB_BOOT_ADD_HASH_FOOTER_ARGS))
   $(if $(filter true,$(BOARD_USES_RECOVERY_AS_BOOT)), \
-    $(hide) $(call assert-max-image-size,$(1),$(BOARD_BOOTIMAGE_PARTITION_SIZE)), \
-    $(hide) $(call assert-max-image-size,$(1),$(BOARD_RECOVERYIMAGE_PARTITION_SIZE)))
-  @echo ----- Made recovery image: $(1) --------
+    $(hide) $(call assert-max-image-size,$(1),$(call get-hash-image-max-size,$(BOARD_BOOTIMAGE_PARTITION_SIZE))), \
+    $(hide) $(call assert-max-image-size,$(1),$(call get-hash-image-max-size,$(BOARD_RECOVERYIMAGE_PARTITION_SIZE))))
+  $(if $(filter true,$(BOARD_AVB_ENABLE)), \
+    $(if $(filter true,$(BOARD_USES_RECOVERY_AS_BOOT)), \
+      $(hide) $(AVBTOOL) add_hash_footer --image $(1) --partition_size $(BOARD_BOOTIMAGE_PARTITION_SIZE) --partition_name boot $(INTERNAL_AVB_BOOT_SIGNING_ARGS) $(BOARD_AVB_BOOT_ADD_HASH_FOOTER_ARGS),\
+      $(hide) $(AVBTOOL) add_hash_footer --image $(1) --partition_size $(BOARD_RECOVERYIMAGE_PARTITION_SIZE) --partition_name recovery $(INTERNAL_AVB_RECOVERY_SIGNING_ARGS) $(BOARD_AVB_RECOVERY_ADD_HASH_FOOTER_ARGS)))
 endef
 
 ADBD := $(TARGET_OUT_EXECUTABLES)/adbd
@@ -1324,12 +1368,12 @@
 .PHONY: recoveryimage
 recoveryimage: $(INSTALLED_RECOVERYIMAGE_TARGET) $(RECOVERY_RESOURCE_ZIP)
 
-ifeq ($(BOARD_NAND_PAGE_SIZE),)
-BOARD_NAND_PAGE_SIZE := 2048
+ifneq ($(BOARD_NAND_PAGE_SIZE),)
+$(error MTD device is no longer supported and thus BOARD_NAND_PAGE_SIZE is deprecated.)
 endif
 
-ifeq ($(BOARD_NAND_SPARE_SIZE),)
-BOARD_NAND_SPARE_SIZE := 64
+ifneq ($(BOARD_NAND_SPARE_SIZE),)
+$(error MTD device is no longer supported and thus BOARD_NAND_SPARE_SIZE is deprecated.)
 endif
 
 # -----------------------------------------------------------------
@@ -1798,8 +1842,11 @@
 
 # We just build this directly to the install location.
 INSTALLED_SYSTEMOTHERIMAGE_TARGET := $(BUILT_SYSTEMOTHERIMAGE_TARGET)
+ifneq (true,$(SANITIZE_LITE))
+# Only create system_other when not building the second stage of a SANITIZE_LITE build.
 $(INSTALLED_SYSTEMOTHERIMAGE_TARGET): $(INTERNAL_USERIMAGES_DEPS) $(INTERNAL_SYSTEMOTHERIMAGE_FILES) $(INSTALLED_FILES_FILE_SYSTEMOTHER)
 	$(build-systemotherimage-target)
+endif
 
 .PHONY: systemotherimage-nodeps
 systemotherimage-nodeps: | $(INTERNAL_USERIMAGES_DEPS)
@@ -1912,6 +1959,7 @@
 DTBO_FOOTER_ARGS := BOARD_AVB_DTBO_ADD_HASH_FOOTER_ARGS
 SYSTEM_FOOTER_ARGS := BOARD_AVB_SYSTEM_ADD_HASHTREE_FOOTER_ARGS
 VENDOR_FOOTER_ARGS := BOARD_AVB_VENDOR_ADD_HASHTREE_FOOTER_ARGS
+RECOVERY_FOOTER_ARGS := BOARD_AVB_RECOVERY_ADD_HASH_FOOTER_ARGS
 
 # Check and set required build variables for a chain partition.
 # $(1): the partition to enable AVB chain, e.g., BOOT or SYSTEM.
@@ -1973,6 +2021,15 @@
 endif
 endif
 
+ifdef INSTALLED_RECOVERYIMAGE_TARGET
+ifdef BOARD_AVB_RECOVERY_KEY_PATH
+$(eval $(call check-and-set-avb-chain-args,RECOVERY))
+else
+INTERNAL_AVB_MAKE_VBMETA_IMAGE_ARGS += \
+    --include_descriptors_from_image $(INSTALLED_RECOVERYIMAGE_TARGET)
+endif
+endif
+
 BOARD_AVB_MAKE_VBMETA_IMAGE_ARGS += --padding_size 4096
 
 # Add kernel cmdline descriptor for kernel to mount system.img as root with
@@ -2011,6 +2068,9 @@
   $(if $(BOARD_AVB_DTBO_KEY_PATH),\
     $(hide) $(AVBTOOL) extract_public_key --key $(BOARD_AVB_DTBO_KEY_PATH) \
       --output $(1)/dtbo.avbpubkey)
+  $(if $(BOARD_AVB_RECOVERY_KEY_PATH),\
+    $(hide) $(AVBTOOL) extract_public_key --key $(BOARD_AVB_RECOVERY_KEY_PATH) \
+      --output $(1)/recovery.avbpubkey)
 endef
 
 define build-vbmetaimage-target
@@ -2026,7 +2086,14 @@
 endef
 
 INSTALLED_VBMETAIMAGE_TARGET := $(BUILT_VBMETAIMAGE_TARGET)
-$(INSTALLED_VBMETAIMAGE_TARGET): $(AVBTOOL) $(INSTALLED_BOOTIMAGE_TARGET) $(INSTALLED_SYSTEMIMAGE) $(INSTALLED_VENDORIMAGE_TARGET) $(INSTALLED_DTBOIMAGE_TARGET) $(BOARD_AVB_KEY_PATH)
+$(INSTALLED_VBMETAIMAGE_TARGET): \
+		$(AVBTOOL) \
+		$(INSTALLED_BOOTIMAGE_TARGET) \
+		$(INSTALLED_SYSTEMIMAGE) \
+		$(INSTALLED_VENDORIMAGE_TARGET) \
+		$(INSTALLED_DTBOIMAGE_TARGET) \
+		$(INSTALLED_RECOVERYIMAGE_TARGET) \
+		$(BOARD_AVB_KEY_PATH)
 	$(build-vbmetaimage-target)
 
 .PHONY: vbmetaimage-nodeps
@@ -2081,6 +2148,7 @@
 ifeq ($(build_ota_package),true)
 OTATOOLS :=  $(HOST_OUT_EXECUTABLES)/minigzip \
   $(HOST_OUT_EXECUTABLES)/aapt \
+  $(HOST_OUT_EXECUTABLES)/checkvintf \
   $(HOST_OUT_EXECUTABLES)/mkbootfs \
   $(HOST_OUT_EXECUTABLES)/mkbootimg \
   $(HOST_OUT_EXECUTABLES)/fs_config \
@@ -2091,8 +2159,6 @@
   $(HOST_OUT_JAVA_LIBRARIES)/signapk.jar \
   $(HOST_OUT_JAVA_LIBRARIES)/BootSignature.jar \
   $(HOST_OUT_JAVA_LIBRARIES)/VeritySigner.jar \
-  $(HOST_OUT_EXECUTABLES)/make_ext4fs \
-  $(HOST_OUT_EXECUTABLES)/mkuserimg.sh \
   $(HOST_OUT_EXECUTABLES)/mke2fs \
   $(HOST_OUT_EXECUTABLES)/mkuserimg_mke2fs.sh \
   $(HOST_OUT_EXECUTABLES)/e2fsdroid \
@@ -2100,6 +2166,7 @@
   $(HOST_OUT_EXECUTABLES)/mksquashfs \
   $(HOST_OUT_EXECUTABLES)/mkf2fsuserimg.sh \
   $(HOST_OUT_EXECUTABLES)/make_f2fs \
+  $(HOST_OUT_EXECUTABLES)/sload_f2fs \
   $(HOST_OUT_EXECUTABLES)/simg2img \
   $(HOST_OUT_EXECUTABLES)/e2fsck \
   $(HOST_OUT_EXECUTABLES)/build_verity_tree \
@@ -2114,7 +2181,7 @@
   $(HOST_OUT_EXECUTABLES)/delta_generator \
   $(AVBTOOL) \
   $(BLK_ALLOC_TO_BASE_FS) \
-  $(BRO)
+  $(BROTLI)
 
 ifeq (true,$(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VBOOT))
 OTATOOLS += \
@@ -2162,9 +2229,10 @@
   system/extras/verity/build_verity_metadata.py \
   system/extras/ext4_utils/mke2fs.conf \
   external/avb/test/data/testkey_rsa4096.pem \
+  $(shell find system/update_engine/scripts -name \*.pyc -prune -o -type f -print | sort) \
   $(shell find build/target/product/security -type f -name \*.x509.pem -o -name \*.pk8 -o \
       -name verity_key | sort) \
-  $(shell find device vendor -type f -name \*.pk8 -o -name verifiedboot\* -o \
+  $(shell find device $(wildcard vendor) -type f -name \*.pk8 -o -name verifiedboot\* -o \
       -name \*.x509.pem -o -name oem\*.prop | sort)
 
 OTATOOLS_RELEASETOOLS := \
@@ -2402,9 +2470,6 @@
 ifdef BOARD_BOOTIMAGE_PARTITION_SIZE
 	$(hide) echo "boot_size=$(BOARD_BOOTIMAGE_PARTITION_SIZE)" >> $(zip_root)/META/misc_info.txt
 endif
-ifeq ($(BOARD_USES_RECOVERY_AS_BOOT),)
-	$(hide) echo "recovery_as_boot=$(BOARD_USES_RECOVERY_AS_BOOT)" >> $(zip_root)/META/misc_info.txt
-endif
 ifeq ($(INSTALLED_RECOVERYIMAGE_TARGET),)
 	$(hide) echo "no_recovery=true" >> $(zip_root)/META/misc_info.txt
 endif
@@ -2459,6 +2524,12 @@
 	$(hide) echo "avb_boot_algorithm=$(BOARD_AVB_BOOT_ALGORITHM)" >> $(zip_root)/META/misc_info.txt
 	$(hide) echo "avb_boot_rollback_index_location=$(BOARD_AVB_BOOT_ROLLBACK_INDEX_LOCATION)" >> $(zip_root)/META/misc_info.txt
 endif # BOARD_AVB_BOOT_KEY_PATH
+	$(hide) echo "avb_recovery_add_hash_footer_args=$(BOARD_AVB_RECOVERY_ADD_HASH_FOOTER_ARGS)" >> $(zip_root)/META/misc_info.txt
+ifdef BOARD_AVB_RECOVERY_KEY_PATH
+	$(hide) echo "avb_recovery_key_path=$(BOARD_AVB_RECOVERY_KEY_PATH)" >> $(zip_root)/META/misc_info.txt
+	$(hide) echo "avb_recovery_algorithm=$(BOARD_AVB_RECOVERY_ALGORITHM)" >> $(zip_root)/META/misc_info.txt
+	$(hide) echo "avb_recovery_rollback_index_location=$(BOARD_AVB_RECOVERY_ROLLBACK_INDEX_LOCATION)" >> $(zip_root)/META/misc_info.txt
+endif # BOARD_AVB_RECOVERY_KEY_PATH
 endif # BOARD_AVB_ENABLE
 ifdef BOARD_BPT_INPUT_FILES
 	$(hide) echo "board_bpt_enable=true" >> $(zip_root)/META/misc_info.txt
@@ -2519,6 +2590,10 @@
 	$(hide) mkdir -p $(zip_root)/IMAGES
 	$(hide) cp $(INSTALLED_VENDORIMAGE_TARGET) $(zip_root)/IMAGES/
 endif
+ifdef BOARD_PREBUILT_BOOTIMAGE
+	$(hide) mkdir -p $(zip_root)/IMAGES
+	$(hide) cp $(INSTALLED_BOOTIMAGE_TARGET) $(zip_root)/IMAGES/
+endif
 ifdef BOARD_PREBUILT_DTBOIMAGE
 	$(hide) mkdir -p $(zip_root)/PREBUILT_IMAGES
 	$(hide) cp $(INSTALLED_DTBOIMAGE_TARGET) $(zip_root)/PREBUILT_IMAGES/
@@ -2555,8 +2630,6 @@
 ifdef INSTALLED_SYSTEMOTHERIMAGE_TARGET
 	$(hide) $(call fs_config,$(zip_root)/SYSTEM_OTHER,system/) > $(zip_root)/META/system_other_filesystem_config.txt
 endif
-
-ifeq ($(PRODUCT_FULL_TREBLE),true)
 	@# Metadata for compatibility verification.
 	$(hide) cp $(BUILT_SYSTEM_MANIFEST) $(zip_root)/META/system_manifest.xml
 	$(hide) cp $(BUILT_SYSTEM_COMPATIBILITY_MATRIX) $(zip_root)/META/system_matrix.xml
@@ -2566,7 +2639,6 @@
 ifdef BUILT_VENDOR_MATRIX
 	$(hide) cp $(BUILT_VENDOR_MATRIX) $(zip_root)/META/vendor_matrix.xml
 endif
-endif
 
 	$(hide) PATH=$(foreach p,$(INTERNAL_USERIMAGES_BINARY_PATHS),$(p):)$$PATH MKBOOTIMG=$(MKBOOTIMG) \
 	    ./build/tools/releasetools/add_img_to_target_files -a -v -p $(HOST_OUT) $(zip_root)
@@ -2609,7 +2681,7 @@
 ifeq ($(AB_OTA_UPDATER),true)
 $(INTERNAL_OTA_PACKAGE_TARGET): $(BRILLO_UPDATE_PAYLOAD)
 else
-$(INTERNAL_OTA_PACKAGE_TARGET): $(BRO)
+$(INTERNAL_OTA_PACKAGE_TARGET): $(BROTLI)
 endif
 
 $(INTERNAL_OTA_PACKAGE_TARGET): $(BUILT_TARGET_FILES_PACKAGE) \
@@ -2659,7 +2731,7 @@
 name := $(name)-symbols-$(FILE_NAME_TAG)
 
 SYMBOLS_ZIP := $(PRODUCT_OUT)/$(name).zip
-# For apps_only build we'll establish the dependency later in build/core/main.mk.
+# For apps_only build we'll establish the dependency later in build/make/core/main.mk.
 ifndef TARGET_BUILD_APPS
 $(SYMBOLS_ZIP): $(INSTALLED_SYSTEMIMAGE) \
 		$(INSTALLED_BOOTIMAGE_TARGET) \
@@ -2721,14 +2793,13 @@
 	fi
 
 ifeq (true,$(EMMA_INSTRUMENT))
-  #------------------------------------------------------------------
-  # An archive of classes for use in generating code-coverage reports
-  # These are the uninstrumented versions of any classes that were
-  # to be instrumented.
-  # Any dependencies are set up later in build/core/main.mk.
+#------------------------------------------------------------------
+# An archive of classes for use in generating code-coverage reports
+# These are the uninstrumented versions of any classes that were
+# to be instrumented.
+# Any dependencies are set up later in build/make/core/main.mk.
 
-  ifeq ($(ANDROID_COMPILE_WITH_JACK),false)
-    JACOCO_REPORT_CLASSES_ALL := $(PRODUCT_OUT)/jacoco-report-classes-all.jar
+JACOCO_REPORT_CLASSES_ALL := $(PRODUCT_OUT)/jacoco-report-classes-all.jar
 $(JACOCO_REPORT_CLASSES_ALL) :
 	@echo "Collecting uninstrumented classes"
 	$(hide) find $(TARGET_COMMON_OUT_ROOT) $(HOST_COMMON_OUT_ROOT) -name "jacoco-report-classes.jar" | \
@@ -2739,14 +2810,6 @@
 # -q supress most output
 # -X skip storing extended file attributes
 
-  else
-    EMMA_META_ZIP := $(PRODUCT_OUT)/emma_meta.zip
-$(EMMA_META_ZIP) :
-	@echo "Collecting Emma coverage meta files."
-	$(hide) find $(TARGET_COMMON_OUT_ROOT) $(HOST_COMMON_OUT_ROOT) -name "coverage.em" | \
-		zip -@ -qX $@
-endif
-
 endif # EMMA_INSTRUMENT=true
 
 
@@ -2756,10 +2819,10 @@
 #
 ifdef TARGET_BUILD_APPS
 PROGUARD_DICT_ZIP := $(PRODUCT_OUT)/$(TARGET_PRODUCT)-proguard-dict-$(FILE_NAME_TAG).zip
-# the dependency will be set up later in build/core/main.mk.
+# the dependency will be set up later in build/make/core/main.mk.
 $(PROGUARD_DICT_ZIP) :
 	@echo "Packaging Proguard obfuscation dictionary files."
-	$(hide) dict_files=`find $(TARGET_OUT_COMMON_INTERMEDIATES)/APPS -name proguard_dictionary -o -name jack_dictionary`; \
+	$(hide) dict_files=`find $(TARGET_OUT_COMMON_INTERMEDIATES)/APPS -name proguard_dictionary`; \
 		if [ -n "$$dict_files" ]; then \
 		  unobfuscated_jars=$${dict_files//proguard_dictionary/classes.jar}; \
 		  zip -qX $@ $$dict_files $$unobfuscated_jars; \
@@ -2942,7 +3005,6 @@
 			-v "TARGET_CPU_ABI=$(TARGET_CPU_ABI)" \
 			-v "DLL_EXTENSION=$(HOST_SHLIB_SUFFIX)" \
 			-v "FONT_OUT=$(SDK_FONT_TEMP)" \
-			-v "JACK_SDKTOOL_VERSION=$(JACK_SDKTOOL_VERSION)" \
 			-o $(PRIVATE_DIR) && \
 		cp -f $(target_notice_file_txt) \
 				$(PRIVATE_DIR)/system-images/android-$(PLATFORM_VERSION)/$(TARGET_CPU_ABI)/NOTICE.txt && \
diff --git a/core/aapt2.mk b/core/aapt2.mk
index 287f933..c582e30 100644
--- a/core/aapt2.mk
+++ b/core/aapt2.mk
@@ -47,8 +47,10 @@
 my_generated_resources_flata += $(my_generated_resources_flata)
 endif
 
+# Always set --pseudo-localize, it will be stripped out later for release
+# builds that don't want it.
 $(my_res_resources_flat) $(my_overlay_resources_flat) $(my_generated_resources_flata): \
-  PRIVATE_AAPT2_CFLAGS := $(PRODUCT_AAPT2_CFLAGS)
+  PRIVATE_AAPT2_CFLAGS := --pseudo-localize
 
 my_static_library_resources := $(foreach l, $(call reverse-list,$(LOCAL_STATIC_ANDROID_LIBRARIES)),\
   $(call intermediates-dir-for,JAVA_LIBRARIES,$(l),,COMMON)/package-res.apk)
@@ -69,23 +71,11 @@
 $(my_res_package): PRIVATE_SHARED_ANDROID_LIBRARIES := $(my_shared_library_resources)
 $(my_res_package): PRIVATE_PROGUARD_OPTIONS_FILE := $(proguard_options_file)
 $(my_res_package): PRIVATE_ASSET_DIRS := $(my_asset_dirs)
-$(my_res_package): $(full_android_manifest) $(my_static_library_resources) $(my_shared_library_resources)
-$(my_res_package): $(my_full_asset_paths)
-$(my_res_package): $(my_res_resources_flat) $(my_overlay_resources_flat) \
-  $(my_generated_resources_flata) $(my_static_library_resources) \
-  $(AAPT2)
-	@echo "AAPT2 link $@"
-	$(call aapt2-link)
+$(my_res_package): .KATI_IMPLICIT_OUTPUTS :=
 
 ifdef R_file_stamp
-$(R_file_stamp) : $(my_res_package) | $(ACP)
-	@echo "target R.java/Manifest.java: $(PRIVATE_MODULE) ($@)"
-	@rm -rf $@ && mkdir -p $(dir $@)
-	$(call find-generated-R.java)
-endif
-
-ifdef proguard_options_file
-$(proguard_options_file) : $(my_res_package)
+$(my_res_package): PRIVATE_R_FILE_STAMP := $(R_file_stamp)
+$(my_res_package): .KATI_IMPLICIT_OUTPUTS += $(R_file_stamp)
 endif
 
 resource_export_package :=
@@ -94,10 +84,37 @@
 # other packages can use to build their own PRODUCT-agnostic R.java (etc.)
 # files.
 resource_export_package := $(intermediates.COMMON)/package-export.apk
-$(R_file_stamp) : $(resource_export_package)
-
-$(resource_export_package) : $(my_res_package) | $(ACP)
-	@echo "target Export Resources: $(PRIVATE_MODULE) $(@)"
-	$(copy-file-to-new-target)
-
+$(my_res_package): PRIVATE_RESOURCE_EXPORT_PACKAGE := $(resource_export_package)
+$(my_res_package): .KATI_IMPLICIT_OUTPUTS += $(resource_export_package)
 endif
+
+ifdef proguard_options_file
+$(my_res_package): .KATI_IMPLICIT_OUTPUTS += $(proguard_options_file)
+endif
+
+$(my_res_package): $(full_android_manifest) $(my_static_library_resources) $(my_shared_library_resources)
+$(my_res_package): $(my_full_asset_paths)
+$(my_res_package): $(my_res_resources_flat) $(my_overlay_resources_flat) \
+  $(my_generated_resources_flata) $(my_static_library_resources) \
+  $(AAPT2)
+	@echo "AAPT2 link $@"
+	$(call aapt2-link)
+ifdef R_file_stamp
+	@rm -f $(PRIVATE_R_FILE_STAMP)
+	$(call find-generated-R.java,$(PRIVATE_R_FILE_STAMP))
+endif
+ifdef LOCAL_EXPORT_PACKAGE_RESOURCES
+	@rm -f $(PRIVATE_RESOURCE_EXPORT_PACKAGE)
+
+	cp $@ $(PRIVATE_RESOURCE_EXPORT_PACKAGE)
+endif
+
+# Clear inputs only used in this file, so that they're not re-used during the next build
+my_res_resources :=
+my_overlay_resources :=
+my_compiled_res_base_dir :=
+my_asset_dirs :=
+my_full_asset_paths :=
+my_apk_split_configs :=
+my_generated_res_dirs :=
+my_generated_res_dirs_deps :=
diff --git a/core/android_manifest.mk b/core/android_manifest.mk
index 0093e02..7d573d3 100644
--- a/core/android_manifest.mk
+++ b/core/android_manifest.mk
@@ -34,10 +34,12 @@
 main_android_manifest := $(full_android_manifest)
 full_android_manifest := $(intermediates.COMMON)/AndroidManifest.xml
 $(full_android_manifest): PRIVATE_LIBS_MANIFESTS := $(my_full_libs_manifest_files)
+$(full_android_manifest): $(ANDROID_MANIFEST_MERGER_CLASSPATH)
 $(full_android_manifest) : $(main_android_manifest) $(my_full_libs_manifest_deps)
 	@echo "Merge android manifest files: $@ <-- $< $(PRIVATE_LIBS_MANIFESTS)"
 	@mkdir -p $(dir $@)
-	$(hide) $(ANDROID_MANIFEST_MERGER) --main $< --libs $(PRIVATE_LIBS_MANIFESTS) \
+	$(hide) $(ANDROID_MANIFEST_MERGER) --main $< \
+	    --libs $(call normalize-path-list,$(PRIVATE_LIBS_MANIFESTS)) \
 	    --out $@
 
 endif
diff --git a/core/base_rules.mk b/core/base_rules.mk
index 07d1cd9..7ce3f0f 100644
--- a/core/base_rules.mk
+++ b/core/base_rules.mk
@@ -65,6 +65,16 @@
   my_host_cross :=
 endif
 
+_path := $(LOCAL_MODULE_PATH) $(LOCAL_MODULE_PATH_32) $(LOCAL_MODULE_PATH_64)
+ifneq ($(filter $(TARGET_OUT_VENDOR)%,$(_path)),)
+LOCAL_VENDOR_MODULE := true
+else ifneq ($(filter $(TARGET_OUT_OEM)/%,$(_path)),)
+LOCAL_OEM_MODULE := true
+else ifneq ($(filter $(TARGET_OUT_ODM)/%,$(_path)),)
+LOCAL_ODM_MODULE := true
+endif
+_path :=
+
 ifndef LOCAL_PROPRIETARY_MODULE
   LOCAL_PROPRIETARY_MODULE := $(LOCAL_VENDOR_MODULE)
 endif
@@ -76,6 +86,7 @@
 endif
 
 include $(BUILD_SYSTEM)/local_vndk.mk
+include $(BUILD_SYSTEM)/local_vsdk.mk
 
 my_module_tags := $(LOCAL_MODULE_TAGS)
 ifeq ($(my_host_cross),true)
@@ -96,6 +107,13 @@
 # base_rules.mk, but it will fix the most common ones.
 LOCAL_ADDITIONAL_DEPENDENCIES := $(filter-out %.mk,$(LOCAL_ADDITIONAL_DEPENDENCIES))
 
+my_bad_deps := $(strip $(foreach dep,$(filter-out | ||,$(LOCAL_ADDITIONAL_DEPENDENCIES)),\
+                 $(if $(findstring /,$(dep)),,$(dep))))
+ifneq ($(my_bad_deps),)
+$(call pretty-warning,"Bad LOCAL_ADDITIONAL_DEPENDENCIES: $(my_bad_deps)")
+$(call pretty-error,"LOCAL_ADDITIONAL_DEPENDENCIES must only contain paths (not module names)")
+endif
+
 ###########################################################
 ## Validate and define fallbacks for input LOCAL_* variables.
 ###########################################################
@@ -131,7 +149,7 @@
 # makefiles. Anything else is either a typo or a source of unexpected
 # behaviors.
 ifneq ($(filter-out debug eng tests optional samples,$(my_module_tags)),)
-$(call pretty-warning,unusual tags $(my_module_tags))
+$(call pretty-error,unusual tags: $(filter-out debug eng tests optional samples,$(my_module_tags)))
 endif
 
 # Add implicit tags.
@@ -579,6 +597,9 @@
 ALL_MODULES.$(my_register_name).EXPLICITLY_REQUIRED := \
     $(strip $(ALL_MODULES.$(my_register_name).EXPLICITLY_REQUIRED)\
         $(my_required_modules))
+ALL_MODULES.$(my_register_name).TARGET_REQUIRED := \
+    $(strip $(ALL_MODULES.$(my_register_name).TARGET_REQUIRED)\
+        $(LOCAL_TARGET_REQUIRED_MODULES))
 ALL_MODULES.$(my_register_name).EVENT_LOG_TAGS := \
     $(ALL_MODULES.$(my_register_name).EVENT_LOG_TAGS) $(event_log_tags)
 ALL_MODULES.$(my_register_name).MAKEFILE := \
@@ -591,6 +612,7 @@
 ALL_MODULES.$(my_register_name).FOR_2ND_ARCH := true
 endif
 ALL_MODULES.$(my_register_name).FOR_HOST_CROSS := $(my_host_cross)
+ALL_MODULES.$(my_register_name).COMPATIBILITY_SUITES := $(LOCAL_COMPATIBILITY_SUITE)
 
 INSTALLABLE_FILES.$(LOCAL_INSTALLED_MODULE).MODULE := $(my_register_name)
 
@@ -619,7 +641,7 @@
 
 # Add this module name to the tag list of each specified tag.
 $(foreach tag,$(my_module_tags),\
-    $(eval ALL_MODULE_NAME_TAGS.$(tag) += $(my_register_name)))
+    $(eval ALL_MODULE_NAME_TAGS.$(tag) := $$(ALL_MODULE_NAME_TAGS.$(tag)) $(my_register_name)))
 
 ###########################################################
 ## umbrella targets used to verify builds
@@ -646,7 +668,7 @@
 
 
 ifdef j_or_n
-$(j_or_n) $(h_or_t) $(j_or_n)-$(h_or_t) : $(my_checked_module)
+$(j_or_n) $(h_or_t) $(j_or_n)-$(h_or_hc_or_t) : $(my_checked_module)
 ifneq (,$(filter $(my_module_tags),tests))
 $(j_or_n)-$(h_or_t)-tests $(j_or_n)-tests $(h_or_t)-tests : $(my_checked_module)
 endif
@@ -657,6 +679,15 @@
 endif
 
 ###########################################################
+# Ensure privileged applications always have LOCAL_PRIVILEGED_MODULE
+###########################################################
+ifndef LOCAL_PRIVILEGED_MODULE
+  ifneq (,$(filter $(TARGET_OUT_APPS_PRIVILEGED)/% $(TARGET_OUT_VENDOR_APPS_PRIVILEGED)/%,$(my_module_path)))
+    LOCAL_PRIVILEGED_MODULE := true
+  endif
+endif
+
+###########################################################
 ## NOTICE files
 ###########################################################
 
diff --git a/core/binary.mk b/core/binary.mk
index 7dd9a13..2f306b2 100644
--- a/core/binary.mk
+++ b/core/binary.mk
@@ -62,8 +62,8 @@
 my_export_c_include_deps := $(LOCAL_EXPORT_C_INCLUDE_DEPS)
 my_arflags :=
 
-ifneq (,$(strip $(foreach dir,$(subst $(comma),$(space),$(COVERAGE_PATHS)),$(filter $(dir)%,$(LOCAL_PATH)))))
-ifeq (,$(strip $(foreach dir,$(subst $(comma),$(space),$(COVERAGE_EXCLUDE_PATHS)),$(filter $(dir)%,$(LOCAL_PATH)))))
+ifneq (,$(strip $(foreach dir,$(COVERAGE_PATHS),$(filter $(dir)%,$(LOCAL_PATH)))))
+ifeq (,$(strip $(foreach dir,$(COVERAGE_EXCLUDE_PATHS),$(filter $(dir)%,$(LOCAL_PATH)))))
   my_native_coverage := true
 else
   my_native_coverage := false
@@ -105,7 +105,7 @@
   ifneq (,$(filter arm64 mips64 x86_64,$(my_arch)))
     my_min_sdk_version := 21
   else
-    my_min_sdk_version := 9
+    my_min_sdk_version := $(MIN_SUPPORTED_SDK_VERSION)
   endif
 
   # Historically we've just set up a bunch of symlinks in prebuilts/ndk to map
@@ -290,6 +290,15 @@
 # Move other ldlibs back to shared libraries
 my_shared_libraries += $(patsubst -l%,lib%,$(filter-out $(my_allowed_ldlibs),$(my_ldlibs)))
 my_ldlibs := $(filter $(my_allowed_ldlibs),$(my_ldlibs))
+else # LOCAL_IS_HOST_MODULE
+  # Add -ldl, -lpthread, -lm and -lrt to host builds to match the default behavior of
+  # device builds
+  ifneq ($($(my_prefix)OS),windows)
+    my_ldlibs += -ldl -lpthread -lm
+    ifneq ($(HOST_OS),darwin)
+      my_ldlibs += -lrt
+    endif
+  endif
 endif
 
 ifneq ($(LOCAL_SDK_VERSION),)
@@ -378,9 +387,9 @@
 # clang is enabled by default for host builds
 # enable it unless we've specifically disabled clang above
 ifdef LOCAL_IS_HOST_MODULE
-    ifeq ($($(my_prefix)OS),windows)
+    ifneq ($($(my_prefix)CLANG_SUPPORTED),true)
         ifeq ($(my_clang),true)
-            $(error $(LOCAL_MODULE_MAKEFILE): $(LOCAL_MODULE): Clang is not yet supported for windows binaries)
+            $(call pretty-error,Clang is not yet supported for $($(my_prefix)OS) binaries)
         endif
         my_clang := false
     else
@@ -797,7 +806,7 @@
 ifneq (,$(LOCAL_SDK_VERSION))
 # Set target-api for LOCAL_SDK_VERSIONs other than current.
 ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
-renderscript_target_api := $(LOCAL_SDK_VERSION)
+renderscript_target_api := $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION))
 endif
 endif  # LOCAL_SDK_VERSION is set
 endif  # LOCAL_RENDERSCRIPT_TARGET_API is set
@@ -1321,6 +1330,22 @@
 asm_objects += $(asm_objects_asm)
 endif
 
+###################################################################
+## When compiling a CFI enabled target, use the .cfi variant of any
+## static dependencies (where they exist).
+##################################################################
+define use_soong_cfi_static_libraries
+  $(foreach l,$(1),$(if $(filter $(l),$(SOONG_CFI_STATIC_LIBRARIES)),\
+      $(l).cfi,$(l)))
+endef
+
+ifneq ($(filter cfi,$(my_sanitize)),)
+  my_whole_static_libraries := $(call use_soong_cfi_static_libraries,\
+    $(my_whole_static_libraries))
+  my_static_libraries := $(call use_soong_cfi_static_libraries,\
+    $(my_static_libraries))
+endif
+
 ###########################################################
 ## When compiling against the VNDK, use LL-NDK libraries
 ###########################################################
@@ -1406,9 +1431,22 @@
 my_warn_types :=
 my_allowed_types := native:ndk
 else ifdef LOCAL_USE_VNDK
-my_link_type := native:vendor
-my_warn_types :=
-my_allowed_types := native:vendor
+    _name := $(patsubst %.vendor,%,$(LOCAL_MODULE))
+    ifneq ($(filter $(_name),$(VNDK_CORE_LIBRARIES) $(VNDK_SAMEPROCESS_LIBRARIES) $(LLNDK_LIBRARIES)),)
+        ifeq ($(filter $(_name),$(VNDK_PRIVATE_LIBRARIES)),)
+            my_link_type := native:vndk
+        else
+            my_link_type := native:vndk_private
+        endif
+        my_warn_types :=
+        my_allowed_types := native:vndk native:vndk_private
+    else
+        # Modules installed to /vendor cannot directly depend on modules marked
+        # with vendor_available: false
+        my_link_type := native:vendor
+        my_warn_types :=
+        my_allowed_types := native:vendor native:vndk
+    endif
 else
 my_link_type := native:platform
 my_warn_types :=
@@ -1642,13 +1680,30 @@
     my_cflags += -DANDROID_STRICT
 endif
 
-# Add -Werror if LOCAL_PATH is in the WARNING_DISALLOWED project list,
-# or not in the WARNING_ALLOWED project list.
-ifneq (,$(strip $(call find_warning_disallowed_projects,$(LOCAL_PATH))))
-  my_cflags_no_override += -Werror
-else
-  ifeq (,$(strip $(call find_warning_allowed_projects,$(LOCAL_PATH))))
-    my_cflags_no_override += -Werror
+# Check if -Werror or -Wno-error is used in C compiler flags.
+# Modules defined in $(SOONG_ANDROID_MK) are checked in soong's cc.go.
+ifneq ($(LOCAL_MODULE_MAKEFILE),$(SOONG_ANDROID_MK))
+  # Header libraries do not need cflags.
+  ifneq (HEADER_LIBRARIES,$(LOCAL_MODULE_CLASS))
+    # Prebuilt modules do not need cflags.
+    ifeq (,$(LOCAL_PREBUILT_MODULE_FILE))
+      my_all_cflags := $(my_cflags) $(my_cppflags) $(my_cflags_no_override)
+      # Issue warning if -Wno-error is used.
+      ifneq (,$(filter -Wno-error,$(my_all_cflags)))
+        $(eval MODULES_USING_WNO_ERROR := $(MODULES_USING_WNO_ERROR) $(LOCAL_MODULE_MAKEFILE):$(LOCAL_MODULE))
+      else
+        # Issue warning if -Werror is not used. Add it.
+        ifeq (,$(filter -Werror,$(my_all_cflags)))
+          # Add -Wall -Werror unless the project is in the WARNING_ALLOWED project list.
+          ifeq (,$(strip $(call find_warning_allowed_projects,$(LOCAL_PATH))))
+            my_cflags := -Wall -Werror $(my_cflags)
+          else
+            $(eval MODULES_ADDED_WALL := $(MODULES_ADDED_WALL) $(LOCAL_MODULE_MAKEFILE):$(LOCAL_MODULE))
+            my_cflags := -Wall $(my_cflags)
+          endif
+        endif
+      endif
+    endif
   endif
 endif
 
@@ -1708,17 +1763,19 @@
 my_ldflags := $(filter-out -l%,$(my_ldlib_flags))
 
 # One last verification check for ldlibs
-ifndef LOCAL_IS_HOST_MODULE
 my_allowed_ldlibs :=
-ifneq ($(LOCAL_SDK_VERSION),)
-  my_allowed_ldlibs := $(addprefix -l,$(NDK_PREBUILT_SHARED_LIBRARIES))
+ifndef LOCAL_IS_HOST_MODULE
+  ifneq ($(LOCAL_SDK_VERSION),)
+    my_allowed_ldlibs := $(addprefix -l,$(NDK_PREBUILT_SHARED_LIBRARIES))
+  endif
+else
+  my_allowed_ldlibs := $($(my_prefix)AVAILABLE_LIBRARIES)
 endif
 
 my_bad_ldlibs := $(filter-out $(my_allowed_ldlibs),$(my_ldlibs))
 ifneq ($(my_bad_ldlibs),)
   $(error $(LOCAL_MODULE_MAKEFILE): $(LOCAL_MODULE): Bad LOCAL_LDLIBS entries: $(my_bad_ldlibs))
 endif
-endif
 
 # my_cxx_ldlibs may contain linker flags need to wrap certain libraries
 # (start-group/end-group), so append after the check above.
@@ -1832,6 +1889,7 @@
         $(my_whole_static_libraries) \
         $(my_shared_libraries) \
         $(my_system_shared_libraries))
+SOONG_CONV.$(LOCAL_MODULE).TYPE := native
 SOONG_CONV := $(SOONG_CONV) $(LOCAL_MODULE)
 endif
 
diff --git a/core/clang/config.mk b/core/clang/config.mk
index 0e59930..408f688 100644
--- a/core/clang/config.mk
+++ b/core/clang/config.mk
@@ -72,6 +72,7 @@
 # INTERNAL_LOCAL_CLANG_EXCEPTION_PROJECTS is defined later in other config.mk.
 LOCAL_CLANG_EXCEPTION_PROJECTS = \
   bionic/tests/ \
+  device/google/contexthub/ \
   device/huawei/angler/ \
   device/lge/bullhead/ \
   external/gentoo/integration/ \
diff --git a/core/clang/versions.mk b/core/clang/versions.mk
index c2473cd..f3a206a 100644
--- a/core/clang/versions.mk
+++ b/core/clang/versions.mk
@@ -1,4 +1,4 @@
 ## Clang/LLVM release versions.
 
-LLVM_PREBUILTS_VERSION ?= clang-4053586
+LLVM_PREBUILTS_VERSION ?= clang-4393122
 LLVM_PREBUILTS_BASE ?= prebuilts/clang/host
diff --git a/core/cleanbuild.mk b/core/cleanbuild.mk
index 0e1c88d..5576785 100644
--- a/core/cleanbuild.mk
+++ b/core/cleanbuild.mk
@@ -13,8 +13,32 @@
 # limitations under the License.
 #
 
-# Don't bother with the cleanspecs if you are running mm/mmm
-ifeq ($(ONE_SHOT_MAKEFILE)$(dont_bother)$(NO_ANDROID_CLEANSPEC),)
+# Absolute path of the present working direcotry.
+# This overrides the shell variable $PWD, which does not necessarily points to
+# the top of the source tree, for example when "make -C" is used in m/mm/mmm.
+PWD := $(shell pwd)
+
+TOP := .
+TOPDIR :=
+
+BUILD_SYSTEM := $(TOPDIR)build/make/core
+
+# Set up various standard variables based on configuration
+# and host information.
+include $(BUILD_SYSTEM)/config.mk
+
+include $(SOONG_MAKEVARS_MK)
+
+include $(BUILD_SYSTEM)/clang/config.mk
+
+# CTS-specific config.
+-include cts/build/config.mk
+# VTS-specific config.
+-include test/vts/tools/vts-tradefed/build/config.mk
+# device-tests-specific-config.
+-include tools/tradefederation/build/suites/device-tests/config.mk
+# general-tests-specific-config.
+-include tools/tradefederation/build/suites/general-tests/config.mk
 
 INTERNAL_CLEAN_STEPS :=
 
@@ -24,7 +48,7 @@
 #
 # $(1): shell command to run
 # $(2): indicate to not use makefile path as part of step id if not empty.
-#       $(2) should only be used in build/core/cleanspec.mk: just for compatibility.
+#       $(2) should only be used in build/make/core/cleanspec.mk: just for compatibility.
 define _add-clean-step
   $(if $(strip $(INTERNAL_CLEAN_BUILD_VERSION)),, \
       $(error INTERNAL_CLEAN_BUILD_VERSION not set))
@@ -43,7 +67,7 @@
   $(eval _acs_makefile_prefix :=)
 endef
 define add-clean-step
-$(eval # for build/core/cleanspec.mk, dont use makefile path as part of step id) \
+$(eval # for build/make/core/cleanspec.mk, dont use makefile path as part of step id) \
 $(if $(filter %/cleanspec.mk,$(lastword $(MAKEFILE_LIST))),\
     $(eval $(call _add-clean-step,$(1),true)),\
     $(eval $(call _add-clean-step,$(1))))
@@ -58,7 +82,7 @@
 
 # If the clean_steps.mk file is missing (usually after a clean build)
 # then we won't do anything.
-CURRENT_CLEAN_BUILD_VERSION := $(INTERNAL_CLEAN_BUILD_VERSION)
+CURRENT_CLEAN_BUILD_VERSION := MISSING
 CURRENT_CLEAN_STEPS := $(INTERNAL_CLEAN_STEPS)
 
 # Read the current state from the file, if present.
@@ -67,7 +91,9 @@
 clean_steps_file := $(PRODUCT_OUT)/clean_steps.mk
 -include $(clean_steps_file)
 
-ifneq ($(CURRENT_CLEAN_BUILD_VERSION),$(INTERNAL_CLEAN_BUILD_VERSION))
+ifeq ($(CURRENT_CLEAN_BUILD_VERSION),MISSING)
+  # Do nothing
+else ifneq ($(CURRENT_CLEAN_BUILD_VERSION),$(INTERNAL_CLEAN_BUILD_VERSION))
   # The major clean version is out-of-date.  Do a full clean, and
   # don't even bother with the clean steps.
   $(info *** A clean build is required because of a recent change.)
@@ -109,57 +135,18 @@
 
 # Write the new state to the file.
 #
-rewrite_clean_steps_file :=
 ifneq ($(CURRENT_CLEAN_BUILD_VERSION)-$(CURRENT_CLEAN_STEPS),$(INTERNAL_CLEAN_BUILD_VERSION)-$(INTERNAL_CLEAN_STEPS))
-rewrite_clean_steps_file := true
-endif
-ifeq ($(wildcard $(clean_steps_file)),)
-# This is the first build.
-rewrite_clean_steps_file := true
-endif
-ifeq ($(rewrite_clean_steps_file),true)
-$(shell \
-  mkdir -p $(dir $(clean_steps_file)) && \
-  echo "CURRENT_CLEAN_BUILD_VERSION := $(INTERNAL_CLEAN_BUILD_VERSION)" > \
-      $(clean_steps_file) ;\
-  echo "CURRENT_CLEAN_STEPS := $(wordlist 1,500,$(INTERNAL_CLEAN_STEPS))" >> $(clean_steps_file) \
- )
-define -cs-write-clean-steps-if-arg1-not-empty
-$(if $(1),$(shell echo "CURRENT_CLEAN_STEPS += $(1)" >> $(clean_steps_file)))
-endef
-$(call -cs-write-clean-steps-if-arg1-not-empty,$(wordlist 501,1000,$(INTERNAL_CLEAN_STEPS)))
-$(call -cs-write-clean-steps-if-arg1-not-empty,$(wordlist 1001,1500,$(INTERNAL_CLEAN_STEPS)))
-$(call -cs-write-clean-steps-if-arg1-not-empty,$(wordlist 1501,2000,$(INTERNAL_CLEAN_STEPS)))
-$(call -cs-write-clean-steps-if-arg1-not-empty,$(wordlist 2001,2500,$(INTERNAL_CLEAN_STEPS)))
-$(call -cs-write-clean-steps-if-arg1-not-empty,$(wordlist 2501,3000,$(INTERNAL_CLEAN_STEPS)))
-$(call -cs-write-clean-steps-if-arg1-not-empty,$(wordlist 3001,99999,$(INTERNAL_CLEAN_STEPS)))
+$(shell mkdir -p $(dir $(clean_steps_file)))
+$(file >$(clean_steps_file).tmp,CURRENT_CLEAN_BUILD_VERSION := $(INTERNAL_CLEAN_BUILD_VERSION)$(newline)CURRENT_CLEAN_STEPS := $(INTERNAL_CLEAN_STEPS)$(newline))
+$(shell if ! cmp -s $(clean_steps_file).tmp $(clean_steps_file); then \
+          mv $(clean_steps_file).tmp $(clean_steps_file); \
+        else \
+          rm $(clean_steps_file).tmp; \
+        fi)
 endif
 
 CURRENT_CLEAN_BUILD_VERSION :=
 CURRENT_CLEAN_STEPS :=
 clean_steps_file :=
-rewrite_clean_steps_file :=
 INTERNAL_CLEAN_STEPS :=
 INTERNAL_CLEAN_BUILD_VERSION :=
-
-endif  # if not ONE_SHOT_MAKEFILE dont_bother NO_ANDROID_CLEANSPEC
-
-###########################################################
-
-.PHONY: clean-jack-files
-clean-jack-files: clean-dex-files
-	$(hide) find $(OUT_DIR) -name "*.jack" | xargs rm -f
-	$(hide) find $(OUT_DIR) -type d -name "jack" | xargs rm -rf
-	@echo "All jack files have been removed."
-
-.PHONY: clean-dex-files
-clean-dex-files:
-	$(hide) find $(OUT_DIR) -name "*.dex" ! -path "*/jack-incremental/*" | xargs rm -f
-	$(hide) for i in `find $(OUT_DIR) -name "*.jar" -o -name "*.apk"` ; do ((unzip -l $$i 2> /dev/null | \
-				grep -q "\.dex$$" && rm -f $$i) || continue ) ; done
-	@echo "All dex files and archives containing dex files have been removed."
-
-.PHONY: clean-jack-incremental
-clean-jack-incremental:
-	$(hide) find $(OUT_DIR) -name "jack-incremental" -type d | xargs rm -rf
-	@echo "All jack incremental dirs have been removed."
diff --git a/core/cleanspec.mk b/core/cleanspec.mk
index 4441e2a..af28954 100644
--- a/core/cleanspec.mk
+++ b/core/cleanspec.mk
@@ -64,6 +64,6 @@
 # ************************************************
 
 subdir_cleanspecs := \
-    $(shell build/tools/findleaves.py $(FIND_LEAVES_EXCLUDES) . CleanSpec.mk)
+    $(file <$(OUT_DIR)/.module_paths/CleanSpec.mk.list)
 include $(subdir_cleanspecs)
 subdir_cleanspecs :=
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index a687866..fc2adde 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -4,6 +4,7 @@
 
 # '',true
 LOCAL_32_BIT_ONLY:=
+LOCAL_AAPT2_ONLY:=
 LOCAL_AAPT_FLAGS:=
 LOCAL_AAPT_INCLUDE_ALL_RESOURCES:=
 LOCAL_ADDITIONAL_CERTIFICATES:=
@@ -121,6 +122,7 @@
 LOCAL_JAR_PROCESSOR:=
 LOCAL_JAR_PROCESSOR_ARGS:=
 LOCAL_JAVACFLAGS:=
+LOCAL_JAVAC_SHARD_SIZE:=
 LOCAL_JAVA_LANGUAGE_VERSION:=
 LOCAL_JAVA_LAYERS_FILE:=
 LOCAL_JAVA_LIBRARIES:=
@@ -200,6 +202,7 @@
 # lite(default),micro,nano,stream,full,nanopb-c,nanopb-c-enable_malloc
 LOCAL_PROTOC_OPTIMIZE_TYPE:=
 LOCAL_PROTO_JAVA_OUTPUT_PARAMS:=
+LOCAL_R8_FLAG_FILES:=
 LOCAL_RECORDED_MODULE_TYPE:=
 LOCAL_RENDERSCRIPT_CC:=
 LOCAL_RENDERSCRIPT_COMPATIBILITY:=
@@ -224,16 +227,24 @@
 LOCAL_SDK_VERSION:=
 LOCAL_SHARED_ANDROID_LIBRARIES:=
 LOCAL_SHARED_LIBRARIES:=
+LOCAL_SOONG_HEADER_JAR :=
+LOCAL_SOONG_DEX_JAR :=
+LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR :=
+LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE :=
+LOCAL_SOONG_RRO_DIRS :=
 # '',true
 LOCAL_SOURCE_FILES_ALL_GENERATED:=
 LOCAL_SRC_FILES:=
 LOCAL_SRC_FILES_EXCLUDE:=
+LOCAL_SRCJARS:=
 LOCAL_STATIC_ANDROID_LIBRARIES:=
 LOCAL_STATIC_JAVA_AAR_LIBRARIES:=
 LOCAL_STATIC_JAVA_LIBRARIES:=
 LOCAL_STATIC_LIBRARIES:=
 LOCAL_STRIP_MODULE:=
 LOCAL_SYSTEM_SHARED_LIBRARIES:=none
+LOCAL_TARGET_REQUIRED_MODULES:=
+LOCAL_TEST_CONFIG_OPTIONS:=
 LOCAL_TEST_DATA:=
 LOCAL_TEST_MODULE_TO_PROGUARD_WITH:=
 LOCAL_TIDY:=
diff --git a/core/combo/TARGET_linux-arm.mk b/core/combo/TARGET_linux-arm.mk
index 2e179f6..73b1c04 100644
--- a/core/combo/TARGET_linux-arm.mk
+++ b/core/combo/TARGET_linux-arm.mk
@@ -29,6 +29,26 @@
 # include defines, and compiler settings for the given architecture
 # version.
 #
+ifeq ($(strip $(TARGET_$(combo_2nd_arch_prefix)CPU_VARIANT)),)
+TARGET_$(combo_2nd_arch_prefix)CPU_VARIANT := generic
+endif
+
+KNOWN_ARMv8_CORES := cortex-a53 cortex-a53.a57 cortex-a73
+KNOWN_ARMv8_CORES += kryo denver64 exynos-m1 exynos-m2
+
+# Many devices (incorrectly) use armv7-a-neon as the 2nd architecture variant
+# for cores that implement armv8-a ISAs. The following sets it to armv8-a.
+ifneq (,$(filter $(TARGET_$(combo_2nd_arch_prefix)CPU_VARIANT), $(KNOWN_ARMv8_CORES)))
+  ifneq ($(TARGET_$(combo_2nd_arch_prefix)ARCH_VARIANT),armv8-a)
+    $(warning $(TARGET_$(combo_2nd_arch_prefix)CPU_VARIANT) is armv8-a.)
+    ifneq (,$(TARGET_$(combo_2nd_arch_prefix)ARCH_VARIANT))
+      $(warning TARGET_$(combo_2nd_arch_prefix)ARCH_VARIANT, $(TARGET_$(combo_2nd_arch_prefix)ARCH_VARIANT), ignored! Use armv8-a instead.)
+    endif
+    # Overwrite TARGET_$(combo_2nd_arch_prefix)ARCH_VARIANT
+    TARGET_$(combo_2nd_arch_prefix)ARCH_VARIANT := armv8-a
+  endif
+endif
+
 ifeq ($(strip $(TARGET_$(combo_2nd_arch_prefix)ARCH_VARIANT)),)
 TARGET_$(combo_2nd_arch_prefix)ARCH_VARIANT := armv5te
 endif
diff --git a/core/combo/arch/arm/armv8-a.mk b/core/combo/arch/arm/armv8-a.mk
new file mode 100644
index 0000000..9ef5c49
--- /dev/null
+++ b/core/combo/arch/arm/armv8-a.mk
@@ -0,0 +1,9 @@
+# Configuration for Linux on ARM.
+# Generating binaries for the ARMv8-a architecture
+#
+# Many libraries are not aware of armv8-a, and AArch32 is (almost) a superset
+# of armv7-a-neon. So just let them think we are just like v7.
+ARCH_ARM_HAVE_ARMV7A            := true
+ARCH_ARM_HAVE_VFP               := true
+ARCH_ARM_HAVE_VFP_D32           := true
+ARCH_ARM_HAVE_NEON              := true
diff --git a/core/combo/arch/x86/atom.mk b/core/combo/arch/x86/atom.mk
index d313a9a..43a170c 100644
--- a/core/combo/arch/x86/atom.mk
+++ b/core/combo/arch/x86/atom.mk
@@ -2,7 +2,7 @@
 # 'x86-atom' arch variant. This is an extension of the 'x86' base variant
 # that adds Atom-specific features.
 #
-# See build/core/combo/arch/x86/x86.mk for differences.
+# See build/make/core/combo/arch/x86/x86.mk for differences.
 #
 ARCH_X86_HAVE_SSSE3 := true
 ARCH_X86_HAVE_MOVBE := true
diff --git a/core/combo/arch/x86/silvermont.mk b/core/combo/arch/x86/silvermont.mk
index 70b718c..cba1079 100644
--- a/core/combo/arch/x86/silvermont.mk
+++ b/core/combo/arch/x86/silvermont.mk
@@ -1,7 +1,7 @@
 # This file contains feature macro definitions specific to the
 # silvermont arch variant.
 #
-# See build/core/combo/arch/x86/x86-atom.mk for differences.
+# See build/make/core/combo/arch/x86/x86-atom.mk for differences.
 #
 
 ARCH_X86_HAVE_SSSE3  := true
diff --git a/core/combo/arch/x86_64/silvermont.mk b/core/combo/arch/x86_64/silvermont.mk
index 70b718c..cba1079 100644
--- a/core/combo/arch/x86_64/silvermont.mk
+++ b/core/combo/arch/x86_64/silvermont.mk
@@ -1,7 +1,7 @@
 # This file contains feature macro definitions specific to the
 # silvermont arch variant.
 #
-# See build/core/combo/arch/x86/x86-atom.mk for differences.
+# See build/make/core/combo/arch/x86/x86-atom.mk for differences.
 #
 
 ARCH_X86_HAVE_SSSE3  := true
diff --git a/core/combo/javac.mk b/core/combo/javac.mk
index 5c2668c..dac2628 100644
--- a/core/combo/javac.mk
+++ b/core/combo/javac.mk
@@ -1,53 +1,19 @@
 # Selects a Java compiler.
 #
-# Inputs:
-#	CUSTOM_JAVA_COMPILER -- "eclipse", "openjdk". or nothing for the system
-#                           default
-#	ALTERNATE_JAVAC -- the alternate java compiler to use
-#
 # Outputs:
-#   COMMON_JAVAC -- Java compiler command with common arguments
+#   ANDROID_JAVA_TOOLCHAIN -- Directory that contains javac and other java tools
 #
 
-ifndef ANDROID_COMPILE_WITH_JACK
-# Defines if compilation with jack is enabled by default.
-ANDROID_COMPILE_WITH_JACK := true
+ANDROID_COMPILE_WITH_JACK := false
+
+ifdef TARGET_BUILD_APPS
+  ifndef TURBINE_ENABLED
+    TURBINE_ENABLED := false
+  endif
 endif
 
-common_jdk_flags := -Xmaxerrs 9999999
+ANDROID_JAVA_TOOLCHAIN := $(ANDROID_JAVA_HOME)/bin
 
-# Use the indexer wrapper to index the codebase instead of the javac compiler
-ifeq ($(ALTERNATE_JAVAC),)
-JAVACC := javac
-else
-JAVACC := $(ALTERNATE_JAVAC)
-endif
-
-JAVA := java
-JAVADOC := javadoc
-JAR := jar
-
-# The actual compiler can be wrapped by setting the JAVAC_WRAPPER var.
-ifdef JAVAC_WRAPPER
-    ifneq ($(JAVAC_WRAPPER),$(firstword $(JAVACC)))
-        JAVACC := $(JAVAC_WRAPPER) $(JAVACC)
-    endif
-endif
-
-# Whatever compiler is on this system.
-COMMON_JAVAC := $(JAVACC) -J-Xmx2048M $(common_jdk_flags)
-
-# Eclipse.
-ifeq ($(CUSTOM_JAVA_COMPILER), eclipse)
-    COMMON_JAVAC := java -Xmx256m -jar prebuilt/common/ecj/ecj.jar -5 \
-        -maxProblems 9999999 -nowarn
-    $(info CUSTOM_JAVA_COMPILER=eclipse)
-endif
-
-GLOBAL_JAVAC_DEBUG_FLAGS := -g
-
-HOST_JAVAC ?= $(COMMON_JAVAC)
-TARGET_JAVAC ?= $(COMMON_JAVAC)
-
-#$(info HOST_JAVAC=$(HOST_JAVAC))
-#$(info TARGET_JAVAC=$(TARGET_JAVAC))
+# TODO(ccross): remove this, it is needed for now because it is used by
+# config.mk before makevars from soong are loaded
+JAVA := $(ANDROID_JAVA_TOOLCHAIN)/java
diff --git a/core/config.mk b/core/config.mk
index cc2b2af..d7e6f1e 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -3,6 +3,20 @@
 # current configuration and platform, which
 # are not specific to what is being built.
 
+ifndef KATI
+$(warning Directly using config.mk from make is no longer supported.)
+$(warning )
+$(warning If you are just attempting to build, you probably need to re-source envsetup.sh:)
+$(warning )
+$(warning $$ source build/envsetup.sh)
+$(warning )
+$(warning If you are attempting to emulate get_build_var, use one of the following:)
+$(warning $$ build/soong/soong_ui.bash --dumpvar-mode)
+$(warning $$ build/soong/soong_ui.bash --dumpvars-mode)
+$(warning )
+$(error done)
+endif
+
 # Only use ANDROID_BUILD_SHELL to wrap around bash.
 # DO NOT use other shells such as zsh.
 ifdef ANDROID_BUILD_SHELL
@@ -44,17 +58,25 @@
 # If a rule fails, delete $@.
 .DELETE_ON_ERROR:
 
-# Check for broken versions of make.
-ifndef KATI
-ifneq (1,$(strip $(shell expr $(MAKE_VERSION) \>= 3.81)))
-$(warning ********************************************************************************)
-$(warning *  You are using version $(MAKE_VERSION) of make.)
-$(warning *  Android can only be built by versions 3.81 and higher.)
-$(warning *  see https://source.android.com/source/download.html)
-$(warning ********************************************************************************)
-$(error stopping)
-endif
-endif
+# Mark variables deprecated/obsolete
+CHANGES_URL := https://android.googlesource.com/platform/build/+/master/Changes.md
+$(KATI_deprecated_var PATH,Do not use PATH directly. See $(CHANGES_URL)#PATH)
+$(KATI_obsolete_var PYTHONPATH,Do not use PYTHONPATH directly. See $(CHANGES_URL)#PYTHONPATH)
+$(KATI_obsolete_var OUT,Use OUT_DIR instead. See $(CHANGES_URL)#OUT)
+$(KATI_obsolete_var ANDROID_HOST_OUT,Use HOST_OUT instead. See $(CHANGES_URL)#ANDROID_HOST_OUT)
+$(KATI_deprecated_var ANDROID_PRODUCT_OUT,Use PRODUCT_OUT instead. See $(CHANGES_URL)#ANDROID_PRODUCT_OUT)
+$(KATI_obsolete_var ANDROID_HOST_OUT_TESTCASES,Use HOST_OUT_TESTCASES instead. See $(CHANGES_URL)#ANDROID_HOST_OUT_TESTCASES)
+$(KATI_obsolete_var ANDROID_TARGET_OUT_TESTCASES,Use TARGET_OUT_TESTCASES instead. See $(CHANGES_URL)#ANDROID_TARGET_OUT_TESTCASES)
+$(KATI_deprecated_var ANDROID_BUILD_TOP,Use '.' instead. See $(CHANGES_URL)#ANDROID_BUILD_TOP)
+$(KATI_obsolete_var \
+  ANDROID_TOOLCHAIN \
+  ANDROID_TOOLCHAIN_2ND_ARCH \
+  ANDROID_DEV_SCRIPTS \
+  ANDROID_EMULATOR_PREBUILTS \
+  ANDROID_PRE_BUILD_PATHS \
+  ,See $(CHANGES_URL)#other_envsetup_variables)
+
+CHANGES_URL :=
 
 # Used to force goals to build.  Only use for conditionally defined goals.
 .PHONY: FORCE
@@ -62,6 +84,9 @@
 
 ORIGINAL_MAKECMDGOALS := $(MAKECMDGOALS)
 
+dist_goal := $(strip $(filter dist,$(MAKECMDGOALS)))
+MAKECMDGOALS := $(strip $(filter-out dist,$(MAKECMDGOALS)))
+
 # Tell python not to spam the source tree with .pyc files.  This
 # only has an effect on python 2.6 and above.
 export PYTHONDONTWRITEBYTECODE := 1
@@ -71,6 +96,8 @@
 $(error Please remove --color=always from your  $$GREP_OPTIONS)
 endif
 
+UNAME := $(shell uname -sm)
+
 SRC_TARGET_DIR := $(TOPDIR)build/target
 SRC_API_DIR := $(TOPDIR)prebuilts/sdk/api
 SRC_SYSTEM_API_DIR := $(TOPDIR)prebuilts/sdk/system-api
@@ -128,6 +155,8 @@
 BUILD_HOST_DALVIK_JAVA_LIBRARY := $(BUILD_SYSTEM)/host_dalvik_java_library.mk
 BUILD_HOST_DALVIK_STATIC_JAVA_LIBRARY := $(BUILD_SYSTEM)/host_dalvik_static_java_library.mk
 
+BUILD_HOST_TEST_CONFIG:= $(BUILD_SYSTEM)/host_test_config.mk
+BUILD_TARGET_TEST_CONFIG:= $(BUILD_SYSTEM)/target_test_config.mk
 
 # ###############################################################
 # Parse out any modifier targets.
@@ -302,7 +331,7 @@
 include $(BUILD_SYSTEM)/combo/select.mk
 endif
 
-ifndef KATI
+ifeq ($(CALLED_FROM_SETUP),true)
 include $(BUILD_SYSTEM)/ccache.mk
 include $(BUILD_SYSTEM)/goma.mk
 
@@ -497,8 +526,29 @@
 prebuilt_sdk_tools := prebuilts/sdk/tools
 prebuilt_sdk_tools_bin := $(prebuilt_sdk_tools)/$(HOST_OS)/bin
 
+# Always use prebuilts for ckati and makeparallel
+prebuilt_build_tools := prebuilts/build-tools
+prebuilt_build_tools_wrappers := prebuilts/build-tools/common/bin
+prebuilt_build_tools_jars := prebuilts/build-tools/common/framework
+ifeq ($(filter address,$(SANITIZE_HOST)),)
+prebuilt_build_tools_bin := $(prebuilt_build_tools)/$(HOST_PREBUILT_TAG)/bin
+else
+prebuilt_build_tools_bin := $(prebuilt_build_tools)/$(HOST_PREBUILT_TAG)/asan/bin
+endif
+
 USE_PREBUILT_SDK_TOOLS_IN_PLACE := true
 
+# Work around for b/68406220
+# This should match the soong version.
+ifndef USE_D8
+  USE_D8 := true
+endif
+
+# Default R8 behavior when USE_R8 is not specified.
+ifndef USE_R8
+  USE_R8 := false
+endif
+
 #
 # Tools that are prebuilts for TARGET_BUILD_APPS
 #
@@ -506,30 +556,25 @@
   AIDL := $(HOST_OUT_EXECUTABLES)/aidl
   AAPT := $(HOST_OUT_EXECUTABLES)/aapt
   AAPT2 := $(HOST_OUT_EXECUTABLES)/aapt2
+  DESUGAR := $(HOST_OUT_JAVA_LIBRARIES)/desugar.jar
   MAINDEXCLASSES := $(HOST_OUT_EXECUTABLES)/mainDexClasses
   SIGNAPK_JAR := $(HOST_OUT_JAVA_LIBRARIES)/signapk$(COMMON_JAVA_PACKAGE_SUFFIX)
   SIGNAPK_JNI_LIBRARY_PATH := $(HOST_OUT_SHARED_LIBRARIES)
   ZIPALIGN := $(HOST_OUT_EXECUTABLES)/zipalign
 
-  ifndef DX_ALT_JAR
-    DX := $(HOST_OUT_EXECUTABLES)/dx
-    DX_COMMAND := $(DX) -JXms16M -JXmx2048M
-  else
-    DX := $(DX_ALT_JAR)
-    DX_COMMAND := $(JAVA) -Xms16M -Xmx2048M -jar $(DX)
-  endif
 else # TARGET_BUILD_APPS || TARGET_BUILD_PDK
-  AIDL := $(prebuilt_sdk_tools_bin)/aidl
+  AIDL := $(prebuilt_build_tools_bin)/aidl
   AAPT := $(prebuilt_sdk_tools_bin)/aapt
   AAPT2 := $(prebuilt_sdk_tools_bin)/aapt2
-  DX := $(prebuilt_sdk_tools)/dx
-  DX_COMMAND := $(DX) -JXms16M -JXmx2048M
+  DESUGAR := $(prebuilt_build_tools_jars)/desugar.jar
   MAINDEXCLASSES := $(prebuilt_sdk_tools)/mainDexClasses
-  ZIPALIGN := $(prebuilt_sdk_tools_bin)/zipalign
   SIGNAPK_JAR := $(prebuilt_sdk_tools)/lib/signapk$(COMMON_JAVA_PACKAGE_SUFFIX)
   SIGNAPK_JNI_LIBRARY_PATH := $(prebuilt_sdk_tools)/$(HOST_OS)/lib64
+  ZIPALIGN := $(prebuilt_build_tools_bin)/zipalign
 endif # TARGET_BUILD_APPS || TARGET_BUILD_PDK
 
+R8_COMPAT_PROGUARD := $(HOST_OUT_EXECUTABLES)/r8-compat-proguard
+
 ifeq (,$(TARGET_BUILD_APPS))
   # Use RenderScript prebuilts for unbundled builds but not PDK builds
   LLVM_RS_CC := $(HOST_OUT_EXECUTABLES)/llvm-rs-cc
@@ -542,28 +587,19 @@
 prebuilt_sdk_tools :=
 prebuilt_sdk_tools_bin :=
 
-# Always use prebuilts for ckati and makeparallel
-prebuilt_build_tools := prebuilts/build-tools
-ifeq ($(filter address,$(SANITIZE_HOST)),)
-prebuilt_build_tools_bin := $(prebuilt_build_tools)/$(HOST_PREBUILT_TAG)/bin
-else
-prebuilt_build_tools_bin := $(prebuilt_build_tools)/$(HOST_PREBUILT_TAG)/asan/bin
-endif
-
 ACP := $(prebuilt_build_tools_bin)/acp
 CKATI := $(prebuilt_build_tools_bin)/ckati
 DEPMOD := $(HOST_OUT_EXECUTABLES)/depmod
 FILESLIST := $(SOONG_HOST_OUT_EXECUTABLES)/fileslist
-IJAR := $(prebuilt_build_tools_bin)/ijar
 MAKEPARALLEL := $(prebuilt_build_tools_bin)/makeparallel
 SOONG_JAVAC_WRAPPER := $(SOONG_HOST_OUT_EXECUTABLES)/soong_javac_wrapper
 SOONG_ZIP := $(SOONG_HOST_OUT_EXECUTABLES)/soong_zip
+MERGE_ZIPS := $(SOONG_HOST_OUT_EXECUTABLES)/merge_zips
 ZIP2ZIP := $(SOONG_HOST_OUT_EXECUTABLES)/zip2zip
 ZIPTIME := $(prebuilt_build_tools_bin)/ziptime
 
 # ---------------------------------------------------------------
 # Generic tools.
-JACK := $(HOST_OUT_EXECUTABLES)/jack
 
 LEX := prebuilts/misc/$(BUILD_OS)-$(HOST_PREBUILT_ARCH)/flex/flex-2.5.39
 # The default PKGDATADIR built in the prebuilt bison is a relative path
@@ -592,7 +628,7 @@
 VTSC := $(HOST_OUT_EXECUTABLES)/vtsc$(HOST_EXECUTABLE_SUFFIX)
 MKBOOTFS := $(HOST_OUT_EXECUTABLES)/mkbootfs$(HOST_EXECUTABLE_SUFFIX)
 MINIGZIP := $(HOST_OUT_EXECUTABLES)/minigzip$(HOST_EXECUTABLE_SUFFIX)
-BRO := $(HOST_OUT_EXECUTABLES)/bro$(HOST_EXECUTABLE_SUFFIX)
+BROTLI := $(HOST_OUT_EXECUTABLES)/brotli$(HOST_EXECUTABLE_SUFFIX)
 ifeq (,$(strip $(BOARD_CUSTOM_MKBOOTIMG)))
 MKBOOTIMG := $(HOST_OUT_EXECUTABLES)/mkbootimg$(HOST_EXECUTABLE_SUFFIX)
 else
@@ -610,15 +646,9 @@
 endif
 APICHECK := $(HOST_OUT_EXECUTABLES)/apicheck$(HOST_EXECUTABLE_SUFFIX)
 FS_GET_STATS := $(HOST_OUT_EXECUTABLES)/fs_get_stats$(HOST_EXECUTABLE_SUFFIX)
-ifeq ($(TARGET_USES_MKE2FS),true)
 MAKE_EXT4FS := $(HOST_OUT_EXECUTABLES)/mke2fs$(HOST_EXECUTABLE_SUFFIX)
 MKEXTUSERIMG := $(HOST_OUT_EXECUTABLES)/mkuserimg_mke2fs.sh
 MKE2FS_CONF := system/extras/ext4_utils/mke2fs.conf
-else
-MAKE_EXT4FS := $(HOST_OUT_EXECUTABLES)/make_ext4fs$(HOST_EXECUTABLE_SUFFIX)
-MKEXTUSERIMG := $(HOST_OUT_EXECUTABLES)/mkuserimg.sh
-MKE2FS_CONF :=
-endif
 BLK_ALLOC_TO_BASE_FS := $(HOST_OUT_EXECUTABLES)/blk_alloc_to_base_fs$(HOST_EXECUTABLE_SUFFIX)
 MAKE_SQUASHFS := $(HOST_OUT_EXECUTABLES)/mksquashfs$(HOST_EXECUTABLE_SUFFIX)
 MKSQUASHFSUSERIMG := $(HOST_OUT_EXECUTABLES)/mksquashfsimage.sh
@@ -630,24 +660,10 @@
 MKTARBALL := build/tools/mktarball.sh
 TUNE2FS := $(HOST_OUT_EXECUTABLES)/tune2fs$(HOST_EXECUTABLE_SUFFIX)
 JARJAR := $(HOST_OUT_JAVA_LIBRARIES)/jarjar.jar
-DESUGAR := $(HOST_OUT_JAVA_LIBRARIES)/desugar.jar
 DATA_BINDING_COMPILER := $(HOST_OUT_JAVA_LIBRARIES)/databinding-compiler.jar
 FAT16COPY := build/tools/fat16copy.py
 CHECK_LINK_TYPE := build/tools/check_link_type.py
 
-ifeq ($(ANDROID_COMPILE_WITH_JACK),true)
-DEFAULT_JACK_ENABLED:=full
-else
-DEFAULT_JACK_ENABLED:=
-endif
-ifneq ($(ANDROID_JACK_EXTRA_ARGS),)
-JACK_DEFAULT_ARGS :=
-DEFAULT_JACK_EXTRA_ARGS := $(ANDROID_JACK_EXTRA_ARGS)
-else
-JACK_DEFAULT_ARGS := $(BUILD_SYSTEM)/jack-default.args
-DEFAULT_JACK_EXTRA_ARGS := @$(JACK_DEFAULT_ARGS)
-endif
-
 PROGUARD := external/proguard/bin/proguard.sh
 JAVATAGS := build/tools/java-event-log-tags.py
 MERGETAGS := build/tools/merge-event-log-tags.py
@@ -669,37 +685,41 @@
 
 FINDBUGS_DIR := external/owasp/sanitizer/tools/findbugs/bin
 FINDBUGS := $(FINDBUGS_DIR)/findbugs
-JACOCO_CLI_JAR := $(HOST_OUT_JAVA_LIBRARIES)/jacoco-cli$(COMMON_JAVA_PACKAGE_SUFFIX)
 
 # Tool to merge AndroidManifest.xmls
-ANDROID_MANIFEST_MERGER := $(JAVA) -classpath prebuilts/devtools/tools/lib/manifest-merger.jar com.android.manifmerger.Main merge
+ANDROID_MANIFEST_MERGER_CLASSPATH := \
+    prebuilts/gradle-plugin/com/android/tools/build/manifest-merger/26.0.0-beta2/manifest-merger-26.0.0-beta2.jar \
+    prebuilts/gradle-plugin/com/android/tools/sdk-common/26.0.0-beta2/sdk-common-26.0.0-beta2.jar \
+    prebuilts/gradle-plugin/com/android/tools/common/26.0.0-beta2/common-26.0.0-beta2.jar \
+    prebuilts/misc/common/guava/guava-21.0.jar
+ANDROID_MANIFEST_MERGER := $(JAVA) \
+    -classpath $(subst $(space),:,$(strip $(ANDROID_MANIFEST_MERGER_CLASSPATH))) \
+    com.android.manifmerger.Merger
 
 COLUMN:= column
 
-# We may not have the right JAVA_HOME/PATH set up yet when this is run from envsetup.sh.
-ifneq ($(CALLED_FROM_SETUP),true)
+ifeq ($(EXPERIMENTAL_USE_OPENJDK9),)
+USE_OPENJDK9 :=
+TARGET_OPENJDK9 :=
+else ifeq ($(EXPERIMENTAL_USE_OPENJDK9),false)
+USE_OPENJDK9 :=
+TARGET_OPENJDK9 :=
+else ifeq ($(EXPERIMENTAL_USE_OPENJDK9),1.8)
+USE_OPENJDK9 := true
+TARGET_OPENJDK9 :=
+else ifeq ($(EXPERIMENTAL_USE_OPENJDK9),true)
+USE_OPENJDK9 := true
+TARGET_OPENJDK9 := true
+endif
 
-# Path to tools.jar, or empty if EXPERIMENTAL_USE_OPENJDK9 is set
+# Path to tools.jar, or empty if USE_OPENJDK9 is unset
 HOST_JDK_TOOLS_JAR :=
 # TODO: Remove HOST_JDK_TOOLS_JAR and all references to it once OpenJDK 8
-# toolchains are no longer supported (i.e. when what is now
-# EXPERIMENTAL_USE_OPENJDK9 becomes the standard). http://b/38418220
-ifeq ($(EXPERIMENTAL_USE_OPENJDK9),)
-HOST_JDK_TOOLS_JAR := $(shell $(BUILD_SYSTEM)/find-jdk-tools-jar.sh)
-
-ifneq ($(HOST_JDK_TOOLS_JAR),)
-ifeq ($(wildcard $(HOST_JDK_TOOLS_JAR)),)
-$(error Error: could not find jdk tools.jar at $(HOST_JDK_TOOLS_JAR), please check if your JDK was installed correctly)
-endif
-endif
-endif # ifeq ($(EXPERIMENTAL_USE_OPENJDK9),)
-
-# Is the host JDK 64-bit version?
-HOST_JDK_IS_64BIT_VERSION :=
-ifneq ($(filter 64-Bit, $(shell $(JAVA) -version 2>&1)),)
-HOST_JDK_IS_64BIT_VERSION := true
-endif
-endif  # CALLED_FROM_SETUP not true
+# toolchains are no longer supported (i.e. when USE_OPENJDK9 is enforced).
+# http://b/38418220
+ifndef USE_OPENJDK9
+HOST_JDK_TOOLS_JAR := $(ANDROID_JAVA_TOOLCHAIN)/../lib/tools.jar
+endif # ifndef USE_OPENJDK9
 
 # It's called md5 on Mac OS and md5sum on Linux
 ifeq ($(HOST_OS),darwin)
@@ -727,6 +747,27 @@
   PRODUCT_FULL_TREBLE := true
 endif
 
+requirements := \
+    PRODUCT_TREBLE_LINKER_NAMESPACES \
+    PRODUCT_SEPOLICY_SPLIT \
+    PRODUCT_ENFORCE_VINTF_MANIFEST \
+
+# If it is overriden, then the requirement override is taken, otherwise it's
+# PRODUCT_FULL_TREBLE
+$(foreach req,$(requirements),$(eval \
+    $(req) := $(if $($(req)_OVERRIDE),$($(req)_OVERRIDE),$(PRODUCT_FULL_TREBLE))))
+
+PRODUCT_FULL_TREBLE_OVERRIDE ?=
+$(foreach req,$(requirements),$(eval $(req)_OVERRIDE ?=))
+
+.KATI_READONLY := \
+    PRODUCT_FULL_TREBLE_OVERRIDE \
+    $(foreach req,$(requirements),$(req)_OVERRIDE) \
+    $(requirements) \
+    PRODUCT_FULL_TREBLE \
+
+requirements :=
+
 ifdef PRODUCT_SHIPPING_API_LEVEL
   ifneq ($(call math_gt_or_eq,$(PRODUCT_SHIPPING_API_LEVEL),27),)
     ifneq ($(TARGET_USES_MKE2FS),true)
@@ -782,6 +823,9 @@
   FRAMEWORK_COMPATIBILITY_MATRIX_FILE := hardware/interfaces/compatibility_matrix.current.xml
 endif
 
+BUILD_NUMBER_FROM_FILE := $$(cat $(OUT_DIR)/build_number.txt)
+BUILD_DATETIME_FROM_FILE := $$(cat $(OUT_DIR)/build_date.txt)
+
 # ###############################################################
 # Set up final options.
 # ###############################################################
@@ -859,9 +903,17 @@
     $(patsubst $(HISTORICAL_SDK_VERSIONS_ROOT)/%/android.jar,%, \
     $(wildcard $(HISTORICAL_SDK_VERSIONS_ROOT)/*/android.jar)))
 
+TARGET_AVAILABLE_SDK_VERSIONS := $(addprefix system_,$(call numerically_sort,\
+    $(patsubst $(HISTORICAL_SDK_VERSIONS_ROOT)/%/android_system.jar,%, \
+    $(wildcard $(HISTORICAL_SDK_VERSIONS_ROOT)/*/android_system.jar)))) \
+    $(TARGET_AVAILABLE_SDK_VERSIONS)
+
 # We don't have prebuilt test_current SDK yet.
 TARGET_AVAILABLE_SDK_VERSIONS := test_current $(TARGET_AVAILABLE_SDK_VERSIONS)
 
+TARGET_SDK_VERSIONS_WITHOUT_JAVA_18_SUPPORT := $(call numbers_less_than,24,$(TARGET_AVAILABLE_SDK_VERSIONS))
+TARGET_SDK_VERSIONS_WITHOUT_JAVA_19_SUPPORT := $(call numbers_less_than,27,$(TARGET_AVAILABLE_SDK_VERSIONS))
+
 INTERNAL_PLATFORM_API_FILE := $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/public_api.txt
 INTERNAL_PLATFORM_REMOVED_API_FILE := $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/removed.txt
 INTERNAL_PLATFORM_SYSTEM_API_FILE := $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/system-api.txt
@@ -882,57 +934,23 @@
 
 RS_PREBUILT_CLCORE := prebuilts/sdk/renderscript/lib/$(TARGET_ARCH)/librsrt_$(TARGET_ARCH).bc
 RS_PREBUILT_COMPILER_RT := prebuilts/sdk/renderscript/lib/$(TARGET_ARCH)/libcompiler_rt.a
-ifeq (true,$(TARGET_IS_64_BIT))
-RS_PREBUILT_LIBPATH := -L prebuilts/ndk/r10/platforms/android-21/arch-$(TARGET_ARCH)/usr/lib64 \
-                       -L prebuilts/ndk/r10/platforms/android-21/arch-$(TARGET_ARCH)/usr/lib
-else
-RS_PREBUILT_LIBPATH := -L prebuilts/ndk/r10/platforms/android-9/arch-$(TARGET_ARCH)/usr/lib
-endif
 
 # API Level lists for Renderscript Compat lib.
 RSCOMPAT_32BIT_ONLY_API_LEVELS := 8 9 10 11 12 13 14 15 16 17 18 19 20
 RSCOMPAT_NO_USAGEIO_API_LEVELS := 8 9 10 11 12 13
 
-ifeq ($(JAVA_NOT_REQUIRED),true)
-# Remove java and tools from our path so that we make sure nobody uses them.
-unexport ANDROID_JAVA_HOME
-unexport JAVA_HOME
-export ANDROID_BUILD_PATHS:=$(abspath $(BUILD_SYSTEM)/no_java_path):$(ANDROID_BUILD_PATHS)
-export PATH:=$(abspath $(BUILD_SYSTEM)/no_java_path):$(PATH)
+# Add BUILD_NUMBER to apps default version name if it's unbundled build.
+ifdef TARGET_BUILD_APPS
+TARGET_BUILD_WITH_APPS_VERSION_NAME := true
 endif
 
-# Projects clean of compiler warnings should be compiled with -Werror.
-# If most modules in a directory such as external/ have warnings,
-# the directory should be in ANDROID_WARNING_ALLOWED_PROJECTS list.
-# When some of its subdirectories are cleaned up, the subdirectories
-# can be added into ANDROID_WARNING_DISALLOWED_PROJECTS list, e.g.
-# external/fio/.
-ANDROID_WARNING_DISALLOWED_PROJECTS := \
-    art/% \
-    bionic/% \
-    external/fio/% \
-    hardware/interfaces/% \
+ifdef TARGET_BUILD_WITH_APPS_VERSION_NAME
+APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)-$(BUILD_NUMBER_FROM_FILE)
+else
+APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)
+endif
 
-define find_warning_disallowed_projects
-    $(filter $(ANDROID_WARNING_DISALLOWED_PROJECTS),$(1)/)
-endef
-
-# Projects with compiler warnings are compiled without -Werror.
-ANDROID_WARNING_ALLOWED_PROJECTS := \
-    bootable/% \
-    cts/% \
-    dalvik/% \
-    development/% \
-    device/% \
-    external/% \
-    frameworks/% \
-    hardware/% \
-    packages/% \
-    system/% \
-    test/vts/% \
-    tools/adt/idea/android/ultimate/get_modification_time/jni/% \
-    vendor/% \
-
+# ANDROID_WARNING_ALLOWED_PROJECTS is generated by build/soong.
 define find_warning_allowed_projects
     $(filter $(ANDROID_WARNING_ALLOWED_PROJECTS),$(1)/)
 endef
@@ -953,7 +971,7 @@
     vbmetaimage-nodeps \
     product-graph dump-products
 
-ifndef KATI
+ifeq ($(CALLED_FROM_SETUP),true)
 include $(BUILD_SYSTEM)/ninja_config.mk
 include $(BUILD_SYSTEM)/soong_config.mk
 endif
diff --git a/core/config_sanitizers.mk b/core/config_sanitizers.mk
index 0d182e9..9415143 100644
--- a/core/config_sanitizers.mk
+++ b/core/config_sanitizers.mk
@@ -22,6 +22,30 @@
   endif
 endif
 
+# Disable global integer_overflow in excluded paths.
+ifneq ($(filter integer_overflow, $(my_global_sanitize)),)
+  combined_exclude_paths := $(INTEGER_OVERFLOW_EXCLUDE_PATHS) \
+                            $(PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS)
+
+  ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
+         $(filter $(dir)%,$(LOCAL_PATH)))),)
+    my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize))
+    my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag))
+  endif
+endif
+
+# Disable global CFI in excluded paths
+ifneq ($(filter cfi, $(my_global_sanitize)),)
+  combined_exclude_paths := $(CFI_EXCLUDE_PATHS) \
+                            $(PRODUCT_CFI_EXCLUDE_PATHS)
+
+  ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
+         $(filter $(dir)%,$(LOCAL_PATH)))),)
+    my_global_sanitize := $(filter-out cfi,$(my_global_sanitize))
+    my_global_sanitize_diag := $(filter-out cfi,$(my_global_sanitize_diag))
+  endif
+endif
+
 ifneq ($(my_global_sanitize),)
   my_sanitize := $(my_global_sanitize) $(my_sanitize)
 endif
@@ -72,6 +96,18 @@
   my_sanitize_diag :=
 endif
 
+# Enable CFI in included paths.
+ifeq ($(filter cfi, $(my_sanitize)),)
+  combined_include_paths := $(CFI_INCLUDE_PATHS) \
+                            $(PRODUCT_CFI_INCLUDE_PATHS)
+
+  ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_include_paths)),\
+         $(filter $(dir)%,$(LOCAL_PATH)))),)
+    my_sanitize := cfi $(my_sanitize)
+    my_sanitize_diag := cfi $(my_sanitize_diag)
+  endif
+endif
+
 # If CFI is disabled globally, remove it from my_sanitize.
 ifeq ($(strip $(ENABLE_CFI)),false)
   my_sanitize := $(filter-out cfi,$(my_sanitize))
@@ -96,6 +132,12 @@
   my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
 endif
 
+# Disable CFI for host targets
+ifdef LOCAL_IS_HOST_MODULE
+  my_sanitize := $(filter-out cfi,$(my_sanitize))
+  my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
+endif
+
 # Support for local sanitize blacklist paths.
 ifneq ($(my_sanitize)$(my_global_sanitize),)
   ifneq ($(LOCAL_SANITIZE_BLACKLIST),)
@@ -103,17 +145,8 @@
   endif
 endif
 
+# Disable integer_overflow if LOCAL_NOSANITIZE=integer.
 ifneq ($(filter integer_overflow, $(my_global_sanitize) $(my_sanitize)),)
-  # Disable integer_overflow in excluded paths.
-  combined_exclude_paths := $(INTEGER_OVERFLOW_EXCLUDE_PATHS) \
-                            $(PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS)
-
-  ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
-         $(filter $(dir)%,$(LOCAL_PATH)))),)
-    my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
-    my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
-  endif
-  # Disable integer_overflow if LOCAL_NOSANITIZE=integer.
   ifneq ($(filter integer, $(strip $(LOCAL_NOSANITIZE))),)
     my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
     my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
@@ -134,6 +167,8 @@
     else
         $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
     endif
+  else
+    my_shared_libraries += $(TSAN_RUNTIME_LIBRARY)
   endif
 endif
 
@@ -172,7 +207,7 @@
   ifeq ($(filter address,$(my_sanitize)),)
     $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address')
   endif
-  my_cflags += -fsanitize-coverage=trace-pc-guard
+  my_cflags += -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp
   my_sanitize := $(filter-out coverage,$(my_sanitize))
 endif
 
@@ -214,7 +249,6 @@
   ifdef LOCAL_IS_HOST_MODULE
     my_cflags += -fno-sanitize-recover=all
     my_ldflags += -fsanitize=$(fsanitize_arg)
-    my_ldlibs += -lrt -ldl
   else
     my_cflags += -fsanitize-trap=all
     my_cflags += -ftrap-function=abort
@@ -231,12 +265,21 @@
   # entire module.
   LOCAL_ARM_MODE := thumb
   my_cflags += $(CFI_EXTRA_CFLAGS)
+  # Only append the default visibility flag if -fvisibility has not already been
+  # set to hidden.
+  ifeq ($(filter -fvisibility=hidden,$(LOCAL_CFLAGS)),)
+    my_cflags += -fvisibility=default
+  endif
   my_ldflags += $(CFI_EXTRA_LDFLAGS)
   my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so
-  # Workaround for b/33678192. CFI jumptables need Thumb2 codegen.  Revert when
-  # Clang is updated past r290384.
-  ifneq ($(filter arm,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
-    my_ldflags += -march=armv7-a
+
+  ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
+        my_ldflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_ldflags))
+        my_cflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_cflags))
+  else
+        # Apply the version script to non-static executables
+        my_ldflags += -Wl,--version-script,build/soong/cc/config/cfi_exports.map
+        LOCAL_ADDITIONAL_DEPENDENCIES += build/soong/cc/config/cfi_exports.map
   endif
 endif
 
@@ -246,7 +289,6 @@
   ifdef LOCAL_IS_HOST_MODULE
     # -nodefaultlibs (provided with libc++) prevents the driver from linking
     # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
-    my_ldlibs += -lm -lpthread
     my_ldflags += -Wl,--no-as-needed
   else
     # Add asan libraries unless LOCAL_MODULE is the asan library.
@@ -284,23 +326,22 @@
   endif
 endif
 
-ifneq ($(filter undefined,$(my_sanitize)),)
-  ifndef LOCAL_IS_HOST_MODULE
-    $(error ubsan is not yet supported on the target)
-  endif
-endif
-
 ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
   recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
   my_cflags += -fsanitize-recover=$(recover_arg)
 endif
 
 ifneq ($(my_sanitize_diag),)
-  notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
-  my_cflags += -fno-sanitize-trap=$(notrap_arg)
-  # Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
-  ifeq ($(filter address thread,$(my_sanitize)),)
-    # Does not have to be the first DT_NEEDED unlike ASan.
-    my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
+  # TODO(vishwath): Add diagnostic support for static executables once
+  # we switch to clang-4393122 (which adds the static ubsan runtime
+  # that this depends on)
+  ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
+    notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
+    my_cflags += -fno-sanitize-trap=$(notrap_arg)
+    # Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
+    ifeq ($(filter address thread,$(my_sanitize)),)
+      # Does not have to be the first DT_NEEDED unlike ASan.
+      my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
+    endif
   endif
 endif
diff --git a/core/configure_local_jack.mk b/core/configure_local_jack.mk
deleted file mode 100644
index f8049a3..0000000
--- a/core/configure_local_jack.mk
+++ /dev/null
@@ -1,44 +0,0 @@
-#
-# 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.
-#
-
-ifdef ANDROID_FORCE_JACK_ENABLED
-LOCAL_JACK_ENABLED := $(ANDROID_FORCE_JACK_ENABLED)
-endif
-
-ifneq ($(ANDROID_COMPILE_WITH_JACK),true)
-LOCAL_JACK_ENABLED :=
-endif
-
-LOCAL_JACK_ENABLED := $(strip $(LOCAL_JACK_ENABLED))
-LOCAL_MODULE := $(strip $(LOCAL_MODULE))
-
-valid_jack_enabled_values := full incremental javac_frontend disabled
-
-ifdef LOCAL_JACK_ENABLED
-  ifneq ($(LOCAL_JACK_ENABLED),$(filter $(firstword $(LOCAL_JACK_ENABLED)),$(valid_jack_enabled_values)))
-    $(error $(LOCAL_PATH): invalid LOCAL_JACK_ENABLED "$(LOCAL_JACK_ENABLED)" for $(LOCAL_MODULE))
-  endif
-
-  ifeq ($(LOCAL_JACK_ENABLED),disabled)
-    LOCAL_JACK_ENABLED :=
-  endif
-endif
-
-ifdef $(LOCAL_MODULE).JACK_VERSION
-LOCAL_JACK_VERSION := $($(LOCAL_MODULE).JACK_VERSION)
-else
-LOCAL_JACK_VERSION := $(JACK_DEFAULT_VERSION)
-endif
diff --git a/core/cxx_stl_setup.mk b/core/cxx_stl_setup.mk
index 0baec26..f07659d 100644
--- a/core/cxx_stl_setup.mk
+++ b/core/cxx_stl_setup.mk
@@ -89,7 +89,6 @@
     ifdef LOCAL_IS_HOST_MODULE
         my_cppflags += -nostdinc++
         my_ldflags += -nodefaultlibs
-        my_ldlibs += -lpthread -lm
         my_cxx_ldlibs += $($($(my_prefix)OS)_$(my_link_type)_gcclibs)
     else
         ifeq (arm,$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH))
diff --git a/core/definitions.mk b/core/definitions.mk
index 95e9a02..96a6663 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -671,27 +671,28 @@
 ## $(2): Non-empty if IS_HOST_MODULE
 ###########################################################
 
-# $(1): library name
-# $(2): Non-empty if IS_HOST_MODULE
-define _java-lib-dir
-$(call intermediates-dir-for, \
-	JAVA_LIBRARIES,$(1),$(2),COMMON)
-endef
-
-# $(1): library name
-# $(2): Non-empty if IS_HOST_MODULE
-define _java-lib-full-classes.jar
-$(call _java-lib-dir,$(1),$(2))/classes.jar
-endef
-
 # Get the jar files (you can pass to "javac -classpath") of static or shared
 # Java libraries that you want to link against.
 # $(1): library name list
 # $(2): Non-empty if IS_HOST_MODULE
 define java-lib-files
-$(foreach lib,$(1),$(call _java-lib-full-classes.jar,$(lib),$(2)))
+$(foreach lib,$(1),$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),$(2),COMMON)/classes.jar)
 endef
 
+# Get the header jar files (you can pass to "javac -classpath") of static or shared
+# Java libraries that you want to link against.
+# $(1): library name list
+# $(2): Non-empty if IS_HOST_MODULE
+ifneq ($(TURBINE_ENABLED),false)
+define java-lib-header-files
+$(foreach lib,$(1),$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),$(2),COMMON)/classes-header.jar)
+endef
+else
+define java-lib-header-files
+$(call java-lib-files,$(1),$(2))
+endef
+endif
+
 # Get the dependency files (you can put on the right side of "|" of a build rule)
 # of the Java libraries.
 # $(1): library name list
@@ -704,25 +705,26 @@
 $(call java-lib-files,$(1),$(2))
 endef
 
-
-###########################################################
-## Convert "core ext framework" to "out/.../classes.jack ..."
-## $(1): library list
-## $(2): Non-empty if IS_HOST_MODULE
-###########################################################
-
-# $(1): library name
-# $(2): Non-empty if IS_HOST_MODULE
-define _jack-lib-full-classes
-$(call _java-lib-dir,$(1),$(2))/classes.jack
-endef
-
+# Get the jar files (you can pass to "javac -classpath") of static or shared
+# APK libraries that you want to link against.
 # $(1): library name list
-# $(2): Non-empty if IS_HOST_MODULE
-define jack-lib-files
-$(foreach lib,$(1),$(call _jack-lib-full-classes,$(lib),$(2)))
+define app-lib-files
+$(foreach lib,$(1),$(call intermediates-dir-for,APPS,$(lib),,COMMON)/classes.jar)
 endef
 
+# Get the header jar files (you can pass to "javac -classpath") of static or shared
+# APK libraries that you want to link against.
+# $(1): library name list
+ifneq ($(TURBINE_ENABLED),false)
+define app-lib-header-files
+$(foreach lib,$(1),$(call intermediates-dir-for,APPS,$(lib),,COMMON)/classes-header.jar)
+endef
+else
+define app-lib-header-files
+$(call app-lib-files,$(1))
+endef
+endif
+
 ###########################################################
 ## Returns true if $(1) and $(2) are equal.  Returns
 ## the empty string if they are not equal.
@@ -1048,8 +1050,9 @@
 	-Wl,-rpath,\$$ORIGIN/../lib \
 	$(dir $@)/$(notdir $(<:.bc=.o)) \
 	$(RS_PREBUILT_COMPILER_RT) \
-	-o $@ $(TARGET_GLOBAL_LDFLAGS) -Wl,--hash-style=sysv -L prebuilts/gcc/ \
-	$(RS_PREBUILT_LIBPATH) \
+	-o $@ $(TARGET_GLOBAL_LDFLAGS) -Wl,--hash-style=sysv \
+	-L $(SOONG_OUT_DIR)/ndk/platforms/android-$(PRIVATE_SDK_VERSION)/arch-$(TARGET_ARCH)/usr/lib64 \
+	-L $(SOONG_OUT_DIR)/ndk/platforms/android-$(PRIVATE_SDK_VERSION)/arch-$(TARGET_ARCH)/usr/lib \
 	$(call intermediates-dir-for,SHARED_LIBRARIES,libRSSupport)/libRSSupport.so \
 	-lm -lc
 endef
@@ -1553,7 +1556,7 @@
         filelist="$$filelist $$ldir/$$ext$$f"; \
     done ; \
     $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_AR) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)TARGET_GLOBAL_ARFLAGS) \
-        $(2) $$filelist
+        $(PRIVATE_ARFLAGS) $(2) $$filelist
 
 endef
 
@@ -1999,17 +2002,6 @@
 ## Commands for running javac to make .class files
 ###########################################################
 
-# Add BUILD_NUMBER to apps default version name if it's unbundled build.
-ifdef TARGET_BUILD_APPS
-TARGET_BUILD_WITH_APPS_VERSION_NAME := true
-endif
-
-ifdef TARGET_BUILD_WITH_APPS_VERSION_NAME
-APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)-$(BUILD_NUMBER_FROM_FILE)
-else
-APPS_DEFAULT_VERSION_NAME := $(PLATFORM_VERSION)
-endif
-
 # b/37750224
 AAPT_ASAN_OPTIONS := ASAN_OPTIONS=detect_leaks=0
 
@@ -2043,9 +2035,11 @@
     $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
     $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
     --skip-symbols-without-default-localization
+# So that we re-run aapt when the list of input files change
+$(hide) echo $(PRIVATE_RESOURCE_LIST) >/dev/null
 endef
 
-# Search for generated R.java/Manifest.java, copy the found R.java as $@.
+# Search for generated R.java/Manifest.java, copy the found R.java as $1.
 # Also copy them to a central 'R' directory to make it easier to add the files to an IDE.
 define find-generated-R.java
 $(hide) for GENERATED_MANIFEST_FILE in `find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) \
@@ -2060,11 +2054,11 @@
     mkdir -p $(TARGET_COMMON_OUT_ROOT)/R/$$dir; \
     $(ACP) -fp $$GENERATED_R_FILE $(TARGET_COMMON_OUT_ROOT)/R/$$dir \
       || exit 31; \
-    $(ACP) -fp $$GENERATED_R_FILE $@ || exit 32; \
+    $(ACP) -fp $$GENERATED_R_FILE $1 || exit 32; \
   done;
 @# Ensure that the target file is always created, i.e. also in case we did not
 @# enter the GENERATED_R_FILE-loop above. This avoids unnecessary rebuilding.
-$(hide) touch $@
+$(hide) touch $1
 endef
 
 ###########################################################
@@ -2184,58 +2178,83 @@
       exit 1; \
     fi; \
     unzip -qo $$f -d $(2); \
+    rm -f $(2)/module-info.class; \
   done
   $(if $(PRIVATE_DONT_DELETE_JAR_META_INF),,$(hide) rm -rf $(2)/META-INF)
 endef
 
-# Call jack
+# Return jar arguments to compress files in a given directory
+# $(1): directory
 #
-define call-jack
- JACK_VERSION=$(PRIVATE_JACK_VERSION) $(JACK) $(DEFAULT_JACK_EXTRA_ARGS)
+# Returns an @-file argument that contains the output of a subshell
+# that looks like -C $(1) path/to/file1 -C $(1) path/to/file2
+# Also adds "-C out/empty ." which avoids errors in jar when
+# there are no files in the directory.
+define jar-args-sorted-files-in-directory
+    @<(find $(1) -type f | sort | $(JAR_ARGS) $(1); echo "-C $(EMPTY_DIRECTORY) .")
 endef
 
-# Common definition to invoke javac on the host and target.
-#
+# append additional Java sources(resources/Proto sources, and etc) to $(1).
+define fetch-additional-java-source
+$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
+    find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(1); \
+fi
+$(if $(PRIVATE_HAS_PROTO_SOURCES), \
+    $(hide) find $(PRIVATE_PROTO_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(1))
+$(if $(PRIVATE_HAS_RS_SOURCES), \
+    $(hide) find $(PRIVATE_RS_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(1))
+endef
+
 # Some historical notes:
 # - below we write the list of java files to java-source-list to avoid argument
 #   list length problems with Cygwin
 # - we filter out duplicate java file names because eclipse's compiler
 #   doesn't like them.
+define write-java-source-list
+@echo "$($(PRIVATE_PREFIX)DISPLAY) Java source list: $(PRIVATE_MODULE)"
+$(hide) rm -f $@
+$(call dump-words-to-file,$(sort $(PRIVATE_JAVA_SOURCES)),$@.tmp)
+$(call fetch-additional-java-source,$@.tmp)
+$(hide) tr ' ' '\n' < $@.tmp | $(NORMALIZE_PATH) | sort -u > $@
+endef
+
+# $(1): sharding number.
+# $(2): Java source files paths.
+define save-sharded-java-source-list
+$(java_source_list_file).shard.$(1): $(2) $$(NORMALIZE_PATH)
+	@echo "shard java source list: $$@"
+	rm -f $$@
+	$$(call dump-words-to-file,$(2),$$@.tmp)
+	$(hide) tr ' ' '\n' < $$@.tmp | $$(NORMALIZE_PATH) | sort -u > $$@
+endef
+
+# Common definition to invoke javac on the host and target.
 #
 # $(1): javac
-# $(2): bootclasspath
+# $(2): classpath_libs
 define compile-java
 $(hide) rm -f $@
 $(hide) rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) $(PRIVATE_ANNO_INTERMEDIATES_DIR)
 $(hide) mkdir -p $(dir $@)
 $(hide) mkdir -p $(PRIVATE_CLASS_INTERMEDIATES_DIR) $(PRIVATE_ANNO_INTERMEDIATES_DIR)
-$(call unzip-jar-files,$(PRIVATE_STATIC_JAVA_LIBRARIES),$(PRIVATE_CLASS_INTERMEDIATES_DIR))
-$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list)
-$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
-          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list; \
-fi
-$(if $(PRIVATE_HAS_PROTO_SOURCES), \
-    $(hide) find $(PRIVATE_PROTO_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list )
-$(if $(PRIVATE_HAS_RS_SOURCES), \
-    $(hide) find $(PRIVATE_RS_SOURCE_INTERMEDIATES_DIR) -name '*.java' -and -not -name '.*' >> $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list )
-$(hide) tr ' ' '\n' < $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list \
-    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
-$(hide) if [ -s $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
-    $(SOONG_JAVAC_WRAPPER) $(1) -encoding UTF-8 \
+$(hide) if [ -s $(PRIVATE_JAVA_SOURCE_LIST) ] ; then \
+    $(SOONG_JAVAC_WRAPPER) $(JAVAC_WRAPPER) $(1) -encoding UTF-8 \
     $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
-    $(2) \
+    $(if $(PRIVATE_USE_SYSTEM_MODULES), \
+      $(addprefix --system=,$(PRIVATE_SYSTEM_MODULES)), \
+      $(addprefix -bootclasspath ,$(strip \
+          $(call normalize-path-list,$(PRIVATE_BOOTCLASSPATH)) \
+          $(PRIVATE_EMPTY_BOOTCLASSPATH)))) \
     $(addprefix -classpath ,$(strip \
-        $(call normalize-path-list,$(PRIVATE_ALL_JAVA_LIBRARIES)))) \
+        $(call normalize-path-list,$(2)))) \
     $(if $(findstring true,$(PRIVATE_WARNINGS_ENABLE)),$(xlint_unchecked),) \
-    -extdirs "" -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) -s $(PRIVATE_ANNO_INTERMEDIATES_DIR) \
+    -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) -s $(PRIVATE_ANNO_INTERMEDIATES_DIR) \
     $(PRIVATE_JAVACFLAGS) \
-    \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq \
+    \@$(PRIVATE_JAVA_SOURCE_LIST) \
     || ( rm -rf $(PRIVATE_CLASS_INTERMEDIATES_DIR) ; exit 41 ) \
 fi
 $(if $(PRIVATE_JAVA_LAYERS_FILE), $(hide) build/tools/java-layers.py \
-    $(PRIVATE_JAVA_LAYERS_FILE) \@$(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq,)
-$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list
-$(hide) rm -f $(PRIVATE_CLASS_INTERMEDIATES_DIR)/java-source-list-uniq
+    $(PRIVATE_JAVA_LAYERS_FILE) @$(PRIVATE_JAVA_SOURCE_LIST),)
 $(if $(PRIVATE_JAR_EXCLUDE_FILES), $(hide) find $(PRIVATE_CLASS_INTERMEDIATES_DIR) \
     -name $(word 1, $(PRIVATE_JAR_EXCLUDE_FILES)) \
     $(addprefix -o -name , $(wordlist 2, 999, $(PRIVATE_JAR_EXCLUDE_FILES))) \
@@ -2248,153 +2267,62 @@
 $(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) rm -rf \
     $(foreach pkg, $(PRIVATE_JAR_EXCLUDE_PACKAGES), \
         $(PRIVATE_CLASS_INTERMEDIATES_DIR)/$(subst .,/,$(pkg))))
-$(if $(PRIVATE_JAR_MANIFEST), \
-    $(hide) sed -e "s/%BUILD_NUMBER%/$(BUILD_NUMBER_FROM_FILE)/" \
-            $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf && \
-        $(JAR) -cfm $@ $(dir $@)/manifest.mf \
-            -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ., \
-    $(hide) $(JAR) -cf $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .)
+$(hide) $(JAR) -cf $@ $(call jar-args-sorted-files-in-directory,$(PRIVATE_CLASS_INTERMEDIATES_DIR))
 $(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
 endef
 
-define transform-java-to-classes.jar
-@echo "$($(PRIVATE_PREFIX)DISPLAY) Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
-$(call compile-java,$(TARGET_JAVAC),$(PRIVATE_BOOTCLASSPATH))
+# $(1): Javac output jar name.
+# $(2): Java source list file.
+# $(3): Java header libs.
+# $(4): Javac sharding number.
+# $(5): Javac sources deps (the arg may neeed $$ in case of containing '#')
+define create-classes-full-debug.jar
+$(1): PRIVATE_JAVACFLAGS := $$(LOCAL_JAVACFLAGS) $$(annotation_processor_flags)
+$(1): PRIVATE_JAR_EXCLUDE_FILES := $$(LOCAL_JAR_EXCLUDE_FILES)
+$(1): PRIVATE_JAR_PACKAGES := $$(LOCAL_JAR_PACKAGES)
+$(1): PRIVATE_JAR_EXCLUDE_PACKAGES := $$(LOCAL_JAR_EXCLUDE_PACKAGES)
+$(1): PRIVATE_DONT_DELETE_JAR_META_INF := $$(LOCAL_DONT_DELETE_JAR_META_INF)
+$(1): PRIVATE_JAVA_SOURCE_LIST := $(2)
+$(1): PRIVATE_ALL_JAVA_HEADER_LIBRARIES := $(3)
+$(1): PRIVATE_CLASS_INTERMEDIATES_DIR := $(intermediates.COMMON)/classes$(4)
+$(1): PRIVATE_ANNO_INTERMEDIATES_DIR := $(intermediates.COMMON)/anno$(4)
+$(1): \
+    $(2) \
+    $(3) \
+    $(5) \
+    $$(full_java_bootclasspath_libs) \
+    $$(full_java_system_modules_deps) \
+    $$(layers_file) \
+    $$(annotation_processor_deps) \
+    $$(NORMALIZE_PATH) \
+    $$(JAR_ARGS) \
+    | $$(SOONG_JAVAC_WRAPPER)
+	@echo "Target Java: $$@ ($$(PRIVATE_CLASS_INTERMEDIATES_DIR))"
+	$$(call compile-java,$$(TARGET_JAVAC),$$(PRIVATE_ALL_JAVA_HEADER_LIBRARIES))
 endef
 
-# Invoke Jack to compile java from source to dex and jack files.
-#
-# Some historical notes:
-# - below we write the list of java files to java-source-list to avoid argument
-#   list length problems with Cygwin
-# - we filter out duplicate java file names because Jack doesn't like them.
-define jack-java-to-dex
-$(hide) rm -f $@
-$(hide) rm -f $(PRIVATE_CLASSES_JACK)
-$(hide) rm -rf $(PRIVATE_JACK_INTERMEDIATES_DIR)
-$(hide) mkdir -p $(dir $@)
-$(hide) mkdir -p $(dir $(PRIVATE_CLASSES_JACK))
-$(hide) mkdir -p $(PRIVATE_JACK_INTERMEDIATES_DIR)
-$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
-$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list)
-$(if $(PRIVATE_SOURCE_INTERMEDIATES_DIR), \
-    $(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
-            find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list; \
-    fi)
-$(if $(PRIVATE_HAS_PROTO_SOURCES), \
-    $(hide) find $(PRIVATE_PROTO_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list )
-$(if $(PRIVATE_HAS_RS_SOURCES), \
-    $(hide) find $(PRIVATE_RS_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list )
-$(hide) tr ' ' '\n' < $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list \
-    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq
-$(if $(PRIVATE_JACK_PROGUARD_FLAGS), \
-    $(hide) echo -basedirectory $(CURDIR) > $@.flags; \
-    echo $(PRIVATE_JACK_PROGUARD_FLAGS) >> $@.flags; \
-)
-$(if $(PRIVATE_EXTRA_JAR_ARGS),
-    $(hide) mkdir -p $@.res.tmp
-    $(hide) $(call create-empty-package-at,$@.res.tmp.zip)
-    $(hide) $(call add-java-resources-to,$@.res.tmp.zip)
-    $(hide) unzip -qo $@.res.tmp.zip -d $@.res.tmp
-    $(hide) rm $@.res.tmp.zip)
-$(if $(PRIVATE_JACK_IMPORT_JAR),
-    $(hide) mkdir -p $@.tmpjill.res
-    $(hide) unzip -qo $(PRIVATE_JACK_IMPORT_JAR) -d $@.tmpjill.res
-    $(hide) find $@.tmpjill.res -iname "*.class" -delete)
-$(hide) if [ -s $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
-    export tmpEcjArg="@$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq"; \
-else \
-    export tmpEcjArg=""; \
-fi; \
-$(call call-jack) \
-    $(strip $(PRIVATE_JACK_FLAGS)) \
-    $(strip $(PRIVATE_JACK_COVERAGE_OPTIONS)) \
-    $(if $(NO_OPTIMIZE_DX), \
-        -D jack.dex.optimize="false") \
-    $(if $(PRIVATE_RMTYPEDEFS), \
-        -D jack.android.remove-typedef="true") \
-    $(if $(PRIVATE_JACK_IMPORT_JAR), \
-        --import $(PRIVATE_JACK_IMPORT_JAR) --import-resource $@.tmpjill.res) \
+define transform-java-to-header.jar
+@echo "$($(PRIVATE_PREFIX)DISPLAY) Turbine: $(PRIVATE_MODULE)"
+@mkdir -p $(dir $@)
+@rm -rf $(dir $@)/classes-turbine
+@mkdir $(dir $@)/classes-turbine
+$(hide) if [ -s $(PRIVATE_JAVA_SOURCE_LIST) ] ; then \
+    $(JAVA) -jar $(TURBINE) \
+    --output $@.premerged --temp_dir $(dir $@)/classes-turbine \
+    --sources \@$(PRIVATE_JAVA_SOURCE_LIST) \
+    --javacopts $(PRIVATE_JAVACFLAGS) $(COMMON_JDK_FLAGS) \
+    $(addprefix --bootclasspath ,$(strip \
+         $(call normalize-path-list,$(PRIVATE_BOOTCLASSPATH)) \
+         $(PRIVATE_EMPTY_BOOTCLASSPATH))) \
     $(addprefix --classpath ,$(strip \
-        $(call normalize-path-list,$(PRIVATE_JACK_SHARED_LIBRARIES)))) \
-    $(addprefix --import ,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES))) \
-    $(addprefix --pluginpath ,$(strip \
-         $(call normalize-path-list,$(PRIVATE_JACK_PLUGIN_PATH)))) \
-    $(if $(PRIVATE_JACK_PLUGIN),--plugin $(call normalize-comma-list,$(PRIVATE_JACK_PLUGIN))) \
-    $(if $(PRIVATE_EXTRA_JAR_ARGS),--import-resource $@.res.tmp) \
-    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
-    -D jack.import.resource.policy=keep-first \
-    -D jack.import.type.policy=keep-first \
-    --output-jack $(PRIVATE_CLASSES_JACK) \
-    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
-    --output-dex $(PRIVATE_JACK_INTERMEDIATES_DIR) \
-    $(addprefix --config-jarjar ,$(strip $(PRIVATE_JARJAR_RULES))) \
-    $(if $(PRIVATE_JACK_PROGUARD_FLAGS),--config-proguard $@.flags) \
-    $$tmpEcjArg \
-    || ( rm -rf $(PRIVATE_CLASSES_JACK); exit 41 )
-$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/classes*.dex $(dir $@)
-$(hide) rm -f $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list
-$(if $(PRIVATE_EXTRA_JAR_ARGS),$(hide) rm -rf $@.res.tmp)
-$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq $(PRIVATE_JACK_INTERMEDIATES_DIR).java-source-list
-$(if $(PRIVATE_JAR_PACKAGES), $(hide) echo unsupported options PRIVATE_JAR_PACKAGES in $@; exit 53)
-$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLUDE_PACKAGES in $@; exit 53)
-$(if $(PRIVATE_JAR_MANIFEST), $(hide) echo unsupported options JAR_MANIFEST in $@; exit 53)
-endef
-
-# Invoke Jack to compile java source just to check it compiles correctly.
-#
-# Some historical notes:
-# - below we write the list of java files to java-source-list to avoid argument
-#   list length problems with Cygwin
-# - we filter out duplicate java file names because Jack doesn't like them.
-define jack-check-java
-$(hide) rm -f $@
-$(hide) rm -f $@.java-source-list
-$(hide) rm -f $@.java-source-list-uniq
-$(hide) mkdir -p $(dir $@)
-$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
-$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$@.java-source-list)
-$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
-          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $@.java-source-list; \
+        $(call normalize-path-list,$(PRIVATE_ALL_JAVA_HEADER_LIBRARIES)))) \
+    || ( rm -rf $(dir $@)/classes-turbine ; exit 41 ) && \
+    $(MERGE_ZIPS) -j -stripDir META-INF $@.tmp $@.premerged $(call reverse-list,$(PRIVATE_STATIC_JAVA_HEADER_LIBRARIES)) ; \
+else \
+    $(MERGE_ZIPS) -j -stripDir META-INF $@.tmp $(call reverse-list,$(PRIVATE_STATIC_JAVA_HEADER_LIBRARIES)) ; \
 fi
-$(if $(PRIVATE_HAS_PROTO_SOURCES), \
-    $(hide) find $(PRIVATE_PROTO_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $@.java-source-list )
-$(if $(PRIVATE_HAS_RS_SOURCES), \
-    $(hide) find $(PRIVATE_RS_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $@.java-source-list )
-$(hide) tr ' ' '\n' < $@.java-source-list \
-    | sort -u > $@.java-source-list-uniq
-$(hide) if [ -s $@.java-source-list-uniq ] ; then \
-	$(call call-jack,$(PRIVATE_JACK_EXTRA_ARGS)) \
-	    $(strip $(PRIVATE_JACK_FLAGS)) \
-	    $(strip $(PRIVATE_JACK_DEBUG_FLAGS)) \
-	    $(addprefix --classpath ,$(strip \
-	        $(call normalize-path-list,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES)) $(PRIVATE_JACK_SHARED_LIBRARIES)))) \
-	    -D jack.import.resource.policy=keep-first \
-	    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
-	    -D jack.import.type.policy=keep-first \
-	    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
-	    @$@.java-source-list-uniq; \
-fi
-touch $@
-endef
-
-define transform-jar-to-jack
-	$(hide) mkdir -p $(dir $@)
-	$(hide) mkdir -p $@.tmpjill.res
-	$(hide) unzip -qo $< -d $@.tmpjill.res
-	$(hide) find $@.tmpjill.res -iname "*.class" -delete
-	$(hide) $(call call-jack) \
-	    $(PRIVATE_JACK_FLAGS) \
-	    $(addprefix --pluginpath ,$(strip \
-                $(call normalize-path-list,$(PRIVATE_JACK_PLUGIN_PATH)))) \
-	    $(if $(PRIVATE_JACK_PLUGIN),--plugin $(call normalize-comma-list,$(PRIVATE_JACK_PLUGIN))) \
-        -D jack.import.resource.policy=keep-first \
-        -D jack.import.type.policy=keep-first \
-        -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
-	    --import $< \
-	    --import-resource $@.tmpjill.res \
-	    --output-jack $@
-	$(hide) rm -rf $@.tmpjill.res
+$(hide) $(ZIPTIME) $@.tmp
+$(hide) $(call commit-change-for-toc,$@)
 endef
 
 # Moves $1.tmp to $1 if necessary. This is designed to be used with
@@ -2409,26 +2337,6 @@
 fi
 endef
 
-# b/37756495
-IJAR_ASAN_OPTIONS := ASAN_OPTIONS=detect_leaks=0
-
-## Rule to create a table of contents from a .jar file.
-## Must be called with $(eval).
-# $(1): A .jar file
-define _transform-jar-to-toc
-$1.toc: $1 | $(IJAR)
-	@echo Generating TOC: $$@
-	$(hide) $(IJAR_ASAN_OPTIONS) $(IJAR) $$< $$@.tmp
-	$$(call commit-change-for-toc,$$@)
-endef
-
-## Define a rule which generates .jar.toc and mark it as .KATI_RESTAT.
-# $(1): A .jar file
-define define-jar-to-toc-rule
-$(eval $(call _transform-jar-to-toc,$1))\
-$(eval .KATI_RESTAT: $1.toc)
-endef
-
 ifeq (,$(TARGET_BUILD_APPS))
 
 ## Rule to create a table of contents from a .dex file.
@@ -2458,75 +2366,6 @@
 endif  # TARGET_BUILD_APPS
 
 
-# Invoke Jack to compile java from source to jack files without shrink or obfuscation.
-#
-# Some historical notes:
-# - below we write the list of java files to java-source-list to avoid argument
-#   list length problems with Cygwin
-# - we filter out duplicate java file names because Jack doesn't like them.
-define java-to-jack
-$(hide) rm -f $@
-$(hide) rm -rf $(PRIVATE_JACK_INTERMEDIATES_DIR)
-$(hide) mkdir -p $(dir $@)
-$(hide) mkdir -p $(PRIVATE_JACK_INTERMEDIATES_DIR)
-$(if $(PRIVATE_JACK_INCREMENTAL_DIR),$(hide) mkdir -p $(PRIVATE_JACK_INCREMENTAL_DIR))
-$(call dump-words-to-file,$(PRIVATE_JAVA_SOURCES),$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list)
-$(hide) if [ -d "$(PRIVATE_SOURCE_INTERMEDIATES_DIR)" ]; then \
-          find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list; \
-fi
-$(if $(PRIVATE_HAS_PROTO_SOURCES), \
-    $(hide) find $(PRIVATE_PROTO_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list )
-$(if $(PRIVATE_HAS_RS_SOURCES), \
-    $(hide) find $(PRIVATE_RS_SOURCE_INTERMEDIATES_DIR) -name '*.java' >> $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list )
-$(hide) tr ' ' '\n' < $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list \
-    | $(NORMALIZE_PATH) | sort -u > $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq
-$(if $(PRIVATE_JACK_PROGUARD_FLAGS), \
-    $(hide) echo -basedirectory $(CURDIR) > $@.flags; \
-    echo $(PRIVATE_JACK_PROGUARD_FLAGS) >> $@.flags; \
-)
-$(if $(PRIVATE_EXTRA_JAR_ARGS),
-	$(hide) mkdir -p $@.res.tmp
-	$(hide) $(call create-empty-package-at,$@.res.tmp.zip)
-	$(hide) $(call add-java-resources-to,$@.res.tmp.zip)
-	$(hide) unzip -qo $@.res.tmp.zip -d $@.res.tmp
-	$(hide) rm $@.res.tmp.zip)
-$(hide) if [ -s $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
-    export tmpEcjArg="@$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq"; \
-else \
-    export tmpEcjArg=""; \
-fi; \
-$(call call-jack) \
-    $(strip $(PRIVATE_JACK_FLAGS)) \
-    $(if $(NO_OPTIMIZE_DX), \
-        -D jack.dex.optimize="false") \
-    $(addprefix --classpath ,$(strip \
-        $(call normalize-path-list,$(PRIVATE_JACK_SHARED_LIBRARIES)))) \
-    $(addprefix --import ,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES))) \
-    $(addprefix --pluginpath ,$(strip \
-        $(call normalize-path-list,$(PRIVATE_JACK_PLUGIN_PATH)))) \
-    $(if $(PRIVATE_JACK_PLUGIN),--plugin $(call normalize-comma-list,$(PRIVATE_JACK_PLUGIN))) \
-    $(if $(PRIVATE_EXTRA_JAR_ARGS),--import-resource $@.res.tmp) \
-    -D jack.import.resource.policy=keep-first \
-    -D jack.import.type.policy=keep-first \
-    -D jack.android.min-api-level=$(PRIVATE_JACK_MIN_SDK_VERSION) \
-    $(if $(PRIVATE_JACK_INCREMENTAL_DIR),--incremental-folder $(PRIVATE_JACK_INCREMENTAL_DIR)) \
-    --output-jack $@ \
-    $(addprefix --config-jarjar ,$(strip $(PRIVATE_JARJAR_RULES))) \
-    $(if $(PRIVATE_JACK_PROGUARD_FLAGS),--config-proguard $@.flags) \
-    $$tmpEcjArg \
-    || ( rm -f $@ ; exit 41 )
-$(hide) rm -f $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list
-$(if $(PRIVATE_EXTRA_JAR_ARGS),$(hide) rm -rf $@.res.tmp)
-$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq $(PRIVATE_JACK_INTERMEDIATES_DIR).java-source-list
-$(if $(PRIVATE_JAR_PACKAGES), $(hide) echo unsupported options PRIVATE_JAR_PACKAGES in $@; exit 53)
-$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLUDE_PACKAGES in $@; exit 53)
-$(if $(PRIVATE_JAR_MANIFEST), $(hide) echo unsupported options JAR_MANIFEST in $@; exit 53)
-endef
-
-define desugar-classpath
-$(filter-out -classpath -bootclasspath "",$(subst :,$(space),$(1)))
-endef
-
 # Takes an sdk version that might be PLATFORM_VERSION_CODENAME (for example P),
 # returns a number greater than the highest existing sdk version if it is, or
 # the input if it is not.
@@ -2542,12 +2381,12 @@
 @rm -rf $(dir $@)/desugar_dumped_classes
 @mkdir $(dir $@)/desugar_dumped_classes
 $(hide) $(JAVA) \
-    $(if $(EXPERIMENTAL_USE_OPENJDK9),--add-opens java.base/java.lang.invoke=ALL-UNNAMED,) \
+    $(if $(USE_OPENJDK9),--add-opens java.base/java.lang.invoke=ALL-UNNAMED,) \
     -Djdk.internal.lambda.dumpProxyClasses=$(abspath $(dir $@))/desugar_dumped_classes \
     -jar $(DESUGAR) \
-    $(addprefix --bootclasspath_entry ,$(call desugar-bootclasspath,$(PRIVATE_BOOTCLASSPATH))) \
-    $(addprefix --classpath_entry ,$(PRIVATE_ALL_JAVA_LIBRARIES)) \
-    --min_sdk_version $(call codename-or-sdk-to-sdk,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
+    $(addprefix --bootclasspath_entry ,$(PRIVATE_BOOTCLASSPATH)) \
+    $(addprefix --classpath_entry ,$(PRIVATE_SHARED_JAVA_HEADER_LIBRARIES)) \
+    --min_sdk_version $(call codename-or-sdk-to-sdk,$(PRIVATE_MIN_SDK_VERSION)) \
     --allow_empty_bootclasspath \
     $(if $(filter --core-library,$(PRIVATE_DX_FLAGS)),--core_library) \
     -i $< -o $@.tmp
@@ -2561,7 +2400,7 @@
 $(hide) rm -f $(dir $@)classes*.dex
 $(hide) $(DX_COMMAND) \
     --dex --output=$(dir $@) \
-    --min-sdk-version=$(call codename-or-sdk-to-sdk,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
+    --min-sdk-version=$(PRIVATE_MIN_SDK_VERSION) \
     $(if $(NO_OPTIMIZE_DX), \
         --no-optimize) \
     $(if $(GENERATE_DEX_DEBUG), \
@@ -2628,6 +2467,8 @@
     $(addprefix --rename-instrumentation-target-package , $(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
     --skip-symbols-without-default-localization \
     -F $@
+# So that we re-run aapt when the list of input files change
+$(hide) echo $(PRIVATE_RESOURCE_LIST) >/dev/null
 endef
 
 # We need the extra blank line, so that the command will be on a separate line.
@@ -2677,17 +2518,15 @@
 @rm -f $(1).jar-arg-list
 endef
 
-# Add resources carried by static Jack libraries.
-#
-define add-carried-jack-resources
- $(hide) if [ -d $(PRIVATE_JACK_INTERMEDIATES_DIR) ] ; then \
-    find $(PRIVATE_JACK_INTERMEDIATES_DIR) -type f | sort \
-        | sed -e "s?^$(PRIVATE_JACK_INTERMEDIATES_DIR)/? -C \"$(PRIVATE_JACK_INTERMEDIATES_DIR)\" \"?" -e "s/$$/\"/" \
-        > $(dir $@)jack_res_jar_flags; \
-    if [ -s $(dir $@)jack_res_jar_flags ] ; then \
-        $(JAR) uf $@ @$(dir $@)jack_res_jar_flags; \
-    fi; \
-fi
+# Add resources (non .class files) from a jar to a package
+# $(1): the package file
+# $(2): the jar file
+# $(3): temporary directory
+define add-jar-resources-to-package
+  rm -rf $(3)
+  mkdir -p $(3)
+  unzip -qo $(2) -d $(3) $$(zipinfo -1 $(2) | grep -v -E "\.class$$")
+  $(JAR) uf $(1) $(call jar-args-sorted-files-in-directory,$(3))
 endef
 
 # Sign a package using the specified key/cert.
@@ -2696,14 +2535,10 @@
 $(call sign-package-arg,$@)
 endef
 
-# signapk uses internal APIs from sun.security.{pkcs,x509}; see http://b/37137869
 # $(1): the package file we are signing.
 define sign-package-arg
 $(hide) mv $(1) $(1).unsigned
-$(hide) $(JAVA) -Djava.library.path=$(SIGNAPK_JNI_LIBRARY_PATH) \
-    $(if $(EXPERIMENTAL_USE_OPENJDK9),--add-exports java.base/sun.security.pkcs=ALL-UNNAMED,) \
-    $(if $(EXPERIMENTAL_USE_OPENJDK9),--add-exports java.base/sun.security.x509=ALL-UNNAMED,) \
-    -jar $(SIGNAPK_JAR) \
+$(hide) $(JAVA) -Djava.library.path=$(SIGNAPK_JNI_LIBRARY_PATH) -jar $(SIGNAPK_JAR) \
     $(PRIVATE_CERTIFICATE) $(PRIVATE_PRIVATE_KEY) \
     $(PRIVATE_ADDITIONAL_CERTIFICATES) $(1).unsigned $(1).signed
 $(hide) mv $(1).signed $(1)
@@ -2738,14 +2573,26 @@
 $(hide) $(ZIPTIME) $@
 endef
 
+# Uncompress dex files embedded in an apk.
+#
+define uncompress-dexs
+$(hide) if (zipinfo $@ '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then \
+  rm -rf $(dir $@)uncompresseddexs && mkdir $(dir $@)uncompresseddexs; \
+  unzip -q $@ '*.dex' -d $(dir $@)uncompresseddexs && \
+  zip -qd $@ '*.dex' && \
+  ( cd $(dir $@)uncompresseddexs && find . -type f | sort | zip -qD -X -0 ../$(notdir $@) -@ ) && \
+  rm -rf $(dir $@)uncompresseddexs; \
+  fi
+endef
+
 # Uncompress shared libraries embedded in an apk.
 #
 define uncompress-shared-libs
 $(hide) if (zipinfo $@ $(PRIVATE_EMBEDDED_JNI_LIBS) 2>/dev/null | grep -v ' stor ' >/dev/null) ; then \
   rm -rf $(dir $@)uncompressedlibs && mkdir $(dir $@)uncompressedlibs; \
-  unzip $@ $(PRIVATE_EMBEDDED_JNI_LIBS) -d $(dir $@)uncompressedlibs && \
-  zip -d $@ 'lib/*.so' && \
-  ( cd $(dir $@)uncompressedlibs && find lib -type f | sort | zip -D -X -0 ../$(notdir $@) -@ ) && \
+  unzip -q $@ $(PRIVATE_EMBEDDED_JNI_LIBS) -d $(dir $@)uncompressedlibs && \
+  zip -qd $@ 'lib/*.so' && \
+  ( cd $(dir $@)uncompressedlibs && find lib -type f | sort | zip -qD -X -0 ../$(notdir $@) -@ ) && \
   rm -rf $(dir $@)uncompressedlibs; \
   fi
 endef
@@ -2757,8 +2604,15 @@
 # Note: we intentionally don't clean PRIVATE_CLASS_INTERMEDIATES_DIR
 # in transform-java-to-classes for the sake of vm-tests.
 define transform-host-java-to-package
-@echo "$($(PRIVATE_PREFIX)DISPLAY) Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
-$(call compile-java,$(HOST_JAVAC),$(PRIVATE_BOOTCLASSPATH))
+@echo "Host Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
+$(call compile-java,$(HOST_JAVAC),$(PRIVATE_ALL_JAVA_LIBRARIES))
+endef
+
+# Note: we intentionally don't clean PRIVATE_CLASS_INTERMEDIATES_DIR
+# in transform-java-to-classes for the sake of vm-tests.
+define transform-host-java-to-dalvik-package
+@echo "Dalvik Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
+$(call compile-java,$(HOST_JAVAC),$(PRIVATE_ALL_JAVA_HEADER_LIBRARIES))
 endef
 
 ###########################################################
@@ -2783,6 +2637,14 @@
 	$$(copy-file-to-target)
 endef
 
+define copy-and-uncompress-dexs
+$(2): $(1) $(ZIPALIGN)
+	@echo "Uncompress dexs in: $$@"
+	$$(copy-file-to-target)
+	$$(uncompress-dexs)
+	$$(align-package)
+endef
+
 # Copies many files.
 # $(1): The files to copy.  Each entry is a ':' separated src:dst pair
 # Evaluates to the list of the dst files (ie suitable for a dependency list)
@@ -2822,7 +2684,7 @@
 define copy-file-to-target
 @mkdir -p $(dir $@)
 $(hide) rm -f $@
-$(hide) cp $< $@
+$(hide) cp "$<" "$@"
 endef
 
 # The same as copy-file-to-target, but use the local
@@ -2830,7 +2692,7 @@
 define copy-file-to-target-with-cp
 @mkdir -p $(dir $@)
 $(hide) rm -f $@
-$(hide) cp -p $< $@
+$(hide) cp -p "$<" "$@"
 endef
 
 # The same as copy-file-to-target, but strip out "# comment"-style
@@ -2898,15 +2760,57 @@
 	$(hide) ln -sf $(2) $$@
 endef
 
+# Copy an apk to a target location while removing classes*.dex
+# $(1): source file
+# $(2): destination file
+# $(3): LOCAL_DEX_PREOPT, if nostripping then leave classes*.dex
+define dexpreopt-copy-jar
+$(2): $(1)
+	@echo $(if $(filter nostripping,$(3)),"Copy: $$@","Copy without dex: $$@")
+	$$(copy-file-to-target)
+	$(if $(filter nostripping,$(3)),,$$(call dexpreopt-remove-classes.dex,$$@))
+endef
+
+# $(1): the .jar or .apk to remove classes.dex
+define dexpreopt-remove-classes.dex
+$(hide) zip --quiet --delete $(1) classes.dex; \
+dex_index=2; \
+while zip --quiet --delete $(1) classes$${dex_index}.dex > /dev/null; do \
+  let dex_index=dex_index+1; \
+done
+endef
+
 ###########################################################
 ## Commands to call Proguard
 ###########################################################
+ifdef TARGET_OPENJDK9
+define transform-jar-to-proguard
+@echo Skipping Proguard: $<$(PRIVATE_PROGUARD_INJAR_FILTERS) $@
+$(hide) cp '$<' $@
+endef
+else
 define transform-jar-to-proguard
 @echo Proguard: $@
 $(hide) $(PROGUARD) -injars '$<$(PRIVATE_PROGUARD_INJAR_FILTERS)' \
-    -outjars $@ $(PRIVATE_PROGUARD_FLAGS) \
+    -outjars $@ \
+    $(PRIVATE_PROGUARD_FLAGS) \
     $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR))
 endef
+endif
+
+
+###########################################################
+## Commands to call R8
+###########################################################
+define transform-jar-to-dex-r8
+@echo R8: $@
+$(hide) $(R8_COMPAT_PROGUARD) -injars '$<$(PRIVATE_PROGUARD_INJAR_FILTERS)' \
+    --min-api $(PRIVATE_MIN_SDK_VERSION) \
+    --force-proguard-compatibility --output $(subst classes.dex,,$@) \
+    $(PRIVATE_PROGUARD_FLAGS) \
+    $(addprefix -injars , $(PRIVATE_EXTRA_INPUT_JAR)) \
+    $(PRIVATE_DX_FLAGS)
+endef
 
 ###########################################################
 ## Stuff source generated from one-off tools
@@ -2928,39 +2832,16 @@
 $(error HOST_OS must define get-file-size)
 endif
 
-# Convert a partition data size (eg, as reported in /proc/mtd) to the
-# size of the image used to flash that partition (which includes a
-# spare area for each page).
-# $(1): the partition data size
-define image-size-from-data-size
-$(strip $(eval _isfds_value := $$(shell echo $$$$(($(1) / $(BOARD_NAND_PAGE_SIZE) * \
-  ($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE))))))\
-$(if $(filter 0, $(_isfds_value)),$(shell echo $$(($(BOARD_NAND_PAGE_SIZE)+$(BOARD_NAND_SPARE_SIZE)))),$(_isfds_value))\
-$(eval _isfds_value :=))
-endef
-
 # $(1): The file(s) to check (often $@)
-# $(2): The maximum total image size, in decimal bytes.
-#    Make sure to take into account any reserved space needed for the FS.
-#
-# If $(2) is empty, evaluates to "true"
-#
-# Reserve bad blocks.  Make sure that MAX(1% of partition size, 2 blocks)
-# is left over after the image has been flashed.  Round the 1% up to the
-# next whole flash block size.
-define assert-max-file-size
+# $(2): The partition size.
+define assert-max-image-size
 $(if $(2), \
   size=$$(for i in $(1); do $(call get-file-size,$$i); echo +; done; echo 0); \
   total=$$(( $$( echo "$$size" ) )); \
   printname=$$(echo -n "$(1)" | tr " " +); \
-  img_blocksize=$(call image-size-from-data-size,$(BOARD_FLASH_BLOCK_SIZE)); \
-  twoblocks=$$((img_blocksize * 2)); \
-  onepct=$$((((($(2) / 100) - 1) / img_blocksize + 1) * img_blocksize)); \
-  reserve=$$((twoblocks > onepct ? twoblocks : onepct)); \
-  maxsize=$$(($(2) - reserve)); \
-  echo "$$printname maxsize=$$maxsize blocksize=$$img_blocksize total=$$total reserve=$$reserve"; \
+  maxsize=$$(($(2))); \
   if [ "$$total" -gt "$$maxsize" ]; then \
-    echo "error: $$printname too large ($$total > [$(2) - $$reserve])"; \
+    echo "error: $$printname too large ($$total > $$maxsize)"; \
     false; \
   elif [ "$$total" -gt $$((maxsize - 32768)) ]; then \
     echo "WARNING: $$printname approaching size limit ($$total now; limit $$maxsize)"; \
@@ -2970,17 +2851,6 @@
  )
 endef
 
-# Like assert-max-file-size, but the second argument is a partition
-# size, which we'll convert to a max image size before checking it
-# against the files.
-#
-# $(1): The file(s) to check (often $@)
-# $(2): The partition size.
-define assert-max-image-size
-$(if $(2), \
-  $(call assert-max-file-size,$(1),$(call image-size-from-data-size,$(2))))
-endef
-
 
 ###########################################################
 ## Define device-specific radio files
@@ -3157,9 +3027,11 @@
   HOST_DALVIK_JAVA_LIBRARY \
   HOST_DALVIK_STATIC_JAVA_LIBRARY \
   base_rules \
-  HEADER_LIBRARY
+  HEADER_LIBRARY \
+  HOST_TEST_CONFIG \
+  TARGET_TEST_CONFIG
 
-$(foreach $(s),$(STATS.MODULE_TYPE),$(eval STATS.MODULE_TYPE.$(s) :=))
+$(foreach s,$(STATS.MODULE_TYPE),$(eval STATS.MODULE_TYPE.$(s) :=))
 define record-module-type
 $(strip $(if $(LOCAL_RECORDED_MODULE_TYPE),,
   $(if $(filter-out $(SOONG_ANDROID_MK),$(LOCAL_MODULE_MAKEFILE)),
@@ -3330,6 +3202,122 @@
 endif
 
 ###########################################################
+## Given a filepath, returns nonempty if the path cannot be
+## validated to be contained in the current directory
+## This is, this function checks for '/' and '..'
+##
+## $(1): path to validate
+define try-validate-path-is-subdir
+$(strip 
+    $(if $(filter /%,$(1)),
+        $(1) starts with a slash
+    )
+    $(if $(filter ../%,$(call clean-path,$(1))),
+        $(1) escapes its parent using '..'
+    )
+    $(if $(strip $(1)),
+    ,
+        '$(1)' is empty
+    )
+)
+endef
+
+define validate-path-is-subdir
+$(if $(call try-validate-path-is-subdir,$(1)),
+  $(call pretty-error, Illegal path: $(call try-validate-path-is-subdir,$(1)))
+)
+endef
+
+###########################################################
+## Given a space-delimited list of filepaths, returns
+## nonempty if any cannot be validated to be contained in
+## the current directory
+##
+## $(1): path list to validate
+define try-validate-paths-are-subdirs
+$(strip \
+  $(foreach my_path,$(1),\
+    $(call try-validate-path-is-subdir,$(my_path))\
+  )
+)
+endef
+
+define validate-paths-are-subdirs
+$(if $(call try-validate-paths-are-subdirs,$(1)),
+    $(call pretty-error,Illegal paths:\'$(call try-validate-paths-are-subdirs,$(1))\')
+)
+endef
+
+###########################################################
+## Tests of try-validate-path-is-subdir
+##     and  try-validate-paths-are-subdirs
+define test-validate-paths-are-subdirs
+$(eval my_error := $(call try-validate-path-is-subdir,/tmp)) \
+$(if $(call streq,$(my_error),/tmp starts with a slash),
+,
+  $(error incorrect error message for path /tmp. Got '$(my_error)')
+) \
+$(eval my_error := $(call try-validate-path-is-subdir,../sibling)) \
+$(if $(call streq,$(my_error),../sibling escapes its parent using '..'),
+,
+  $(error incorrect error message for path ../sibling. Got '$(my_error)')
+) \
+$(eval my_error := $(call try-validate-path-is-subdir,child/../../sibling)) \
+$(if $(call streq,$(my_error),child/../../sibling escapes its parent using '..'),
+,
+  $(error incorrect error message for path child/../../sibling. Got '$(my_error)')
+) \
+$(eval my_error := $(call try-validate-path-is-subdir,)) \
+$(if $(call streq,$(my_error),'' is empty),
+,
+  $(error incorrect error message for empty path ''. Got '$(my_error)')
+) \
+$(eval my_error := $(call try-validate-path-is-subdir,subdir/subsubdir)) \
+$(if $(call streq,$(my_error),),
+,
+  $(error rejected valid path 'subdir/subsubdir'. Got '$(my_error)')
+)
+
+$(eval my_error := $(call try-validate-paths-are-subdirs,a/b /c/d e/f))
+$(if $(call streq,$(my_error),/c/d starts with a slash),
+,
+  $(error incorrect error message for path list 'a/b /c/d e/f'. Got '$(my_error)')
+)
+$(eval my_error := $(call try-validate-paths-are-subdirs,a/b c/d))
+$(if $(call streq,$(my_error),),
+,
+  $(error rejected valid path list 'a/b c/d'. Got '$(my_error)')
+)
+endef
+# run test
+$(strip $(call test-validate-paths-are-subdirs))
+
+###########################################################
+## Validate jacoco class filters and convert them to
+## file arguments
+## Jacoco class filters are comma-separated lists of class
+## files (android.app.Application), and may have '*' as the
+## last character to match all classes in a package
+## including subpackages.
+define jacoco-class-filter-to-file-args
+$(strip $(call jacoco-validate-file-args,\
+  $(subst $(comma),$(space),\
+    $(subst .,/,\
+      $(strip $(1))))))
+endef
+
+define jacoco-validate-file-args
+$(strip $(1)\
+  $(call validate-paths-are-subdirs,$(1))
+  $(foreach arg,$(1),\
+    $(if $(findstring ?,$(arg)),$(call pretty-error,\
+      '?' filters are not supported in LOCAL_JACK_COVERAGE_INCLUDE_FILTER or LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))\
+    $(if $(findstring *,$(patsubst %*,%,$(arg))),$(call pretty-error,\
+      '*' is only supported at the end of a filter in LOCAL_JACK_COVERAGE_INCLUDE_FILTER or LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))\
+  ))
+endef
+
+###########################################################
 ## Other includes
 ###########################################################
 
@@ -3409,3 +3397,22 @@
   $(eval ALL_MODULES.$(enforce_rro_source_module).REQUIRED += $(enforce_rro_module)) \
 )
 endef
+
+###########################################################
+## Find system_$(VER) in LOCAL_SDK_VERSION
+##
+## $(1): LOCAL_SDK_VERSION
+###########################################################
+define has-system-sdk-version
+$(filter system_%,$(1))
+endef
+
+###########################################################
+## Get numerical version in LOCAL_SDK_VERSION
+##
+## $(1): LOCAL_SDK_VERSION
+###########################################################
+define get-numeric-sdk-version
+$(filter-out current,\
+  $(if $(call has-system-sdk-version,$(1)),$(patsubst system_%,%,$(1)),$(1)))
+endef
diff --git a/core/dex_preopt.mk b/core/dex_preopt.mk
index 8e496a7..0dcb07f 100644
--- a/core/dex_preopt.mk
+++ b/core/dex_preopt.mk
@@ -24,7 +24,12 @@
 SYSTEM_OTHER_ODEX_FILTER ?= app/% priv-app/%
 
 # Method returning whether the install path $(1) should be for system_other.
+# Under SANITIZE_LITE, we do not want system_other. Just put things under /data/asan.
+ifeq ($(SANITIZE_LITE),true)
+install-on-system-other =
+else
 install-on-system-other = $(filter-out $(PRODUCT_DEXPREOPT_SPEED_APPS) $(PRODUCT_SYSTEM_SERVER_APPS),$(basename $(notdir $(filter $(foreach f,$(SYSTEM_OTHER_ODEX_FILTER),$(TARGET_OUT)/$(f)),$(1)))))
+endif
 
 # The default values for pre-opting: always preopt PIC.
 # Conditional to building on linux, as dex2oat currently does not work on darwin.
@@ -43,15 +48,6 @@
 
 GLOBAL_DEXPREOPT_FLAGS :=
 
-# $(1): the .jar or .apk to remove classes.dex
-define dexpreopt-remove-classes.dex
-$(hide) zip --quiet --delete $(1) classes.dex; \
-dex_index=2; \
-while zip --quiet --delete $(1) classes$${dex_index}.dex > /dev/null; do \
-  let dex_index=dex_index+1; \
-done
-endef
-
 # Special rules for building stripped boot jars that override java_library.mk rules
 
 # $(1): boot jar module name
@@ -59,11 +55,7 @@
 _dbj_jar_no_dex := $(DEXPREOPT_BOOT_JAR_DIR_FULL_PATH)/$(1)_nodex.jar
 _dbj_src_jar := $(call intermediates-dir-for,JAVA_LIBRARIES,$(1),,COMMON)/javalib.jar
 
-$$(_dbj_jar_no_dex) : $$(_dbj_src_jar)
-	$$(call copy-file-to-target)
-ifneq ($(DEX_PREOPT_DEFAULT),nostripping)
-	$$(call dexpreopt-remove-classes.dex,$$@)
-endif
+$(call dexpreopt-copy-jar,$$(_dbj_src_jar),$$(_dbj_jar_no_dex),$(DEX_PREOPT_DEFAULT))
 
 _dbj_jar_no_dex :=
 _dbj_src_jar :=
diff --git a/core/dex_preopt_libart.mk b/core/dex_preopt_libart.mk
index 9616036..79e72c1 100644
--- a/core/dex_preopt_libart.mk
+++ b/core/dex_preopt_libart.mk
@@ -11,10 +11,6 @@
 DEX2OAT := $(HOST_OUT_EXECUTABLES)/dex2oatd$(HOST_EXECUTABLE_SUFFIX)
 endif
 
-# Pass special class loader context to skip the classpath and collision check.
-# Should modify build system to pass used libraries properly later.
-DEX2OAT_CLASS_LOADER_CONTEXT := "&"
-
 DEX2OAT_DEPENDENCY += $(DEX2OAT)
 
 # Use the first preloaded-classes file in PRODUCT_COPY_FILES.
@@ -30,7 +26,9 @@
     $(filter %system/etc/dirty-image-objects,$(PRODUCT_COPY_FILES))))
 
 define get-product-default-property
-$(strip $(patsubst $(1)=%,%,$(filter $(1)=%,$(PRODUCT_DEFAULT_PROPERTY_OVERRIDES))))
+$(strip \
+  $(eval _prop := $(patsubst $(1)=%,%,$(filter $(1)=%,$(PRODUCT_DEFAULT_PROPERTY_OVERRIDES))))\
+  $(if $(_prop),$(_prop),$(patsubst $(1)=%,%,$(filter $(1)=%,$(PRODUCT_SYSTEM_DEFAULT_PROPERTIES)))))
 endef
 
 DEX2OAT_IMAGE_XMS := $(call get-product-default-property,dalvik.vm.image-dex2oat-Xms)
@@ -150,7 +148,7 @@
 $(hide) mkdir -p $(dir $(2))
 $(hide) ANDROID_LOG_TAGS="*:e" $(DEX2OAT) \
 	--runtime-arg -Xms$(DEX2OAT_XMS) --runtime-arg -Xmx$(DEX2OAT_XMX) \
-	--class-loader-context=$(DEX2OAT_CLASS_LOADER_CONTEXT) \
+	--class-loader-context=$(PRIVATE_DEX2OAT_CLASS_LOADER_CONTEXT) \
 	--boot-image=$(PRIVATE_DEX_PREOPT_IMAGE_LOCATION) \
 	--dex-file=$(1) \
 	--dex-location=$(PRIVATE_DEX_LOCATION) \
diff --git a/core/dex_preopt_libart_boot.mk b/core/dex_preopt_libart_boot.mk
index e3eb9ef..8b71198 100644
--- a/core/dex_preopt_libart_boot.mk
+++ b/core/dex_preopt_libart_boot.mk
@@ -65,6 +65,12 @@
 my_boot_image_flags += --profile-file=$(my_out_boot_image_profile_location)
 endif
 
+ifneq (addresstrue,$(SANITIZE_TARGET)$(SANITIZE_LITE))
+# Skip recompiling the boot image for the second sanitization phase. We'll get separate paths
+# and invalidate first-stage artifacts which are crucial to SANITIZE_LITE builds.
+# Note: this is technically incorrect. Compiled code contains stack checks which may depend
+#       on ASAN settings.
+
 $($(my_2nd_arch_prefix)DEFAULT_DEX_PREOPT_BUILT_IMAGE_FILENAME): PRIVATE_BOOT_IMAGE_FLAGS := $(my_boot_image_flags)
 $($(my_2nd_arch_prefix)DEFAULT_DEX_PREOPT_BUILT_IMAGE_FILENAME): PRIVATE_2ND_ARCH_VAR_PREFIX := $(my_2nd_arch_prefix)
 # Use dex2oat debug version for better error reporting
@@ -75,7 +81,7 @@
 	@rm -f $(dir $@)/*.art $(dir $@)/*.oat
 	@rm -f $(dir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)LIBART_TARGET_BOOT_OAT_UNSTRIPPED))/*.art
 	@rm -f $(dir $($(PRIVATE_2ND_ARCH_VAR_PREFIX)LIBART_TARGET_BOOT_OAT_UNSTRIPPED))/*.oat
-	$(DEX2OAT) --runtime-arg -Xms$(DEX2OAT_IMAGE_XMS) \
+	$(hide) ANDROID_LOG_TAGS="*:e" $(DEX2OAT) --runtime-arg -Xms$(DEX2OAT_IMAGE_XMS) \
 		--runtime-arg -Xmx$(DEX2OAT_IMAGE_XMX) \
 		$(PRIVATE_BOOT_IMAGE_FLAGS) \
 		$(addprefix --dex-file=,$(LIBART_TARGET_BOOT_DEX_FILES)) \
@@ -91,4 +97,8 @@
 		--runtime-arg -Xnorelocate --compile-pic \
 		--no-generate-debug-info --generate-build-id \
 		--multi-image --no-inline-from=core-oj.jar \
+		--abort-on-hard-verifier-error \
+		--abort-on-soft-verifier-error \
 		$(PRODUCT_DEX_PREOPT_BOOT_FLAGS) $(GLOBAL_DEXPREOPT_FLAGS) $(ART_BOOT_IMAGE_EXTRA_ARGS)
+
+endif
diff --git a/core/dex_preopt_odex_install.mk b/core/dex_preopt_odex_install.mk
index 235f4bc..93824c3 100644
--- a/core/dex_preopt_odex_install.mk
+++ b/core/dex_preopt_odex_install.mk
@@ -30,7 +30,7 @@
 ifdef LOCAL_UNINSTALLABLE_MODULE
 LOCAL_DEX_PREOPT :=
 endif
-ifeq (,$(strip $(built_dex)$(my_prebuilt_src_file))) # contains no java code
+ifeq (,$(strip $(built_dex)$(my_prebuilt_src_file)$(LOCAL_SOONG_DEX_JAR))) # contains no java code
 LOCAL_DEX_PREOPT :=
 endif
 # if WITH_DEXPREOPT_BOOT_IMG_AND_SYSTEM_SERVER_ONLY=true and module is not in boot class path skip
diff --git a/core/distdir.mk b/core/distdir.mk
index 89c5966..c074186 100644
--- a/core/distdir.mk
+++ b/core/distdir.mk
@@ -17,9 +17,6 @@
 # When specifying "dist", the user has asked that we copy the important
 # files from this build into DIST_DIR.
 
-dist_goal := $(strip $(filter dist,$(MAKECMDGOALS)))
-MAKECMDGOALS := $(strip $(filter-out dist,$(MAKECMDGOALS)))
-
 ifdef dist_goal
 
 # $(1): source file
diff --git a/core/dpi_specific_apk.mk b/core/dpi_specific_apk.mk
index 1b0be07..e29cde7 100644
--- a/core/dpi_specific_apk.mk
+++ b/core/dpi_specific_apk.mk
@@ -9,7 +9,7 @@
 
 # Set up all the target-specific variables.
 $(built_dpi_apk): PRIVATE_MODULE := $(dpi_apk_name)
-$(built_dpi_apk): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) $(PRODUCT_AAPT_FLAGS) $($(LOCAL_PACKAGE_NAME)_aapt_flags_$(my_dpi))
+$(built_dpi_apk): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --pseudo-localize $($(LOCAL_PACKAGE_NAME)_aapt_flags_$(my_dpi))
 # Clear PRIVATE_PRODUCT_AAPT_CONFIG to include everything by default.
 $(built_dpi_apk): PRIVATE_PRODUCT_AAPT_CONFIG :=
 $(built_dpi_apk): PRIVATE_PRODUCT_AAPT_PREF_CONFIG := $(my_dpi)
@@ -17,8 +17,9 @@
 $(built_dpi_apk): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
 $(built_dpi_apk): PRIVATE_ASSET_DIR := $(LOCAL_ASSET_DIR)
 $(built_dpi_apk): PRIVATE_AAPT_INCLUDES := $(all_library_res_package_exports)
+$(built_dpi_apk): PRIVATE_RESOURCE_LIST := $(all_res_assets)
 ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
-$(built_dpi_apk): PRIVATE_DEFAULT_APP_TARGET_SDK := $(LOCAL_SDK_VERSION)
+$(built_dpi_apk): PRIVATE_DEFAULT_APP_TARGET_SDK := $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION))
 else
 $(built_dpi_apk): PRIVATE_DEFAULT_APP_TARGET_SDK := $(DEFAULT_APP_TARGET_SDK)
 endif
@@ -34,12 +35,8 @@
 $(built_dpi_apk): PRIVATE_SOURCE_ARCHIVE :=
 ifneq ($(full_classes_jar),)
 $(built_dpi_apk): PRIVATE_DEX_FILE := $(built_dex)
-ifndef LOCAL_JACK_ENABLED
 # Use the jarjar processed arhive as the initial package file.
 $(built_dpi_apk): PRIVATE_SOURCE_ARCHIVE := $(full_classes_pre_proguard_jar)
-else
-$(built_dpi_apk): PRIVATE_JACK_INTERMEDIATES_DIR := $(intermediates.COMMON)/jack-rsc
-endif # LOCAL_JACK_ENABLED
 $(built_dpi_apk): $(built_dex)
 else
 $(built_dpi_apk): PRIVATE_DEX_FILE :=
@@ -64,9 +61,6 @@
 	$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
 else
 	$(add-dex-to-package)
-ifdef LOCAL_JACK_ENABLED
-	$(add-carried-jack-resources)
-endif
 endif
 	$(sign-package)
 
diff --git a/core/droiddoc.mk b/core/droiddoc.mk
index 94e282d..176a01d 100644
--- a/core/droiddoc.mk
+++ b/core/droiddoc.mk
@@ -57,7 +57,6 @@
 $(full_target): PRIVATE_BOOTCLASSPATH :=
 full_java_libs := $(addprefix $(HOST_OUT_JAVA_LIBRARIES)/,\
   $(addsuffix $(COMMON_JAVA_PACKAGE_SUFFIX),$(LOCAL_JAVA_LIBRARIES)))
-full_java_lib_deps := $(full_java_libs)
 
 else
 
@@ -73,8 +72,18 @@
     LOCAL_JAVA_LIBRARIES := android_test_stubs_current $(LOCAL_JAVA_LIBRARIES)
     $(full_target): PRIVATE_BOOTCLASSPATH := $(call java-lib-files, android_test_stubs_current)
   else
-    LOCAL_JAVA_LIBRARIES := sdk_v$(LOCAL_SDK_VERSION) $(LOCAL_JAVA_LIBRARIES)
-    $(full_target): PRIVATE_BOOTCLASSPATH := $(call java-lib-files, sdk_v$(LOCAL_SDK_VERSION))
+    ifneq (,$(call has-system-sdk-version,$(LOCAL_SDK_VERSION)))
+      ifeq (,$(TARGET_BUILD_APPS))
+        LOCAL_JAVA_LIBRARIES := system_sdk_v$(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION)) $(LOCAL_JAVA_LIBRARIES)
+        $(full_target): PRIVATE_BOOTCLASSPATH := $(call java-lib-files, system_sdk_v$(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION)))
+      else
+        LOCAL_JAVA_LIBRARIES := sdk_v$(LOCAL_SDK_VERSION) $(LOCAL_JAVA_LIBRARIES)
+        $(full_target): PRIVATE_BOOTCLASSPATH := $(call java-lib-files, sdk_v$(LOCAL_SDK_VERSION))
+      endif
+    else
+      LOCAL_JAVA_LIBRARIES := sdk_v$(LOCAL_SDK_VERSION) $(LOCAL_JAVA_LIBRARIES)
+      $(full_target): PRIVATE_BOOTCLASSPATH := $(call java-lib-files, sdk_v$(LOCAL_SDK_VERSION))
+    endif
   endif
 else
   LOCAL_JAVA_LIBRARIES := core-oj core-libart ext framework $(LOCAL_JAVA_LIBRARIES)
@@ -83,19 +92,21 @@
 LOCAL_JAVA_LIBRARIES := $(sort $(LOCAL_JAVA_LIBRARIES))
 
 full_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES)) $(LOCAL_CLASSPATH)
-full_java_lib_deps := $(call java-lib-deps,$(LOCAL_JAVA_LIBRARIES)) $(LOCAL_CLASSPATH)
 endif # !LOCAL_IS_HOST_MODULE
 
-$(full_target): PRIVATE_CLASSPATH := $(subst $(space),:,$(full_java_libs))
-
+$(full_target): PRIVATE_CLASSPATH := $(call normalize-path-list,$(full_java_libs))
 
 intermediates.COMMON := $(call local-intermediates-dir,COMMON)
 
 $(full_target): PRIVATE_SOURCE_PATH := $(call normalize-path-list,$(LOCAL_DROIDDOC_SOURCE_PATH))
 $(full_target): PRIVATE_JAVA_FILES := $(filter %.java,$(full_src_files))
 $(full_target): PRIVATE_JAVA_FILES += $(addprefix $($(my_prefix)OUT_COMMON_INTERMEDIATES)/, $(filter %.java,$(LOCAL_INTERMEDIATE_SOURCES)))
+$(full_target): PRIVATE_JAVA_FILES += $(filter %.java,$(LOCAL_GENERATED_SOURCES))
+$(full_target): PRIVATE_SRCJARS := $(LOCAL_SRCJARS)
 $(full_target): PRIVATE_SOURCE_INTERMEDIATES_DIR := $(intermediates.COMMON)/src
+$(full_target): PRIVATE_SRCJAR_INTERMEDIATES_DIR := $(intermediates.COMMON)/srcjars
 $(full_target): PRIVATE_SRC_LIST_FILE := $(intermediates.COMMON)/droiddoc-src-list
+$(full_target): PRIVATE_SRCJAR_LIST_FILE := $(intermediates.COMMON)/droiddoc-srcjar-list
 
 ifneq ($(strip $(LOCAL_ADDITIONAL_JAVA_DIR)),)
 $(full_target): PRIVATE_ADDITIONAL_JAVA_DIR := $(LOCAL_ADDITIONAL_JAVA_DIR)
@@ -172,21 +183,26 @@
 # keep -bootclasspath here since it works in combination with -source 1.8.
 $(full_target): \
         $(full_src_files) \
+        $(LOCAL_GENERATED_SOURCES) \
         $(droiddoc_templates) \
         $(droiddoc) \
         $(html_dir_files) \
-        $(full_java_lib_deps) \
+        $(full_java_libs) \
+        $(EXTRACT_SRCJARS) \
+        $(LOCAL_SRCJARS) \
         $(LOCAL_ADDITIONAL_DEPENDENCIES)
 	@echo Docs droiddoc: $(PRIVATE_OUT_DIR)
 	$(hide) mkdir -p $(dir $@)
-	$(addprefix $(hide) rm -rf ,$(PRIVATE_STUB_OUT_DIR))
+	$(hide) rm -rf $(PRIVATE_STUB_OUT_DIR) $(PRIVATE_SRCJAR_INTERMEDIATES_DIR)
 	$(call prepare-doc-source-list,$(PRIVATE_SRC_LIST_FILE),$(PRIVATE_JAVA_FILES), \
 			$(PRIVATE_SOURCE_INTERMEDIATES_DIR) $(PRIVATE_ADDITIONAL_JAVA_DIR))
+	$(EXTRACT_SRCJARS) $(PRIVATE_SRCJAR_INTERMEDIATES_DIR) $(PRIVATE_SRCJAR_LIST_FILE) $(PRIVATE_SRCJARS)
 	$(hide) ( \
 		$(JAVADOC) \
                 -encoding UTF-8 \
                 -source 1.8 \
                 \@$(PRIVATE_SRC_LIST_FILE) \
+                \@$(PRIVATE_SRCJAR_LIST_FILE) \
                 -J-Xmx1600m \
                 -J-XX:-OmitStackTraceInFastThrow \
                 -XDignore.symbol.file \
@@ -216,7 +232,7 @@
 ##
 ##
 
-ifneq ($(EXPERIMENTAL_USE_OPENJDK9),)
+ifdef USE_OPENJDK9
 # For OpenJDK 9 we use --patch-module to define the core libraries code.
 # TODO(tobiast): Reorganize this when adding proper support for OpenJDK 9
 # modules. Here we treat all code in core libraries as being in java.base
@@ -226,17 +242,19 @@
 # For OpenJDK 8 we can use -bootclasspath to define the core libraries code.
 $(full_target): PRIVATE_BOOTCLASSPATH_ARG := $(addprefix -bootclasspath ,$(PRIVATE_BOOTCLASSPATH))
 endif
-
-$(full_target): $(full_src_files) $(full_java_lib_deps)
+$(full_target): $(full_src_files) $(LOCAL_GENERATED_SOURCES) $(full_java_libs) $(EXTRACT_SRCJARS) $(LOCAL_SRCJARS) $(LOCAL_ADDITIONAL_DEPENDENCIES)
 	@echo Docs javadoc: $(PRIVATE_OUT_DIR)
 	@mkdir -p $(dir $@)
+	rm -rf $(PRIVATE_SRCJAR_INTERMEDIATES_DIR)
 	$(call prepare-doc-source-list,$(PRIVATE_SRC_LIST_FILE),$(PRIVATE_JAVA_FILES), \
 			$(PRIVATE_SOURCE_INTERMEDIATES_DIR) $(PRIVATE_ADDITIONAL_JAVA_DIR))
+	$(EXTRACT_SRCJARS) $(PRIVATE_SRCJAR_INTERMEDIATES_DIR) $(PRIVATE_SRCJAR_LIST_FILE) $(PRIVATE_SRCJARS)
 	$(hide) ( \
 		$(JAVADOC) \
                 -encoding UTF-8 \
                 $(PRIVATE_DROIDDOC_OPTIONS) \
                 \@$(PRIVATE_SRC_LIST_FILE) \
+                \@$(PRIVATE_SRCJAR_LIST_FILE) \
                 -J-Xmx1024m \
                 -XDignore.symbol.file \
                 -Xdoclint:none \
diff --git a/core/dumpvar.mk b/core/dumpvar.mk
index acae48e..59efb04 100644
--- a/core/dumpvar.mk
+++ b/core/dumpvar.mk
@@ -1,37 +1,3 @@
-
-# List of variables we want to print in the build banner.
-print_build_config_vars := \
-  PLATFORM_VERSION_CODENAME \
-  PLATFORM_VERSION \
-  TARGET_PRODUCT \
-  TARGET_BUILD_VARIANT \
-  TARGET_BUILD_TYPE \
-  TARGET_PLATFORM_VERSION \
-  TARGET_BUILD_APPS \
-  TARGET_ARCH \
-  TARGET_ARCH_VARIANT \
-  TARGET_CPU_VARIANT \
-  TARGET_2ND_ARCH \
-  TARGET_2ND_ARCH_VARIANT \
-  TARGET_2ND_CPU_VARIANT \
-  HOST_ARCH \
-  HOST_2ND_ARCH \
-  HOST_OS \
-  HOST_OS_EXTRA \
-  HOST_CROSS_OS \
-  HOST_CROSS_ARCH \
-  HOST_CROSS_2ND_ARCH \
-  HOST_BUILD_TYPE \
-  BUILD_ID \
-  OUT_DIR \
-  AUX_OS_VARIANT_LIST
-
-ifeq ($(TARGET_BUILD_PDK),true)
-print_build_config_vars += \
-  TARGET_BUILD_PDK \
-  PDK_FUSION_PLATFORM_ZIP
-endif
-
 # ---------------------------------------------------------------
 # the setpath shell function in envsetup.sh uses this to figure out
 # what to add to the path given the config we have chosen.
@@ -52,79 +18,14 @@
 ANDROID_PREBUILTS := prebuilt/$(HOST_PREBUILT_TAG)
 ANDROID_GCC_PREBUILTS := prebuilts/gcc/$(HOST_PREBUILT_TAG)
 
-# The "dumpvar" stuff lets you say something like
-#
-#     CALLED_FROM_SETUP=true \
-#       make -f config/envsetup.make dumpvar-TARGET_OUT
-# or
-#     CALLED_FROM_SETUP=true \
-#       make -f config/envsetup.make dumpvar-abs-HOST_OUT_EXECUTABLES
-#
-# The plain (non-abs) version just dumps the value of the named variable.
-# The "abs" version will treat the variable as a path, and dumps an
-# absolute path to it.
-#
-dumpvar_goals := \
-	$(strip $(patsubst dumpvar-%,%,$(filter dumpvar-%,$(MAKECMDGOALS))))
-ifdef dumpvar_goals
-
-  ifneq ($(words $(dumpvar_goals)),1)
-    $(error Only one "dumpvar-" goal allowed. Saw "$(MAKECMDGOALS)")
-  endif
-
-  # If the goal is of the form "dumpvar-abs-VARNAME", then
-  # treat VARNAME as a path and return the absolute path to it.
-  absolute_dumpvar := $(strip $(filter abs-%,$(dumpvar_goals)))
-  ifdef absolute_dumpvar
-    dumpvar_goals := $(patsubst abs-%,%,$(dumpvar_goals))
-    DUMPVAR_VALUE := $(abspath $($(dumpvar_goals)))
-    dumpvar_target := dumpvar-abs-$(dumpvar_goals)
-  else
-    DUMPVAR_VALUE := $($(dumpvar_goals))
-    dumpvar_target := dumpvar-$(dumpvar_goals)
-  endif
-
-.PHONY: $(dumpvar_target)
-$(dumpvar_target):
-	@echo $(DUMPVAR_VALUE)
-
-endif # dumpvar_goals
-
-ifneq ($(dumpvar_goals),report_config)
-PRINT_BUILD_CONFIG:=
-endif
-
-ifneq ($(filter report_config,$(DUMP_MANY_VARS)),)
-# Construct the shell commands that print the config banner.
-report_config_sh := echo '============================================';
-report_config_sh += $(foreach v,$(print_build_config_vars),echo '$v=$($(v))';)
-report_config_sh += echo '============================================';
-endif
-
 # Dump mulitple variables to "<var>=<value>" pairs, one per line.
 # The output may be executed as bash script.
 # Input variables:
 #   DUMP_MANY_VARS: the list of variable names.
 #   DUMP_VAR_PREFIX: an optional prefix of the variable name added to the output.
-#   DUMP_MANY_ABS_VARS: the list of abs variable names.
-#   DUMP_ABS_VAR_PREFIX: an optional prefix of the abs variable name added to the output.
 .PHONY: dump-many-vars
 dump-many-vars :
-	@$(foreach v, $(filter-out report_config, $(DUMP_MANY_VARS)),\
+	@$(foreach v, $(DUMP_MANY_VARS),\
 	  echo "$(DUMP_VAR_PREFIX)$(v)='$($(v))'";)
-ifneq ($(filter report_config, $(DUMP_MANY_VARS)),)
-	@# Construct a special variable for report_config.
-	@# Escape \` to defer the execution of report_config_sh to preserve the line breaks.
-	@echo "$(DUMP_VAR_PREFIX)report_config=\`$(report_config_sh)\`"
-endif
-	@$(foreach v, $(sort $(DUMP_MANY_ABS_VARS)),\
-	  echo "$(DUMP_ABS_VAR_PREFIX)$(v)='$(abspath $($(v)))'";)
 
 endif # CALLED_FROM_SETUP
-
-ifneq ($(PRINT_BUILD_CONFIG),)
-$(info ============================================)
-$(foreach v, $(print_build_config_vars),\
-  $(info $v=$($(v))))
-$(info ============================================)
-endif
diff --git a/core/dynamic_binary.mk b/core/dynamic_binary.mk
index 949793b..f44b8a8 100644
--- a/core/dynamic_binary.mk
+++ b/core/dynamic_binary.mk
@@ -99,10 +99,15 @@
 my_breakpad_path := $(TARGET_OUT_BREAKPAD)/$(patsubst $(PRODUCT_OUT)/%,%,$(my_module_path))
 breakpad_input := $(relocation_packer_output)
 breakpad_output := $(my_breakpad_path)/$(my_installed_module_stem).sym
-$(breakpad_output) : $(breakpad_input) | $(BREAKPAD_DUMP_SYMS)
+$(breakpad_output) : $(breakpad_input) | $(BREAKPAD_DUMP_SYMS) $(PRIVATE_READELF)
 	@echo "target breakpad: $(PRIVATE_MODULE) ($@)"
 	@mkdir -p $(dir $@)
-	$(hide) $(BREAKPAD_DUMP_SYMS) -c $< > $@
+	$(hide) if $(PRIVATE_READELF) -S $< > /dev/null 2>&1 ; then \
+	  $(BREAKPAD_DUMP_SYMS) -c $< > $@ ; \
+	else \
+	  echo "skipped for non-elf file."; \
+	  touch $@; \
+	fi
 $(LOCAL_BUILT_MODULE) : $(breakpad_output)
 endif
 
diff --git a/core/envsetup.mk b/core/envsetup.mk
index 46066e3..e220fae 100644
--- a/core/envsetup.mk
+++ b/core/envsetup.mk
@@ -97,8 +97,6 @@
 # compiles except for arm/mips, so the HOST is whatever we are
 # running on
 
-UNAME := $(shell uname -sm)
-
 # HOST_OS
 ifneq (,$(findstring Linux,$(UNAME)))
   HOST_OS := linux
@@ -110,7 +108,15 @@
   HOST_OS := darwin
 endif
 
-HOST_OS_EXTRA:=$(shell python -c "import platform; print(platform.platform())")
+HOST_OS_EXTRA := $(shell uname -rsm)
+ifeq ($(HOST_OS),linux)
+  ifneq ($(wildcard /etc/os-release),)
+    HOST_OS_EXTRA += $(shell source /etc/os-release; echo $$PRETTY_NAME)
+  endif
+else ifeq ($(HOST_OS),darwin)
+  HOST_OS_EXTRA += $(shell sw_vers -productVersion)
+endif
+HOST_OS_EXTRA := $(subst $(space),-,$(HOST_OS_EXTRA))
 
 # BUILD_OS is the real host doing the build.
 BUILD_OS := $(HOST_OS)
@@ -118,11 +124,13 @@
 HOST_CROSS_OS :=
 # We can cross-build Windows binaries on Linux
 ifeq ($(HOST_OS),linux)
+ifeq ($(BUILD_HOST_static),)
 HOST_CROSS_OS := windows
 HOST_CROSS_ARCH := x86
 HOST_CROSS_2ND_ARCH := x86_64
 2ND_HOST_CROSS_IS_64_BIT := true
 endif
+endif
 
 ifeq ($(HOST_OS),)
 $(error Unable to determine HOST_OS from uname -sm: $(UNAME)!)
@@ -194,6 +202,14 @@
 # Set up minimal BOOTCLASSPATH list of jars to build/execute
 # java code with dalvikvm/art.
 TARGET_CORE_JARS := core-oj core-libart conscrypt okhttp bouncycastle apache-xml
+ifeq ($(EMMA_INSTRUMENT),true)
+  ifneq ($(EMMA_INSTRUMENT_STATIC),true)
+    # For instrumented build, if Jacoco is not being included statically
+    # in instrumented packages then include Jacoco classes into the
+    # bootclasspath.
+    TARGET_CORE_JARS += jacocoagent
+  endif # EMMA_INSTRUMENT_STATIC
+endif # EMMA_INSTRUMENT
 HOST_CORE_JARS := $(addsuffix -hostdex,$(TARGET_CORE_JARS))
 #################################################################
 
@@ -269,9 +285,14 @@
 endif
 
 # Check BOARD_VNDK_VERSION
+define check_vndk_version
+  $(eval vndk_path := prebuilts/vndk/v$(1)) \
+  $(if $(wildcard $(vndk_path)/Android.bp),,$(error VNDK version $(1) not found))
+endef
+
 ifdef BOARD_VNDK_VERSION
   ifneq ($(BOARD_VNDK_VERSION),current)
-    $(error BOARD_VNDK_VERSION: Only "current" is implemented)
+    $(call check_vndk_version,$(BOARD_VNDK_VERSION))
   endif
 
   TARGET_VENDOR_TEST_SUFFIX := /vendor
@@ -279,6 +300,10 @@
   TARGET_VENDOR_TEST_SUFFIX :=
 endif
 
+ifdef PRODUCT_EXTRA_VNDK_VERSIONS
+  $(foreach v,$(PRODUCT_EXTRA_VNDK_VERSIONS),$(call check_vndk_version,$(v)))
+endif
+
 # ---------------------------------------------------------------
 # Set up configuration for target machine.
 # The following must be set:
@@ -309,29 +334,17 @@
 
 SOONG_OUT_DIR := $(OUT_DIR)/soong
 
-DEBUG_OUT_DIR := $(OUT_DIR)/debug
+TARGET_OUT_ROOT := $(OUT_DIR)/target
 
-# Move the host or target under the debug/ directory
-# if necessary.
-TARGET_OUT_ROOT_release := $(OUT_DIR)/target
-TARGET_OUT_ROOT_debug := $(DEBUG_OUT_DIR)/target
-TARGET_OUT_ROOT := $(TARGET_OUT_ROOT_$(TARGET_BUILD_TYPE))
-
-HOST_OUT_ROOT_release := $(OUT_DIR)/host
-HOST_OUT_ROOT_debug := $(DEBUG_OUT_DIR)/host
-HOST_OUT_ROOT := $(HOST_OUT_ROOT_$(HOST_BUILD_TYPE))
+HOST_OUT_ROOT := $(OUT_DIR)/host
 
 # We want to avoid two host bin directories in multilib build.
-HOST_OUT_release := $(HOST_OUT_ROOT_release)/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
-HOST_OUT_debug := $(HOST_OUT_ROOT_debug)/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
-HOST_OUT := $(HOST_OUT_$(HOST_BUILD_TYPE))
+HOST_OUT := $(HOST_OUT_ROOT)/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
 SOONG_HOST_OUT := $(SOONG_OUT_DIR)/host/$(HOST_OS)-$(HOST_PREBUILT_ARCH)
 # TODO: remove
 BUILD_OUT := $(HOST_OUT)
 
-HOST_CROSS_OUT_release := $(HOST_OUT_ROOT_release)/windows-$(HOST_PREBUILT_ARCH)
-HOST_CROSS_OUT_debug := $(HOST_OUT_ROOT_debug)/windows-$(HOST_PREBUILT_ARCH)
-HOST_CROSS_OUT := $(HOST_CROSS_OUT_$(HOST_BUILD_TYPE))
+HOST_CROSS_OUT := $(HOST_OUT_ROOT)/windows-$(HOST_PREBUILT_ARCH)
 
 TARGET_PRODUCT_OUT_ROOT := $(TARGET_OUT_ROOT)/product
 
@@ -341,6 +354,7 @@
 PRODUCT_OUT := $(TARGET_PRODUCT_OUT_ROOT)/$(TARGET_DEVICE)
 
 OUT_DOCS := $(TARGET_COMMON_OUT_ROOT)/docs
+OUT_NDK_DOCS := $(TARGET_COMMON_OUT_ROOT)/ndk-docs
 
 BUILD_OUT_EXECUTABLES := $(BUILD_OUT)/bin
 SOONG_HOST_OUT_EXECUTABLES := $(SOONG_HOST_OUT)/bin
@@ -377,6 +391,8 @@
 
 HOST_CROSS_OUT_GEN := $(HOST_CROSS_OUT)/gen
 
+HOST_OUT_TEST_CONFIG := $(HOST_OUT)/test_config
+
 # Out for HOST_2ND_ARCH
 HOST_2ND_ARCH_VAR_PREFIX := 2ND_
 HOST_2ND_ARCH_MODULE_SUFFIX := _32
@@ -421,8 +437,16 @@
 TARGET_OUT := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_SYSTEM)
 ifneq ($(filter address,$(SANITIZE_TARGET)),)
 target_out_shared_libraries_base := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_ASAN)/system
+ifeq ($(SANITIZE_LITE),true)
+# When using SANITIZE_LITE, APKs must not be packaged with sanitized libraries, as they will not
+# work with unsanitized app_process. For simplicity, generate APKs into /data/asan/.
+target_out_app_base := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_ASAN)/system
+else
+target_out_app_base := $(TARGET_OUT)
+endif
 else
 target_out_shared_libraries_base := $(TARGET_OUT)
+target_out_app_base := $(TARGET_OUT)
 endif
 
 TARGET_OUT_EXECUTABLES := $(TARGET_OUT)/bin
@@ -436,16 +460,23 @@
 endif
 TARGET_OUT_RENDERSCRIPT_BITCODE := $(TARGET_OUT_SHARED_LIBRARIES)
 TARGET_OUT_JAVA_LIBRARIES := $(TARGET_OUT)/framework
-TARGET_OUT_APPS := $(TARGET_OUT)/app
-TARGET_OUT_APPS_PRIVILEGED := $(TARGET_OUT)/priv-app
+TARGET_OUT_APPS := $(target_out_app_base)/app
+TARGET_OUT_APPS_PRIVILEGED := $(target_out_app_base)/priv-app
 TARGET_OUT_KEYLAYOUT := $(TARGET_OUT)/usr/keylayout
 TARGET_OUT_KEYCHARS := $(TARGET_OUT)/usr/keychars
 TARGET_OUT_ETC := $(TARGET_OUT)/etc
 TARGET_OUT_NOTICE_FILES := $(TARGET_OUT_INTERMEDIATES)/NOTICE_FILES
 TARGET_OUT_FAKE := $(PRODUCT_OUT)/fake_packages
 TARGET_OUT_TESTCASES := $(PRODUCT_OUT)/testcases
+TARGET_OUT_TEST_CONFIG := $(PRODUCT_OUT)/test_config
 
+ifeq ($(SANITIZE_LITE),true)
+# When using SANITIZE_LITE, APKs must not be packaged with sanitized libraries, as they will not
+# work with unsanitized app_process. For simplicity, generate APKs into /data/asan/.
+TARGET_OUT_SYSTEM_OTHER := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_ASAN)/$(TARGET_COPY_OUT_SYSTEM_OTHER)
+else
 TARGET_OUT_SYSTEM_OTHER := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_SYSTEM_OTHER)
+endif
 
 # Out for TARGET_2ND_ARCH
 TARGET_2ND_ARCH_VAR_PREFIX := $(HOST_2ND_ARCH_VAR_PREFIX)
@@ -514,8 +545,16 @@
 TARGET_OUT_VENDOR := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_VENDOR)
 ifneq ($(filter address,$(SANITIZE_TARGET)),)
 target_out_vendor_shared_libraries_base := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_ASAN)/vendor
+ifeq ($(SANITIZE_LITE),true)
+# When using SANITIZE_LITE, APKs must not be packaged with sanitized libraries, as they will not
+# work with unsanitized app_process. For simplicity, generate APKs into /data/asan/.
+target_out_vendor_app_base := $(PRODUCT_OUT)/$(TARGET_COPY_OUT_ASAN)/vendor
+else
+target_out_vendor_app_base := $(TARGET_OUT_VENDOR)
+endif
 else
 target_out_vendor_shared_libraries_base := $(TARGET_OUT_VENDOR)
+target_out_vendor_app_base := $(TARGET_OUT_VENDOR)
 endif
 
 TARGET_OUT_VENDOR_EXECUTABLES := $(TARGET_OUT_VENDOR)/bin
@@ -527,8 +566,8 @@
 endif
 TARGET_OUT_VENDOR_RENDERSCRIPT_BITCODE := $(TARGET_OUT_VENDOR_SHARED_LIBRARIES)
 TARGET_OUT_VENDOR_JAVA_LIBRARIES := $(TARGET_OUT_VENDOR)/framework
-TARGET_OUT_VENDOR_APPS := $(TARGET_OUT_VENDOR)/app
-TARGET_OUT_VENDOR_APPS_PRIVILEGED := $(TARGET_OUT_VENDOR)/priv-app
+TARGET_OUT_VENDOR_APPS := $(target_out_vendor_app_base)/app
+TARGET_OUT_VENDOR_APPS_PRIVILEGED := $(target_out_vendor_app_base)/priv-app
 TARGET_OUT_VENDOR_ETC := $(TARGET_OUT_VENDOR)/etc
 
 $(TARGET_2ND_ARCH_VAR_PREFIX)TARGET_OUT_VENDOR_EXECUTABLES := $(TARGET_OUT_VENDOR_EXECUTABLES)
@@ -615,7 +654,7 @@
   DIST_DIR := $(OUT_DIR)/dist
 endif
 
-ifndef KATI
+ifeq ($(CALLED_FROM_SETUP),true)
 PRINT_BUILD_CONFIG ?= true
 endif
 
diff --git a/core/executable_internal.mk b/core/executable_internal.mk
index 3509bd2..0aec275 100644
--- a/core/executable_internal.mk
+++ b/core/executable_internal.mk
@@ -76,4 +76,39 @@
 	$(PRIVATE_POST_LINK_CMD)
 endif
 
+ifeq ($(my_native_coverage),true)
+gcno_suffix := .gcnodir
+
+built_whole_gcno_libraries := \
+    $(foreach lib,$(my_whole_static_libraries), \
+      $(call intermediates-dir-for, \
+        STATIC_LIBRARIES,$(lib),$(my_kind),,$(LOCAL_2ND_ARCH_VAR_PREFIX), \
+        $(my_host_cross))/$(lib)$(gcno_suffix))
+
+built_static_gcno_libraries := \
+    $(foreach lib,$(my_static_libraries), \
+      $(call intermediates-dir-for, \
+        STATIC_LIBRARIES,$(lib),$(my_kind),,$(LOCAL_2ND_ARCH_VAR_PREFIX), \
+        $(my_host_cross))/$(lib)$(gcno_suffix))
+
+ifdef LOCAL_IS_HOST_MODULE
+my_coverage_path := $($(my_prefix)OUT_COVERAGE)/$(patsubst $($(my_prefix)OUT)/%,%,$(my_module_path))
+else
+my_coverage_path := $(TARGET_OUT_COVERAGE)/$(patsubst $(PRODUCT_OUT)/%,%,$(my_module_path))
+endif
+
+GCNO_ARCHIVE := $(my_installed_module_stem)$(gcno_suffix)
+
+$(intermediates)/$(GCNO_ARCHIVE) : PRIVATE_ALL_OBJECTS := $(strip $(LOCAL_GCNO_FILES))
+$(intermediates)/$(GCNO_ARCHIVE) : PRIVATE_ALL_WHOLE_STATIC_LIBRARIES := $(strip $(built_whole_gcno_libraries)) $(strip $(built_static_gcno_libraries))
+$(intermediates)/$(GCNO_ARCHIVE) : PRIVATE_INTERMEDIATES_DIR := $(intermediates)
+$(intermediates)/$(GCNO_ARCHIVE) : $(LOCAL_GCNO_FILES) $(built_whole_gcno_libraries) $(built_static_gcno_libraries)
+	$(transform-o-to-static-lib)
+
+$(my_coverage_path)/$(GCNO_ARCHIVE) : $(intermediates)/$(GCNO_ARCHIVE)
+	$(copy-file-to-target)
+
+$(LOCAL_BUILT_MODULE): $(my_coverage_path)/$(GCNO_ARCHIVE)
+endif
+
 endif  # skip_build_from_source
diff --git a/core/find-jdk-tools-jar.sh b/core/find-jdk-tools-jar.sh
deleted file mode 100755
index ac0b3b6..0000000
--- a/core/find-jdk-tools-jar.sh
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/bin/sh
-if [ "x$ANDROID_JAVA_HOME" != x ] && [ -e "$ANDROID_JAVA_HOME/lib/tools.jar" ] ; then
-    echo $ANDROID_JAVA_HOME/lib/tools.jar
-else
-    JAVAC=$(realpath $(which javac) 2>/dev/null)
-    if [ -z "$JAVAC" ]; then
-        JAVAC=$(readlink -f $(which javac) 2>/dev/null)
-    fi
-    if [ -z "$JAVAC" ]; then
-        JAVAC=$(which javac)
-    fi
-    if [ -z "$JAVAC" ] ; then
-        exit 1
-    fi
-    while [ -L "$JAVAC" ] ; do
-        LSLINE=$(ls -l "$JAVAC")
-        JAVAC=$(echo -n "$LSLINE" | sed -e "s/.* -> //")
-    done
-    echo $JAVAC | sed -e 's:\(.*\)/javac$:\1/../lib/tools.jar:'
-fi
diff --git a/core/fuzz_test.mk b/core/fuzz_test.mk
index 4d41871..7e3995a 100644
--- a/core/fuzz_test.mk
+++ b/core/fuzz_test.mk
@@ -8,7 +8,7 @@
     $(error $(LOCAL_PATH): $(LOCAL_MODULE): NDK fuzz tests are not supported.)
 endif
 
-LOCAL_CFLAGS += -fsanitize-coverage=trace-pc-guard
+LOCAL_CFLAGS += -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp
 LOCAL_STATIC_LIBRARIES += libFuzzer
 
 ifdef LOCAL_MODULE_PATH
diff --git a/core/help.mk b/core/help.mk
deleted file mode 100644
index c034e79..0000000
--- a/core/help.mk
+++ /dev/null
@@ -1,35 +0,0 @@
-#
-# Copyright (C) 2010 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.
-#
-
-.PHONY: help
-help:
-	@echo
-	@echo "Common make targets:"
-	@echo "----------------------------------------------------------------------------------"
-	@echo "droid                   Default target"
-	@echo "clean                   (aka clobber) equivalent to rm -rf out/"
-	@echo "snod                    Quickly rebuild the system image from built packages"
-	@echo "vnod                    Quickly rebuild the vendor image from built packages"
-	@echo "offline-sdk-docs        Generate the HTML for the developer SDK docs"
-	@echo "doc-comment-check-docs  Check HTML doc links & validity, without generating HTML"
-	@echo "libandroid_runtime      All the JNI framework stuff"
-	@echo "framework               All the java framework stuff"
-	@echo "services                The system server (Java) and friends"
-	@echo "help                    You're reading it right now"
-
-.PHONY: out
-out:
-	@echo "I'm sure you're nice and all, but no thanks."
diff --git a/core/host_dalvik_java_library.mk b/core/host_dalvik_java_library.mk
index 4f3069f..1b3f967 100644
--- a/core/host_dalvik_java_library.mk
+++ b/core/host_dalvik_java_library.mk
@@ -24,53 +24,39 @@
 ifeq ($(HOST_OS),linux)
 USE_CORE_LIB_BOOTCLASSPATH := true
 
-#################################
-include $(BUILD_SYSTEM)/configure_local_jack.mk
-#################################
-
 #######################################
 include $(BUILD_SYSTEM)/host_java_library_common.mk
 #######################################
-ifdef LOCAL_JACK_ENABLED
-ifeq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true)
-  # For static library, $(LOCAL_BUILT_MODULE) is $(full_classes_jack).
-  LOCAL_BUILT_MODULE_STEM := classes.jack
-endif
-endif
 
-ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-  LOCAL_JAVA_LIBRARIES :=  core-oj-hostdex core-libart-hostdex $(LOCAL_JAVA_LIBRARIES)
-endif
-
+full_classes_turbine_jar := $(intermediates.COMMON)/classes-turbine.jar
+full_classes_header_jarjar := $(intermediates.COMMON)/classes-header-jarjar.jar
+full_classes_header_jar := $(intermediates.COMMON)/classes-header.jar
 full_classes_compiled_jar := $(intermediates.COMMON)/classes-full-debug.jar
+full_classes_combined_jar := $(intermediates.COMMON)/classes-combined.jar
 full_classes_desugar_jar := $(intermediates.COMMON)/desugar.classes.jar
 full_classes_jarjar_jar := $(intermediates.COMMON)/classes-jarjar.jar
 full_classes_jar := $(intermediates.COMMON)/classes.jar
-full_classes_jack := $(intermediates.COMMON)/classes.jack
-jack_check_timestamp := $(intermediates.COMMON)/jack.check.timestamp
 built_dex := $(intermediates.COMMON)/classes.dex
+java_source_list_file := $(intermediates.COMMON)/java-source-list
 
 LOCAL_INTERMEDIATE_TARGETS += \
+    $(full_classes_turbine_jar) \
     $(full_classes_compiled_jar) \
+    $(full_classes_combined_jar) \
     $(full_classes_desugar_jar) \
     $(full_classes_jarjar_jar) \
-    $(full_classes_jack) \
     $(full_classes_jar) \
-    $(jack_check_timestamp) \
-    $(built_dex)
+    $(built_dex) \
+    $(java_source_list_file)
 
 # See comment in java.mk
 ifndef LOCAL_CHECKED_MODULE
-ifdef LOCAL_JACK_ENABLED
-LOCAL_CHECKED_MODULE := $(jack_check_timestamp)
-else
 ifeq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true)
 LOCAL_CHECKED_MODULE := $(full_classes_compiled_jar)
 else
 LOCAL_CHECKED_MODULE := $(built_dex)
 endif
 endif
-endif
 
 #######################################
 include $(BUILD_SYSTEM)/base_rules.mk
@@ -83,49 +69,99 @@
 
 $(cleantarget): PRIVATE_CLEAN_FILES += $(intermediates.COMMON)
 
-ifndef LOCAL_JACK_ENABLED
+# List of dependencies for anything that needs all java sources in place
+java_sources_deps := \
+    $(java_sources) \
+    $(java_resource_sources) \
+    $(proto_java_sources_file_stamp) \
+    $(LOCAL_ADDITIONAL_DEPENDENCIES)
+
+$(java_source_list_file): $(java_sources_deps)
+	$(write-java-source-list)
 
 $(full_classes_compiled_jar): PRIVATE_JAVA_LAYERS_FILE := $(layers_file)
-$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
+$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
 $(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_FILES :=
 $(full_classes_compiled_jar): PRIVATE_JAR_PACKAGES :=
 $(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_PACKAGES :=
 $(full_classes_compiled_jar): \
-        $(java_sources) \
-        $(java_resource_sources) \
-        $(full_java_lib_deps) \
-        $(jar_manifest_file) \
-        $(proto_java_sources_file_stamp) \
-        $(annotation_processor_deps) \
-        $(NORMALIZE_PATH) \
-        $(LOCAL_ADDITIONAL_DEPENDENCIES) \
-        | $(SOONG_JAVAC_WRAPPER)
-	$(transform-host-java-to-package)
+    $(java_source_list_file) \
+    $(java_sources_deps) \
+    $(full_java_header_libs) \
+    $(full_java_bootclasspath_libs) \
+    $(full_java_system_modules_deps) \
+    $(annotation_processor_deps) \
+    $(NORMALIZE_PATH) \
+    $(JAR_ARGS) \
+    | $(SOONG_JAVAC_WRAPPER)
+	$(transform-host-java-to-dalvik-package)
+
+ifneq ($(TURBINE_ENABLED),false)
+
+$(full_classes_turbine_jar): PRIVATE_JAVACFLAGS := $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
+$(full_classes_turbine_jar): PRIVATE_DONT_DELETE_JAR_META_INF := $(LOCAL_DONT_DELETE_JAR_META_INF)
+$(full_classes_turbine_jar): \
+    $(java_source_list_file) \
+    $(java_sources_deps) \
+    $(full_java_header_libs) \
+    $(full_java_bootclasspath_libs) \
+    $(NORMALIZE_PATH) \
+    $(JAR_ARGS) \
+    $(ZIPTIME) \
+    | $(TURBINE) \
+    $(MERGE_ZIPS)
+	$(transform-java-to-header.jar)
+
+.KATI_RESTAT: $(full_classes_turbine_jar)
+
+# Run jarjar before generate classes-header.jar if necessary.
+ifneq ($(strip $(LOCAL_JARJAR_RULES)),)
+$(full_classes_header_jarjar): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
+$(full_classes_header_jarjar): $(full_classes_turbine_jar) $(LOCAL_JARJAR_RULES) | $(JARJAR)
+	@echo Header JarJar: $@
+	$(hide) $(JAVA) -jar $(JARJAR) process $(PRIVATE_JARJAR_RULES) $< $@
+else
+full_classes_header_jarjar := $(full_classes_turbine_jar)
+endif
+
+$(eval $(call copy-one-file,$(full_classes_header_jarjar),$(full_classes_header_jar)))
+
+endif # TURBINE_ENABLED != false
+
+$(full_classes_combined_jar): PRIVATE_DONT_DELETE_JAR_META_INF := $(LOCAL_DONT_DELETE_JAR_META_INF)
+$(full_classes_combined_jar): $(full_classes_compiled_jar) \
+                              $(jar_manifest_file) \
+                              $(full_static_java_libs)  | $(MERGE_ZIPS)
+	$(if $(PRIVATE_JAR_MANIFEST), $(hide) sed -e "s/%BUILD_NUMBER%/$(BUILD_NUMBER_FROM_FILE)/" \
+            $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf)
+	$(MERGE_ZIPS) -j $(if $(PRIVATE_JAR_MANIFEST),-m $(dir $@)/manifest.mf) \
+            $(if $(PRIVATE_DONT_DELETE_JAR_META_INF),,-stripDir META-INF -zipToNotStrip $<) \
+            $@ $< $(call reverse-list,$(PRIVATE_STATIC_JAVA_LIBRARIES))
+
+# Run jarjar if necessary, otherwise just copy the file.
+ifneq ($(strip $(LOCAL_JARJAR_RULES)),)
+$(full_classes_jarjar_jar): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
+$(full_classes_jarjar_jar): $(full_classes_combined_jar) $(LOCAL_JARJAR_RULES) | $(JARJAR)
+	@echo JarJar: $@
+	$(hide) $(JAVA) -jar $(JARJAR) process $(PRIVATE_JARJAR_RULES) $< $@
+else
+full_classes_jarjar_jar := $(full_classes_combined_jar)
+endif
+
+$(eval $(call copy-one-file,$(full_classes_jarjar_jar),$(full_classes_jar)))
 
 my_desugaring :=
 ifeq ($(LOCAL_JAVA_LANGUAGE_VERSION),1.8)
 my_desugaring := true
 $(full_classes_desugar_jar): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS)
-$(full_classes_desugar_jar): $(full_classes_compiled_jar) $(DESUGAR)
+$(full_classes_desugar_jar): $(full_classes_jar) $(full_java_header_libs) $(DESUGAR)
 	$(desugar-classes-jar)
 endif
 
 ifndef my_desugaring
-full_classes_desugar_jar := $(full_classes_compiled_jar)
+full_classes_desugar_jar := $(full_classes_jar)
 endif
 
-# Run jarjar if necessary, otherwise just copy the file.
-ifneq ($(strip $(LOCAL_JARJAR_RULES)),)
-$(full_classes_jarjar_jar): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
-$(full_classes_jarjar_jar): $(full_classes_desugar_jar) $(LOCAL_JARJAR_RULES) | $(JARJAR)
-	@echo JarJar: $@
-	$(hide) $(JAVA) -jar $(JARJAR) process $(PRIVATE_JARJAR_RULES) $< $@
-else
-full_classes_jarjar_jar := $(full_classes_desugar_jar)
-endif
-
-$(eval $(call copy-one-file,$(full_classes_jarjar_jar),$(full_classes_jar)))
-
 ifeq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true)
 # No dex; all we want are the .class files with resources.
 $(LOCAL_BUILT_MODULE) : $(java_resource_sources)
@@ -136,7 +172,7 @@
 else # !LOCAL_IS_STATIC_JAVA_LIBRARY
 $(built_dex): PRIVATE_INTERMEDIATES_DIR := $(intermediates.COMMON)
 $(built_dex): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS)
-$(built_dex): $(full_classes_jar) $(DX)
+$(built_dex): $(full_classes_desugar_jar) $(DX)
 	$(transform-classes.jar-to-dex)
 
 $(LOCAL_BUILT_MODULE): PRIVATE_DEX_FILE := $(built_dex)
@@ -150,66 +186,22 @@
 endif # !LOCAL_IS_STATIC_JAVA_LIBRARY
 
 ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEFAULT_APP_TARGET_SDK := $(LOCAL_SDK_VERSION)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SDK_VERSION := $(LOCAL_SDK_VERSION)
+  my_default_app_target_sdk := $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION))
+  my_sdk_version := $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION))
 else
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEFAULT_APP_TARGET_SDK := $(DEFAULT_APP_TARGET_SDK)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SDK_VERSION := $(PLATFORM_SDK_VERSION)
+  my_default_app_target_sdk := $(DEFAULT_APP_TARGET_SDK)
+  my_sdk_version := $(PLATFORM_SDK_VERSION)
 endif
 
-else # LOCAL_JACK_ENABLED
-$(LOCAL_INTERMEDIATE_TARGETS): \
-  PRIVATE_JACK_INTERMEDIATES_DIR := $(intermediates.COMMON)/jack-rsc
-
-ifeq ($(LOCAL_JACK_ENABLED),incremental)
-$(LOCAL_INTERMEDIATE_TARGETS): \
-  PRIVATE_JACK_INCREMENTAL_DIR := $(intermediates.COMMON)/jack-incremental
+ifdef LOCAL_MIN_SDK_VERSION
+  my_min_sdk_version := $(LOCAL_MIN_SDK_VERSION)
 else
-$(LOCAL_INTERMEDIATE_TARGETS): \
-  PRIVATE_JACK_INCREMENTAL_DIR :=
+  my_min_sdk_version := $(call codename-or-sdk-to-sdk,$(my_default_app_target_sdk))
 endif
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_FLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JACK_FLAGS)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_VERSION := $(LOCAL_JACK_VERSION)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_MIN_SDK_VERSION := $(PLATFORM_JACK_MIN_SDK_VERSION)
 
-jack_all_deps := $(java_sources) $(java_resource_sources) $(full_jack_deps) \
-        $(jar_manifest_file) $(proto_java_sources_file_stamp) \
-        $(LOCAL_ADDITIONAL_DEPENDENCIES) $(NORMALIZE_PATH) $(JACK_DEFAULT_ARGS) $(JACK)
-
-ifneq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true)
-$(built_dex): PRIVATE_CLASSES_JACK := $(full_classes_jack)
-$(built_dex): PRIVATE_JACK_PLUGIN_PATH := $(LOCAL_JACK_PLUGIN_PATH)
-$(built_dex): PRIVATE_JACK_PLUGIN := $(LOCAL_JACK_PLUGIN)
-$(built_dex): $(jack_all_deps) $(LOCAL_JACK_PLUGIN_PATH) | setup-jack-server
-	@echo Building with Jack: $@
-	$(jack-java-to-dex)
-
-# $(full_classes_jack) is just by-product of $(built_dex).
-# The dummy command was added because, without it, make misses the fact the $(built_dex) also
-# change $(full_classes_jack).
-$(full_classes_jack): $(built_dex)
-	$(hide) touch $@
-
-$(LOCAL_BUILT_MODULE): PRIVATE_DEX_FILE := $(built_dex)
-$(LOCAL_BUILT_MODULE): $(built_dex) $(java_resource_sources)
-	@echo "Host Jar: $(PRIVATE_MODULE) ($@)"
-	$(create-empty-package)
-	$(add-dex-to-package)
-	$(add-carried-jack-resources)
-
-else  # LOCAL_IS_STATIC_JAVA_LIBRARY
-$(full_classes_jack): PRIVATE_JACK_PLUGIN_PATH := $(LOCAL_JACK_PLUGIN_PATH)
-$(full_classes_jack): PRIVATE_JACK_PLUGIN := $(LOCAL_JACK_PLUGIN)
-$(full_classes_jack): $(jack_all_deps) $(LOCAL_JACK_PLUGIN_PATH) | setup-jack-server
-	@echo Building with Jack: $@
-	$(java-to-jack)
-
-endif  # LOCAL_IS_STATIC_JAVA_LIBRARY
-
-$(jack_check_timestamp): $(jack_all_deps) | setup-jack-server
-	@echo Checking build with Jack: $@
-	$(jack-check-java)
-endif # LOCAL_JACK_ENABLED
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEFAULT_APP_TARGET_SDK := $(my_default_app_target_sdk)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SDK_VERSION := $(my_sdk_version)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MIN_SDK_VERSION := $(my_min_sdk_version)
 
 USE_CORE_LIB_BOOTCLASSPATH :=
 
diff --git a/core/host_fuzz_test.mk b/core/host_fuzz_test.mk
index 8622911..556e02f 100644
--- a/core/host_fuzz_test.mk
+++ b/core/host_fuzz_test.mk
@@ -4,7 +4,7 @@
 ################################################
 $(call record-module-type,HOST_FUZZ_TEST)
 
-LOCAL_CFLAGS += -fsanitize-coverage=trace-pc-guard
+LOCAL_CFLAGS += -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp
 LOCAL_STATIC_LIBRARIES += libLLVMFuzzer
 
 include $(BUILD_HOST_EXECUTABLE)
diff --git a/core/host_java_library.mk b/core/host_java_library.mk
index a68be6a..9bead3f 100644
--- a/core/host_java_library.mk
+++ b/core/host_java_library.mk
@@ -34,10 +34,15 @@
 full_classes_compiled_jar := $(intermediates.COMMON)/classes-full-debug.jar
 full_classes_jarjar_jar := $(intermediates.COMMON)/classes-jarjar.jar
 full_classes_jar := $(intermediates.COMMON)/classes.jar
+java_source_list_file := $(intermediates.COMMON)/java-source-list
+full_classes_header_jar := $(intermediates.COMMON)/classes-header.jar
+full_classes_combined_jar := $(intermediates.COMMON)/classes-combined.jar
 
 LOCAL_INTERMEDIATE_TARGETS += \
     $(full_classes_compiled_jar) \
     $(full_classes_jarjar_jar) \
+    $(java_source_list_file) \
+    $(full_classes_combined_jar)
 
 #######################################
 include $(BUILD_SYSTEM)/base_rules.mk
@@ -58,36 +63,53 @@
 LOCAL_JAVACFLAGS += $(LOCAL_ERROR_PRONE_FLAGS)
 endif
 
+# List of dependencies for anything that needs all java sources in place
+java_sources_deps := \
+    $(java_sources) \
+    $(java_resource_sources) \
+    $(proto_java_sources_file_stamp) \
+    $(LOCAL_ADDITIONAL_DEPENDENCIES)
+
+$(java_source_list_file): $(java_sources_deps)
+	$(write-java-source-list)
+
 $(full_classes_compiled_jar): PRIVATE_JAVA_LAYERS_FILE := $(layers_file)
-$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
+$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
 $(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_FILES :=
 $(full_classes_compiled_jar): PRIVATE_JAR_PACKAGES :=
 $(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_PACKAGES :=
 $(full_classes_compiled_jar): \
-        $(java_sources) \
-        $(java_resource_sources) \
-        $(full_java_lib_deps) \
-        $(jar_manifest_file) \
-        $(proto_java_sources_file_stamp) \
-        $(annotation_processor_deps) \
-        $(NORMALIZE_PATH) \
-        $(ZIPTIME) \
-        $(LOCAL_ADDITIONAL_DEPENDENCIES) \
-        | $(SOONG_JAVAC_WRAPPER)
+    $(java_source_list_file) \
+    $(java_sources_deps) \
+    $(full_java_libs) \
+    $(full_java_bootclasspath_libs) \
+    $(annotation_processor_deps) \
+    $(NORMALIZE_PATH) \
+    $(ZIPTIME) \
+    $(JAR_ARGS) \
+    | $(SOONG_JAVAC_WRAPPER)
 	$(transform-host-java-to-package)
 	$(remove-timestamps-from-package)
 
 javac-check : $(full_classes_compiled_jar)
 javac-check-$(LOCAL_MODULE) : $(full_classes_compiled_jar)
 
+$(full_classes_combined_jar): $(full_classes_compiled_jar) \
+                              $(jar_manifest_file) \
+                              $(full_static_java_libs) | $(MERGE_ZIPS)
+	$(if $(PRIVATE_JAR_MANIFEST), $(hide) sed -e "s/%BUILD_NUMBER%/$(BUILD_NUMBER_FROM_FILE)/" \
+            $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf)
+	$(MERGE_ZIPS) -j $(if $(PRIVATE_JAR_MANIFEST),-m $(dir $@)/manifest.mf) \
+            -stripDir META-INF -zipToNotStrip $< $@ $< $(call reverse-list,$(PRIVATE_STATIC_JAVA_LIBRARIES))
+
 # Run jarjar if necessary, otherwise just copy the file.
 ifneq ($(strip $(LOCAL_JARJAR_RULES)),)
 $(full_classes_jarjar_jar): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
-$(full_classes_jarjar_jar): $(full_classes_compiled_jar) $(LOCAL_JARJAR_RULES) | $(JARJAR)
+$(full_classes_jarjar_jar): $(full_classes_combined_jar) $(LOCAL_JARJAR_RULES) | $(JARJAR)
 	@echo JarJar: $@
 	$(hide) $(JAVA) -jar $(JARJAR) process $(PRIVATE_JARJAR_RULES) $< $@
 else
-full_classes_jarjar_jar := $(full_classes_compiled_jar)
+full_classes_jarjar_jar := $(full_classes_combined_jar)
 endif
 
 
@@ -100,3 +122,6 @@
 $(eval $(call copy-one-file,$(LOCAL_FULL_CLASSES_JACOCO_JAR),$(LOCAL_BUILT_MODULE)))
 $(eval $(call copy-one-file,$(LOCAL_FULL_CLASSES_JACOCO_JAR),$(full_classes_jar)))
 
+ifeq ($(TURBINE_ENABLED),false)
+$(eval $(call copy-one-file,$(LOCAL_FULL_CLASSES_JACOCO_JAR),$(full_classes_header_jar)))
+endif
diff --git a/core/host_shared_library_internal.mk b/core/host_shared_library_internal.mk
index bfbde21..5e199cc 100644
--- a/core/host_shared_library_internal.mk
+++ b/core/host_shared_library_internal.mk
@@ -39,7 +39,7 @@
 include $(BUILD_SYSTEM)/binary.mk
 
 my_host_libprofile_rt := $($(LOCAL_2ND_ARCH_VAR_PREFIX)$(my_prefix)LIBPROFILE_RT)
-$(LOCAL_BUILD_MODULE): PRIVATE_HOST_LIBPROFILE_RT := $(my_host_libprofile_rt)
+$(LOCAL_BUILT_MODULE): PRIVATE_HOST_LIBPROFILE_RT := $(my_host_libprofile_rt)
 
 $(LOCAL_BUILT_MODULE): \
         $(all_objects) \
diff --git a/core/host_test_config.mk b/core/host_test_config.mk
new file mode 100644
index 0000000..b9975e5
--- /dev/null
+++ b/core/host_test_config.mk
@@ -0,0 +1,25 @@
+#
+# Copyright (C) 2017 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.
+#
+
+#
+# Common rules for building a TradeFed test XML file for host side tests.
+#
+
+$(call record-module-type,HOST_TEST_CONFIG)
+
+LOCAL_IS_HOST_MODULE := true
+
+include $(BUILD_SYSTEM)/test_config_common.mk
diff --git a/core/host_test_internal.mk b/core/host_test_internal.mk
index ffb22c7..42e01e1 100644
--- a/core/host_test_internal.mk
+++ b/core/host_test_internal.mk
@@ -5,9 +5,7 @@
 ifeq ($(LOCAL_GTEST),true)
   LOCAL_CFLAGS_windows += -DGTEST_OS_WINDOWS
   LOCAL_CFLAGS_linux += -DGTEST_OS_LINUX
-  LOCAL_LDLIBS_linux += -lpthread
   LOCAL_CFLAGS_darwin += -DGTEST_OS_MAC
-  LOCAL_LDLIBS_darwin += -lpthread
 
   LOCAL_CFLAGS += -DGTEST_HAS_STD_STRING -O0 -g
 
diff --git a/core/install_jni_libs.mk b/core/install_jni_libs.mk
index e88db40..b7d83dc 100644
--- a/core/install_jni_libs.mk
+++ b/core/install_jni_libs.mk
@@ -18,19 +18,9 @@
 ifneq ($(filter tests samples, $(LOCAL_MODULE_TAGS)),)
 my_embed_jni := true
 endif
-ifeq ($(PRODUCT_FULL_TREBLE),true)
-  ifeq ($(filter $(TARGET_OUT)/%, $(my_module_path)),)
-    # If this app isn't to be installed to the system partition, and the device
-    # is fully treble-ized then jni libs are embedded, Otherwise, access to the
-    # directory where the lib is installed to (usually /vendor/lib) needs to be
-    # allowed for system processes, which is a Treble violation.
-    my_embed_jni := true
-  endif
-else
-  ifeq ($(filter $(TARGET_OUT)/% $(TARGET_OUT_VENDOR)/% $(TARGET_OUT_OEM)/%, $(my_module_path)),)
-    # If this app isn't to be installed to system, vendor, or oem partitions.
-    my_embed_jni := true
-  endif
+ifeq ($(filter $(TARGET_OUT)/% $(TARGET_OUT_VENDOR)/% $(TARGET_OUT_OEM)/%, $(my_module_path)),)
+# If this app isn't to be installed to system partitions.
+my_embed_jni := true
 endif
 # If we're installing this APP as a compressed module, we include all JNI libraries
 # in the compressed artifact, rather than as separate files on the partition in question.
diff --git a/core/install_jni_libs_internal.mk b/core/install_jni_libs_internal.mk
index b5f10cf..265d482 100644
--- a/core/install_jni_libs_internal.mk
+++ b/core/install_jni_libs_internal.mk
@@ -116,7 +116,7 @@
 else
 my_link_type := app:platform
 my_warn_types :=
-my_allowed_types := native:ndk native:platform native:vendor
+my_allowed_types := native:ndk native:platform native:vendor native:vndk native:vndk_private
 endif
 
 my_link_deps := $(addprefix SHARED_LIBRARIES:,$(LOCAL_JNI_SHARED_LIBRARIES))
diff --git a/core/jack-default.args b/core/jack-default.args
deleted file mode 100644
index 433bc53..0000000
--- a/core/jack-default.args
+++ /dev/null
@@ -1,9 +0,0 @@
--D sched.runner=multi-threaded
--D sched.runner.thread.kind=fixed
--D sched.runner.thread.fixed.count=4
---sanity-checks off
--D jack.reporter.level.file=error=--,warning=-
---verbose error
--D jack.jayce.cache=false
--D jack.lambda.grouping-scope=package
--D jack.lambda.simplify-stateless=true
diff --git a/core/jacoco.mk b/core/jacoco.mk
index a04d25e..f51790d 100644
--- a/core/jacoco.mk
+++ b/core/jacoco.mk
@@ -19,44 +19,20 @@
 # (at the time of authorship, it is included by java.mk and
 # java_host_library.mk)
 
-my_include_filter :=
-my_exclude_filter :=
+# determine Jacoco include/exclude filters even when coverage is not enabled
+# to get syntax checking on LOCAL_JACK_COVERAGE_(INCLUDE|EXCLUDE)_FILTER
+# copy filters from Jack but also skip some known java packages
+my_include_filter := $(strip $(LOCAL_JACK_COVERAGE_INCLUDE_FILTER))
+my_exclude_filter := $(strip $(DEFAULT_JACOCO_EXCLUDE_FILTER),$(LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))
+
+my_include_args := $(call jacoco-class-filter-to-file-args, $(my_include_filter))
+my_exclude_args := $(call jacoco-class-filter-to-file-args, $(my_exclude_filter))
+
+# single-quote each arg of the include args so the '*' gets evaluated by zip
+# don't quote the exclude args they need to be evaluated by bash for rm -rf
+my_include_args := $(foreach arg,$(my_include_args),'$(arg)')
 
 ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
-  ifeq ($(ANDROID_COMPILE_WITH_JACK),false)
-    # determine Jacoco include/exclude filters
-    DEFAULT_JACOCO_EXCLUDE_FILTER := org/junit/*,org/jacoco/*,org/mockito/*
-    # copy filters from Jack but also skip some known java packages
-    my_include_filter := $(strip $(LOCAL_JACK_COVERAGE_INCLUDE_FILTER))
-    my_exclude_filter := $(strip $(DEFAULT_JACOCO_EXCLUDE_FILTER),$(LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))
-
-    # replace '.' with '/' and ',' with ' ', and quote each arg
-    ifneq ($(strip $(my_include_filter)),)
-      my_include_args := $(strip $(my_include_filter))
-
-      my_include_args := $(subst .,/,$(my_include_args))
-      my_include_args := '$(subst $(comma),' ',$(my_include_args))'
-    else
-      my_include_args :=
-    endif
-
-    # replace '.' with '/' and ',' with ' ', and quote each arg
-    ifneq ($(strip $(my_exclude_filter)),)
-      my_exclude_args := $(my_exclude_filter)
-
-      my_exclude_args := $(subst .,/,$(my_exclude_args))
-      my_exclude_args := $(subst $(comma)$(comma),$(comma),$(my_exclude_args))
-      my_exclude_args := '$(subst $(comma),' ', $(my_exclude_args))'
-    else
-      my_exclude_args :=
-    endif
-  endif # ANDROID_COMPILE_WITH_JACK==false
-endif # LOCAL_EMMA_INSTRUMENT == true
-
-# determine whether to run the instrumenter based on whether there is any work
-# for it to do
-ifneq ($(my_include_filter),)
-
   my_files := $(intermediates.COMMON)/jacoco
 
   # make a task that unzips the classes that we want to instrument from the
@@ -74,7 +50,8 @@
 	unzip -q $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) \
 	  -d $(PRIVATE_UNZIPPED_PATH) \
 	  $(PRIVATE_INCLUDE_ARGS)
-	rm -rf $(PRIVATE_EXCLUDE_ARGS)
+	(cd $(PRIVATE_UNZIPPED_PATH) && rm -rf $(PRIVATE_EXCLUDE_ARGS))
+	(cd $(PRIVATE_UNZIPPED_PATH) && find -not -name "*.class" -type f | xargs --no-run-if-empty rm)
 	touch $(PRIVATE_UNZIPPED_TIMESTAMP_PATH)
 # Unfortunately in the previous task above,
 # 'rm -rf $(PRIVATE_EXCLUDE_ARGS)' needs to be a separate
@@ -120,12 +97,13 @@
 $(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_TEMP_JAR_PATH := $(my_temp_jar_path)
 $(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_INSTRUMENTED_PATH := $(my_instrumented_path)
 $(LOCAL_FULL_CLASSES_JACOCO_JAR): PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
+$(LOCAL_FULL_CLASSES_JACOCO_JAR): $(JAR_ARGS)
 $(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_instrumented_timestamp_path) $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
 	rm -f $@ $(PRIVATE_TEMP_JAR_PATH)
 	# copy the pre-jacoco jar (containing files excluded from instrumentation)
 	cp $(PRIVATE_FULL_CLASSES_PRE_JACOCO_JAR) $(PRIVATE_TEMP_JAR_PATH)
 	# copy instrumented files back into the resultant jar
-	$(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) -C $(PRIVATE_INSTRUMENTED_PATH) .
+	$(JAR) -uf $(PRIVATE_TEMP_JAR_PATH) $(call jar-args-sorted-files-in-directory,$(PRIVATE_INSTRUMENTED_PATH))
 	mv $(PRIVATE_TEMP_JAR_PATH) $@
 
   # this is used to trigger $(my_classes_to_report_on_path) to build
@@ -133,8 +111,8 @@
   # dependency.
 $(LOCAL_FULL_CLASSES_JACOCO_JAR): $(my_classes_to_report_on_path)
 
-else # my_include_filter == ''
+else # LOCAL_EMMA_INSTRUMENT != true
   LOCAL_FULL_CLASSES_JACOCO_JAR := $(LOCAL_FULL_CLASSES_PRE_JACOCO_JAR)
-endif # my_include_filter != ''
+endif # LOCAL_EMMA_INSTRUMENT == true
 
 LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_FULL_CLASSES_JACOCO_JAR)
diff --git a/core/java.mk b/core/java.mk
index 3601292..75bc387 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -18,47 +18,6 @@
 LOCAL_NO_STANDARD_LIBRARIES:=$(strip $(LOCAL_NO_STANDARD_LIBRARIES))
 LOCAL_SDK_VERSION:=$(strip $(LOCAL_SDK_VERSION))
 
-ifneq ($(LOCAL_SDK_VERSION),)
-  ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-    $(error $(LOCAL_PATH): Must not define both LOCAL_NO_STANDARD_LIBRARIES and LOCAL_SDK_VERSION)
-  else
-    ifeq ($(strip $(filter $(LOCAL_SDK_VERSION),$(TARGET_AVAILABLE_SDK_VERSIONS))),)
-      $(error $(LOCAL_PATH): Invalid LOCAL_SDK_VERSION '$(LOCAL_SDK_VERSION)' \
-             Choices are: $(TARGET_AVAILABLE_SDK_VERSIONS))
-    else
-      ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),current)
-        # Use android_stubs_current if LOCAL_SDK_VERSION is current and no TARGET_BUILD_APPS.
-        LOCAL_JAVA_LIBRARIES := android_stubs_current $(LOCAL_JAVA_LIBRARIES)
-      else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),system_current)
-        LOCAL_JAVA_LIBRARIES := android_system_stubs_current $(LOCAL_JAVA_LIBRARIES)
-      else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),test_current)
-        LOCAL_JAVA_LIBRARIES := android_test_stubs_current $(LOCAL_JAVA_LIBRARIES)
-      else
-        LOCAL_JAVA_LIBRARIES := sdk_v$(LOCAL_SDK_VERSION) $(LOCAL_JAVA_LIBRARIES)
-      endif
-
-      ifeq ($(LOCAL_SDK_VERSION),current)
-        my_jack_min_sdk_version := $(PLATFORM_JACK_MIN_SDK_VERSION)
-      else ifeq ($(LOCAL_SDK_VERSION),system_current)
-        my_jack_min_sdk_version := $(PLATFORM_JACK_MIN_SDK_VERSION)
-      else ifeq ($(LOCAL_SDK_VERSION),test_current)
-        my_jack_min_sdk_version := $(PLATFORM_JACK_MIN_SDK_VERSION)
-      else
-        my_jack_min_sdk_version := $(LOCAL_SDK_VERSION)
-      endif
-    endif
-  endif
-else
-  my_jack_min_sdk_version := $(PLATFORM_JACK_MIN_SDK_VERSION)
-  ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-    LOCAL_JAVA_LIBRARIES := $(TARGET_DEFAULT_JAVA_LIBRARIES) $(LOCAL_JAVA_LIBRARIES)
-  endif
-endif
-
-ifneq (,$(strip $(LOCAL_MIN_SDK_VERSION)))
-  my_jack_min_sdk_version := $(LOCAL_MIN_SDK_VERSION)
-endif
-
 proto_sources := $(filter %.proto,$(LOCAL_SRC_FILES))
 ifneq ($(proto_sources),)
 ifeq ($(LOCAL_PROTOC_OPTIMIZE_TYPE),micro)
@@ -113,14 +72,20 @@
 LOCAL_PROGUARD_ENABLED :=
 endif
 
+full_classes_turbine_jar := $(intermediates.COMMON)/classes-turbine.jar
+full_classes_header_jarjar := $(intermediates.COMMON)/classes-header-jarjar.jar
+full_classes_header_jar := $(intermediates.COMMON)/classes-header.jar
 full_classes_compiled_jar := $(intermediates.COMMON)/$(full_classes_compiled_jar_leaf)
 full_classes_processed_jar := $(intermediates.COMMON)/classes-processed.jar
 full_classes_desugar_jar := $(intermediates.COMMON)/classes-desugar.jar
 jarjar_leaf := classes-jarjar.jar
 full_classes_jarjar_jar := $(intermediates.COMMON)/$(jarjar_leaf)
 full_classes_proguard_jar := $(intermediates.COMMON)/classes-proguard.jar
+full_classes_combined_jar := $(intermediates.COMMON)/classes-combined.jar
 built_dex_intermediate := $(intermediates.COMMON)/$(built_dex_intermediate_leaf)/classes.dex
 full_classes_stubs_jar := $(intermediates.COMMON)/stubs.jar
+java_source_list_file := $(intermediates.COMMON)/java-source-list
+
 
 ifeq ($(LOCAL_MODULE_CLASS)$(LOCAL_SRC_FILES)$(LOCAL_STATIC_JAVA_LIBRARIES)$(LOCAL_SOURCE_FILES_ALL_GENERATED),APPS)
 # If this is an apk without any Java code (e.g. framework-res), we should skip compiling Java.
@@ -130,25 +95,19 @@
 full_classes_jar := $(intermediates.COMMON)/classes.jar
 built_dex := $(intermediates.COMMON)/classes.dex
 endif
-# final Jack library, shrinked and obfuscated if it must be
-full_classes_jack := $(intermediates.COMMON)/classes.jack
-# intermediate Jack library without shrink and obfuscation
-noshrob_classes_jack := $(intermediates.COMMON)/classes.noshrob.jack
-jack_check_timestamp := $(intermediates.COMMON)/jack.check.timestamp
 
 LOCAL_INTERMEDIATE_TARGETS += \
+    $(full_classes_turbine_jar) \
     $(full_classes_compiled_jar) \
     $(full_classes_desugar_jar) \
     $(full_classes_jarjar_jar) \
     $(full_classes_jar) \
+    $(full_classes_combined_jar) \
     $(full_classes_proguard_jar) \
     $(built_dex_intermediate) \
-    $(full_classes_jack) \
-    $(noshrob_classes_jack) \
-    $(jack_check_timestamp) \
     $(built_dex) \
-    $(full_classes_stubs_jar)
-
+    $(full_classes_stubs_jar) \
+    $(java_source_list_file)
 
 LOCAL_INTERMEDIATE_SOURCE_DIR := $(intermediates.COMMON)/src
 
@@ -176,7 +135,7 @@
   ifneq (,$(LOCAL_SDK_VERSION))
     # Set target-api for LOCAL_SDK_VERSIONs other than current.
     ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
-      renderscript_target_api := $(LOCAL_SDK_VERSION)
+      renderscript_target_api := $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION))
     endif
   endif  # LOCAL_SDK_VERSION is set
 endif  # LOCAL_RENDERSCRIPT_TARGET_API is set
@@ -278,13 +237,21 @@
 LOCAL_JNI_SHARED_LIBRARIES += libRSSupportIO
 endif
 
+my_arch := $(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)
+ifneq (,$(filter arm64 mips64 x86_64,$(my_arch)))
+  my_min_sdk_version := 21
+else
+  my_min_sdk_version := $(MIN_SUPPORTED_SDK_VERSION)
+endif
 
 $(rs_compatibility_jni_libs): $(RenderScript_file_stamp) $(RS_PREBUILT_CLCORE) \
     $(rs_support_lib) $(rs_support_io_lib) $(rs_jni_lib) $(rs_compiler_rt)
 $(rs_compatibility_jni_libs): $(BCC_COMPAT)
 $(rs_compatibility_jni_libs): PRIVATE_CXX := $(CXX_WRAPPER) $(TARGET_CXX)
+$(rs_compatibility_jni_libs): PRIVATE_SDK_VERSION := $(my_min_sdk_version)
 $(rs_compatibility_jni_libs): $(renderscript_intermediate)/librs.%.so: \
-    $(renderscript_intermediate.bc_folder)%.bc
+    $(renderscript_intermediate.bc_folder)%.bc \
+    $(SOONG_OUT_DIR)/ndk.timestamp
 	$(transform-bc-to-so)
 
 endif
@@ -320,7 +287,7 @@
 endif # LOCAL_SDK_VERSION
 
 $(foreach s,$(aidl_sources),\
-    $(eval $(call define-aidl-java-rule,$(s),$(intermediates.COMMON),aidl_java_sources)))
+    $(eval $(call define-aidl-java-rule,$(s),$(intermediates.COMMON)/aidl,aidl_java_sources)))
 $(foreach java,$(aidl_java_sources), \
     $(call include-depfile,$(java:%.java=%.P),$(java)))
 
@@ -339,15 +306,7 @@
 # command line.
 ifndef LOCAL_CHECKED_MODULE
 ifdef full_classes_jar
-ifdef LOCAL_JACK_ENABLED
-ifeq ($(LOCAL_JACK_ENABLED),javac_frontend)
 LOCAL_CHECKED_MODULE := $(full_classes_compiled_jar)
-else
-LOCAL_CHECKED_MODULE := $(jack_check_timestamp)
-endif
-else
-LOCAL_CHECKED_MODULE := $(full_classes_compiled_jar)
-endif
 endif
 endif
 
@@ -360,11 +319,11 @@
 ###########################################################
 ifneq ($(strip $(logtags_sources)),)
 
-logtags_java_sources := $(patsubst %.logtags,%.java,$(addprefix $(intermediates.COMMON)/src/, $(logtags_sources)))
+logtags_java_sources := $(patsubst %.logtags,%.java,$(addprefix $(intermediates.COMMON)/logtags/, $(logtags_sources)))
 logtags_sources := $(addprefix $(LOCAL_PATH)/, $(logtags_sources))
 
 $(logtags_java_sources): PRIVATE_MERGED_TAG := $(TARGET_OUT_COMMON_INTERMEDIATES)/all-event-log-tags.txt
-$(logtags_java_sources): $(intermediates.COMMON)/src/%.java: $(LOCAL_PATH)/%.logtags $(TARGET_OUT_COMMON_INTERMEDIATES)/all-event-log-tags.txt $(JAVATAGS) build/tools/event_log_tags.py
+$(logtags_java_sources): $(intermediates.COMMON)/logtags/%.java: $(LOCAL_PATH)/%.logtags $(TARGET_OUT_COMMON_INTERMEDIATES)/all-event-log-tags.txt $(JAVATAGS) build/tools/event_log_tags.py
 	$(transform-logtags-to-java)
 
 else
@@ -374,7 +333,36 @@
 ##########################################
 java_sources := $(addprefix $(LOCAL_PATH)/, $(filter %.java,$(LOCAL_SRC_FILES))) $(aidl_java_sources) $(logtags_java_sources) \
                 $(filter %.java,$(LOCAL_GENERATED_SOURCES))
-all_java_sources := $(java_sources) $(addprefix $(TARGET_OUT_COMMON_INTERMEDIATES)/, $(filter %.java,$(LOCAL_INTERMEDIATE_SOURCES)))
+java_intermediate_sources := $(addprefix $(TARGET_OUT_COMMON_INTERMEDIATES)/, $(filter %.java,$(LOCAL_INTERMEDIATE_SOURCES)))
+all_java_sources := $(java_sources) $(java_intermediate_sources)
+
+enable_sharding :=
+ifneq ($(TURBINE_ENABLED),false)
+ifneq ($(LOCAL_JAVAC_SHARD_SIZE),)
+ifneq ($(LOCAL_JAR_PROCESSOR),)
+$(call pretty-error,Cannot set both LOCAL_JAVAC_SHARD_SIZE and LOCAL_JAR_PROCESSOR!)
+endif # LOCAL_JAR_PROCESSOR is not empty
+enable_sharding := true
+
+num_shards := $(call int_divide,$(words $(java_sources)),$(LOCAL_JAVAC_SHARD_SIZE))
+ifneq ($(words $(java_sources)),$(call int_multiply,$(LOCAL_JAVAC_SHARD_SIZE),$(num_shards)))
+# increment number of shards by 1.
+num_shards := $(call int_plus,$(num_shards),1)
+endif
+
+shard_idx_list := $(call int_range_list,1,$(num_shards))
+sharded_java_source_list_files += $(foreach x,$(shard_idx_list),$(java_source_list_file).shard.$(x))
+sharded_jar_list += $(foreach x,$(shard_idx_list),$(full_classes_compiled_jar).shard.$(x))
+
+# always put dynamically-located .java files (generated by Proto/resource, etc) in a new final shard.
+# increment number of shards by 1.
+num_shards := $(call int_plus,$(num_shards),1)
+sharded_java_source_list_files += $(java_source_list_file).shard.$(num_shards)
+sharded_jar_list += $(full_classes_compiled_jar).shard.$(num_shards)
+LOCAL_INTERMEDIATE_TARGETS += $(sharded_java_source_list_files)
+LOCAL_INTERMEDIATE_TARGETS += $(sharded_jar_list)
+endif # LOCAL_JAVAC_SHARD_SIZE is not empty
+endif # TURBINE_ENABLED != false
 
 include $(BUILD_SYSTEM)/java_common.mk
 
@@ -406,9 +394,6 @@
 endif
 endif
 
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_MIN_SDK_VERSION := $(my_jack_min_sdk_version)
-my_jack_min_sdk_version :=
-
 # Since we're using intermediates.COMMON, make sure that it gets cleaned
 # properly.
 $(cleantarget): PRIVATE_CLEAN_FILES += $(intermediates.COMMON)
@@ -443,38 +428,114 @@
 ifneq (,$(PRODUCT_MINIMIZE_JAVA_DEBUG_INFO))
 ifneq (,$(filter userdebug user,$(TARGET_BUILD_VARIANT)))
 LOCAL_JAVACFLAGS+= -g:source,lines
-LOCAL_JACK_FLAGS+= -D jack.dex.debug.vars=false -D jack.dex.debug.vars.synthetic=false
 endif
 endif
 
-$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
-$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_FILES := $(LOCAL_JAR_EXCLUDE_FILES)
-$(full_classes_compiled_jar): PRIVATE_JAR_PACKAGES := $(LOCAL_JAR_PACKAGES)
-$(full_classes_compiled_jar): PRIVATE_JAR_EXCLUDE_PACKAGES := $(LOCAL_JAR_EXCLUDE_PACKAGES)
-$(full_classes_compiled_jar): PRIVATE_DONT_DELETE_JAR_META_INF := $(LOCAL_DONT_DELETE_JAR_META_INF)
-$(full_classes_compiled_jar): \
-        $(java_sources) \
-        $(java_resource_sources) \
-        $(full_java_lib_deps) \
-        $(jar_manifest_file) \
-        $(layers_file) \
-        $(RenderScript_file_stamp) \
-        $(proto_java_sources_file_stamp) \
-        $(annotation_processor_deps) \
-        $(NORMALIZE_PATH) \
-        $(LOCAL_ADDITIONAL_DEPENDENCIES) \
-        | $(SOONG_JAVAC_WRAPPER)
-	$(transform-java-to-classes.jar)
+# List of dependencies for anything that needs all java sources in place
+java_sources_deps := \
+    $(java_sources) \
+    $(java_resource_sources) \
+    $(RenderScript_file_stamp) \
+    $(proto_java_sources_file_stamp) \
+    $(LOCAL_ADDITIONAL_DEPENDENCIES)
+
+$(java_source_list_file): $(java_sources_deps)
+	$(write-java-source-list)
+
+ifdef enable_sharding
+$(foreach x,$(shard_idx_list),\
+  $(eval $(call save-sharded-java-source-list,$(x),\
+    $(wordlist $(call int_plus,1,$(call int_multiply,$(LOCAL_JAVAC_SHARD_SIZE),$(call int_subtract,$(x),1))),\
+      $(call int_multiply,$(LOCAL_JAVAC_SHARD_SIZE),$(x)),$(sort $(java_sources))))))
+
+# always put dynamically-located .java files (generated by Proto/resource, etc) in a new final shard.
+$(java_source_list_file).shard.$(num_shards): PRIVATE_JAVA_INTERMEDIATE_SOURCES := $(java_intermediate_sources)
+$(java_source_list_file).shard.$(num_shards): $(java_resource_sources) \
+    $(RenderScript_file_stamp) \
+    $(proto_java_sources_file_stamp) \
+    $(LOCAL_ADDITIONAL_DEPENDENCIES) \
+    $(NORMALIZE_PATH)
+	$(hide) rm -f $@
+	$(call dump-words-to-file,$(PRIVATE_JAVA_INTERMEDIATE_SOURCES),$@.tmp)
+	$(call fetch-additional-java-source,$@.tmp)
+	$(hide) tr ' ' '\n' < $@.tmp | $(NORMALIZE_PATH) | sort -u > $@
+
+# Javac sharding with header libs including its own header jar as one of dependency.
+$(foreach x,$(shard_idx_list),\
+  $(eval $(call create-classes-full-debug.jar,$(full_classes_compiled_jar).shard.$(x),\
+    $(java_source_list_file).shard.$(x),\
+      $(full_java_header_libs) $(full_classes_header_jar),$(x),\
+        $(wordlist $(call int_plus,1,$(call int_multiply,$(LOCAL_JAVAC_SHARD_SIZE),$(call int_subtract,$(x),1))),\
+          $(call int_multiply,$(LOCAL_JAVAC_SHARD_SIZE),$(x)),$(sort $(java_sources))))))
+
+# Javac sharding for last shard with additional Java dependencies.
+$(eval $(call create-classes-full-debug.jar,$(full_classes_compiled_jar).shard.$(num_shards),\
+  $(java_source_list_file).shard.$(num_shards),$(full_java_header_libs) $(full_classes_header_jar),$(strip \
+    $(num_shards)),$$(java_resource_sources) $$(RenderScript_file_stamp) \
+      $$(proto_java_sources_file_stamp) $$(LOCAL_ADDITIONAL_DEPENDENCIES)))
+
+$(full_classes_compiled_jar): PRIVATE_SHARDED_JAR_LIST := $(sharded_jar_list)
+$(full_classes_compiled_jar): $(sharded_jar_list) | $(MERGE_ZIPS)
+	$(MERGE_ZIPS) -j $@ $(PRIVATE_SHARDED_JAR_LIST)
+else
+# we can't use single $ for java_sources_deps since it may contain hash '#' sign.
+$(eval $(call create-classes-full-debug.jar,$(full_classes_compiled_jar),\
+  $(java_source_list_file),$(full_java_header_libs),,$$(java_sources_deps)))
+
+endif # ifdef enable_sharding
+
+ifneq ($(TURBINE_ENABLED),false)
+
+$(full_classes_turbine_jar): PRIVATE_JAVACFLAGS := $(LOCAL_JAVACFLAGS) $(annotation_processor_flags)
+$(full_classes_turbine_jar): PRIVATE_DONT_DELETE_JAR_META_INF := $(LOCAL_DONT_DELETE_JAR_META_INF)
+$(full_classes_turbine_jar): \
+    $(java_source_list_file) \
+    $(java_sources_deps) \
+    $(full_java_header_libs) \
+    $(full_java_bootclasspath_libs) \
+    $(full_java_system_modules_deps) \
+    $(NORMALIZE_PATH) \
+    $(JAR_ARGS) \
+    $(ZIPTIME) \
+    | $(TURBINE) \
+    $(MERGE_ZIPS)
+	$(transform-java-to-header.jar)
+
+.KATI_RESTAT: $(full_classes_turbine_jar)
+
+# Run jarjar before generate classes-header.jar if necessary.
+ifneq ($(strip $(LOCAL_JARJAR_RULES)),)
+$(full_classes_header_jarjar): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
+$(full_classes_header_jarjar): $(full_classes_turbine_jar) $(LOCAL_JARJAR_RULES) | $(JARJAR)
+	@echo Header JarJar: $@
+	$(hide) $(JAVA) -jar $(JARJAR) process $(PRIVATE_JARJAR_RULES) $< $@
+else
+full_classes_header_jarjar := $(full_classes_turbine_jar)
+endif
+
+$(eval $(call copy-one-file,$(full_classes_header_jarjar),$(full_classes_header_jar)))
+
+endif # TURBINE_ENABLED != false
 
 javac-check : $(full_classes_compiled_jar)
 javac-check-$(LOCAL_MODULE) : $(full_classes_compiled_jar)
 
+$(full_classes_combined_jar): PRIVATE_DONT_DELETE_JAR_META_INF := $(LOCAL_DONT_DELETE_JAR_META_INF)
+$(full_classes_combined_jar): $(full_classes_compiled_jar) \
+                              $(jar_manifest_file) \
+                              $(full_static_java_libs) | $(MERGE_ZIPS)
+	$(if $(PRIVATE_JAR_MANIFEST), $(hide) sed -e "s/%BUILD_NUMBER%/$(BUILD_NUMBER_FROM_FILE)/" \
+            $(PRIVATE_JAR_MANIFEST) > $(dir $@)/manifest.mf)
+	$(MERGE_ZIPS) -j $(if $(PRIVATE_JAR_MANIFEST),-m $(dir $@)/manifest.mf) \
+            $(if $(PRIVATE_DONT_DELETE_JAR_META_INF),,-stripDir META-INF -zipToNotStrip $<) \
+            $@ $< $(call reverse-list,$(PRIVATE_STATIC_JAVA_LIBRARIES))
+
 ifdef LOCAL_JAR_PROCESSOR
 # LOCAL_JAR_PROCESSOR_ARGS must be evaluated here to set up the rule-local
 # PRIVATE_JAR_PROCESSOR_ARGS variable, but $< and $@ are not available yet.
 # Set ${in} and ${out} so they can be referenced by LOCAL_JAR_PROCESSOR_ARGS
 # using deferred evaluation (LOCAL_JAR_PROCESSOR_ARGS = instead of :=).
-in := $(full_classes_compiled_jar)
+in := $(full_classes_combined_jar)
 out := $(full_classes_processed_jar).tmp
 my_jar_processor := $(HOST_OUT_JAVA_LIBRARIES)/$(LOCAL_JAR_PROCESSOR).jar
 
@@ -484,7 +545,7 @@
 in :=
 out :=
 
-$(full_classes_processed_jar): $(full_classes_compiled_jar) $(my_jar_processor)
+$(full_classes_processed_jar): $(full_classes_combined_jar) $(my_jar_processor)
 	@echo Processing $@ with $(PRIVATE_JAR_PROCESSOR)
 	$(hide) rm -f $@ $(PRIVATE_TMP_OUT)
 	$(hide) $(JAVA) -jar $(PRIVATE_JAR_PROCESSOR) $(PRIVATE_JAR_PROCESSOR_ARGS)
@@ -492,34 +553,40 @@
 
 my_jar_processor :=
 else
-full_classes_processed_jar := $(full_classes_compiled_jar)
-endif
-
-my_desugaring :=
-ifndef LOCAL_JACK_ENABLED
-ifndef LOCAL_IS_STATIC_JAVA_LIBRARY
-my_desugaring := true
-$(full_classes_desugar_jar): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS)
-$(full_classes_desugar_jar): $(full_classes_processed_jar) $(DESUGAR)
-	$(desugar-classes-jar)
-endif
-endif
-
-ifndef my_desugaring
-full_classes_desugar_jar := $(full_classes_processed_jar)
+full_classes_processed_jar := $(full_classes_combined_jar)
 endif
 
 # Run jarjar if necessary
 ifneq ($(strip $(LOCAL_JARJAR_RULES)),)
 $(full_classes_jarjar_jar): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
-$(full_classes_jarjar_jar): $(full_classes_desugar_jar) $(LOCAL_JARJAR_RULES) | $(JARJAR)
+$(full_classes_jarjar_jar): $(full_classes_processed_jar) $(LOCAL_JARJAR_RULES) | $(JARJAR)
 	@echo JarJar: $@
 	$(hide) $(JAVA) -jar $(JARJAR) process $(PRIVATE_JARJAR_RULES) $< $@
 else
-full_classes_jarjar_jar := $(full_classes_desugar_jar)
+full_classes_jarjar_jar := $(full_classes_processed_jar)
 endif
 
-LOCAL_FULL_CLASSES_PRE_JACOCO_JAR := $(full_classes_jarjar_jar)
+$(eval $(call copy-one-file,$(full_classes_jarjar_jar),$(full_classes_jar)))
+
+# Temporarily enable --multi-dex until proguard supports v53 class files
+# ( http://b/67673860 ) or we move away from proguard altogether.
+ifdef TARGET_OPENJDK9
+LOCAL_DX_FLAGS := $(filter-out --multi-dex,$(LOCAL_DX_FLAGS)) --multi-dex
+endif
+
+my_desugaring :=
+ifndef LOCAL_IS_STATIC_JAVA_LIBRARY
+my_desugaring := true
+$(full_classes_desugar_jar): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS)
+$(full_classes_desugar_jar): $(full_classes_jar) $(full_java_header_libs) $(DESUGAR)
+	$(desugar-classes-jar)
+endif
+
+ifndef my_desugaring
+full_classes_desugar_jar := $(full_classes_jar)
+endif
+
+LOCAL_FULL_CLASSES_PRE_JACOCO_JAR := $(full_classes_desugar_jar)
 
 #######################################
 include $(BUILD_SYSTEM)/jacoco.mk
@@ -537,34 +604,36 @@
     $(error invalid value for LOCAL_PROGUARD_ENABLED: $(LOCAL_PROGUARD_ENABLED))
 endif
 proguard_dictionary := $(intermediates.COMMON)/proguard_dictionary
-jack_dictionary := $(intermediates.COMMON)/jack_dictionary
 
-# Hack: see b/20667396
-# When an app's LOCAL_SDK_VERSION is lower than the support library's LOCAL_SDK_VERSION,
-# we artifically raises the "SDK version" "linked" by ProGuard, to
+# When an app contains references to APIs that are not in the SDK specified by
+# its LOCAL_SDK_VERSION for example added by support library or by runtime 
+# classes added by desugar, we artifically raise the "SDK version" "linked" by
+# ProGuard, to
 # - suppress ProGuard warnings of referencing symbols unknown to the lower SDK version.
 # - prevent ProGuard stripping subclass in the support library that extends class added in the higher SDK version.
-my_support_library_sdk_raise :=
-ifneq (,$(filter android-support-%,$(LOCAL_STATIC_JAVA_LIBRARIES)))
+# See b/20667396
+my_proguard_sdk_raise :=
 ifdef LOCAL_SDK_VERSION
 ifdef TARGET_BUILD_APPS
 ifeq (,$(filter current system_current test_current, $(LOCAL_SDK_VERSION)))
-  my_support_library_sdk_raise := $(call java-lib-files, sdk_vcurrent)
+  my_proguard_sdk_raise := $(call java-lib-header-files, sdk_vcurrent)
 endif
 else
   # For platform build, we can't just raise to the "current" SDK,
   # that would break apps that use APIs removed from the current SDK.
-  my_support_library_sdk_raise := $(call java-lib-files,$(TARGET_DEFAULT_JAVA_LIBRARIES))
-endif
+  my_proguard_sdk_raise := $(call java-lib-header-files,$(TARGET_DEFAULT_BOOTCLASSPATH_LIBRARIES) $(TARGET_DEFAULT_JAVA_LIBRARIES))
 endif
 endif
 
-# jack already has the libraries in its classpath and doesn't support jars
-legacy_proguard_flags := $(addprefix -libraryjars ,$(my_support_library_sdk_raise) \
-  $(filter-out $(my_support_library_sdk_raise),$(full_shared_java_libs)))
+legacy_proguard_flags := $(addprefix -libraryjars ,$(my_proguard_sdk_raise) \
+  $(filter-out $(my_proguard_sdk_raise), \
+    $(full_java_bootclasspath_libs) \
+    $(full_shared_java_header_libs)))
+
+legacy_proguard_lib_deps := $(my_proguard_sdk_raise) \
+  $(filter-out $(my_proguard_sdk_raise),$(full_shared_java_header_libs))
 
 legacy_proguard_flags += -printmapping $(proguard_dictionary)
-jack_proguard_flags := -printmapping $(jack_dictionary)
 
 common_proguard_flags := -forceprocessing
 
@@ -572,11 +641,7 @@
 ifeq ($(filter nosystem,$(LOCAL_PROGUARD_ENABLED)),)
 common_proguard_flag_files += $(BUILD_SYSTEM)/proguard.flags
 ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
-ifdef LOCAL_JACK_ENABLED
-common_proguard_flag_files += $(BUILD_SYSTEM)/proguard.jacoco.flags
-else
 common_proguard_flags += -include $(BUILD_SYSTEM)/proguard.emma.flags
-endif # LOCAL_JACK_ENABLED
 endif
 # If this is a test package, add proguard keep flags for tests.
 ifneq ($(LOCAL_INSTRUMENTATION_FOR)$(filter tests,$(LOCAL_MODULE_TAGS)),)
@@ -604,8 +669,8 @@
 ifeq ($(filter obfuscation,$(LOCAL_PROGUARD_ENABLED)),)
 # If no obfuscation, link in the instrmented package's classes.jar as a library.
 # link_instr_classes_jar is defined in base_rule.mk
-# jack already has this library in its classpath and doesn't support jars
 legacy_proguard_flags += -libraryjars $(link_instr_classes_jar)
+legacy_proguard_lib_deps += $(link_instr_classes_jar)
 else # obfuscation
 # If obfuscation is enabled, the main app must be obfuscated too.
 # We need to run obfuscation using the main app's dictionary,
@@ -616,10 +681,6 @@
     -applymapping $(link_instr_intermediates_dir.COMMON)/proguard_dictionary \
     -verbose \
     $(legacy_proguard_flags)
-ifdef LOCAL_JACK_ENABLED
-jack_proguard_flags += -applymapping $(link_instr_intermediates_dir.COMMON)/jack_dictionary
-full_jack_deps += $(link_instr_intermediates_dir.COMMON)/jack_dictionary
-endif
 
 # Sometimes (test + main app) uses different keep rules from the main app -
 # apply the main app's dictionary anyway.
@@ -633,6 +694,9 @@
 endif  # LOCAL_PROGUARD_ENABLED is not nosystem
 
 proguard_flag_files := $(addprefix $(LOCAL_PATH)/, $(LOCAL_PROGUARD_FLAG_FILES))
+ifeq ($(USE_R8),true)
+proguard_flag_files += $(addprefix $(LOCAL_PATH)/, $(LOCAL_R8_FLAG_FILES))
+endif # USE_R8
 LOCAL_PROGUARD_FLAGS += $(addprefix -include , $(proguard_flag_files))
 
 ifdef LOCAL_TEST_MODULE_TO_PROGUARD_WITH
@@ -641,37 +705,47 @@
 extra_input_jar :=
 endif
 
-# If not using jack and building against the current SDK version then filter
-# out the junit, android.test and c.a.i.u.Predicate classes that are to be
-# removed from the Android API as part of b/30188076 but which are still
-# present in the Android API. This is to allow changes to be made to the
-# build to statically include those classes into the application without
+# If building against the current SDK version then filter out the junit,
+# android.test and c.a.i.u.Predicate classes that are to be removed from
+# the Android API as part of b/30188076 but which are still present in
+# the Android API. This is to allow changes to be made to the build to
+# statically include those classes into the application without
 # simultaneously removing those classes from the API.
 proguard_injar_filters :=
-ifndef LOCAL_JACK_ENABLED
 ifdef LOCAL_SDK_VERSION
 ifeq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
 proguard_injar_filters := (!junit/framework/**,!junit/runner/**,!junit/textui/**,!android/test/**,!com/android/internal/util/*)
 endif
 endif
+
+ifneq ($(filter obfuscation,$(LOCAL_PROGUARD_ENABLED)),)
+ifneq ($(USE_R8),true)
+  $(full_classes_proguard_jar): .KATI_IMPLICIT_OUTPUTS := $(proguard_dictionary)
+else
+  $(built_dex_intermediate): .KATI_IMPLICIT_OUTPUTS := $(proguard_dictionary)
+endif
 endif
 
+# If R8 is not enabled run Proguard.
+ifneq ($(USE_R8),true)
+# Changes to these dependencies need to be replicated below when using R8
+# instead of Proguard + dx.
 $(full_classes_proguard_jar): PRIVATE_PROGUARD_INJAR_FILTERS := $(proguard_injar_filters)
 $(full_classes_proguard_jar): PRIVATE_EXTRA_INPUT_JAR := $(extra_input_jar)
 $(full_classes_proguard_jar): PRIVATE_PROGUARD_FLAGS := $(legacy_proguard_flags) $(common_proguard_flags) $(LOCAL_PROGUARD_FLAGS)
-$(full_classes_proguard_jar) : $(full_classes_pre_proguard_jar) $(extra_input_jar) $(my_support_library_sdk_raise) $(common_proguard_flag_files) $(proguard_flag_files) | $(PROGUARD)
+$(full_classes_proguard_jar) : $(full_classes_pre_proguard_jar) $(extra_input_jar) $(my_proguard_sdk_raise) $(common_proguard_flag_files) $(proguard_flag_files) $(legacy_proguard_lib_deps) | $(PROGUARD)
 	$(call transform-jar-to-proguard)
+else # !USE_R8
+# Running R8 instead of Proguard, proguarded jar is actually the pre-Proguarded jar.
+full_classes_proguard_jar := $(full_classes_pre_proguard_jar)
+endif # !USE_R8
 
 else  # LOCAL_PROGUARD_ENABLED not defined
+proguard_flag_files :=
 full_classes_proguard_jar := $(full_classes_pre_proguard_jar)
 endif # LOCAL_PROGUARD_ENABLED defined
 
-$(eval $(call copy-one-file,$(full_classes_proguard_jar),$(full_classes_jar)))
-
-$(call define-jar-to-toc-rule, $(full_classes_jar))
-
 ifneq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true)
-ifndef LOCAL_JACK_ENABLED
 $(built_dex_intermediate): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS)
 # If you instrument class files that have local variable debug information in
 # them emma does not correctly maintain the local variable table.
@@ -682,9 +756,26 @@
 ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
 $(built_dex_intermediate): PRIVATE_DX_FLAGS += --no-locals
 endif
-$(built_dex_intermediate): $(full_classes_jar) $(DX)
+
+my_r8 :=
+ifdef LOCAL_PROGUARD_ENABLED
+ifeq ($(USE_R8),true)
+# These are the dependencies for the proguarded jar when running
+# Proguard + dx. They are used for the generated dex when using R8, as
+# R8 does Proguard + dx
+my_r8 := true
+$(built_dex_intermediate): PRIVATE_PROGUARD_INJAR_FILTERS := $(proguard_injar_filters)
+$(built_dex_intermediate): PRIVATE_EXTRA_INPUT_JAR := $(extra_input_jar)
+$(built_dex_intermediate): PRIVATE_PROGUARD_FLAGS := $(legacy_proguard_flags) $(common_proguard_flags) $(LOCAL_PROGUARD_FLAGS)
+$(built_dex_intermediate) : $(full_classes_proguard_jar) $(extra_input_jar) $(my_support_library_sdk_raise) $(common_proguard_flag_files) $(proguard_flag_files) $(legacy_proguard_lib_deps) $(R8_COMPAT_PROGUARD)
+	$(transform-jar-to-dex-r8)
+endif # USE_R8
+endif # LOCAL_PROGUARD_ENABLED
+
+ifndef my_r8
+$(built_dex_intermediate): $(full_classes_proguard_jar) $(DX)
 	$(transform-classes.jar-to-dex)
-endif # LOCAL_JACK_ENABLED is disabled
+endif
 
 $(built_dex): $(built_dex_intermediate)
 	@echo Copying: $@
@@ -723,134 +814,19 @@
 endif  # full_classes_jar is defined
 
 ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEFAULT_APP_TARGET_SDK := $(LOCAL_SDK_VERSION)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SDK_VERSION := $(LOCAL_SDK_VERSION)
+  my_default_app_target_sdk := $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION))
+  my_sdk_version := $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION))
 else
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEFAULT_APP_TARGET_SDK := $(DEFAULT_APP_TARGET_SDK)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SDK_VERSION := $(PLATFORM_SDK_VERSION)
+  my_default_app_target_sdk := $(DEFAULT_APP_TARGET_SDK)
+  my_sdk_version := $(PLATFORM_SDK_VERSION)
 endif
 
-ifdef LOCAL_JACK_ENABLED
-$(LOCAL_INTERMEDIATE_TARGETS): \
-	PRIVATE_JACK_INTERMEDIATES_DIR := $(intermediates.COMMON)/jack-rsc
-ifeq ($(LOCAL_JACK_ENABLED),incremental)
-$(LOCAL_INTERMEDIATE_TARGETS): \
-	PRIVATE_JACK_INCREMENTAL_DIR := $(intermediates.COMMON)/jack-incremental
-$(noshrob_classes_jack): PRIVATE_JACK_INCREMENTAL_DIR := $(intermediates.COMMON)/jack-noshrob-incremental
-$(jack_check_timestamp): PRIVATE_JACK_INCREMENTAL_DIR := $(intermediates.COMMON)/jack-check-incremental
+ifdef LOCAL_MIN_SDK_VERSION
+  my_min_sdk_version := $(LOCAL_MIN_SDK_VERSION)
 else
-$(LOCAL_INTERMEDIATE_TARGETS): \
-	PRIVATE_JACK_INCREMENTAL_DIR :=
-$(noshrob_classes_jack): PRIVATE_JACK_INCREMENTAL_DIR :=
-$(jack_check_timestamp): PRIVATE_JACK_INCREMENTAL_DIR :=
+  my_min_sdk_version := $(call codename-or-sdk-to-sdk,$(my_default_app_target_sdk))
 endif
 
-ifdef full_classes_jar
-ifdef LOCAL_PROGUARD_ENABLED
-
-ifndef LOCAL_JACK_PROGUARD_FLAGS
-    LOCAL_JACK_PROGUARD_FLAGS := $(LOCAL_PROGUARD_FLAGS)
-endif
-LOCAL_JACK_PROGUARD_FLAGS += $(addprefix -include , $(proguard_flag_files))
-ifdef LOCAL_TEST_MODULE_TO_PROGUARD_WITH
-    $(error $(LOCAL_MODULE): Build with jack when LOCAL_TEST_MODULE_TO_PROGUARD_WITH is defined is not yet implemented)
-endif
-
-# $(jack_dictionary) is just by-product of $(built_dex_intermediate).
-# The dummy command was added because, without it, make misses the fact the $(built_dex) also
-# change $(jack_dictionary).
-$(jack_dictionary): $(full_classes_jack)
-	$(hide) touch $@
-
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_PROGUARD_FLAGS := $(common_proguard_flags) $(jack_proguard_flags) $(LOCAL_JACK_PROGUARD_FLAGS)
-else  # LOCAL_PROGUARD_ENABLED not defined
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_PROGUARD_FLAGS :=
-endif # LOCAL_PROGUARD_ENABLED defined
-
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_FLAGS := $(GLOBAL_JAVAC_DEBUG_FLAGS) $(LOCAL_JACK_FLAGS) $(annotation_processor_flags)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_VERSION := $(LOCAL_JACK_VERSION)
-
-jack_all_deps := $(java_sources) $(java_resource_sources) $(full_jack_deps) \
-        $(jar_manifest_file) $(layers_file) $(RenderScript_file_stamp) \
-        $(common_proguard_flag_files) $(proguard_flag_files) \
-        $(proto_java_sources_file_stamp) $(annotation_processor_deps) \
-        $(LOCAL_ADDITIONAL_DEPENDENCIES) $(LOCAL_JARJAR_RULES) \
-        $(NORMALIZE_PATH) $(JACK_DEFAULT_ARGS) $(JACK)
-
-$(jack_check_timestamp): $(jack_all_deps) | setup-jack-server
-	@echo Checking build with Jack: $@
-	$(jack-check-java)
-
-ifeq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true)
-$(full_classes_jack): PRIVATE_JACK_PLUGIN_PATH := $(LOCAL_JACK_PLUGIN_PATH)
-$(full_classes_jack): PRIVATE_JACK_PLUGIN := $(LOCAL_JACK_PLUGIN)
-$(full_classes_jack): $(jack_all_deps) $(LOCAL_JACK_PLUGIN_PATH) | setup-jack-server
-	@echo Building with Jack: $@
-	$(java-to-jack)
-
-# Update timestamps of .toc files for static java libraries so
-# dependents will be always rebuilt.
-$(built_dex).toc: $(full_classes_jack)
-	touch $@
-
-else #LOCAL_IS_STATIC_JAVA_LIBRARY
-$(built_dex_intermediate): PRIVATE_CLASSES_JACK := $(full_classes_jack)
-
-ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
-LOCAL_JACK_PLUGIN_PATH += $(HOST_OUT_JAVA_LIBRARIES)/jack-coverage-plugin.jar
-LOCAL_JACK_PLUGIN += com.android.jack.coverage.CodeCoverage
-$(built_dex_intermediate): PRIVATE_JACK_COVERAGE_OPTIONS := \
-    -D jack.coverage=true \
-    -D jack.coverage.metadata.file=$(intermediates.COMMON)/coverage.em \
-    -D jack.coverage.jacoco.package=$(JACOCO_PACKAGE_NAME) \
-    $(addprefix -D jack.coverage.jacoco.include=,$(LOCAL_JACK_COVERAGE_INCLUDE_FILTER)) \
-    $(addprefix -D jack.coverage.jacoco.exclude=,$(LOCAL_JACK_COVERAGE_EXCLUDE_FILTER))
-else
-$(built_dex_intermediate): PRIVATE_JACK_COVERAGE_OPTIONS :=
-endif
-
-# Compiling with javac to jar, then converting jar to dex with jack
-ifeq ($(LOCAL_JACK_ENABLED),javac_frontend)
-
-# PRIVATE_EXTRA_JAR_ARGS and source files were already handled during javac
-$(built_dex_intermediate): PRIVATE_EXTRA_JAR_ARGS :=
-$(built_dex_intermediate): PRIVATE_JAVA_SOURCES :=
-$(built_dex_intermediate): PRIVATE_SOURCE_INTERMEDIATES_DIR :=
-$(built_dex_intermediate): PRIVATE_HAS_PROTO_SOURCES :=
-$(built_dex_intermediate): PRIVATE_HAS_RS_SOURCES :=
-
-# Incremental compilation is not supported when mixing javac and jack
-$(built_dex_intermediate): PRIVATE_JACK_INCREMENTAL_DIR :=
-
-# Pass output of javac to jack
-$(built_dex_intermediate): PRIVATE_JACK_IMPORT_JAR := $(full_classes_compiled_jar)
-$(built_dex_intermediate): $(full_classes_compiled_jar)
-else # LOCAL_JACK_ENABLED != javac_frontend
-$(built_dex_intermediate): PRIVATE_JACK_IMPORT_JAR :=
-endif # LOCAL_JACK_ENABLED != javac_frontend
-
-$(built_dex_intermediate): PRIVATE_JACK_PLUGIN_PATH := $(LOCAL_JACK_PLUGIN_PATH)
-$(built_dex_intermediate): PRIVATE_JACK_PLUGIN := $(LOCAL_JACK_PLUGIN)
-$(built_dex_intermediate): $(jack_all_deps) $(LOCAL_JACK_PLUGIN_PATH) | setup-jack-server
-	@echo Building with Jack: $@
-	$(jack-java-to-dex)
-
-# $(full_classes_jack) is just by-product of $(built_dex_intermediate).
-# The dummy command was added because, without it, make misses the fact the $(built_dex) also
-# change $(full_classes_jack).
-$(full_classes_jack): $(built_dex_intermediate)
-	$(hide) touch $@
-
-$(call define-dex-to-toc-rule, $(intermediates.COMMON))
-
-endif #LOCAL_IS_STATIC_JAVA_LIBRARY
-
-$(noshrob_classes_jack): PRIVATE_JACK_PLUGIN_PATH := $(LOCAL_JACK_PLUGIN_PATH)
-$(noshrob_classes_jack): PRIVATE_JACK_PLUGIN := $(LOCAL_JACK_PLUGIN)
-$(noshrob_classes_jack): PRIVATE_JACK_INTERMEDIATES_DIR := $(intermediates.COMMON)/jack-noshrob-rsc
-$(noshrob_classes_jack): PRIVATE_JACK_PROGUARD_FLAGS :=
-$(noshrob_classes_jack): $(jack_all_deps) $(LOCAL_JACK_PLUGIN_PATH) | setup-jack-server
-	@echo Building with Jack: $@
-	$(java-to-jack)
-endif  # full_classes_jar is defined
-endif # LOCAL_JACK_ENABLED
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEFAULT_APP_TARGET_SDK := $(my_default_app_target_sdk)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SDK_VERSION := $(my_sdk_version)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MIN_SDK_VERSION := $(my_min_sdk_version)
diff --git a/core/java_common.mk b/core/java_common.mk
index 0859a46..a816324 100644
--- a/core/java_common.mk
+++ b/core/java_common.mk
@@ -1,5 +1,11 @@
 # Common to host and target Java modules.
 
+my_soong_problems :=
+
+ifneq ($(filter ../%,$(LOCAL_SRC_FILES)),)
+my_soong_problems += dotdot_srcs
+endif
+
 ###########################################################
 ## Java version
 ###########################################################
@@ -15,11 +21,16 @@
 # Modules can override this logic by specifying
 # LOCAL_JAVA_LANGUAGE_VERSION explicitly.
 ifeq (,$(LOCAL_JAVA_LANGUAGE_VERSION))
-  private_sdk_versions_without_any_java_18_support := 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
-  ifneq (,$(filter $(LOCAL_SDK_VERSION), $(private_sdk_versions_without_any_java_18_support)))
+  ifneq (,$(filter $(LOCAL_SDK_VERSION), $(TARGET_SDK_VERSIONS_WITHOUT_JAVA_18_SUPPORT)))
     LOCAL_JAVA_LANGUAGE_VERSION := 1.7
-  else
+  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))
+    # TODO(ccross): allow 1.9 for current and unbundled once we have SDK system modules
+    LOCAL_JAVA_LANGUAGE_VERSION := 1.8
+  else
+    # DEFAULT_JAVA_LANGUAGE_VERSION is 1.8, unless TARGET_OPENJDK9 in which case it is 1.9
+    LOCAL_JAVA_LANGUAGE_VERSION := $(DEFAULT_JAVA_LANGUAGE_VERSION)
   endif
 endif
 LOCAL_JAVACFLAGS += -source $(LOCAL_JAVA_LANGUAGE_VERSION) -target $(LOCAL_JAVA_LANGUAGE_VERSION)
@@ -101,12 +112,20 @@
   java_resource_file_groups := $(filter-out %:,$(java_resource_file_groups))
 endif # LOCAL_JAVA_RESOURCE_DIRS
 
-LOCAL_JAVA_RESOURCE_FILES := $(strip $(LOCAL_JAVA_RESOURCE_FILES))
 ifneq ($(LOCAL_JAVA_RESOURCE_FILES),)
-  java_resource_file_groups += \
-    $(foreach f,$(LOCAL_JAVA_RESOURCE_FILES), \
-	$(patsubst %/,%,$(dir $(f)))::$(notdir $(f)) \
-     )
+  # Converts LOCAL_JAVA_RESOURCE_FILES := <file> to $(dir $(file))::$(notdir $(file))
+  # and LOCAL_JAVA_RESOURCE_FILES := <dir>:<file> to <dir>::<file>
+  java_resource_file_groups += $(strip $(foreach res,$(LOCAL_JAVA_RESOURCE_FILES), \
+    $(eval _file := $(call word-colon,2,$(res))) \
+    $(if $(_file), \
+      $(eval _base := $(call word-colon,1,$(res))), \
+      $(eval _base := $(dir $(res))) \
+        $(eval _file := $(notdir $(res)))) \
+    $(if $(filter /%, \
+      $(filter-out $(OUT_DIR)/%,$(_base) $(_file))), \
+        $(call pretty-error,LOCAL_JAVA_RESOURCE_FILES may not include absolute paths: $(_base) $(_file))) \
+    $(patsubst %/,%,$(_base))::$(_file)))
+
 endif # LOCAL_JAVA_RESOURCE_FILES
 
 ifdef java_resource_file_groups
@@ -152,7 +171,7 @@
 annotation_processor_deps :=
 
 ifdef LOCAL_ANNOTATION_PROCESSORS
-  annotation_processor_jars := $(call java-lib-deps,$(LOCAL_ANNOTATION_PROCESSORS),true)
+  annotation_processor_jars := $(call java-lib-files,$(LOCAL_ANNOTATION_PROCESSORS),true)
   annotation_processor_flags += -processorpath $(call normalize-path-list,$(annotation_processor_jars))
   annotation_processor_deps += $(annotation_processor_jars)
 
@@ -162,12 +181,11 @@
   annotation_processor_jars :=
 endif
 
-full_static_java_libs := \
-    $(foreach lib,$(LOCAL_STATIC_JAVA_LIBRARIES), \
-      $(call intermediates-dir-for, \
-        JAVA_LIBRARIES,$(lib),$(LOCAL_IS_HOST_MODULE),COMMON)/classes.jar)
+full_static_java_libs := $(call java-lib-files,$(LOCAL_STATIC_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
+full_static_java_header_libs := $(call java-lib-header-files,$(LOCAL_STATIC_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
 
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_STATIC_JAVA_LIBRARIES := $(full_static_java_libs)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_STATIC_JAVA_HEADER_LIBRARIES := $(full_static_java_header_libs)
 
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASSET_DIR := $(LOCAL_ASSET_DIR)
@@ -179,90 +197,140 @@
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PROTO_SOURCE_INTERMEDIATES_DIR := $(intermediates.COMMON)/proto
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_HAS_RS_SOURCES :=
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JAVA_SOURCES := $(all_java_sources)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JAVA_SOURCE_LIST := $(java_source_list_file)
 
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RMTYPEDEFS := $(LOCAL_RMTYPEDEFS)
 
-# full_java_libs: The list of files that should be used as the classpath.
-#                 Using this list as a dependency list WILL NOT WORK.
-# full_java_lib_deps: Should be specified as a prerequisite of this module
-#                 to guarantee that the files in full_java_libs will
-#                 be up-to-date.
+full_java_bootclasspath_libs :=
+empty_bootclasspath :=
+my_system_modules :=
+
 ifndef LOCAL_IS_HOST_MODULE
-ifeq ($(LOCAL_SDK_VERSION),)
-ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-# No bootclasspath. But we still need "" to prevent javac from using default host bootclasspath.
-my_bootclasspath := ""
-else  # LOCAL_NO_STANDARD_LIBRARIES
-my_bootclasspath := $(call java-lib-files,core-oj):$(call java-lib-files,core-libart)
-endif  # LOCAL_NO_STANDARD_LIBRARIES
-else
-ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),current)
-# LOCAL_SDK_VERSION is current and no TARGET_BUILD_APPS.
-my_bootclasspath := $(call java-lib-files,android_stubs_current)
-else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),system_current)
-my_bootclasspath := $(call java-lib-files,android_system_stubs_current)
-else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),test_current)
-my_bootclasspath := $(call java-lib-files,android_test_stubs_current)
-else
-my_bootclasspath := $(call java-lib-files,sdk_v$(LOCAL_SDK_VERSION))
-endif # current, system_current, or test_current
-endif # LOCAL_SDK_VERSION
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(my_bootclasspath)
+  ifeq ($(LOCAL_SDK_VERSION),)
+    ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
+      # No bootclasspath. But we still need "" to prevent javac from using default host bootclasspath.
+      empty_bootclasspath := ""
+      # Most users of LOCAL_NO_STANDARD_LIBRARIES really mean no framework libs,
+      # and manually add back the core libs.  The ones that don't are in soong
+      # now, so just always assume that they want the default system modules
+      my_system_modules := $(DEFAULT_SYSTEM_MODULES)
+    else  # LOCAL_NO_STANDARD_LIBRARIES
+      full_java_bootclasspath_libs := $(call java-lib-header-files,$(TARGET_DEFAULT_BOOTCLASSPATH_LIBRARIES) $(TARGET_DEFAULT_JAVA_LIBRARIES))
+      LOCAL_JAVA_LIBRARIES := $(filter-out $(TARGET_DEFAULT_BOOTCLASSPATH_LIBRARIES) $(TARGET_DEFAULT_JAVA_LIBRARIES),$(LOCAL_JAVA_LIBRARIES))
+      my_system_modules := $(DEFAULT_SYSTEM_MODULES)
+    endif  # LOCAL_NO_STANDARD_LIBRARIES
+  else
+    ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
+      $(call pretty-error,Must not define both LOCAL_NO_STANDARD_LIBRARIES and LOCAL_SDK_VERSION)
+    endif
+    ifeq ($(strip $(filter $(LOCAL_SDK_VERSION),$(TARGET_AVAILABLE_SDK_VERSIONS))),)
+      $(call pretty-error,Invalid LOCAL_SDK_VERSION '$(LOCAL_SDK_VERSION)' \
+             Choices are: $(TARGET_AVAILABLE_SDK_VERSIONS))
+    endif
+    ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),current)
+      # LOCAL_SDK_VERSION is current and no TARGET_BUILD_APPS.
+      full_java_bootclasspath_libs := $(call java-lib-header-files,android_stubs_current)
+    else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),system_current)
+      full_java_bootclasspath_libs := $(call java-lib-header-files,android_system_stubs_current)
+    else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),test_current)
+      full_java_bootclasspath_libs := $(call java-lib-header-files,android_test_stubs_current)
+    else
+      ifneq (,$(call has-system-sdk-version,$(LOCAL_SDK_VERSION)))
+        ifeq (,$(TARGET_BUILD_APPS))
+          full_java_bootclasspath_libs := $(call java-lib-header-files,system_sdk_v$(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION)))
+        else
+          full_java_bootclasspath_libs := $(call java-lib-header-files,sdk_v$(LOCAL_SDK_VERSION))
+        endif
+      else
+        full_java_bootclasspath_libs := $(call java-lib-header-files,sdk_v$(LOCAL_SDK_VERSION))
+      endif
+    endif # current, system_current, system_${VER} or test_current
+  endif # LOCAL_SDK_VERSION
 
-# In order to compile lambda code javac requires various invokedynamic-
-# related classes to be present. This change adds stubs needed for
-# javac to compile lambdas.
-my_additional_javac_libs :=
-ifndef TARGET_BUILD_APPS
-# TODO: support to build lamdbas using javac in unbundled build.
-# We may need to check in a prebuilt core-lambda-stubs to prebuilts/sdk.
-ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-my_additional_javac_libs := core-lambda-stubs
-endif
-endif
+  ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
+    ifneq ($(LOCAL_MODULE),jacocoagent)
+      ifeq ($(EMMA_INSTRUMENT),true)
+        ifneq ($(EMMA_INSTRUMENT_STATIC),true)
+          # For instrumented build, if Jacoco is not being included statically
+          # in instrumented packages then include Jacoco classes into the
+          # bootclasspath.
+          full_java_bootclasspath_libs += $(call java-lib-header-files,jacocoagent)
+        endif # EMMA_INSTRUMENT_STATIC
+      endif # EMMA_INSTRUMENT
+    endif # LOCAL_MODULE == jacocoagent
+  endif # LOCAL_NO_STANDARD_LIBRARIES
 
-full_shared_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES) $(my_additional_javac_libs),$(LOCAL_IS_HOST_MODULE))
-full_java_lib_deps := $(call java-lib-deps,$(LOCAL_JAVA_LIBRARIES) $(my_additional_javac_libs),$(LOCAL_IS_HOST_MODULE))
-full_java_lib_deps := $(addsuffix .toc, $(full_java_lib_deps))
+  # In order to compile lambda code javac requires various invokedynamic-
+  # 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
+      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)
+    endif
+  endif
+
+  full_shared_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
+  full_shared_java_header_libs := $(call java-lib-header-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
 
 else # LOCAL_IS_HOST_MODULE
 
-ifeq ($(USE_CORE_LIB_BOOTCLASSPATH),true)
-ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-my_bootclasspath := ""
-else
-my_bootclasspath := $(call normalize-path-list,$(call java-lib-files,core-oj-hostdex core-libart-hostdex,true))
-endif
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(my_bootclasspath)
+  ifeq ($(USE_CORE_LIB_BOOTCLASSPATH),true)
+    ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
+      empty_bootclasspath := ""
+    else
+      full_java_bootclasspath_libs := $(call java-lib-header-files,$(addsuffix -hostdex,$(TARGET_DEFAULT_BOOTCLASSPATH_LIBRARIES)),true)
+    endif
 
-full_shared_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES),true)
-full_java_lib_deps := $(full_shared_java_libs)
-else # !USE_CORE_LIB_BOOTCLASSPATH
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH :=
+    my_system_modules := $(DEFAULT_SYSTEM_MODULES)
+    full_shared_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES),true)
+    full_shared_java_header_libs := $(call java-lib-header-files,$(LOCAL_JAVA_LIBRARIES),true)
+  else # !USE_CORE_LIB_BOOTCLASSPATH
 
-full_shared_java_libs := $(addprefix $(HOST_OUT_JAVA_LIBRARIES)/,\
-    $(addsuffix $(COMMON_JAVA_PACKAGE_SUFFIX),$(LOCAL_JAVA_LIBRARIES)))
-full_java_lib_deps := $(full_shared_java_libs)
-endif # USE_CORE_LIB_BOOTCLASSPATH
+    full_shared_java_libs := $(addprefix $(HOST_OUT_JAVA_LIBRARIES)/,\
+      $(addsuffix $(COMMON_JAVA_PACKAGE_SUFFIX),$(LOCAL_JAVA_LIBRARIES)))
+    full_shared_java_header_libs := $(full_shared_java_libs)
+  endif # USE_CORE_LIB_BOOTCLASSPATH
 endif # !LOCAL_IS_HOST_MODULE
 
-full_java_libs := $(full_shared_java_libs) $(full_static_java_libs) $(LOCAL_CLASSPATH)
-full_java_lib_deps := $(full_java_lib_deps) $(full_static_java_libs) $(LOCAL_CLASSPATH)
+ifdef empty_bootclasspath
+  ifdef full_java_bootclasspath_libs
+    $(call pretty-error,internal error: empty_bootclasspath and full_java_bootclasspath_libs should not both be set)
+  endif
+endif
+
+full_java_system_modules_deps :=
+my_system_modules_dir :=
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_USE_SYSTEM_MODULES :=
+ifeq ($(LOCAL_JAVA_LANGUAGE_VERSION),1.9)
+  $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_USE_SYSTEM_MODULES := true
+  ifdef my_system_modules
+    ifneq ($(my_system_modules),none)
+      ifndef SOONG_SYSTEM_MODULES_$(my_system_modules)
+        $(call pretty-error, Invalid system modules $(my_system_modules))
+      endif
+      full_java_system_modules_deps := $(SOONG_SYSTEM_MODULES_$(my_system_modules))
+      my_system_modules_dir := $(patsubst %/lib/modules,%,$(SOONG_SYSTEM_MODULES_$(my_system_modules)))
+    endif
+  endif
+endif
+
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := $(full_java_bootclasspath_libs)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_EMPTY_BOOTCLASSPATH := $(empty_bootclasspath)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SYSTEM_MODULES := $(my_system_modules_dir)
 
 ifndef LOCAL_IS_HOST_MODULE
 # This is set by packages that are linking to other packages that export
 # shared libraries, allowing them to make use of the code in the linked apk.
 apk_libraries := $(sort $(LOCAL_APK_LIBRARIES) $(LOCAL_RES_LIBRARIES))
 ifneq ($(apk_libraries),)
-  link_apk_libraries := \
-      $(foreach lib,$(apk_libraries), \
-        $(call intermediates-dir-for, \
-              APPS,$(lib),,COMMON)/classes-pre-proguard.jar)
+  link_apk_libraries := $(call app-lib-files,$(apk_libraries))
+  link_apk_header_libs := $(call app-lib-header-files,$(apk_libraries))
 
   # link against the jar with full original names (before proguard processing).
   full_shared_java_libs += $(link_apk_libraries)
-  full_java_libs += $(link_apk_libraries)
-  full_java_lib_deps += $(link_apk_libraries)
+  full_shared_java_header_libs += $(link_apk_header_libs)
 endif
 
 # This is set by packages that contain instrumentation, allowing them to
@@ -279,8 +347,13 @@
       APPS,$(LOCAL_INSTRUMENTATION_FOR),,COMMON)
   # link against the jar with full original names (before proguard processing).
   link_instr_classes_jar := $(link_instr_intermediates_dir.COMMON)/classes-pre-proguard.jar
-  full_java_libs += $(link_instr_classes_jar)
-  full_java_lib_deps += $(link_instr_classes_jar)
+  ifneq ($(TURBINE_ENABLED),false)
+    link_instr_classes_header_jar := $(link_instr_intermediates_dir.COMMON)/classes-header.jar
+  else
+    link_instr_classes_header_jar := $(link_instr_intermediates_dir.COMMON)/classes.jar
+  endif
+  full_shared_java_libs += $(link_instr_classes_jar)
+  full_shared_java_header_libs += $(link_instr_classes_header_jar)
 endif  # LOCAL_INSTRUMENTATION_FOR
 endif  # LOCAL_IS_HOST_MODULE
 
@@ -327,72 +400,17 @@
 endif
 endif  # !LOCAL_IS_HOST_MODULE
 
+full_java_libs := $(full_shared_java_libs) $(full_static_java_libs) $(LOCAL_CLASSPATH)
+full_java_header_libs := $(full_shared_java_header_libs) $(full_static_java_header_libs)
+
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_JAVA_LIBRARIES := $(full_java_libs)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_JAVA_HEADER_LIBRARIES := $(full_java_header_libs)
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SHARED_JAVA_HEADER_LIBRARIES := $(full_shared_java_header_libs)
 
 ALL_MODULES.$(my_register_name).INTERMEDIATE_SOURCE_DIR := \
     $(ALL_MODULES.$(my_register_name).INTERMEDIATE_SOURCE_DIR) $(LOCAL_INTERMEDIATE_SOURCE_DIR)
 
 ###########################################################
-# JACK
-###########################################################
-ifdef LOCAL_JACK_ENABLED
-ifdef need_compile_java
-
-LOCAL_JACK_FLAGS += -D jack.java.source.version=$(LOCAL_JAVA_LANGUAGE_VERSION)
-
-full_static_jack_libs := \
-    $(foreach lib,$(LOCAL_STATIC_JAVA_LIBRARIES), \
-      $(call intermediates-dir-for, \
-        JAVA_LIBRARIES,$(lib),$(LOCAL_IS_HOST_MODULE),COMMON)/classes.jack)
-
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_STATIC_JACK_LIBRARIES := $(full_static_jack_libs)
-
-full_shared_jack_libs := $(call jack-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
-full_jack_deps := $(full_shared_jack_libs)
-
-ifndef LOCAL_IS_HOST_MODULE
-# Turn off .toc optimization for apps build as we cannot build dexdump.
-ifeq (,$(TARGET_BUILD_APPS))
-full_jack_deps := $(patsubst %.jack, %.dex.toc, $(full_jack_deps))
-endif
-endif # !LOCAL_IS_HOST_MODULE
-full_shared_jack_libs += $(LOCAL_JACK_CLASSPATH)
-full_jack_deps += $(full_static_jack_libs) $(LOCAL_JACK_CLASSPATH)
-
-ifndef LOCAL_IS_HOST_MODULE
-# This is set by packages that are linking to other packages that export
-# shared libraries, allowing them to make use of the code in the linked apk.
-ifneq ($(apk_libraries),)
-  link_apk_jack_libraries := \
-      $(foreach lib,$(apk_libraries), \
-        $(call intermediates-dir-for, \
-              APPS,$(lib),,COMMON)/classes.jack)
-
-  # link against the jar with full original names (before proguard processing).
-  full_shared_jack_libs += $(link_apk_jack_libraries)
-  full_jack_deps += $(link_apk_jack_libraries)
-endif
-
-# This is set by packages that contain instrumentation, allowing them to
-# link against the package they are instrumenting.  Currently only one such
-# package is allowed.
-ifdef LOCAL_INSTRUMENTATION_FOR
-   # link against the jar with full original names (before proguard processing).
-   link_instr_classes_jack := $(link_instr_intermediates_dir.COMMON)/classes.noshrob.jack
-   full_shared_jack_libs += $(link_instr_classes_jack)
-   full_jack_deps += $(link_instr_classes_jack)
-endif  # LOCAL_INSTRUMENTATION_FOR
-endif  # !LOCAL_IS_HOST_MODULE
-
-# Propagate local configuration options to this target.
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JACK_SHARED_LIBRARIES:= $(full_shared_jack_libs)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
-
-endif  # need_compile_java
-endif # LOCAL_JACK_ENABLED
-
-
-###########################################################
 # Verify that all libraries are safe to use
 ###########################################################
 ifndef LOCAL_IS_HOST_MODULE
@@ -400,6 +418,10 @@
 my_link_type := java:system
 my_warn_types := java:platform
 my_allowed_types := java:sdk java:system
+else ifneq (,$(call has-system-sdk-version,$(LOCAL_SDK_VERSION)))
+my_link_type := java:system
+my_warn_types := java:platform
+my_allowed_types := java:sdk java:system
 else ifneq ($(LOCAL_SDK_VERSION),)
 my_link_type := java:sdk
 my_warn_types := java:system java:platform
@@ -410,10 +432,31 @@
 my_allowed_types := java:sdk java:system java:platform
 endif
 
-my_link_deps := $(addprefix JAVA_LIBRARIES:,$(LOCAL_STATIC_JAVA_LIBRARIES))
+ifdef LOCAL_AAPT2_ONLY
+my_link_type += aapt2_only
+endif
+ifdef LOCAL_USE_AAPT2
+my_allowed_types += aapt2_only
+endif
+
+my_link_deps := $(addprefix JAVA_LIBRARIES:,$(LOCAL_STATIC_JAVA_LIBRARIES) $(LOCAL_JAVA_LIBRARIES))
 my_link_deps += $(addprefix APPS:,$(apk_libraries))
 
 my_2nd_arch_prefix := $(LOCAL_2ND_ARCH_VAR_PREFIX)
 my_common := COMMON
 include $(BUILD_SYSTEM)/link_type.mk
 endif  # !LOCAL_IS_HOST_MODULE
+
+ifneq ($(LOCAL_MODULE_MAKEFILE),$(SOONG_ANDROID_MK))
+
+SOONG_CONV.$(LOCAL_MODULE).PROBLEMS := \
+    $(SOONG_CONV.$(LOCAL_MODULE).PROBLEMS) $(my_soong_problems)
+SOONG_CONV.$(LOCAL_MODULE).DEPS := \
+    $(SOONG_CONV.$(LOCAL_MODULE).DEPS) \
+    $(LOCAL_STATIC_JAVA_LIBRARIES) \
+    $(LOCAL_JAVA_LIBRARIES) \
+    $(LOCAL_JNI_SHARED_LIBRARIES)
+SOONG_CONV.$(LOCAL_MODULE).TYPE := java
+SOONG_CONV := $(SOONG_CONV) $(LOCAL_MODULE)
+
+endif
diff --git a/core/java_library.mk b/core/java_library.mk
index 84f4419..e4916b8 100644
--- a/core/java_library.mk
+++ b/core/java_library.mk
@@ -25,21 +25,8 @@
 
 LOCAL_BUILT_MODULE_STEM := javalib.jar
 
-#################################
-include $(BUILD_SYSTEM)/configure_local_jack.mk
-#################################
-
-ifdef LOCAL_JACK_ENABLED
-ifdef LOCAL_IS_STATIC_JAVA_LIBRARY
-LOCAL_BUILT_MODULE_STEM := classes.jack
-endif
-endif
-
-# For non-static java libraries, other modules should depend on
-# out/target/common/obj/JAVA_LIBRARIES/.../javalib.jar (for jack)
-# or out/target/common/obj/JAVA_LIBRARIES/.../classes.jar (for javac).
-# For static java libraries, other modules should depend on
-# out/target/common/obj/JAVA_LIBRARIES/.../classes.jar
+# For java libraries, other modules should depend on
+# out/target/common/obj/JAVA_LIBRARIES/.../classes.jar.
 # There are some dependencies outside the build system that assume static
 # java libraries produce javalib.jar, so we will copy classes.jar there too.
 intermediates.COMMON := $(call local-intermediates-dir,COMMON)
@@ -68,31 +55,27 @@
 ifeq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true)
 # There are some dependencies outside the build system that assume classes.jar
 # is available as javalib.jar so copy it there too.
-$(eval $(call copy-one-file,$(full_classes_jar),$(common_javalib.jar)))
+$(eval $(call copy-one-file,$(full_classes_proguard_jar),$(common_javalib.jar)))
 
-ifdef LOCAL_JACK_ENABLED
-$(eval $(call copy-one-file,$(full_classes_jack),$(LOCAL_BUILT_MODULE)))
-else
-$(eval $(call copy-one-file,$(full_classes_jar),$(LOCAL_BUILT_MODULE)))
-endif
+$(eval $(call copy-one-file,$(full_classes_proguard_jar),$(LOCAL_BUILT_MODULE)))
 
 else # !LOCAL_IS_STATIC_JAVA_LIBRARY
 
 $(common_javalib.jar): PRIVATE_DEX_FILE := $(built_dex)
 $(common_javalib.jar): PRIVATE_SOURCE_ARCHIVE := $(full_classes_pre_proguard_jar)
 $(common_javalib.jar): PRIVATE_DONT_DELETE_JAR_DIRS := $(LOCAL_DONT_DELETE_JAR_DIRS)
-$(common_javalib.jar) : $(built_dex) $(java_resource_sources) | $(ZIPTIME)
+$(common_javalib.jar) : $(built_dex) $(java_resource_sources) | $(ZIPTIME) $(ZIPALIGN)
 	@echo "target Jar: $(PRIVATE_MODULE) ($@)"
-ifdef LOCAL_JACK_ENABLED
-	$(create-empty-package)
-else
-	$(call initialize-package-file,$(PRIVATE_SOURCE_ARCHIVE),$@)
-endif
-	$(add-dex-to-package)
-ifdef LOCAL_JACK_ENABLED
-	$(add-carried-jack-resources)
-endif
-	$(remove-timestamps-from-package)
+	$(call initialize-package-file,$(PRIVATE_SOURCE_ARCHIVE),$@.tmp)
+	$(call add-dex-to-package-arg,$@.tmp)
+	$(hide) $(ZIPTIME) $@.tmp
+	$(call commit-change-for-toc,$@)
+ifneq (,$(filter $(PRODUCT_LOADED_BY_PRIVILEGED_MODULES), $(LOCAL_MODULE)))
+	$(uncompress-dexs)
+	$(align-package)
+endif  # PRODUCT_LOADED_BY_PRIVILEGED_MODULES
+
+.KATI_RESTAT: $(common_javalib.jar)
 
 ifdef LOCAL_DEX_PREOPT
 ifneq ($(dexpreopt_boot_jar_module),) # boot jar
@@ -108,10 +91,7 @@
 	@echo "Dexpreopt Jar: $(PRIVATE_MODULE) ($@)"
 	$(call dexpreopt-one-file,$<,$@)
 
-$(eval $(call copy-one-file,$(common_javalib.jar),$(LOCAL_BUILT_MODULE)))
-ifneq (nostripping,$(LOCAL_DEX_PREOPT))
-	$(call dexpreopt-remove-classes.dex,$@)
-endif
+$(eval $(call dexpreopt-copy-jar,$(common_javalib.jar),$(LOCAL_BUILT_MODULE),$(LOCAL_DEX_PREOPT)))
 
 endif # ! boot jar
 
diff --git a/core/local_vndk.mk b/core/local_vndk.mk
index 640aac7..3677d40 100644
--- a/core/local_vndk.mk
+++ b/core/local_vndk.mk
@@ -5,10 +5,8 @@
 ifndef LOCAL_SDK_VERSION
   ifneq (,$(filter true,$(LOCAL_VENDOR_MODULE) $(LOCAL_ODM_MODULE) $(LOCAL_OEM_MODULE) $(LOCAL_PROPRIETARY_MODULE)))
     LOCAL_USE_VNDK:=true
-  else
-    ifneq (,$(filter $(TARGET_OUT_VENDOR)%,$(LOCAL_MODULE_PATH) $(LOCAL_MODULE_PATH_32) $(LOCAL_MODULE_PATH_64)))
-      LOCAL_USE_VNDK:=true
-    endif
+    # Note: no need to check LOCAL_MODULE_PATH* since LOCAL_[VENDOR|ODM|OEM]_MODULE is already
+    # set correctly before this is included.
   endif
 endif
 endif
diff --git a/core/local_vsdk.mk b/core/local_vsdk.mk
new file mode 100644
index 0000000..f798d47
--- /dev/null
+++ b/core/local_vsdk.mk
@@ -0,0 +1,19 @@
+
+ifdef BOARD_VSDK_VERSION
+# Set LOCAL_SDK_VERSION to system_current, If LOCAL_SDK_VERSION is not defined and LOCAL_VENDOR_MODULE is true
+  _is_vendor_app :=
+  ifneq (,$(filter true,$(LOCAL_VENDOR_MODULE) $(LOCAL_ODM_MODULE) $(LOCAL_OEM_MODULE) $(LOCAL_PROPRIETARY_MODULE)))
+    _is_vendor_app := true
+  else
+    ifneq (,$(filter $(TARGET_OUT_VENDOR)%,$(LOCAL_MODULE_PATH) $(LOCAL_MODULE_PATH_32) $(LOCAL_MODULE_PATH_64)))
+      _is_vendor_app := true
+    endif
+  endif
+  ifneq (,$(filter JAVA_LIBRARIES APPS,$(LOCAL_MODULE_CLASS)))
+    ifndef LOCAL_SDK_VERSION
+      ifeq ($(_is_vendor_app),true)
+        LOCAL_SDK_VERSION := system_current
+      endif
+    endif
+  endif
+endif
diff --git a/core/main.mk b/core/main.mk
index 1ea2160..90c2058 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -34,7 +34,7 @@
 TOP := .
 TOPDIR :=
 
-BUILD_SYSTEM := $(TOPDIR)build/core
+BUILD_SYSTEM := $(TOPDIR)build/make/core
 
 # This is the default target.  It must be the first declared target.
 .PHONY: droid
@@ -62,14 +62,16 @@
 $(shell mkdir -p $(OUT_DIR) && \
     echo -n $(BUILD_NUMBER) > $(OUT_DIR)/build_number.txt && \
     echo -n $(BUILD_DATETIME) > $(OUT_DIR)/build_date.txt)
-BUILD_NUMBER_FROM_FILE := $$(cat $(OUT_DIR)/build_number.txt)
-BUILD_DATETIME_FROM_FILE := $$(cat $(OUT_DIR)/build_date.txt)
 ifeq ($(HOST_OS),darwin)
 DATE_FROM_FILE := date -r $(BUILD_DATETIME_FROM_FILE)
 else
 DATE_FROM_FILE := date -d @$(BUILD_DATETIME_FROM_FILE)
 endif
 
+# Make an empty directory, which can be used to make empty jars
+EMPTY_DIRECTORY := $(OUT_DIR)/empty
+$(shell mkdir -p $(EMPTY_DIRECTORY) && rm -rf $(EMPTY_DIRECTORY)/*)
+
 # CTS-specific config.
 -include cts/build/config.mk
 # VTS-specific config.
@@ -81,11 +83,13 @@
 # STS-specific config.
 -include test/sts/tools/sts-tradefed/build/config.mk
 
-# This allows us to force a clean build - included after the config.mk
-# environment setup is done, but before we generate any dependencies.  This
-# file does the rm -rf inline so the deps which are all done below will
-# be generated correctly
-include $(BUILD_SYSTEM)/cleanbuild.mk
+# Clean rules
+.PHONY: clean-dex-files
+clean-dex-files:
+	$(hide) find $(OUT_DIR) -name "*.dex" | xargs rm -f
+	$(hide) for i in `find $(OUT_DIR) -name "*.jar" -o -name "*.apk"` ; do ((unzip -l $$i 2> /dev/null | \
+				grep -q "\.dex$$" && rm -f $$i) || continue ) ; done
+	@echo "All dex files and archives containing dex files have been removed."
 
 # Include the google-specific config
 -include vendor/google/build/config.mk
@@ -95,11 +99,21 @@
 # (must be defined before including definitions.make)
 INTERNAL_MODIFIER_TARGETS := all
 
-# EMMA_INSTRUMENT_STATIC merges the static emma library to each emma-enabled module.
+# EMMA_INSTRUMENT_STATIC merges the static jacoco library to each
+# jacoco-enabled module.
 ifeq (true,$(EMMA_INSTRUMENT_STATIC))
 EMMA_INSTRUMENT := true
 endif
 
+ifeq (true,$(EMMA_INSTRUMENT))
+# Adding the jacoco library can cause the inclusion of
+# some typically banned classes
+# So if the user didn't specify SKIP_BOOT_JARS_CHECK, enable it here
+ifndef SKIP_BOOT_JARS_CHECK
+SKIP_BOOT_JARS_CHECK := true
+endif
+endif
+
 #
 # -----------------------------------------------------------------
 # Validate ADDITIONAL_DEFAULT_PROPERTIES.
@@ -169,23 +183,7 @@
 
 # -----------------------------------------------------------------
 # The pdk (Platform Development Kit) build
-include build/core/pdk_config.mk
-
-#
-# -----------------------------------------------------------------
-# Jack version configuration
--include $(TOPDIR)prebuilts/sdk/tools/jack_versions.mk
--include $(TOPDIR)prebuilts/sdk/tools/jack_for_module.mk
-
-#
-# -----------------------------------------------------------------
-# Install and start Jack server
--include $(TOPDIR)prebuilts/sdk/tools/jack_server_setup.mk
-
-#
-# -----------------------------------------------------------------
-# Jacoco package name for Jack
--include $(TOPDIR)external/jacoco/config.mk
+include build/make/core/pdk_config.mk
 
 #
 # -----------------------------------------------------------------
@@ -418,7 +416,7 @@
 # Include all of the makefiles in the system
 #
 
-subdir_makefiles := $(SOONG_ANDROID_MK) $(call first-makefiles-under,$(TOP))
+subdir_makefiles := $(SOONG_ANDROID_MK) $(file <$(OUT_DIR)/.module_paths/Android.mk.list)
 subdir_makefiles_total := $(words $(subdir_makefiles))
 .KATI_READONLY := subdir_makefiles_total
 
@@ -500,9 +498,10 @@
 # If a module is for a cross host os, the required modules must be for
 # that OS too.
 # If a module is built for 32-bit, the required modules must be 32-bit too;
-# Otherwise if the module is an exectuable or shared library,
+# Otherwise if the module is an executable or shared library,
 #   the required modules must be 64-bit;
 #   otherwise we require both 64-bit and 32-bit variant, if one exists.
+define select-bitness-of-required-modules
 $(foreach m,$(ALL_MODULES),\
   $(eval r := $(ALL_MODULES.$(m).REQUIRED))\
   $(if $(r),\
@@ -518,6 +517,8 @@
      $(eval ALL_MODULES.$(m).REQUIRED := $(strip $(r_r)))\
   )\
 )
+endef
+$(call select-bitness-of-required-modules)
 r_r :=
 
 define add-required-deps
@@ -532,24 +533,68 @@
 $(1): $(2)
 endef
 
+# Sets up dependencies such that whenever a host module is installed,
+# any other host modules listed in $(ALL_MODULES.$(m).REQUIRED) will also be installed
+define add-all-host-to-host-required-modules-deps
+$(foreach m,$(ALL_MODULES), \
+  $(eval r := $(ALL_MODULES.$(m).REQUIRED)) \
+  $(if $(r), \
+    $(eval r := $(call module-installed-files,$(r))) \
+    $(eval h_m := $(filter $(HOST_OUT)/%, $(ALL_MODULES.$(m).INSTALLED))) \
+    $(eval hc_m := $(filter $(HOST_CROSS_OUT)/%, $(ALL_MODULES.$(m).INSTALLED))) \
+    $(eval h_r := $(filter $(HOST_OUT)/%, $(r))) \
+    $(eval hc_r := $(filter $(HOST_CROSS_OUT)/%, $(r))) \
+    $(eval h_m := $(filter-out $(h_r), $(h_m))) \
+    $(eval hc_m := $(filter-out $(hc_r), $(hc_m))) \
+    $(if $(h_m), $(eval $(call add-required-deps, $(h_m),$(h_r)))) \
+    $(if $(hc_m), $(eval $(call add-required-deps, $(hc_m),$(hc_r)))) \
+  ) \
+)
+endef
+$(call add-all-host-to-host-required-modules-deps)
+
+# Sets up dependencies such that whenever a target module is installed,
+# any other target modules listed in $(ALL_MODULES.$(m).REQUIRED) will also be installed
+define add-all-target-to-target-required-modules-deps
 $(foreach m,$(ALL_MODULES), \
   $(eval r := $(ALL_MODULES.$(m).REQUIRED)) \
   $(if $(r), \
     $(eval r := $(call module-installed-files,$(r))) \
     $(eval t_m := $(filter $(TARGET_OUT_ROOT)/%, $(ALL_MODULES.$(m).INSTALLED))) \
-    $(eval h_m := $(filter $(HOST_OUT)/%, $(ALL_MODULES.$(m).INSTALLED))) \
-    $(eval hc_m := $(filter $(HOST_CROSS_OUT)/%, $(ALL_MODULES.$(m).INSTALLED))) \
     $(eval t_r := $(filter $(TARGET_OUT_ROOT)/%, $(r))) \
-    $(eval h_r := $(filter $(HOST_OUT)/%, $(r))) \
-    $(eval hc_r := $(filter $(HOST_CROSS_OUT)/%, $(r))) \
     $(eval t_m := $(filter-out $(t_r), $(t_m))) \
-    $(eval h_m := $(filter-out $(h_r), $(h_m))) \
-    $(eval hc_m := $(filter-out $(hc_r), $(hc_m))) \
     $(if $(t_m), $(eval $(call add-required-deps, $(t_m),$(t_r)))) \
-    $(if $(h_m), $(eval $(call add-required-deps, $(h_m),$(h_r)))) \
-    $(if $(hc_m), $(eval $(call add-required-deps, $(hc_m),$(hc_r)))) \
-   ) \
- )
+  ) \
+)
+endef
+$(call add-all-target-to-target-required-modules-deps)
+
+# Sets up dependencies such that whenever a host module is installed,
+# any target modules listed in $(ALL_MODULES.$(m).TARGET_REQUIRED) will also be installed
+define add-all-host-to-target-required-modules-deps
+$(foreach m,$(ALL_MODULES), \
+  $(eval req_mods := $(ALL_MODULES.$(m).TARGET_REQUIRED))\
+  $(if $(req_mods), \
+    $(eval req_files := )\
+    $(foreach req_mod,$(req_mods), \
+      $(eval req_file := $(filter $(TARGET_OUT_ROOT)/%, $(call module-installed-files,$(req_mod)))) \
+      $(if $(strip $(req_file)),\
+        ,\
+        $(error $(m).LOCAL_TARGET_REQUIRED_MODULES : illegal value $(req_mod) : not a device module. If you want to specify host modules to be required to be installed along with your host module, add those module names to LOCAL_REQUIRED_MODULES instead)\
+      )\
+      $(eval req_files := $(req_files)$(space)$(req_file))\
+    )\
+    $(eval req_files := $(strip $(req_files)))\
+    $(eval mod_files := $(filter $(HOST_OUT)/%, $(call module-installed-files,$(m)))) \
+    $(eval mod_files := $(filter-out $(req_files),$(mod_files)))\
+    $(if $(mod_files),\
+      $(eval $(call add-required-deps, $(mod_files),$(req_files))) \
+    )\
+  )\
+)
+endef
+$(call add-all-host-to-target-required-modules-deps)
+
 
 t_m :=
 h_m :=
@@ -558,7 +603,7 @@
 h_r :=
 hc_r :=
 
-# Establish the dependecies on the shared libraries.
+# Establish the dependencies on the shared libraries.
 # It also adds the shared library module names to ALL_MODULES.$(m).REQUIRED,
 # so they can be expanded to product_MODULES later.
 # $(1): TARGET_ or HOST_ or HOST_CROSS_.
@@ -905,7 +950,7 @@
       $(warning $(ALL_MODULES.$(m).MAKEFILE): Module '$(m)' in PRODUCT_PACKAGES_TESTS has nothing to install!)))
 endif
 
-# build/core/Makefile contains extra stuff that we don't want to pollute this
+# build/make/core/Makefile contains extra stuff that we don't want to pollute this
 # top-level makefile with.  It expects that ALL_DEFAULT_INSTALLED_MODULES
 # contains everything that's built during the current make, but it also further
 # extends ALL_DEFAULT_INSTALLED_MODULES.
@@ -1037,13 +1082,8 @@
   $(call dist-for-goals,apps_only, $(apps_only_dist_built_files))
 
   ifeq ($(EMMA_INSTRUMENT),true)
-    ifeq ($(ANDROID_COMPILE_WITH_JACK),false)
-      $(JACOCO_REPORT_CLASSES_ALL) : $(apps_only_installed_files)
-      $(call dist-for-goals,apps_only, $(JACOCO_REPORT_CLASSES_ALL))
-    else
-      $(EMMA_META_ZIP) : $(apps_only_installed_files)
-      $(call dist-for-goals,apps_only, $(EMMA_META_ZIP))
-    endif
+    $(JACOCO_REPORT_CLASSES_ALL) : $(apps_only_installed_files)
+    $(call dist-for-goals,apps_only, $(JACOCO_REPORT_CLASSES_ALL))
   endif
 
   $(PROGUARD_DICT_ZIP) : $(apps_only_installed_files)
@@ -1100,13 +1140,8 @@
   endif
 
   ifeq ($(EMMA_INSTRUMENT),true)
-    ifeq ($(ANDROID_COMPILE_WITH_JACK),false)
-      $(JACOCO_REPORT_CLASSES_ALL) : $(INSTALLED_SYSTEMIMAGE)
-      $(call dist-for-goals, dist_files, $(JACOCO_REPORT_CLASSES_ALL))
-    else
-      $(EMMA_META_ZIP) : $(INSTALLED_SYSTEMIMAGE)
-      $(call dist-for-goals, dist_files, $(EMMA_META_ZIP))
-    endif
+    $(JACOCO_REPORT_CLASSES_ALL) : $(INSTALLED_SYSTEMIMAGE)
+    $(call dist-for-goals, dist_files, $(JACOCO_REPORT_CLASSES_ALL))
   endif
 
 # Building a full system-- the default is to build droidcore
@@ -1142,7 +1177,7 @@
 target-native-tests : native-target-tests
 tests : host-tests target-tests
 
-# Phony target to run all java compilations that use javac instead of jack.
+# Phony target to run all java compilations that use javac
 .PHONY: javac-check
 
 ifneq (,$(filter samplecode, $(MAKECMDGOALS)))
diff --git a/core/math.mk b/core/math.mk
index 047d046..44e03ce 100644
--- a/core/math.mk
+++ b/core/math.mk
@@ -42,6 +42,11 @@
 $(if $(call math_is_number,$(1)),,$(error Only positive integers <= 100 are supported (not $(1))))
 endef
 
+# return a list containing integers ranging from [$(1),$(2)]
+define int_range_list
+$(call _math_check_valid,$(1))$(call _math_check_valid,$(2))$(wordlist $(1),$(2),$(__MATH_NUMBERS))
+endef
+
 #$(call _math_check_valid,0)
 #$(call _math_check_valid,1)
 #$(call _math_check_valid,100)
@@ -67,6 +72,10 @@
 $(if $(filter $(1),$(call math_max,$(1),$(2))),true)
 endef
 
+define math_lt
+$(if $(call math_gt_or_eq,$(1),$(2)),,true)
+endef
+
 #$(warning $(call math_gt_or_eq, 2, 1))
 #$(warning $(call math_gt_or_eq, 1, 1))
 #$(warning $(if $(call math_gt_or_eq, 1, 2),false,true))
@@ -75,3 +84,69 @@
 define inc_and_print
 $(strip $(eval $(1) := $($(1)) .)$(words $($(1))))
 endef
+
+# Returns the words in $2 that are numbers and are less than $1
+define numbers_less_than
+$(strip \
+  $(foreach n,$2, \
+    $(if $(call math_is_number,$(n)), \
+      $(if $(call math_lt,$(n),$(1)), \
+        $(n)))))
+endef
+
+_INT_LIMIT_WORDS := $(foreach a,x x,$(foreach b,x x x x x x x x x x x x x x x x,\
+  $(foreach c,x x x x x x x x x x x x x x x x,x x x x x x x x x x x x x x x x)))
+
+define _int_encode
+$(if $(filter $(words x $(_INT_LIMIT_WORDS)),$(words $(wordlist 1,$(1),x $(_INT_LIMIT_WORDS)))),\
+  $(call pretty-error,integer greater than $(words $(_INT_LIMIT_WORDS)) is not supported!),\
+    $(wordlist 1,$(1),$(_INT_LIMIT_WORDS)))
+endef
+
+# _int_max returns the maximum of the two arguments
+# input: two (x) lists; output: one (x) list
+# integer cannot be passed in directly. It has to be converted using _int_encode.
+define _int_max
+$(subst xx,x,$(join $(1),$(2)))
+endef
+
+# first argument is greater than second argument
+# output: non-empty if true
+# integer cannot be passed in directly. It has to be converted using _int_encode.
+define _int_greater-than
+$(filter-out $(words $(2)),$(words $(call _int_max,$(1),$(2))))
+endef
+
+# first argument equals to second argument
+# output: non-empty if true
+# integer cannot be passed in directly. It has to be converted using _int_encode.
+define _int_equal
+$(filter $(words $(1)),$(words $(2)))
+endef
+
+# first argument is greater than or equal to second argument
+# output: non-empty if true
+# integer cannot be passed in directly. It has to be converted using _int_encode.
+define _int_greater-or-equal
+$(call _int_greater-than,$(1),$(2))$(call _int_equal,$(1),$(2))
+endef
+
+define int_plus
+$(words $(call _int_encode,$(1)) $(call _int_encode,$(2)))
+endef
+
+define int_subtract
+$(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))),\
+  $(words $(filter-out xx,$(join $(call _int_encode,$(1)),$(call _int_encode,$(2))))),\
+    $(call pretty-error,$(1) subtract underflow $(2)))
+endef
+
+define int_multiply
+$(words $(foreach a,$(call _int_encode,$(1)),$(call _int_encode,$(2))))
+endef
+
+define int_divide
+$(if $(filter 0,$(2)),$(call pretty-error,division by zero is not allowed!),$(strip \
+  $(if $(call _int_greater-or-equal,$(call _int_encode,$(1)),$(call _int_encode,$(2))), \
+    $(call int_plus,$(call int_divide,$(call int_subtract,$(1),$(2)),$(2)),1),0)))
+endef
diff --git a/core/no_java_path/jar b/core/no_java_path/jar
deleted file mode 120000
index 8586397..0000000
--- a/core/no_java_path/jar
+++ /dev/null
@@ -1 +0,0 @@
-java
\ No newline at end of file
diff --git a/core/no_java_path/jarsigner b/core/no_java_path/jarsigner
deleted file mode 120000
index 8586397..0000000
--- a/core/no_java_path/jarsigner
+++ /dev/null
@@ -1 +0,0 @@
-java
\ No newline at end of file
diff --git a/core/no_java_path/java b/core/no_java_path/java
deleted file mode 100755
index f3422f3..0000000
--- a/core/no_java_path/java
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/bin/bash
-
-echo "Error: JAVA_NOT_REQUIRED=true, $(basename $0) is unavailable." 1>&2
-exit 1
diff --git a/core/no_java_path/javac b/core/no_java_path/javac
deleted file mode 120000
index 8586397..0000000
--- a/core/no_java_path/javac
+++ /dev/null
@@ -1 +0,0 @@
-java
\ No newline at end of file
diff --git a/core/no_java_path/keytool b/core/no_java_path/keytool
deleted file mode 120000
index 8586397..0000000
--- a/core/no_java_path/keytool
+++ /dev/null
@@ -1 +0,0 @@
-java
\ No newline at end of file
diff --git a/core/notice_files.mk b/core/notice_files.mk
index f850fff..383d73c 100644
--- a/core/notice_files.mk
+++ b/core/notice_files.mk
@@ -57,7 +57,7 @@
       # javalib.jar is the default name for the build module (and isn't meaningful)
       # If that's what we have, substitute the module name instead.  These files
       # aren't included on the device, so this name is synthetic anyway.
-      ifneq ($(filter javalib.jar classes.jack,$(module_leaf)),)
+      ifneq ($(filter javalib.jar,$(module_leaf)),)
         module_leaf := $(LOCAL_MODULE).jar
       endif
       module_installed_filename := \
diff --git a/core/package_internal.mk b/core/package_internal.mk
index 5c4ad28..2a63817 100644
--- a/core/package_internal.mk
+++ b/core/package_internal.mk
@@ -60,10 +60,6 @@
 intermediates := $(call local-intermediates-dir)
 intermediates.COMMON := $(call local-intermediates-dir,COMMON)
 
-#################################
-include $(BUILD_SYSTEM)/configure_local_jack.mk
-#################################
-
 # Package LOCAL_MODULE_TAGS default to optional
 LOCAL_MODULE_TAGS := $(strip $(LOCAL_MODULE_TAGS))
 ifeq ($(LOCAL_MODULE_TAGS),)
@@ -150,6 +146,10 @@
 need_compile_asset := true
 endif
 
+ifdef LOCAL_AAPT2_ONLY
+LOCAL_USE_AAPT2 := true
+endif
+
 my_res_package :=
 ifdef LOCAL_USE_AAPT2
 # In aapt2 the last takes precedence.
@@ -259,13 +259,6 @@
 endif # !custom
 LOCAL_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_PROGUARD_FLAGS)
 
-ifdef LOCAL_JACK_ENABLED
-ifndef LOCAL_JACK_PROGUARD_FLAGS
-    LOCAL_JACK_PROGUARD_FLAGS := $(LOCAL_PROGUARD_FLAGS)
-endif
-LOCAL_JACK_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_JACK_PROGUARD_FLAGS)
-endif # LOCAL_JACK_ENABLED
-
 ifeq (true,$(EMMA_INSTRUMENT))
 ifndef LOCAL_EMMA_INSTRUMENT
 # No jacoco for test apks.
@@ -292,20 +285,8 @@
 LOCAL_STATIC_JAVA_LIBRARIES += jacocoagent
 # Exclude jacoco classes from proguard
 LOCAL_PROGUARD_FLAGS += -include $(BUILD_SYSTEM)/proguard.jacoco.flags
-LOCAL_JACK_PROGUARD_FLAGS += -include $(BUILD_SYSTEM)/proguard.jacoco.flags
 endif # Contains java code
-else  # ! TARGET_BUILD_APPS
-ifdef LOCAL_JACK_ENABLED
-# If build against the SDK in full build, core.jar is not used
-# so coverage classes are not present.
-# Jack needs jacoco on the classpath but we do not want it to be in
-# the final apk. While it is a static library, we add it to the
-# LOCAL_JAVA_LIBRARIES which are only present on the classpath.
-# Note: we have nothing to do for proguard since jacoco will be
-# on the classpath only, thus not modified during the compilation.
-LOCAL_JAVA_LIBRARIES += jacocoagent
-endif # ! LOCAL_JACK_ENABLED
-endif # ! TARGET_BUILD_APPS
+endif # TARGET_BUILD_APPS
 endif # LOCAL_SDK_VERSION
 endif # EMMA_INSTRUMENT_STATIC
 endif # LOCAL_EMMA_INSTRUMENT
@@ -316,7 +297,6 @@
 data_binding_intermediates := $(intermediates.COMMON)/data-binding
 
 LOCAL_JAVACFLAGS += -processorpath $(DATA_BINDING_COMPILER) -s $(data_binding_intermediates)/anno-src
-LOCAL_JACK_FLAGS += --processorpath $(DATA_BINDING_COMPILER)
 
 LOCAL_STATIC_JAVA_LIBRARIES += databinding-baselibrary
 LOCAL_STATIC_JAVA_AAR_LIBRARIES += databinding-library databinding-adapters
@@ -375,9 +355,10 @@
 	$(hide) touch $@
 
 # Make sure the data-binding process happens before javac and generation of R.java.
-$(R_file_stamp) $(full_classes_compiled_jar) : $(data_binding_stamp)
-# The dependency path when jack is enabled
-$(built_dex_intermediate) : $(data_binding_stamp)
+$(R_file_stamp): $(data_binding_stamp)
+$(java_source_list_file): $(data_binding_stamp)
+$(foreach x,$(sharded_java_source_list_files),$(eval $(x): $(data_binding_stamp)))
+$(full_classes_compiled_jar): $(data_binding_stamp)
 endif  # LOCAL_DATA_BINDING
 
 ifeq ($(need_compile_res),true)
@@ -400,7 +381,7 @@
 endif
 
 ifdef LOCAL_USE_AAPT2
-my_compiled_res_base_dir := $(intermediates)/flat-res
+my_compiled_res_base_dir := $(intermediates.COMMON)/flat-res
 renderscript_target_api :=
 ifneq (,$(LOCAL_RENDERSCRIPT_TARGET_API))
 renderscript_target_api := $(LOCAL_RENDERSCRIPT_TARGET_API)
@@ -408,7 +389,7 @@
 ifneq (,$(LOCAL_SDK_VERSION))
 # Set target-api for LOCAL_SDK_VERSIONs other than current.
 ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
-renderscript_target_api := $(LOCAL_SDK_VERSION)
+renderscript_target_api := $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION))
 endif
 endif  # LOCAL_SDK_VERSION is set
 endif  # LOCAL_RENDERSCRIPT_TARGET_API is set
@@ -437,11 +418,12 @@
 $(R_file_stamp): PRIVATE_RESOURCE_PUBLICS_OUTPUT := \
 			$(intermediates.COMMON)/public_resources.xml
 $(R_file_stamp): PRIVATE_PROGUARD_OPTIONS_FILE := $(proguard_options_file)
+$(R_file_stamp): PRIVATE_RESOURCE_LIST := $(all_res_assets)
 $(R_file_stamp): $(all_res_assets) $(full_android_manifest) $(RenderScript_file_stamp) $(AAPT) | $(ACP)
 	@echo "target R.java/Manifest.java: $(PRIVATE_MODULE) ($@)"
 	@rm -rf $@ && mkdir -p $(dir $@)
 	$(create-resource-java-files)
-	$(call find-generated-R.java)
+	$(call find-generated-R.java,$@)
 
 $(proguard_options_file): $(R_file_stamp)
 
@@ -457,6 +439,7 @@
 # can't know anything about PRODUCT.  Clear it out just for this target.
 $(resource_export_package): PRIVATE_PRODUCT_AAPT_CONFIG :=
 $(resource_export_package): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
+$(resource_export_package): PRIVATE_RESOURCE_LIST := $(all_res_assets)
 $(resource_export_package): $(all_res_assets) $(full_android_manifest) $(RenderScript_file_stamp) $(AAPT)
 	@echo "target Export Resources: $(PRIVATE_MODULE) ($@)"
 	$(create-empty-package)
@@ -469,26 +452,10 @@
 # they want to use this module's R.java file.
 $(LOCAL_BUILT_MODULE): $(R_file_stamp)
 
-ifdef LOCAL_JACK_ENABLED
-ifneq ($(built_dex_intermediate),)
-$(built_dex_intermediate): $(R_file_stamp)
-endif
-ifneq ($(noshrob_classes_jack),)
-$(noshrob_classes_jack): $(R_file_stamp)
-endif
-ifneq ($(full_classes_jack),)
-$(full_classes_jack): $(R_file_stamp)
-$(jack_check_timestamp): $(R_file_stamp)
-endif
-endif # LOCAL_JACK_ENABLED
-
-ifneq ($(full_classes_jar),)
-# If full_classes_jar is non-empty, we're building sources.
-# If we're building sources, the initial javac step (which
-# produces full_classes_compiled_jar) needs to ensure the
-# R.java and Manifest.java files have been generated first.
-$(full_classes_compiled_jar): $(R_file_stamp)
-endif
+# The R.java file must exist by the time the java source
+# list is generated
+$(java_source_list_file): $(R_file_stamp)
+$(foreach x,$(sharded_java_source_list_files),$(eval $(x): $(R_file_stamp)))
 
 endif  # need_compile_res
 
@@ -598,13 +565,18 @@
     $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG := $(PRODUCT_AAPT_PREF_CONFIG)
 endif
 endif
+
 $(LOCAL_BUILT_MODULE): PRIVATE_DONT_DELETE_JAR_DIRS := $(LOCAL_DONT_DELETE_JAR_DIRS)
+$(LOCAL_BUILT_MODULE): PRIVATE_RESOURCE_INTERMEDIATES_DIR := $(intermediates.COMMON)/resources
+$(LOCAL_BUILT_MODULE): PRIVATE_FULL_CLASSES_JAR := $(full_classes_jar)
 $(LOCAL_BUILT_MODULE) : $(jni_shared_libraries)
+$(LOCAL_BUILT_MODULE) : $(JAR_ARGS)
 ifdef LOCAL_USE_AAPT2
 $(LOCAL_BUILT_MODULE): PRIVATE_RES_PACKAGE := $(my_res_package)
 $(LOCAL_BUILT_MODULE) : $(my_res_package) $(AAPT2) | $(ACP)
 else
-$(LOCAL_BUILT_MODULE) : $(all_res_assets) $(full_android_manifest) $(AAPT)
+$(LOCAL_BUILT_MODULE): PRIVATE_RESOURCE_LIST := $(all_res_assets)
+$(LOCAL_BUILT_MODULE) : $(all_res_assets) $(full_android_manifest) $(AAPT) $(ZIPALIGN)
 endif
 ifdef LOCAL_COMPRESSED_MODULE
 $(LOCAL_BUILT_MODULE) : $(MINIGZIP)
@@ -613,13 +585,9 @@
 ifdef LOCAL_USE_AAPT2
 	$(call copy-file-to-new-target)
 else  # ! LOCAL_USE_AAPT2
-ifdef LOCAL_JACK_ENABLED
-	$(create-empty-package)
-else
 	$(if $(PRIVATE_SOURCE_ARCHIVE),\
 	  $(call initialize-package-file,$(PRIVATE_SOURCE_ARCHIVE),$@),\
 	  $(create-empty-package))
-endif
 	$(add-assets-to-package)
 endif  # LOCAL_USE_AAPT2
 ifneq ($(jni_shared_libraries),)
@@ -630,19 +598,28 @@
 	$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
 else  # full_classes_jar
 	$(add-dex-to-package)
-endif  # full_classes_jar
-ifdef LOCAL_JACK_ENABLED
-	$(add-carried-jack-resources)
+ifdef LOCAL_USE_AAPT2
+	$(call add-jar-resources-to-package,$@,$(PRIVATE_FULL_CLASSES_JAR),$(PRIVATE_RESOURCE_INTERMEDIATES_DIR))
 endif
+endif  # full_classes_jar
 ifdef LOCAL_DEX_PREOPT
 ifneq ($(BUILD_PLATFORM_ZIP),)
 	@# Keep a copy of apk with classes.dex unstripped
 	$(hide) cp -f $@ $(dir $@)package.dex.apk
 endif  # BUILD_PLATFORM_ZIP
+ifneq (true,$(DONT_UNCOMPRESS_PRIV_APPS_DEXS))
+ifeq (true,$(LOCAL_PRIVILEGED_MODULE))
+	@# No need to align, sign-package below will do it.
+	$(uncompress-dexs)
+endif  # LOCAL_PRIVILEGED_MODULE
+endif  # DONT_UNCOMPRESS_PRIV_APPS_DEXS
 ifneq (nostripping,$(LOCAL_DEX_PREOPT))
 	$(call dexpreopt-remove-classes.dex,$@)
 endif
 endif
+ifneq (,$(filter $(PRODUCT_LOADED_BY_PRIVILEGED_MODULES), $(LOCAL_MODULE)))
+	$(uncompress-dexs)
+endif  # PRODUCT_LOADED_BY_PRIVILEGED_MODULES
 	$(sign-package)
 ifdef LOCAL_COMPRESSED_MODULE
 	$(compress-package)
diff --git a/core/pdk_config.mk b/core/pdk_config.mk
index e12be29..c2f8b0a 100644
--- a/core/pdk_config.mk
+++ b/core/pdk_config.mk
@@ -13,6 +13,9 @@
 # all paths under out dir
 PDK_PLATFORM_JAVA_ZIP_JAVA_TARGET_LIB_DIR += \
   target/common/obj/JAVA_LIBRARIES/android.test.runner_intermediates \
+  target/common/obj/JAVA_LIBRARIES/android.hidl.base-V1.0-java_intermediates \
+  target/common/obj/JAVA_LIBRARIES/android.hidl.base-V1.0-java-static_intermediates \
+  target/common/obj/JAVA_LIBRARIES/android.hidl.manager-V1.0-java_intermediates \
   target/common/obj/JAVA_LIBRARIES/android-common_intermediates \
   target/common/obj/JAVA_LIBRARIES/android-ex-camera2_intermediates \
   target/common/obj/JAVA_LIBRARIES/android_stubs_current_intermediates \
@@ -24,6 +27,7 @@
   target/common/obj/JAVA_LIBRARIES/legacy-android-test_intermediates \
   target/common/obj/JAVA_LIBRARIES/ext_intermediates \
   target/common/obj/JAVA_LIBRARIES/framework_intermediates \
+  target/common/obj/JAVA_LIBRARIES/hwbinder_intermediates \
   target/common/obj/JAVA_LIBRARIES/ims-common_intermediates \
   target/common/obj/JAVA_LIBRARIES/okhttp_intermediates \
   target/common/obj/JAVA_LIBRARIES/telephony-common_intermediates \
@@ -40,7 +44,7 @@
 	$(PDK_PLATFORM_JAVA_ZIP_JAVA_HOST_LIB_DIR)
 
 PDK_PLATFORM_JAVA_ZIP_CONTENTS += $(foreach lib_dir,$(PDK_PLATFORM_JAVA_ZIP_JAVA_LIB_DIR),\
-    $(lib_dir)/classes.jack $(lib_dir)/classes.jar $(lib_dir)/classes.jar.toc \
+    $(lib_dir)/classes.jar $(lib_dir)/classes.jar.toc \
     $(lib_dir)/javalib.jar  $(lib_dir)/classes*.dex \
     $(lib_dir)/classes.dex.toc )
 
@@ -103,9 +107,6 @@
 ifeq (true,$(TARGET_BUILD_PDK_JAVA_PLATFORM))
 
 PDK_FUSION_OUT_DIR := $(OUT_DIR)
-ifeq (debug,$(TARGET_BUILD_TYPE))
-PDK_FUSION_OUT_DIR := $(DEBUG_OUT_DIR)
-endif
 
 define JAVA_dependency_template
 $(call add-dependency,$(PDK_FUSION_OUT_DIR)/$(strip $(1)),\
@@ -122,14 +123,6 @@
 $(eval $(call JAVA_dependency_template,$(lib_dir)/javalib.jar,\
 $(lib_dir)/classes.jar)))
 
-# pull .jack and .dex files
-$(foreach lib_dir,$(PDK_PLATFORM_JAVA_ZIP_JAVA_TARGET_LIB_DIR),\
-  $(eval $(call JAVA_dependency_template,$(lib_dir)/classes.jar.toc,\
-    $(lib_dir)/classes.jar $(lib_dir)/classes.jack)))
-$(foreach lib_dir,$(PDK_PLATFORM_JAVA_ZIP_JAVA_TARGET_LIB_DIR),\
-  $(eval $(call JAVA_dependency_template,$(lib_dir)/classes.dex.toc,\
-    $(lib_dir)/classes.jar $(lib_dir)/classes.jack $(lib_dir)/classes%.dex)))
-
 # implicit rules for all other target files
 $(TARGET_COMMON_OUT_ROOT)/% : $(_pdk_fusion_intermediates)/target/common/% $(_pdk_fusion_stamp)
 	@mkdir -p $(dir $@)
diff --git a/core/prebuilt_internal.mk b/core/prebuilt_internal.mk
index 620c22c..69df2d1 100644
--- a/core/prebuilt_internal.mk
+++ b/core/prebuilt_internal.mk
@@ -176,7 +176,16 @@
 ifdef LOCAL_SDK_VERSION
 my_link_type := native:ndk
 else ifdef LOCAL_USE_VNDK
-my_link_type := native:vendor
+    _name := $(patsubst %.vendor,%,$(LOCAL_MODULE))
+    ifneq ($(filter $(_name),$(VNDK_CORE_LIBRARIES) $(VNDK_SAMEPROCESS_LIBRARIES) $(LLNDK_LIBRARIES)),)
+        ifeq ($(filter $(_name),$(VNDK_PRIVATE_LIBRARIES)),)
+            my_link_type := native:vndk
+        else
+            my_link_type := native:vndk_private
+        endif
+    else
+        my_link_type := native:vendor
+    endif
 else
 my_link_type := native:platform
 endif
@@ -228,7 +237,7 @@
     else
       my_coverage_path := $(TARGET_OUT_COVERAGE)/$(patsubst $(PRODUCT_OUT)/%,%,$(my_module_path))
     endif
-    my_coverage_path := $(my_coverage_path)/$(basename $(my_installed_module_stem)).gcnodir
+    my_coverage_path := $(my_coverage_path)/$(patsubst %.so,%,$(my_installed_module_stem)).gcnodir
     $(eval $(call copy-one-file,$(LOCAL_PREBUILT_COVERAGE_ARCHIVE),$(my_coverage_path)))
     $(LOCAL_BUILT_MODULE): $(my_coverage_path)
   endif
@@ -379,6 +388,15 @@
 $(built_module) : $(my_prebuilt_src_file) | $(ZIPALIGN) $(SIGNAPK_JAR)
 	$(transform-prebuilt-to-target)
 	$(uncompress-shared-libs)
+ifneq (true,$(DONT_UNCOMPRESS_PRIV_APPS_DEXS))
+ifeq (true,$(LOCAL_PRIVILEGED_MODULE))
+	$(uncompress-dexs)
+else
+  ifneq (,$(filter $(PRODUCT_LOADED_BY_PRIVILEGED_MODULES), $(LOCAL_MODULE)))
+	  $(uncompress-dexs)
+  endif  # PRODUCT_LOADED_BY_PRIVILEGED_MODULES
+endif  # LOCAL_PRIVILEGED_MODULE
+endif  # DONT_UNCOMPRESS_PRIV_APPS_DEXS
 ifdef LOCAL_DEX_PREOPT
 ifneq ($(BUILD_PLATFORM_ZIP),)
 	@# Keep a copy of apk with classes.dex unstripped
@@ -470,11 +488,7 @@
 	@echo "Dexpreopt Jar: $(PRIVATE_MODULE) ($@)"
 	$(call dexpreopt-one-file,$<,$@)
 
-$(built_module) : $(my_prebuilt_src_file)
-	$(call copy-file-to-target)
-ifneq (nostripping,$(LOCAL_DEX_PREOPT))
-	$(call dexpreopt-remove-classes.dex,$@)
-endif
+$(eval $(call dexpreopt-copy-jar,$(my_prebuilt_src_file),$(built_module),$(LOCAL_DEX_PREOPT)))
 endif # boot jar
 else # ! LOCAL_DEX_PREOPT
 $(built_module) : $(my_prebuilt_src_file)
@@ -500,6 +514,7 @@
 # for host java libraries deps should be in the common dir, so we make a copy in
 # the common dir.
 common_classes_jar := $(intermediates.COMMON)/classes.jar
+common_header_jar := $(intermediates.COMMON)/classes-header.jar
 
 $(common_classes_jar): PRIVATE_MODULE := $(LOCAL_MODULE)
 $(common_classes_jar): PRIVATE_PREFIX := $(my_prefix)
@@ -507,10 +522,16 @@
 $(common_classes_jar) : $(my_src_jar)
 	$(transform-prebuilt-to-target)
 
+ifneq ($(TURBINE_ENABLED),false)
+$(common_header_jar) : $(my_src_jar)
+	$(transform-prebuilt-to-target)
+endif
+
 else # !LOCAL_IS_HOST_MODULE
 # for target java libraries, the LOCAL_BUILT_MODULE is in a product-specific dir,
 # while the deps should be in the common dir, so we make a copy in the common dir.
 common_classes_jar := $(intermediates.COMMON)/classes.jar
+common_header_jar := $(intermediates.COMMON)/classes-header.jar
 common_classes_pre_proguard_jar := $(intermediates.COMMON)/classes-pre-proguard.jar
 common_javalib_jar := $(intermediates.COMMON)/javalib.jar
 
@@ -519,6 +540,8 @@
 
 ifeq ($(LOCAL_SDK_VERSION),system_current)
 my_link_type := java:system
+else ifneq (,$(call has-system-sdk-version,$(LOCAL_SDK_VERSION)))
+my_link_type := java:system
 else ifneq ($(LOCAL_SDK_VERSION),)
 my_link_type := java:sdk
 else
@@ -555,13 +578,20 @@
 $(common_classes_jar) : $(my_src_jar)
 	$(transform-prebuilt-to-target)
 
+ifneq ($(TURBINE_ENABLED),false)
+$(common_header_jar) : $(my_src_jar)
+	$(transform-prebuilt-to-target)
+endif
+
 $(common_classes_pre_proguard_jar) : $(my_src_jar)
 	$(transform-prebuilt-to-target)
 
 $(common_javalib_jar) : $(common_classes_jar)
 	$(transform-prebuilt-to-target)
 
-$(call define-jar-to-toc-rule, $(common_classes_jar))
+ifdef LOCAL_AAPT2_ONLY
+LOCAL_USE_AAPT2 := true
+endif
 
 ifdef LOCAL_USE_AAPT2
 ifneq ($(my_src_aar),)
@@ -590,7 +620,7 @@
 
 # We needed only very few PRIVATE variables and aapt2.mk input variables. Reset the unnecessary ones.
 $(my_res_package): PRIVATE_AAPT2_CFLAGS :=
-$(my_res_package): PRIVATE_AAPT_FLAGS := --static-lib --no-static-lib-packages
+$(my_res_package): PRIVATE_AAPT_FLAGS := --static-lib --no-static-lib-packages --auto-add-overlay
 $(my_res_package): PRIVATE_ANDROID_MANIFEST := $(intermediates.COMMON)/aar/AndroidManifest.xml
 $(my_res_package): PRIVATE_AAPT_INCLUDES := $(framework_res_package_export)
 $(my_res_package): PRIVATE_SOURCE_INTERMEDIATES_DIR :=
@@ -622,25 +652,6 @@
 endif # ! prebuilt_module_is_dex_javalib
 endif # LOCAL_IS_HOST_MODULE is not set
 
-ifneq ($(prebuilt_module_is_dex_javalib),true)
-
-ifdef LOCAL_JACK_ENABLED
-# We may be building classes.jack from a host jar for host dalvik Java library.
-$(intermediates.COMMON)/classes.jack : PRIVATE_JACK_FLAGS:=$(LOCAL_JACK_FLAGS)
-$(intermediates.COMMON)/classes.jack : PRIVATE_JACK_MIN_SDK_VERSION := $(if $(strip $(LOCAL_MIN_SDK_VERSION)),$(LOCAL_MIN_SDK_VERSION),1)
-$(intermediates.COMMON)/classes.jack : PRIVATE_JACK_PLUGIN_PATH := $(LOCAL_JACK_PLUGIN_PATH)
-$(intermediates.COMMON)/classes.jack : PRIVATE_JACK_PLUGIN := $(LOCAL_JACK_PLUGIN)
-$(intermediates.COMMON)/classes.jack : $(LOCAL_JACK_PLUGIN_PATH) $(my_src_jar) \
-        $(LOCAL_ADDITIONAL_DEPENDENCIES) $(JACK_DEFAULT_ARGS) $(JACK) \
-        | setup-jack-server
-	$(transform-jar-to-jack)
-
-# Update timestamps of .toc files for prebuilts so dependents will be
-# always rebuilt.
-$(intermediates.COMMON)/classes.dex.toc: $(intermediates.COMMON)/classes.jack
-	touch $@
-endif # LOCAL_JACK_ENABLED
-endif # ! prebuilt_module_is_dex_javalib
 endif # JAVA_LIBRARIES
 
 $(built_module) : $(LOCAL_ADDITIONAL_DEPENDENCIES)
diff --git a/core/product-graph.mk b/core/product-graph.mk
index 36e9037..268688a 100644
--- a/core/product-graph.mk
+++ b/core/product-graph.mk
@@ -34,7 +34,7 @@
 endef
 
 
-this_makefile := build/core/product-graph.mk
+this_makefile := build/make/core/product-graph.mk
 
 products_svg := $(OUT_DIR)/products.svg
 products_pdf := $(OUT_DIR)/products.pdf
@@ -103,6 +103,7 @@
 	$(hide) echo 'PRODUCT_MANUFACTURER=$$(PRODUCTS.$(strip $(1)).PRODUCT_MANUFACTURER)' >> $$@
 	$(hide) echo 'PRODUCT_PROPERTY_OVERRIDES=$$(PRODUCTS.$(strip $(1)).PRODUCT_PROPERTY_OVERRIDES)' >> $$@
 	$(hide) echo 'PRODUCT_DEFAULT_PROPERTY_OVERRIDES=$$(PRODUCTS.$(strip $(1)).PRODUCT_DEFAULT_PROPERTY_OVERRIDES)' >> $$@
+	$(hide) echo 'PRODUCT_SYSTEM_DEFAULT_PROPERTIES=$$(PRODUCTS.$(strip $(1)).PRODUCT_SYSTEM_DEFAULT_PROPERTIES)' >> $$@
 	$(hide) echo 'PRODUCT_CHARACTERISTICS=$$(PRODUCTS.$(strip $(1)).PRODUCT_CHARACTERISTICS)' >> $$@
 	$(hide) echo 'PRODUCT_COPY_FILES=$$(PRODUCTS.$(strip $(1)).PRODUCT_COPY_FILES)' >> $$@
 	$(hide) echo 'PRODUCT_OTA_PUBLIC_KEYS=$$(PRODUCTS.$(strip $(1)).PRODUCT_OTA_PUBLIC_KEYS)' >> $$@
diff --git a/core/product.mk b/core/product.mk
index 4682dac..f15f6b3 100644
--- a/core/product.mk
+++ b/core/product.mk
@@ -102,6 +102,7 @@
     PRODUCT_SDK_ADDON_COPY_MODULES \
     PRODUCT_SDK_ADDON_DOC_MODULES \
     PRODUCT_SDK_ADDON_SYS_IMG_SOURCE_PROP \
+    PRODUCT_SOONG_NAMESPACES \
     PRODUCT_DEFAULT_WIFI_CHANNELS \
     PRODUCT_DEFAULT_DEV_CERTIFICATE \
     PRODUCT_RESTRICT_VENDOR_FILES \
@@ -112,11 +113,13 @@
     PRODUCT_SUPPORTS_VERITY \
     PRODUCT_SUPPORTS_VERITY_FEC \
     PRODUCT_OEM_PROPERTIES \
+    PRODUCT_SYSTEM_DEFAULT_PROPERTIES \
     PRODUCT_SYSTEM_PROPERTY_BLACKLIST \
     PRODUCT_SYSTEM_SERVER_APPS \
     PRODUCT_SYSTEM_SERVER_JARS \
     PRODUCT_ALWAYS_PREOPT_EXTRACTED_APK \
     PRODUCT_DEXPREOPT_SPEED_APPS \
+    PRODUCT_LOADED_BY_PRIVILEGED_MODULES \
     PRODUCT_VBOOT_SIGNING_KEY \
     PRODUCT_VBOOT_SIGNING_SUBKEY \
     PRODUCT_VERITY_SIGNING_KEY \
@@ -143,8 +146,9 @@
     PRODUCT_SYSTEM_HEADROOM \
     PRODUCT_MINIMIZE_JAVA_DEBUG_INFO \
     PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS \
-
-
+    PRODUCT_ADB_KEYS \
+    PRODUCT_CFI_INCLUDE_PATHS \
+    PRODUCT_CFI_EXCLUDE_PATHS \
 
 define dump-product
 $(info ==== $(1) ====)\
diff --git a/core/product_config.mk b/core/product_config.mk
index f7ae834..5b0e257 100644
--- a/core/product_config.mk
+++ b/core/product_config.mk
@@ -88,7 +88,7 @@
 # Provide "PRODUCT-<prodname>-<goal>" targets, which lets you build
 # a particular configuration without needing to set up the environment.
 #
-ifndef KATI
+ifeq ($(CALLED_FROM_SETUP),true)
 product_goals := $(strip $(filter PRODUCT-%,$(MAKECMDGOALS)))
 ifdef product_goals
   # Scrape the product and build names out of the goal,
@@ -129,14 +129,14 @@
   # position, in case it matters.
   override MAKECMDGOALS := $(patsubst $(goal_name),$(default_goal_substitution),$(MAKECMDGOALS))
 endif
-endif # !KATI
+endif # CALLED_FROM_SETUP
 # else: Use the value set in the environment or buildspec.mk.
 
 # ---------------------------------------------------------------
 # Provide "APP-<appname>" targets, which lets you build
 # an unbundled app.
 #
-ifndef KATI
+ifeq ($(CALLED_FROM_SETUP),true)
 unbundled_goals := $(strip $(filter APP-%,$(MAKECMDGOALS)))
 ifdef unbundled_goals
   ifneq ($(words $(unbundled_goals)),1)
@@ -151,6 +151,13 @@
 endif # unbundled_goals
 endif
 
+# Now that we've parsed APP-* and PRODUCT-*, mark these as readonly
+TARGET_BUILD_APPS ?=
+.KATI_READONLY := \
+  TARGET_PRODUCT \
+  TARGET_BUILD_VARIANT \
+  TARGET_BUILD_APPS
+
 # Default to building dalvikvm on hosts that support it...
 ifeq ($(HOST_OS),linux)
 # ... or if the if the option is already set
@@ -259,6 +266,7 @@
 PRODUCT_SYSTEM_SERVER_JARS := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SYSTEM_SERVER_JARS))
 PRODUCT_SYSTEM_SERVER_APPS := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SYSTEM_SERVER_APPS))
 PRODUCT_DEXPREOPT_SPEED_APPS := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_DEXPREOPT_SPEED_APPS))
+PRODUCT_LOADED_BY_PRIVILEGED_MODULES := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_LOADED_BY_PRIVILEGED_MODULES))
 
 # All of the apps that we force preopt, this overrides WITH_DEXPREOPT.
 PRODUCT_ALWAYS_PREOPT_EXTRACTED_APK := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_ALWAYS_PREOPT_EXTRACTED_APK))
@@ -295,15 +303,6 @@
 PRODUCT_AAPT_CONFIG := \
     $(subst $(space),$(comma),$(strip $(PRODUCT_AAPT_CONFIG)))
 
-# product-scoped aapt flags
-PRODUCT_AAPT_FLAGS :=
-PRODUCT_AAPT2_CFLAGS :=
-ifneq ($(filter en_XA ar_XB,$(PRODUCT_LOCALES)),)
-  # Force generating resources for pseudo-locales.
-  PRODUCT_AAPT2_CFLAGS += --pseudo-localize
-  PRODUCT_AAPT_FLAGS += --pseudo-localize
-endif
-
 PRODUCT_BRAND := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_BRAND))
 
 PRODUCT_MODEL := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_MODEL))
@@ -339,7 +338,7 @@
 # The file at the source path should be copied to the destination path
 # when building  this product.  <destination path> is relative to
 # $(PRODUCT_OUT), so it should look like, e.g., "system/etc/file.xml".
-# The rules for these copy steps are defined in build/core/Makefile.
+# The rules for these copy steps are defined in build/make/core/Makefile.
 # The optional :<owner> is used to indicate the owner of a vendor file.
 PRODUCT_COPY_FILES := \
     $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_COPY_FILES))
@@ -359,6 +358,13 @@
     $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_DEFAULT_PROPERTY_OVERRIDES))
 .KATI_READONLY := PRODUCT_DEFAULT_PROPERTY_OVERRIDES
 
+# A list of property assignments, like "key = value", with zero or more
+# whitespace characters on either side of the '='.
+# used for adding properties to default.prop of system partition
+PRODUCT_SYSTEM_DEFAULT_PROPERTIES := \
+    $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SYSTEM_DEFAULT_PROPERTIES))
+.KATI_READONLY := PRODUCT_SYSTEM_DEFAULT_PROPERTIES
+
 # Should we use the default resources or add any product specific overlays
 PRODUCT_PACKAGE_OVERLAYS := \
     $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_PACKAGE_OVERLAYS))
@@ -456,3 +462,25 @@
 # Whether any paths are excluded from sanitization when SANITIZE_TARGET=integer_overflow
 PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS := \
     $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS))
+
+# ADB keys for debuggable builds
+PRODUCT_ADB_KEYS :=
+ifneq ($(filter eng userdebug,$(TARGET_BUILD_VARIANT)),)
+  PRODUCT_ADB_KEYS := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_ADB_KEYS))
+endif
+ifneq ($(filter-out 0 1,$(words $(PRODUCT_ADB_KEYS))),)
+  $(error Only one file may be in PRODUCT_ADB_KEYS: $(PRODUCT_ADB_KEYS))
+endif
+.KATI_READONLY := PRODUCT_ADB_KEYS
+
+# Whether any paths are excluded from sanitization when SANITIZE_TARGET=cfi
+PRODUCT_CFI_EXCLUDE_PATHS := \
+    $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_CFI_EXCLUDE_PATHS))
+
+# Whether any paths should have CFI enabled for components
+PRODUCT_CFI_INCLUDE_PATHS := \
+    $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_CFI_INCLUDE_PATHS))
+
+# which Soong namespaces to export to Make
+PRODUCT_SOONG_NAMESPACES :=
+    $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SOONG_NAMESPACES))
diff --git a/core/root.mk b/core/root.mk
index 6c8f795..1ef9aca 100644
--- a/core/root.mk
+++ b/core/root.mk
@@ -1,3 +1,3 @@
 ### DO NOT EDIT THIS FILE ###
-include build/core/main.mk
+include build/make/core/main.mk
 ### DO NOT EDIT THIS FILE ###
diff --git a/core/setup_one_odex.mk b/core/setup_one_odex.mk
index f0ffe81..e0d9926 100644
--- a/core/setup_one_odex.mk
+++ b/core/setup_one_odex.mk
@@ -32,6 +32,10 @@
     $(DEXPREOPT_ONE_FILE_DEPENDENCY_TOOLS) \
     $(my_dex_preopt_image_filename)
 
+# Pass special class loader context to skip the classpath and collision check.
+# Should modify build system to pass used libraries properly later.
+$(my_built_odex): PRIVATE_DEX2OAT_CLASS_LOADER_CONTEXT := \&
+
 my_installed_odex := $(call get-odex-installed-file-path,$($(my_2nd_arch_prefix)DEX2OAT_TARGET_ARCH),$(LOCAL_INSTALLED_MODULE))
 
 my_built_vdex := $(patsubst %.odex,%.vdex,$(my_built_odex))
diff --git a/core/soong_app_prebuilt.mk b/core/soong_app_prebuilt.mk
new file mode 100644
index 0000000..0b8fa4d
--- /dev/null
+++ b/core/soong_app_prebuilt.mk
@@ -0,0 +1,74 @@
+# App prebuilt coming from Soong.
+# Extra inputs:
+# LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE
+
+ifneq ($(LOCAL_MODULE_MAKEFILE),$(SOONG_ANDROID_MK))
+  $(call pretty-error,soong_app_prebuilt.mk may only be used from Soong)
+endif
+
+LOCAL_MODULE_SUFFIX := .apk
+LOCAL_BUILT_MODULE_STEM := package.apk
+
+#######################################
+include $(BUILD_SYSTEM)/base_rules.mk
+#######################################
+
+ifdef LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR
+  $(eval $(call copy-one-file,$(LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR),\
+    $(intermediates.COMMON)/jacoco-report-classes.jar))
+endif
+
+$(eval $(call copy-one-file,$(LOCAL_PREBUILT_MODULE_FILE),$(LOCAL_BUILT_MODULE)))
+
+ifdef LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE
+resource_export_package := $(intermediates.COMMON)/package-export.apk
+resource_export_stamp := $(intermediates.COMMON)/src/R.stamp
+
+$(resource_export_package): PRIVATE_STAMP := $(resource_export_stamp)
+$(resource_export_package): .KATI_IMPLICIT_OUTPUTS := $(resource_export_stamp)
+$(resource_export_package): $(LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE)
+	@echo "Copy: $$@"
+	$(copy-file-to-target)
+	touch $(PRIVATE_STAMP)
+
+endif # LOCAL_SOONG_RESOURCE_EXPORT_PACKAGE
+
+java-dex: $(LOCAL_SOONG_DEX_JAR)
+
+ifdef LOCAL_DEX_PREOPT
+# defines built_odex along with rule to install odex
+include $(BUILD_SYSTEM)/dex_preopt_odex_install.mk
+
+$(built_odex): $(LOCAL_SOONG_DEX_JAR)
+	$(call dexpreopt-one-file,$<,$@)
+endif
+
+ifndef LOCAL_IS_HOST_MODULE
+ifeq ($(LOCAL_SDK_VERSION),system_current)
+my_link_type := java:system
+my_warn_types := java:platform
+my_allowed_types := java:sdk java:system
+else ifneq ($(LOCAL_SDK_VERSION),)
+my_link_type := java:sdk
+my_warn_types := java:system java:platform
+my_allowed_types := java:sdk
+else
+my_link_type := java:platform
+my_warn_types :=
+my_allowed_types := java:sdk java:system java:platform
+endif
+
+my_link_deps :=
+my_2nd_arch_prefix := $(LOCAL_2ND_ARCH_VAR_PREFIX)
+my_common := COMMON
+include $(BUILD_SYSTEM)/link_type.mk
+endif # !LOCAL_IS_HOST_MODULE
+
+ifdef LOCAL_SOONG_RRO_DIRS
+  $(call append_enforce_rro_sources, \
+      $(my_register_name), \
+      false, \
+      $(LOCAL_FULL_MANIFEST_FILE), \
+      $(LOCAL_EXPORT_PACKAGE_RESOURCES), \
+      $(LOCAL_SOONG_RRO_DIRS))
+endif
diff --git a/core/soong_config.mk b/core/soong_config.mk
index cdd2896..f1f1e09 100644
--- a/core/soong_config.mk
+++ b/core/soong_config.mk
@@ -13,6 +13,7 @@
 endif
 endif
 
+ifeq ($(WRITE_SOONG_VARIABLES),true)
 # Converts a list to a JSON list.
 # $1: List separator.
 # $2: List.
@@ -24,75 +25,126 @@
 # Converts a comma-separated list to a JSON list.
 csv_to_json_list = $(call _json_list,$(comma),$(1))
 
+# 1: Key name
+# 2: Value
+add_json_val = $(eval _contents := $$(_contents)    "$$(strip $$(1))":$$(space)$$(strip $$(2))$$(comma)$$(newline))
+add_json_str = $(call add_json_val,$(1),"$(strip $(2))")
+add_json_list = $(call add_json_val,$(1),$(call json_list,$(patsubst %,%,$(2))))
+add_json_csv = $(call add_json_val,$(1),$(call csv_to_json_list,$(strip $(2))))
+add_json_bool = $(call add_json_val,$(1),$(if $(strip $(2)),true,false))
+
+invert_bool = $(if $(strip $(1)),,true)
+
 # Create soong.variables with copies of makefile settings.  Runs every build,
 # but only updates soong.variables if it changes
-SOONG_VARIABLES_TMP := $(SOONG_VARIABLES).$$$$
-$(SOONG_VARIABLES): FORCE
-	$(hide) mkdir -p $(dir $@)
-	$(hide) (\
-	echo '{'; \
-	echo '    "Make_suffix": "-$(TARGET_PRODUCT)",'; \
-	echo ''; \
-	echo '    "Platform_sdk_version": $(PLATFORM_SDK_VERSION),'; \
-	echo '    "Platform_version_all_codenames": $(call csv_to_json_list,$(PLATFORM_VERSION_ALL_CODENAMES)),'; \
-	echo '    "Unbundled_build": $(if $(TARGET_BUILD_APPS),true,false),'; \
-	echo '    "Brillo": $(if $(BRILLO),true,false),'; \
-	echo '    "Malloc_not_svelte": $(if $(filter true,$(MALLOC_SVELTE)),false,true),'; \
-	echo '    "Allow_missing_dependencies": $(if $(ALLOW_MISSING_DEPENDENCIES),true,false),'; \
-	echo '    "SanitizeHost": $(call json_list,$(SANITIZE_HOST)),'; \
-	echo '    "SanitizeDevice": $(call json_list,$(SANITIZE_TARGET)),'; \
-	echo '    "SanitizeDeviceDiag": $(call json_list,$(SANITIZE_TARGET_DIAG)),'; \
-	echo '    "SanitizeDeviceArch": $(call json_list,$(SANITIZE_TARGET_ARCH)),'; \
-	echo '    "HostStaticBinaries": $(if $(strip $(BUILD_HOST_static)),true,false),'; \
-	echo '    "Binder32bit": $(if $(BINDER32BIT),true,false),'; \
-	echo '    "DevicePrefer32BitExecutables": $(if $(filter true,$(TARGET_PREFER_32_BIT_EXECUTABLES)),true,false),'; \
-	echo '    "UseGoma": $(if $(filter-out false,$(USE_GOMA)),true,false),'; \
-	echo '    "Debuggable": $(if $(filter userdebug eng,$(TARGET_BUILD_VARIANT)),true,false),'; \
-	echo '    "Eng": $(if $(filter eng,$(TARGET_BUILD_VARIANT)),true,false),'; \
-	echo '    "VendorPath": "$(TARGET_COPY_OUT_VENDOR)",'; \
-	echo ''; \
-	echo '    "ClangTidy": $(if $(filter 1 true,$(WITH_TIDY)),true,false),'; \
-	echo '    "TidyChecks": "$(WITH_TIDY_CHECKS)",'; \
-	echo ''; \
-	echo '    "NativeCoverage": $(if $(filter true,$(NATIVE_COVERAGE)),true,false),'; \
-	echo '    "CoveragePaths": $(call csv_to_json_list,$(COVERAGE_PATHS)),'; \
-	echo '    "CoverageExcludePaths": $(call csv_to_json_list,$(COVERAGE_EXCLUDE_PATHS)),'; \
-	echo ''; \
-	echo '    "DeviceName": "$(TARGET_DEVICE)",'; \
-	echo '    "DeviceArch": "$(TARGET_ARCH)",'; \
-	echo '    "DeviceArchVariant": "$(TARGET_ARCH_VARIANT)",'; \
-	echo '    "DeviceCpuVariant": "$(TARGET_CPU_VARIANT)",'; \
-	echo '    "DeviceAbi": ["$(TARGET_CPU_ABI)", "$(TARGET_CPU_ABI2)"],'; \
-	echo '    "DeviceUsesClang": $(if $(USE_CLANG_PLATFORM_BUILD),$(USE_CLANG_PLATFORM_BUILD),false),'; \
-	echo '    "DeviceVndkVersion": "$(BOARD_VNDK_VERSION)",'; \
-	echo ''; \
-	echo '    "DeviceSecondaryArch": "$(TARGET_2ND_ARCH)",'; \
-	echo '    "DeviceSecondaryArchVariant": "$(TARGET_2ND_ARCH_VARIANT)",'; \
-	echo '    "DeviceSecondaryCpuVariant": "$(TARGET_2ND_CPU_VARIANT)",'; \
-	echo '    "DeviceSecondaryAbi": ["$(TARGET_2ND_CPU_ABI)", "$(TARGET_2ND_CPU_ABI2)"],'; \
-	echo ''; \
-	echo '    "HostArch": "$(HOST_ARCH)",'; \
-	echo '    "HostSecondaryArch": "$(HOST_2ND_ARCH)",'; \
-	echo ''; \
-	echo '    "CrossHost": "$(HOST_CROSS_OS)",'; \
-	echo '    "CrossHostArch": "$(HOST_CROSS_ARCH)",'; \
-	echo '    "CrossHostSecondaryArch": "$(HOST_CROSS_2ND_ARCH)",'; \
-	echo '    "Safestack": $(if $(filter true,$(USE_SAFESTACK)),true,false),'; \
-	echo '    "EnableCFI": $(if $(filter false,$(ENABLE_CFI)),false,true),'; \
-	echo '    "IntegerOverflowExcludePaths": $(call json_list,$(INTEGER_OVERFLOW_EXCLUDE_PATHS) $(PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS)),'; \
-	echo '    "Device_uses_hwc2": $(if $(filter true,$(TARGET_USES_HWC2)),true,false),'; \
-	echo '    "Override_rs_driver": "$(OVERRIDE_RS_DRIVER)",'; \
-	echo '    "Treble": $(if $(filter true,$(PRODUCT_FULL_TREBLE)),true,false),'; \
-	echo '    "Pdk": $(if $(filter true,$(TARGET_BUILD_PDK)),true,false),'; \
-	echo ''; \
-	echo '    "ArtUseReadBarrier": $(if $(filter false,$(PRODUCT_ART_USE_READ_BARRIER)),false,true),'; \
-	echo ''; \
-	echo '    "BtConfigIncludeDir": "$(BOARD_BLUETOOTH_BDROID_BUILDCFG_INCLUDE_DIR)",'; \
-	echo ''; \
-	echo '    "DeviceKernelHeaders": $(call json_list,$(strip $(TARGET_PROJECT_SYSTEM_INCLUDES)))'; \
-	echo '}') > $(SOONG_VARIABLES_TMP); \
-	if ! cmp -s $(SOONG_VARIABLES_TMP) $(SOONG_VARIABLES); then \
-	  mv $(SOONG_VARIABLES_TMP) $(SOONG_VARIABLES); \
+$(shell mkdir -p $(dir $(SOONG_VARIABLES)))
+_contents := {$(newline)
+
+$(call add_json_str,  Make_suffix, -$(TARGET_PRODUCT))
+
+$(call add_json_val,  Platform_sdk_version,              $(PLATFORM_SDK_VERSION))
+$(call add_json_csv,  Platform_version_active_codenames, $(PLATFORM_VERSION_ALL_CODENAMES))
+$(call add_json_csv,  Platform_version_future_codenames, $(PLATFORM_VERSION_FUTURE_CODENAMES))
+
+$(call add_json_bool, Allow_missing_dependencies,        $(ALLOW_MISSING_DEPENDENCIES))
+$(call add_json_bool, Unbundled_build,                   $(TARGET_BUILD_APPS))
+$(call add_json_bool, Pdk,                               $(filter true,$(TARGET_BUILD_PDK)))
+
+$(call add_json_bool, Debuggable,                        $(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
+$(call add_json_bool, Eng,                               $(filter eng,$(TARGET_BUILD_VARIANT)))
+
+$(call add_json_str,  DeviceName,                        $(TARGET_DEVICE))
+$(call add_json_str,  DeviceArch,                        $(TARGET_ARCH))
+$(call add_json_str,  DeviceArchVariant,                 $(TARGET_ARCH_VARIANT))
+$(call add_json_str,  DeviceCpuVariant,                  $(TARGET_CPU_VARIANT))
+$(call add_json_list, DeviceAbi,                         $(TARGET_CPU_ABI) $(TARGET_CPU_ABI2))
+
+$(call add_json_str,  DeviceSecondaryArch,               $(TARGET_2ND_ARCH))
+$(call add_json_str,  DeviceSecondaryArchVariant,        $(TARGET_2ND_ARCH_VARIANT))
+$(call add_json_str,  DeviceSecondaryCpuVariant,         $(TARGET_2ND_CPU_VARIANT))
+$(call add_json_list, DeviceSecondaryAbi,                $(TARGET_2ND_CPU_ABI) $(TARGET_2ND_CPU_ABI2))
+
+$(call add_json_str,  HostArch,                          $(HOST_ARCH))
+$(call add_json_str,  HostSecondaryArch,                 $(HOST_2ND_ARCH))
+$(call add_json_bool, HostStaticBinaries,                $(BUILD_HOST_static))
+
+$(call add_json_str,  CrossHost,                         $(HOST_CROSS_OS))
+$(call add_json_str,  CrossHostArch,                     $(HOST_CROSS_ARCH))
+$(call add_json_str,  CrossHostSecondaryArch,            $(HOST_CROSS_2ND_ARCH))
+
+$(call add_json_list, ResourceOverlays,                  $(PRODUCT_PACKAGE_OVERLAYS) $(DEVICE_PACKAGE_OVERLAYS))
+$(call add_json_list, EnforceRROTargets,                 $(PRODUCT_ENFORCE_RRO_TARGETS))
+$(call add_json_list, EnforceRROExcludedOverlays,        $(PRODUCT_ENFORCE_RRO_EXCLUDED_OVERLAYS))
+
+$(call add_json_str,  AAPTCharacteristics,               $(TARGET_AAPT_CHARACTERISTICS))
+$(call add_json_list, AAPTConfig,                        $(PRODUCT_AAPT_CONFIG))
+$(call add_json_str,  AAPTPreferredConfig,               $(PRODUCT_AAPT_PREF_CONFIG))
+$(call add_json_list, AAPTPrebuiltDPI,                   $(PRODUCT_AAPT_PREBUILT_DPI))
+
+$(call add_json_str,  DefaultAppCertificate,             $(PRODUCT_DEFAULT_DEV_CERTIFICATE))
+
+$(call add_json_str,  AppsDefaultVersionName,            $(APPS_DEFAULT_VERSION_NAME))
+
+$(call add_json_list, SanitizeHost,                      $(SANITIZE_HOST))
+$(call add_json_list, SanitizeDevice,                    $(SANITIZE_TARGET))
+$(call add_json_list, SanitizeDeviceDiag,                $(SANITIZE_TARGET_DIAG))
+$(call add_json_list, SanitizeDeviceArch,                $(SANITIZE_TARGET_ARCH))
+
+$(call add_json_bool, Safestack,                         $(filter true,$(USE_SAFESTACK)))
+$(call add_json_bool, EnableCFI,                         $(call invert_bool,$(filter false,$(ENABLE_CFI))))
+$(call add_json_list, CFIExcludePaths,                   $(CFI_EXCLUDE_PATHS) $(PRODUCT_CFI_EXCLUDE_PATHS))
+$(call add_json_list, CFIIncludePaths,                   $(CFI_INCLUDE_PATHS) $(PRODUCT_CFI_INCLUDE_PATHS))
+$(call add_json_list, IntegerOverflowExcludePaths,       $(INTEGER_OVERFLOW_EXCLUDE_PATHS) $(PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS))
+
+$(call add_json_bool, ClangTidy,                         $(filter 1 true,$(WITH_TIDY)))
+$(call add_json_str,  TidyChecks,                        $(WITH_TIDY_CHECKS))
+
+$(call add_json_bool, NativeCoverage,                    $(filter true,$(NATIVE_COVERAGE)))
+$(call add_json_list, CoveragePaths,                     $(COVERAGE_PATHS))
+$(call add_json_list, CoverageExcludePaths,              $(COVERAGE_EXCLUDE_PATHS))
+
+$(call add_json_bool, ArtUseReadBarrier,                 $(call invert_bool,$(filter false,$(PRODUCT_ART_USE_READ_BARRIER))))
+$(call add_json_bool, Binder32bit,                       $(BINDER32BIT))
+$(call add_json_bool, Brillo,                            $(BRILLO))
+$(call add_json_str,  BtConfigIncludeDir,                $(BOARD_BLUETOOTH_BDROID_BUILDCFG_INCLUDE_DIR))
+$(call add_json_bool, Device_uses_hwc2,                  $(filter true,$(TARGET_USES_HWC2)))
+$(call add_json_list, DeviceKernelHeaders,               $(TARGET_PROJECT_SYSTEM_INCLUDES))
+$(call add_json_bool, DevicePrefer32BitExecutables,      $(filter true,$(TARGET_PREFER_32_BIT_EXECUTABLES)))
+$(call add_json_val,  DeviceUsesClang,                   $(if $(USE_CLANG_PLATFORM_BUILD),$(USE_CLANG_PLATFORM_BUILD),false))
+$(call add_json_str,  DeviceVndkVersion,                 $(BOARD_VNDK_VERSION))
+$(call add_json_list, ExtraVndkVersions,                 $(PRODUCT_EXTRA_VNDK_VERSIONS))
+$(call add_json_bool, Malloc_not_svelte,                 $(call invert_bool,$(filter true,$(MALLOC_SVELTE))))
+$(call add_json_str,  Override_rs_driver,                $(OVERRIDE_RS_DRIVER))
+$(call add_json_bool, Treble,                            $(filter true,$(PRODUCT_FULL_TREBLE)))
+$(call add_json_bool, Uml,                               $(filter true,$(TARGET_USER_MODE_LINUX)))
+$(call add_json_str,  VendorPath,                        $(TARGET_COPY_OUT_VENDOR))
+$(call add_json_bool, MinimizeJavaDebugInfo,             $(filter true,$(PRODUCT_MINIMIZE_JAVA_DEBUG_INFO)))
+
+$(call add_json_bool, UseGoma,                           $(filter-out false,$(USE_GOMA)))
+
+$(call add_json_str,  DistDir,                           $(if $(dist_goal), $(DIST_DIR)))
+
+$(call add_json_list, NamespacesToExport,                $(PRODUCT_SOONG_NAMESPACES))
+
+_contents := $(subst $(comma)$(newline)__SV_END,$(newline)}$(newline),$(_contents)__SV_END)
+
+$(file >$(SOONG_VARIABLES).tmp,$(_contents))
+
+$(shell if ! cmp -s $(SOONG_VARIABLES).tmp $(SOONG_VARIABLES); then \
+	  mv $(SOONG_VARIABLES).tmp $(SOONG_VARIABLES); \
 	else \
-	  rm $(SOONG_VARIABLES_TMP); \
-	fi
+	  rm $(SOONG_VARIABLES).tmp; \
+	fi)
+
+_json_list :=
+json_list :=
+csv_to_json_list :=
+add_json_val :=
+add_json_str :=
+add_json_list :=
+add_json_csv :=
+add_json_bool :=
+invert_bool :=
+_contents :=
+
+endif # CONFIGURE_SOONG
diff --git a/core/soong_java_prebuilt.mk b/core/soong_java_prebuilt.mk
new file mode 100644
index 0000000..6cf9422
--- /dev/null
+++ b/core/soong_java_prebuilt.mk
@@ -0,0 +1,108 @@
+# Java prebuilt coming from Soong.
+# Extra inputs:
+# LOCAL_SOONG_HEADER_JAR
+# LOCAL_SOONG_DEX_JAR
+# LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR
+
+ifneq ($(LOCAL_MODULE_MAKEFILE),$(SOONG_ANDROID_MK))
+  $(call pretty-error,soong_java_prebuilt.mk may only be used from Soong)
+endif
+
+LOCAL_MODULE_SUFFIX := .jar
+LOCAL_BUILT_MODULE_STEM := javalib.jar
+
+#######################################
+include $(BUILD_SYSTEM)/base_rules.mk
+#######################################
+
+full_classes_jar := $(intermediates.COMMON)/classes.jar
+full_classes_pre_proguard_jar := $(intermediates.COMMON)/classes-pre-proguard.jar
+full_classes_header_jar := $(intermediates.COMMON)/classes-header.jar
+common_javalib.jar := $(intermediates.COMMON)/javalib.jar
+
+$(eval $(call copy-one-file,$(LOCAL_PREBUILT_MODULE_FILE),$(full_classes_jar)))
+$(eval $(call copy-one-file,$(LOCAL_PREBUILT_MODULE_FILE),$(full_classes_pre_proguard_jar)))
+
+ifdef LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR
+  $(eval $(call copy-one-file,$(LOCAL_SOONG_JACOCO_REPORT_CLASSES_JAR),\
+    $(intermediates.COMMON)/jacoco-report-classes.jar))
+  $(call add-dependency,$(common_javalib.jar),\
+    $(intermediates.COMMON)/jacoco-report-classes.jar)
+endif
+
+ifneq ($(TURBINE_DISABLED),false)
+ifdef LOCAL_SOONG_HEADER_JAR
+$(eval $(call copy-one-file,$(LOCAL_SOONG_HEADER_JAR),$(full_classes_header_jar)))
+else
+$(eval $(call copy-one-file,$(full_classes_jar),$(full_classes_header_jar)))
+endif
+endif # TURBINE_DISABLED != false
+
+ifdef LOCAL_SOONG_DEX_JAR
+  ifndef LOCAL_IS_HOST_MODULE
+    $(eval $(call copy-one-file,$(LOCAL_SOONG_DEX_JAR),$(common_javalib.jar)))
+    $(eval $(call add-dependency,$(common_javalib.jar),$(full_classes_jar) $(full_classes_header_jar)))
+
+    dex_preopt_profile_src_file := $(common_javalib.jar)
+
+    # defines built_odex along with rule to install odex
+    include $(BUILD_SYSTEM)/dex_preopt_odex_install.mk
+
+    dex_preopt_profile_src_file :=
+
+    ifdef LOCAL_DEX_PREOPT
+      ifneq ($(dexpreopt_boot_jar_module),) # boot jar
+        # boot jar's rules are defined in dex_preopt.mk
+        dexpreopted_boot_jar := $(DEXPREOPT_BOOT_JAR_DIR_FULL_PATH)/$(dexpreopt_boot_jar_module)_nodex.jar
+        $(eval $(call copy-one-file,$(dexpreopted_boot_jar),$(LOCAL_BUILT_MODULE)))
+
+        # For libart boot jars, we don't have .odex files.
+      else # ! boot jar
+        $(built_odex): PRIVATE_MODULE := $(LOCAL_MODULE)
+        # Use pattern rule - we may have multiple built odex files.
+$(built_odex) : $(dir $(LOCAL_BUILT_MODULE))% : $(common_javalib.jar)
+	@echo "Dexpreopt Jar: $(PRIVATE_MODULE) ($@)"
+	$(call dexpreopt-one-file,$<,$@)
+
+       $(eval $(call dexpreopt-copy-jar,$(common_javalib.jar),$(LOCAL_BUILT_MODULE),$(LOCAL_DEX_PREOPT)))
+      endif # ! boot jar
+    else # LOCAL_DEX_PREOPT
+      $(eval $(call copy-one-file,$(common_javalib.jar),$(LOCAL_BUILT_MODULE)))
+    endif # LOCAL_DEX_PREOPT
+  else # LOCAL_IS_HOST_MODULE
+    $(eval $(call copy-one-file,$(LOCAL_SOONG_DEX_JAR),$(LOCAL_BUILT_MODULE)))
+    $(eval $(call add-dependency,$(LOCAL_BUILT_MODULE),$(full_classes_jar) $(full_classes_header_jar)))
+  endif
+
+  java-dex : $(LOCAL_BUILT_MODULE)
+else
+  $(eval $(call copy-one-file,$(full_classes_jar),$(LOCAL_BUILT_MODULE)))
+endif
+
+javac-check : $(full_classes_jar)
+javac-check-$(LOCAL_MODULE) : $(full_classes_jar)
+
+ifndef LOCAL_IS_HOST_MODULE
+ifeq ($(LOCAL_SDK_VERSION),system_current)
+my_link_type := java:system
+my_warn_types := java:platform
+my_allowed_types := java:sdk java:system
+else ifneq (,$(call has-system-sdk-version,$(LOCAL_SDK_VERSION)))
+my_link_type := java:system
+my_warn_types := java:platform
+my_allowed_types := java:sdk java:system
+else ifneq ($(LOCAL_SDK_VERSION),)
+my_link_type := java:sdk
+my_warn_types := java:system java:platform
+my_allowed_types := java:sdk
+else
+my_link_type := java:platform
+my_warn_types :=
+my_allowed_types := java:sdk java:system java:platform
+endif
+
+my_link_deps :=
+my_2nd_arch_prefix := $(LOCAL_2ND_ARCH_VAR_PREFIX)
+my_common := COMMON
+include $(BUILD_SYSTEM)/link_type.mk
+endif # !LOCAL_IS_HOST_MODULE
diff --git a/core/static_java_library.mk b/core/static_java_library.mk
index 1373011..6645af5 100644
--- a/core/static_java_library.mk
+++ b/core/static_java_library.mk
@@ -24,14 +24,14 @@
 LOCAL_IS_STATIC_JAVA_LIBRARY := true
 LOCAL_MODULE_CLASS := JAVA_LIBRARIES
 
-#################################
-include $(BUILD_SYSTEM)/configure_local_jack.mk
-#################################
-
 intermediates.COMMON := $(call local-intermediates-dir,COMMON)
 
 my_res_package :=
 
+ifdef LOCAL_AAPT2_ONLY
+LOCAL_USE_AAPT2 := true
+endif
+
 # Hack to build static Java library with Android resource
 # See bug 5714516
 all_resources :=
@@ -76,13 +76,6 @@
 
 LOCAL_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_PROGUARD_FLAGS)
 
-ifdef LOCAL_JACK_ENABLED
-ifndef LOCAL_JACK_PROGUARD_FLAGS
-    LOCAL_JACK_PROGUARD_FLAGS := $(LOCAL_PROGUARD_FLAGS)
-endif
-LOCAL_JACK_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_JACK_PROGUARD_FLAGS)
-endif # LOCAL_JACK_ENABLED
-
 R_file_stamp := $(intermediates.COMMON)/src/R.stamp
 LOCAL_INTERMEDIATE_TARGETS += $(R_file_stamp)
 
@@ -157,7 +150,7 @@
 ifneq (,$(LOCAL_SDK_VERSION))
 # Set target-api for LOCAL_SDK_VERSIONs other than current.
 ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
-renderscript_target_api := $(LOCAL_SDK_VERSION)
+renderscript_target_api := $(call get-numeric-sdk-version,$(LOCAL_SDK_VERSION))
 endif
 endif  # LOCAL_SDK_VERSION is set
 endif  # LOCAL_RENDERSCRIPT_TARGET_API is set
@@ -170,6 +163,7 @@
 include $(BUILD_SYSTEM)/aapt2.mk
 $(my_res_package) : $(framework_res_package_export_deps)
 else
+$(R_file_stamp): PRIVATE_RESOURCE_LIST := $(all_resources)
 $(R_file_stamp) : $(all_resources) $(full_android_manifest) $(AAPT) $(framework_res_package_export_deps)
 	@echo "target R.java/Manifest.java: $(PRIVATE_MODULE) ($@)"
 	$(create-resource-java-files)
@@ -177,12 +171,10 @@
 endif  # LOCAL_USE_AAPT2
 
 $(LOCAL_BUILT_MODULE): $(R_file_stamp)
-ifdef LOCAL_JACK_ENABLED
-$(noshrob_classes_jack): $(R_file_stamp)
-$(full_classes_jack): $(R_file_stamp)
-$(jack_check_timestamp): $(R_file_stamp)
-endif # LOCAL_JACK_ENABLED
+$(java_source_list_file): $(R_file_stamp)
+$(foreach x,$(sharded_java_source_list_files),$(eval $(x): $(R_file_stamp)))
 $(full_classes_compiled_jar): $(R_file_stamp)
+$(full_classes_turbine_jar): $(R_file_stamp)
 
 
 # if we have custom proguarding done use the proguarded classes jar instead of the normal classes jar
@@ -199,6 +191,7 @@
 $(built_aar): PRIVATE_CLASSES_JAR := $(aar_classes_jar)
 $(built_aar): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
 $(built_aar): PRIVATE_R_TXT := $(LOCAL_INTERMEDIATE_SOURCE_DIR)/R.txt
+$(built_aar): $(JAR_ARGS)
 $(built_aar) : $(aar_classes_jar) $(full_android_manifest)
 	@echo "target AAR:  $(PRIVATE_MODULE) ($@)"
 	$(hide) rm -rf $(dir $@)aar && mkdir -p $(dir $@)aar/res
@@ -208,7 +201,7 @@
 	$(hide) $(foreach res,$(PRIVATE_RESOURCE_DIR),cp -Rfn $(res)/* $(dir $@)aar/res;)
 	$(hide) cp $(PRIVATE_R_TXT) $(dir $@)aar/R.txt
 	$(hide) $(JAR) -cMf $@ \
-	  -C $(dir $@)aar .
+	  $(call jar-args-sorted-files-in-directory,$(dir $@)aar)
 
 # Register the aar file.
 ALL_MODULES.$(LOCAL_MODULE).AAR := $(built_aar)
diff --git a/core/target_test_config.mk b/core/target_test_config.mk
new file mode 100644
index 0000000..61f5d2b
--- /dev/null
+++ b/core/target_test_config.mk
@@ -0,0 +1,23 @@
+#
+# Copyright (C) 2017 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.
+#
+
+#
+# Common rules for building a TradeFed test XML file for target side tests.
+#
+
+$(call record-module-type,TARGET_TEST_CONFIG)
+
+include $(BUILD_SYSTEM)/test_config_common.mk
diff --git a/core/tasks/boot_jars_package_check.mk b/core/tasks/boot_jars_package_check.mk
index 188c267..dc79e23 100644
--- a/core/tasks/boot_jars_package_check.mk
+++ b/core/tasks/boot_jars_package_check.mk
@@ -24,8 +24,8 @@
 stamp := $(intermediates)/stamp
 built_boot_jars := $(foreach j, $(PRODUCT_BOOT_JARS), \
   $(call intermediates-dir-for, JAVA_LIBRARIES, $(j),,COMMON)/classes.jar)
-script := build/core/tasks/check_boot_jars/check_boot_jars.py
-whitelist_file := build/core/tasks/check_boot_jars/package_whitelist.txt
+script := build/make/core/tasks/check_boot_jars/check_boot_jars.py
+whitelist_file := build/make/core/tasks/check_boot_jars/package_whitelist.txt
 
 $(stamp): PRIVATE_BOOT_JARS := $(built_boot_jars)
 $(stamp): PRIVATE_SCRIPT := $(script)
diff --git a/core/tasks/collect_gpl_sources.mk b/core/tasks/collect_gpl_sources.mk
index 30ba62b..70f0afe 100644
--- a/core/tasks/collect_gpl_sources.mk
+++ b/core/tasks/collect_gpl_sources.mk
@@ -12,20 +12,22 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-gpl_source_tgz := $(call intermediates-dir-for,PACKAGING,gpl_source,HOST,COMMON)/gpl_source.tgz
+ifdef dist_goal
+
+# The rule below doesn't have dependenices on the files that it copies,
+# so manually generate directly into the DIST_DIR directory that is always
+# wiped between dist builds.
+gpl_source_tgz := $(DIST_DIR)/gpl_source.tgz
 
 # FORCE since we can't know whether any of the sources changed
 $(gpl_source_tgz): PRIVATE_PATHS := $(sort $(patsubst %/, %, $(dir $(ALL_GPL_MODULE_LICENSE_FILES))))
-$(gpl_source_tgz) : $(ALL_GPL_MODULE_LICENSE_FILES) FORCE
+$(gpl_source_tgz) : $(ALL_GPL_MODULE_LICENSE_FILES)
 	@echo Package gpl sources: $@
-	@rm -rf $(dir $@) && mkdir -p $(dir $@)
 	$(hide) tar cfz $@ --exclude ".git*" $(PRIVATE_PATHS)
 
-
-.PHONY: gpl_source_tgz
-gpl_source_tgz : $(gpl_source_tgz)
-
 # Dist the tgz only if we are doing a full build
 ifeq (,$(TARGET_BUILD_APPS))
-$(call dist-for-goals, droidcore, $(gpl_source_tgz))
+droidcore: $(gpl_source_tgz)
 endif
+
+endif # dist_goal
diff --git a/core/tasks/device-tests.mk b/core/tasks/device-tests.mk
index b1b936a..a2b626e 100644
--- a/core/tasks/device-tests.mk
+++ b/core/tasks/device-tests.mk
@@ -6,7 +6,7 @@
 #
 #      http://www.apache.org/licenses/LICENSE-2.0
 #
-# Unless required by applicable law or agrls eed to in writing, software
+# 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
@@ -16,14 +16,23 @@
 .PHONY: device-tests
 
 device-tests-zip := $(PRODUCT_OUT)/device-tests.zip
-$(device-tests-zip): $(COMPATIBILITY.device-tests.FILES) $(SOONG_ZIP)
-	echo $(sort $(COMPATIBILITY.device-tests.FILES)) > $@.list
-	sed -i -e 's/\s\+/\n/g' $@.list
+# Create an artifact to include a list of test config files in device-tests.
+device-tests-list-zip := $(PRODUCT_OUT)/device-tests_list.zip
+$(device-tests-zip) : .KATI_IMPLICIT_OUTPUTS := $(device-tests-list-zip)
+$(device-tests-zip) : PRIVATE_device_tests_list := $(PRODUCT_OUT)/device-tests_list
+
+$(device-tests-zip) : $(COMPATIBILITY.device-tests.FILES) $(SOONG_ZIP)
+	echo $(sort $(COMPATIBILITY.device-tests.FILES)) | tr " " "\n" > $@.list
 	grep $(HOST_OUT_TESTCASES) $@.list > $@-host.list || true
 	grep $(TARGET_OUT_TESTCASES) $@.list > $@-target.list || true
 	$(hide) $(SOONG_ZIP) -d -o $@ -P host -C $(HOST_OUT) -l $@-host.list -P target -C $(PRODUCT_OUT) -l $@-target.list
+	rm -f $(PRIVATE_device_tests_list)
+	$(hide) grep -e .*.config$$ $@-host.list | sed s%$(HOST_OUT)%host%g > $(PRIVATE_device_tests_list)
+	$(hide) grep -e .*.config$$ $@-target.list | sed s%$(PRODUCT_OUT)%target%g >> $(PRIVATE_device_tests_list)
+	$(hide) $(SOONG_ZIP) -d -o $(device-tests-list-zip) -C $(dir $@) -f $(PRIVATE_device_tests_list)
+	rm -f $@.list $@-host.list $@-target.list $(PRIVATE_device_tests_list)
 
 device-tests: $(device-tests-zip)
-$(call dist-for-goals, device-tests, $(device-tests-zip))
+$(call dist-for-goals, device-tests, $(device-tests-zip) $(device-tests-list-zip))
 
 tests: device-tests
diff --git a/core/tasks/general-tests.mk b/core/tasks/general-tests.mk
index 763dd51..c7f1dc9 100644
--- a/core/tasks/general-tests.mk
+++ b/core/tasks/general-tests.mk
@@ -6,7 +6,7 @@
 #
 #      http://www.apache.org/licenses/LICENSE-2.0
 #
-# Unless required by applicable law or agrls eed to in writing, software
+# 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
@@ -15,12 +15,21 @@
 .PHONY: general-tests
 
 general-tests-zip := $(PRODUCT_OUT)/general-tests.zip
-$(general-tests-zip): $(COMPATIBILITY.general-tests.FILES) $(SOONG_ZIP)
-	echo $(sort $(COMPATIBILITY.general-tests.FILES)) > $@.list
-	sed -i -e 's/\s\+/\n/g' $@.list
+# Create an artifact to include a list of test config files in general-tests.
+general-tests-list-zip := $(PRODUCT_OUT)/general-tests_list.zip
+$(general-tests-zip) : .KATI_IMPLICIT_OUTPUTS := $(general-tests-list-zip)
+$(general-tests-zip) : PRIVATE_general_tests_list := $(PRODUCT_OUT)/general-tests_list
+
+$(general-tests-zip) : $(COMPATIBILITY.general-tests.FILES) $(SOONG_ZIP)
+	echo $(sort $(COMPATIBILITY.general-tests.FILES)) | tr " " "\n" > $@.list
 	grep $(HOST_OUT_TESTCASES) $@.list > $@-host.list || true
 	grep $(TARGET_OUT_TESTCASES) $@.list > $@-target.list || true
 	$(hide) $(SOONG_ZIP) -d -o $@ -P host -C $(HOST_OUT) -l $@-host.list -P target -C $(PRODUCT_OUT) -l $@-target.list
+	rm -f $(PRIVATE_general_tests_list)
+	$(hide) grep -e .*.config$$ $@-host.list | sed s%$(HOST_OUT)%host%g > $(PRIVATE_general_tests_list)
+	$(hide) grep -e .*.config$$ $@-target.list | sed s%$(PRODUCT_OUT)%target%g >> $(PRIVATE_general_tests_list)
+	$(hide) $(SOONG_ZIP) -d -o $(general-tests-list-zip) -C $(dir $@) -f $(PRIVATE_general_tests_list)
+	rm -f $@.list $@-host.list $@-target.list $(PRIVATE_general_tests_list)
 
 general-tests: $(general-tests-zip)
-$(call dist-for-goals, general-tests, $(general-tests-zip))
+$(call dist-for-goals, general-tests, $(general-tests-zip) $(general-tests-list-zip))
diff --git a/core/tasks/module-info.mk b/core/tasks/module-info.mk
index e9b2ac7..f6d688c 100644
--- a/core/tasks/module-info.mk
+++ b/core/tasks/module-info.mk
@@ -11,6 +11,7 @@
 			'"path": [$(foreach w,$(sort $(ALL_MODULES.$(m).PATH)),"$(w)", )], ' \
 			'"tags": [$(foreach w,$(sort $(ALL_MODULES.$(m).TAGS)),"$(w)", )], ' \
 			'"installed": [$(foreach w,$(sort $(ALL_MODULES.$(m).INSTALLED)),"$(w)", )], ' \
+			'"compatibility_suites": [$(foreach w,$(sort $(ALL_MODULES.$(m).COMPATIBILITY_SUITES)),"$(w)", )], ' \
 			'},\n' \
 	 ) | sed -e 's/, *\]/]/g' -e 's/, *\}/ }/g' -e '$$s/,$$//' >> $@
 	$(hide) echo '}' >> $@
diff --git a/core/tasks/sdk-addon.mk b/core/tasks/sdk-addon.mk
index e26773c..197d41a 100644
--- a/core/tasks/sdk-addon.mk
+++ b/core/tasks/sdk-addon.mk
@@ -105,21 +105,21 @@
 
 $(full_target): PRIVATE_STAGING_DIR := $(call append-path,$(staging),$(addon_dir_leaf))
 
-$(full_target): $(sdk_addon_deps) | $(ACP)
+$(full_target): $(sdk_addon_deps) | $(ACP) $(SOONG_ZIP)
 	@echo Packaging SDK Addon: $@
 	$(hide) mkdir -p $(PRIVATE_STAGING_DIR)/docs
 	$(hide) for d in $(PRIVATE_DOCS_DIRS); do \
 	    $(ACP) -r $$d $(PRIVATE_STAGING_DIR)/docs ;\
 	  done
 	$(hide) mkdir -p $(dir $@)
-	$(hide) ( F=$$(pwd)/$@ ; cd $(PRIVATE_STAGING_DIR)/.. && zip -rqX $$F $(notdir $(PRIVATE_STAGING_DIR)) )
+	$(hide) $(SOONG_ZIP) -o $@ -C $(dir $(PRIVATE_STAGING_DIR)) -D $(PRIVATE_STAGING_DIR)
 
 $(full_target_img): PRIVATE_STAGING_DIR := $(call append-path,$(staging),$(addon_dir_img))/images/$(TARGET_CPU_ABI)
-$(full_target_img): $(full_target) $(addon_img_source_prop) | $(ACP)
+$(full_target_img): $(full_target) $(addon_img_source_prop) | $(ACP) $(SOONG_ZIP)
 	@echo Packaging SDK Addon System-Image: $@
 	$(hide) mkdir -p $(dir $@)
 	$(ACP) -r $(PRODUCT_OUT)/data $(PRIVATE_STAGING_DIR)/data
-	$(hide) ( F=$$(pwd)/$@ ; cd $(PRIVATE_STAGING_DIR)/.. && zip -rqX $$F $(notdir $(PRIVATE_STAGING_DIR)) )
+	$(hide) $(SOONG_ZIP) -o $@ -C $(dir $(PRIVATE_STAGING_DIR)) -D $(PRIVATE_STAGING_DIR)
 
 
 .PHONY: sdk_addon
diff --git a/core/tasks/tools/build_custom_image.mk b/core/tasks/tools/build_custom_image.mk
index 719279c..7c38546 100644
--- a/core/tasks/tools/build_custom_image.mk
+++ b/core/tasks/tools/build_custom_image.mk
@@ -62,12 +62,16 @@
   $(eval my_image_copy_files += $(src))\
   $(eval my_copy_pairs += $(src):$(my_staging_dir)/$(word 2,$(pair))))
 
-ifndef CUSTOM_IMAGE_AVB_KEY_PATH
-# If key path isn't specified, use the default signing args.
-my_avb_signing_args := $(INTERNAL_AVB_SIGNING_ARGS)
-else
-my_avb_signing_args := \
-  --algorithm $(CUSTOM_IMAGE_AVB_ALGORITHM) --key $(CUSTOM_IMAGE_AVB_KEY_PATH)
+ifdef CUSTOM_IMAGE_AVB_KEY_PATH
+ifndef CUSTOM_IMAGE_AVB_ALGORITHM
+  $(error CUSTOM_IMAGE_AVB_ALGORITHM is not defined)
+endif
+ifndef CUSTOM_IMAGE_AVB_ROLLBACK_INDEX
+  $(error CUSTOM_IMAGE_AVB_ROLLBACK_INDEX is not defined)
+endif
+# set rollback_index via footer args
+CUSTOM_IMAGE_AVB_ADD_HASH_FOOTER_ARGS += --rollback_index $(CUSTOM_IMAGE_AVB_ROLLBACK_INDEX)
+CUSTOM_IMAGE_AVB_ADD_HASHTREE_FOOTER_ARGS += --rollback_index $(CUSTOM_IMAGE_AVB_ROLLBACK_INDEX)
 endif
 
 $(my_built_custom_image): PRIVATE_INTERMEDIATES := $(intermediates)
@@ -84,7 +88,8 @@
 $(my_built_custom_image): PRIVATE_VERITY_BLOCK_DEVICE := $(CUSTOM_IMAGE_VERITY_BLOCK_DEVICE)
 $(my_built_custom_image): PRIVATE_DICT_FILE := $(CUSTOM_IMAGE_DICT_FILE)
 $(my_built_custom_image): PRIVATE_AVB_AVBTOOL := $(AVBTOOL)
-$(my_built_custom_image): PRIVATE_AVB_SIGNING_ARGS := $(my_avb_signing_args)
+$(my_built_custom_image): PRIVATE_AVB_KEY_PATH := $(CUSTOM_IMAGE_AVB_KEY_PATH)
+$(my_built_custom_image): PRIVATE_AVB_ALGORITHM:= $(CUSTOM_IMAGE_AVB_ALGORITHM)
 $(my_built_custom_image): PRIVATE_AVB_HASH_ENABLE := $(CUSTOM_IMAGE_AVB_HASH_ENABLE)
 $(my_built_custom_image): PRIVATE_AVB_ADD_HASH_FOOTER_ARGS := $(CUSTOM_IMAGE_AVB_ADD_HASH_FOOTER_ARGS)
 $(my_built_custom_image): PRIVATE_AVB_HASHTREE_ENABLE := $(CUSTOM_IMAGE_AVB_HASHTREE_ENABLE)
@@ -130,8 +135,11 @@
 	    echo "verity_block_device=$(PRIVATE_VERITY_BLOCK_DEVICE)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
 	$(if $(PRIVATE_SUPPORT_VERITY_FEC),\
 	  $(hide) echo "verity_fec=$(PRIVATE_SUPPORT_VERITY_FEC)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
+	$(if $(filter eng, $(TARGET_BUILD_VARIANT)),$(hide) echo "verity_disable=true" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
 	$(hide) echo "avb_avbtool=$(PRIVATE_AVB_AVBTOOL)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt
-	$(hide) echo "avb_signing_args=$(PRIVATE_AVB_SIGNING_ARGS)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt
+	$(if $(PRIVATE_AVB_KEY_PATH),\
+	  $(hide) echo "avb_key_path=$(PRIVATE_AVB_KEY_PATH)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt;\
+	    echo "avb_algorithm=$(PRIVATE_AVB_ALGORITHM)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
 	$(if $(PRIVATE_AVB_HASH_ENABLE),\
 	  $(hide) echo "avb_hash_enable=$(PRIVATE_AVB_HASH_ENABLE)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt;\
 	    echo "avb_add_hash_footer_args=$(PRIVATE_AVB_ADD_HASH_FOOTER_ARGS)" >> $(PRIVATE_INTERMEDIATES)/image_info.txt)
diff --git a/core/tasks/tradefed-tests-list.mk b/core/tasks/tradefed-tests-list.mk
new file mode 100644
index 0000000..3b5f5da
--- /dev/null
+++ b/core/tasks/tradefed-tests-list.mk
@@ -0,0 +1,36 @@
+# Copyright (C) 2017 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.
+
+# List all TradeFed tests from COMPATIBILITY.tradefed_tests_dir
+.PHONY: tradefed-tests-list
+
+tradefed_tests :=
+$(foreach dir, $(COMPATIBILITY.tradefed_tests_dir), \
+  $(eval tradefed_tests += $(shell find $(dir) -type f -name "*.xml")))
+tradefed_tests_list_intermediates := $(call intermediates-dir-for,PACKAGING,tradefed_tests_list,HOST,COMMON)
+tradefed_tests_list_zip := $(tradefed_tests_list_intermediates)/tradefed-tests-list.zip
+all_tests :=
+$(foreach test, $(tradefed_tests), \
+  $(eval all_tests += $(word 2,$(subst /res/config/,$(space),$(test)))))
+$(tradefed_tests_list_zip) : PRIVATE_tradefed_tests := $(subst .xml,,$(subst $(space),\n,$(sort $(all_tests))))
+$(tradefed_tests_list_zip) : PRIVATE_tradefed_tests_list := $(tradefed_tests_list_intermediates)/tradefed-tests-list
+
+$(tradefed_tests_list_zip) : $(tradefed_tests) $(SOONG_ZIP)
+	@echo "Package: $@"
+	$(hide) rm -rf $(dir $@) && mkdir -p $(dir $@)
+	$(hide) echo -e "$(PRIVATE_tradefed_tests)" > $(PRIVATE_tradefed_tests_list)
+	$(hide) $(SOONG_ZIP) -d -o $@ -C $(dir $@) -f $(PRIVATE_tradefed_tests_list)
+
+tradefed-tests-list : $(tradefed_tests_list_zip)
+$(call dist-for-goals, tradefed-tests-list, $(tradefed_tests_list_zip))
diff --git a/core/tasks/vndk.mk b/core/tasks/vndk.mk
new file mode 100644
index 0000000..962fae1
--- /dev/null
+++ b/core/tasks/vndk.mk
@@ -0,0 +1,207 @@
+# Copyright (C) 2017 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.
+
+current_makefile := $(lastword $(MAKEFILE_LIST))
+
+# BOARD_VNDK_VERSION must be set to 'current' in order to generate a VNDK snapshot.
+ifeq ($(BOARD_VNDK_VERSION),current)
+
+# Returns arch-specific libclang_rt.ubsan* library name.
+# Because VNDK_CORE_LIBRARIES includes all arch variants for libclang_rt.ubsan*
+# libs, the arch-specific libs are selected separately.
+#
+# Args:
+#   $(1): if not empty, evaluates for TARGET_2ND_ARCH
+define clang-ubsan-vndk-core
+$(strip \
+  $(eval prefix := $(if $(1),2ND_,)) \
+  $(addsuffix .vendor,$($(addprefix $(prefix),UBSAN_RUNTIME_LIBRARY))) \
+)
+endef
+
+# Returns list of file paths of the intermediate objs
+#
+# Args:
+#   $(1): list of obj names (e.g., libfoo.vendor, ld.config.txt, ...)
+#   $(2): target class (e.g., SHARED_LIBRARIES, STATIC_LIBRARIES, ETC)
+#   $(3): if not empty, evaluates for TARGET_2ND_ARCH
+define paths-of-intermediates
+$(strip \
+  $(foreach obj,$(1), \
+    $(eval file_name := $(if $(filter SHARED_LIBRARIES,$(2)),$(patsubst %.so,%,$(obj)).so,$(obj))) \
+    $(eval dir := $(call intermediates-dir-for,$(2),$(obj),,,$(3))) \
+    $(call append-path,$(dir),$(file_name)) \
+  ) \
+)
+endef
+
+# If in the future libclang_rt.ubsan* is removed from the VNDK-core list,
+# need to update the related logic in this file.
+ifeq (,$(filter libclang_rt.ubsan%,$(VNDK_CORE_LIBRARIES)))
+  $(warning libclang_rt.ubsan* is no longer a VNDK-core library. Please update this file.)
+  vndk_core_libs := $(addsuffix .vendor,$(VNDK_CORE_LIBRARIES))
+else
+  vndk_core_libs := $(addsuffix .vendor,$(filter-out libclang_rt.ubsan%,$(VNDK_CORE_LIBRARIES)))
+
+  # for TARGET_ARCH
+  vndk_core_libs += $(call clang-ubsan-vndk-core)
+
+  # TODO(b/69834489): Package additional arch variants
+  # ifdef TARGET_2ND_ARCH
+  #   vndk_core_libs += $(call clang-ubsan-vndk-core,true)
+  # endif
+endif
+
+vndk_sp_libs := $(addsuffix .vendor,$(VNDK_SAMEPROCESS_LIBRARIES))
+vndk_private_libs := $(addsuffix .vendor,$(VNDK_PRIVATE_LIBRARIES))
+
+vndk_snapshot_libs := \
+  $(vndk_core_libs) \
+  $(vndk_sp_libs)
+
+vndk_prebuilt_txts := \
+  ld.config.txt \
+  vndksp.libraries.txt \
+  llndk.libraries.txt
+
+vndk_snapshot_top := $(call intermediates-dir-for,PACKAGING,vndk-snapshot)
+vndk_snapshot_out := $(vndk_snapshot_top)/vndk-snapshot
+vndk_snapshot_configs_out := $(vndk_snapshot_top)/configs
+
+#######################################
+# vndkcore.libraries.txt
+vndkcore.libraries.txt := $(vndk_snapshot_configs_out)/vndkcore.libraries.txt
+$(vndkcore.libraries.txt): $(vndk_core_libs)
+	@echo 'Generating: $@'
+	@rm -f $@
+	@mkdir -p $(dir $@)
+	$(hide) echo -n > $@
+	$(hide) $(foreach lib,$^,echo $(patsubst %.vendor,%,$(lib)).so >> $@;)
+
+
+#######################################
+# vndkprivate.libraries.txt
+vndkprivate.libraries.txt := $(vndk_snapshot_configs_out)/vndkprivate.libraries.txt
+$(vndkprivate.libraries.txt): $(vndk_private_libs)
+	@echo 'Generating: $@'
+	@rm -f $@
+	@mkdir -p $(dir $@)
+	$(hide) echo -n > $@
+	$(hide) $(foreach lib,$^,echo $(patsubst %.vendor,%,$(lib)).so >> $@;)
+
+
+vndk_snapshot_configs := \
+  $(vndkcore.libraries.txt) \
+  $(vndkprivate.libraries.txt)
+
+#######################################
+# vndk_snapshot_zip
+vndk_snapshot_arch := $(vndk_snapshot_out)/arch-$(TARGET_ARCH)-$(TARGET_ARCH_VARIANT)
+vndk_snapshot_zip := $(PRODUCT_OUT)/android-vndk-$(TARGET_ARCH).zip
+
+$(vndk_snapshot_zip): PRIVATE_VNDK_SNAPSHOT_OUT := $(vndk_snapshot_out)
+
+$(vndk_snapshot_zip): PRIVATE_VNDK_CORE_OUT := $(vndk_snapshot_arch)/shared/vndk-core
+$(vndk_snapshot_zip): PRIVATE_VNDK_CORE_INTERMEDIATES := \
+  $(call paths-of-intermediates,$(vndk_core_libs),SHARED_LIBRARIES)
+
+$(vndk_snapshot_zip): PRIVATE_VNDK_SP_OUT := $(vndk_snapshot_arch)/shared/vndk-sp
+$(vndk_snapshot_zip): PRIVATE_VNDK_SP_INTERMEDIATES := \
+  $(call paths-of-intermediates,$(vndk_sp_libs),SHARED_LIBRARIES)
+
+$(vndk_snapshot_zip): PRIVATE_CONFIGS_OUT := $(vndk_snapshot_arch)/configs
+$(vndk_snapshot_zip): PRIVATE_CONFIGS_INTERMEDIATES := \
+  $(call paths-of-intermediates,$(vndk_prebuilt_txts),ETC) \
+  $(vndk_snapshot_configs)
+
+# TODO(b/69834489): Package additional arch variants
+# ifdef TARGET_2ND_ARCH
+# vndk_snapshot_arch_2ND := $(vndk_snapshot_out)/arch-$(TARGET_2ND_ARCH)-$(TARGET_2ND_ARCH_VARIANT)
+# $(vndk_snapshot_zip): PRIVATE_VNDK_CORE_OUT_2ND := $(vndk_snapshot_arch_2ND)/shared/vndk-core
+# $(vndk_snapshot_zip): PRIVATE_VNDK_CORE_INTERMEDIATES_2ND := \
+#   $(call paths-of-intermediates,$(vndk_core_libs),SHARED_LIBRARIES,true)
+# $(vndk_snapshot_zip): PRIVATE_VNDK_SP_OUT_2ND := $(vndk_snapshot_arch_2ND)/shared/vndk-sp
+# $(vndk_snapshot_zip): PRIVATE_VNDK_SP_INTERMEDIATES_2ND := \
+#   $(call paths-of-intermediates,$(vndk_sp_libs),SHARED_LIBRARIES,true)
+# endif
+
+# Args
+#   $(1): destination directory
+#   $(2): list of files to copy
+$(vndk_snapshot_zip): private-copy-vndk-intermediates = \
+  $(if $(2),$(strip \
+    @mkdir -p $(1); \
+    $(foreach file,$(2), \
+      if [ -e $(file) ]; then \
+        cp -p $(file) $(call append-path,$(1),$(subst .vendor,,$(notdir $(file)))); \
+      fi; \
+    ) \
+  ))
+
+vndk_snapshot_dependencies := \
+  $(vndk_snapshot_libs) \
+  $(vndk_prebuilt_txts) \
+  $(vndk_snapshot_configs)
+
+$(vndk_snapshot_zip): $(vndk_snapshot_dependencies) $(SOONG_ZIP)
+	@echo 'Generating VNDK snapshot: $@'
+	@rm -f $@
+	@rm -rf $(PRIVATE_VNDK_SNAPSHOT_OUT)
+	@mkdir -p $(PRIVATE_VNDK_SNAPSHOT_OUT)
+	$(call private-copy-vndk-intermediates, \
+		$(PRIVATE_VNDK_CORE_OUT),$(PRIVATE_VNDK_CORE_INTERMEDIATES))
+	$(call private-copy-vndk-intermediates, \
+		$(PRIVATE_VNDK_SP_OUT),$(PRIVATE_VNDK_SP_INTERMEDIATES))
+	$(call private-copy-vndk-intermediates, \
+		$(PRIVATE_CONFIGS_OUT),$(PRIVATE_CONFIGS_INTERMEDIATES))
+# TODO(b/69834489): Package additional arch variants
+# ifdef TARGET_2ND_ARCH
+# 	$(call private-copy-vndk-intermediates, \
+# 		$(PRIVATE_VNDK_CORE_OUT_2ND),$(PRIVATE_VNDK_CORE_INTERMEDIATES_2ND))
+# 	$(call private-copy-vndk-intermediates, \
+# 		$(PRIVATE_VNDK_SP_OUT_2ND),$(PRIVATE_VNDK_SP_INTERMEDIATES_2ND))
+# endif
+	$(hide) $(SOONG_ZIP) -o $@ -C $(PRIVATE_VNDK_SNAPSHOT_OUT) -D $(PRIVATE_VNDK_SNAPSHOT_OUT)
+
+.PHONY: vndk
+vndk: $(vndk_snapshot_zip)
+
+$(call dist-for-goals, vndk, $(vndk_snapshot_zip))
+
+# clear global vars
+clang-ubsan-vndk-core :=
+paths-of-intermediates :=
+vndk_core_libs :=
+vndk_sp_libs :=
+vndk_snapshot_libs :=
+vndk_prebuilt_txts :=
+vndk_snapshot_configs :=
+vndk_snapshot_top :=
+vndk_snapshot_out :=
+vndk_snapshot_configs_out :=
+vndk_snapshot_arch :=
+vndk_snapshot_dependencies :=
+# TODO(b/69834489): Package additional arch variants
+# ifdef TARGET_2ND_ARCH
+# vndk_snapshot_arch_2ND :=
+# endif
+
+else # BOARD_VNDK_VERSION is NOT set to 'current'
+
+.PHONY: vndk
+vndk:
+	$(call echo-error,$(current_makefile),CANNOT generate VNDK snapshot. BOARD_VNDK_VERSION must be set to 'current'.)
+	exit 1
+
+endif # BOARD_VNDK_VERSION
diff --git a/core/test_config_common.mk b/core/test_config_common.mk
new file mode 100644
index 0000000..487f9f2
--- /dev/null
+++ b/core/test_config_common.mk
@@ -0,0 +1,56 @@
+#
+# Copyright (C) 2017 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.
+#
+
+LOCAL_MODULE_CLASS := TEST_CONFIG
+
+# Output test config files to testcases directory.
+ifeq (,$(filter general-tests, $(LOCAL_COMPATIBILITY_SUITE)))
+  LOCAL_COMPATIBILITY_SUITE += general-tests
+endif
+
+LOCAL_MODULE_SUFFIX := .config
+
+my_test_config_file := $(wildcard $(LOCAL_PATH)/$(LOCAL_MODULE).xml)
+LOCAL_SRC_FILES :=
+
+include $(BUILD_SYSTEM)/base_rules.mk
+
+# The test config is not in a standalone XML file.
+ifndef my_test_config_file
+
+ifndef LOCAL_TEST_CONFIG_OPTIONS
+  $(call pretty-error,LOCAL_TEST_CONFIG_OPTIONS must be set if the test XML file is not provided.)
+endif
+
+my_base_test_config_file := $(LOCAL_PATH)/AndroidTest.xml
+my_test_config_file := $(dir $(LOCAL_BUILT_MODULE))AndroidTest.xml
+
+$(my_test_config_file) : PRIVATE_test_config_options := $(LOCAL_TEST_CONFIG_OPTIONS)
+$(my_test_config_file) : $(my_base_test_config_file)
+	@echo "Create $(notdir $@) with options: $(PRIVATE_test_config_options)."
+	$(eval _option_xml := \
+		$(foreach option,$(PRIVATE_test_config_options), \
+			$(eval p := $(subst :,$(space),$(option))) \
+			<option name="$(word 1,$(p))" value="$(word 2,$(p))" \/>\n))
+	$(hide) sed 's&</configuration>&$(_option_xml)</configuration>&' $< > $@
+
+endif # my_test_config_file
+
+$(LOCAL_BUILT_MODULE) : $(my_test_config_file)
+	$(copy-file-to-target)
+
+my_base_test_config_file :=
+my_test_config_file :=
diff --git a/core/version_defaults.mk b/core/version_defaults.mk
index 23fb68e..da799db 100644
--- a/core/version_defaults.mk
+++ b/core/version_defaults.mk
@@ -38,9 +38,9 @@
   include $(INTERNAL_BUILD_ID_MAKEFILE)
 endif
 
-DEFAULT_PLATFORM_VERSION := OPM1
-MIN_PLATFORM_VERSION := OPM1
-MAX_PLATFORM_VERSION := OPM1
+DEFAULT_PLATFORM_VERSION := PPR1
+MIN_PLATFORM_VERSION := PPR1
+MAX_PLATFORM_VERSION := PPR1
 
 ALLOWED_VERSIONS := $(call allowed-platform-versions,\
   $(MIN_PLATFORM_VERSION),\
@@ -74,11 +74,11 @@
 # please add that PLATFORM_VERSION as well as clean up obsolete PLATFORM_VERSION's
 # in the following text file:
 # cts/tests/tests/os/assets/platform_versions.txt
-PLATFORM_VERSION.OPM1 := 8.1.0
+PLATFORM_VERSION.PPR1 := P
 
 # These are the current development codenames, if the build is not a final
 # release build.  If this is a final release build, it is simply "REL".
-PLATFORM_VERSION_CODENAME.OPM1 := REL
+PLATFORM_VERSION_CODENAME.PPR1 := P
 
 ifndef PLATFORM_VERSION
   PLATFORM_VERSION := $(PLATFORM_VERSION.$(TARGET_PLATFORM_VERSION))
@@ -108,15 +108,6 @@
   PLATFORM_SDK_VERSION := 27
 endif
 
-ifndef PLATFORM_JACK_MIN_SDK_VERSION
-  # This is definition of the min SDK version given to Jack for the current
-  # platform. For released version it should be the same as
-  # PLATFORM_SDK_VERSION. During development, this number may be incremented
-  # before PLATFORM_SDK_VERSION if the platform starts to add new java
-  # language supports.
-  PLATFORM_JACK_MIN_SDK_VERSION := o-b1
-endif
-
 ifndef PLATFORM_VERSION_CODENAME
   PLATFORM_VERSION_CODENAME := $(PLATFORM_VERSION_CODENAME.$(TARGET_PLATFORM_VERSION))
   ifndef PLATFORM_VERSION_CODENAME
@@ -124,14 +115,17 @@
     PLATFORM_VERSION_CODENAME := $(TARGET_PLATFORM_VERSION)
   endif
 
-  # This is all of the development codenames that are active.  Should be either
-  # the same as PLATFORM_VERSION_CODENAME or a comma-separated list of additional
-  # codenames after PLATFORM_VERSION_CODENAME.
+  # This is all of the *active* development codenames. There are future
+  # codenames not included in this list. This confusing name is needed because
+  # all_codenames has been baked into build.prop for ages.
+  #
+  # Should be either the same as PLATFORM_VERSION_CODENAME or a comma-separated
+  # list of additional codenames after PLATFORM_VERSION_CODENAME.
   PLATFORM_VERSION_ALL_CODENAMES :=
 
-  # Build a list of all possible code names. Avoid duplicates, and stop when we
+  # Build a list of all active code names. Avoid duplicates, and stop when we
   # reach a codename that matches PLATFORM_VERSION_CODENAME (anything beyond
-  # that is not included in our build.
+  # that is not included in our build).
   _versions_in_target := \
     $(call find_and_earlier,$(ALL_VERSIONS),$(TARGET_PLATFORM_VERSION))
   $(foreach version,$(_versions_in_target),\
@@ -139,9 +133,23 @@
     $(if $(filter $(_codename),$(PLATFORM_VERSION_ALL_CODENAMES)),,\
       $(eval PLATFORM_VERSION_ALL_CODENAMES += $(_codename))))
 
+  # This is all of the inactive development codenames. Available to be targeted
+  # in this branch but in the future relative to our current target.
+  PLATFORM_VERSION_FUTURE_CODENAMES :=
+
+  # Build a list of all untargeted code names. Avoid duplicates.
+  _versions_not_in_target := \
+    $(filter-out $(PLATFORM_VERSION_ALL_CODENAMES),$(ALL_VERSIONS))
+  $(foreach version,$(_versions_not_in_target),\
+    $(eval _codename := $(PLATFORM_VERSION_CODENAME.$(version)))\
+    $(if $(filter $(_codename),$(PLATFORM_VERSION_FUTURE_CODENAMES)),,\
+      $(eval PLATFORM_VERSION_FUTURE_CODENAMES += $(_codename))))
+
   # And convert from space separated to comma separated.
   PLATFORM_VERSION_ALL_CODENAMES := \
     $(subst $(space),$(comma),$(strip $(PLATFORM_VERSION_ALL_CODENAMES)))
+  PLATFORM_VERSION_FUTURE_CODENAMES := \
+    $(subst $(space),$(comma),$(strip $(PLATFORM_VERSION_FUTURE_CODENAMES)))
 
 endif
 
@@ -207,7 +215,7 @@
   BUILD_DATETIME := $(shell date +%s)
 endif
 
-ifneq (,$(findstring Darwin,$(shell uname -sm)))
+ifneq (,$(findstring Darwin,$(UNAME)))
 DATE := date -r $(BUILD_DATETIME)
 else
 DATE := date -d @$(BUILD_DATETIME)
diff --git a/envsetup.sh b/envsetup.sh
index 4cbe3e7..394df65 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -1,7 +1,13 @@
 function hmm() {
 cat <<EOF
+
+Run "m help" for help with the build system itself.
+
 Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
 - lunch:     lunch <product_name>-<build_variant>
+             Selects <product_name> as the product to build, and <build_variant> as the variant to
+             build, and stores those selections in the environment to be read by subsequent
+             invocations of 'm' etc.
 - tapas:     tapas [<App1> <App2> ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
 - croot:     Changes directory to the top of the tree.
 - m:         Makes from the top of the tree.
@@ -45,20 +51,18 @@
     cached_vars=`cat $T/build/envsetup.sh | tr '()' '  ' | awk '{for(i=1;i<=NF;i++) if($i~/get_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`
     cached_abs_vars=`cat $T/build/envsetup.sh | tr '()' '  ' | awk '{for(i=1;i<=NF;i++) if($i~/get_abs_build_var/) print $(i+1)}' | sort -u | tr '\n' ' '`
     # Call the build system to dump the "<val>=<value>" pairs as a shell script.
-    build_dicts_script=`\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
-                        command make --no-print-directory -f build/core/config.mk \
-                        dump-many-vars \
-                        DUMP_MANY_VARS="$cached_vars" \
-                        DUMP_MANY_ABS_VARS="$cached_abs_vars" \
-                        DUMP_VAR_PREFIX="var_cache_" \
-                        DUMP_ABS_VAR_PREFIX="abs_var_cache_"`
+    build_dicts_script=`\cd $T; build/soong/soong_ui.bash --dumpvars-mode \
+                        --vars="$cached_vars" \
+                        --abs-vars="$cached_abs_vars" \
+                        --var-prefix=var_cache_ \
+                        --abs-var-prefix=abs_var_cache_`
     local ret=$?
     if [ $ret -ne 0 ]
     then
         unset build_dicts_script
         return $ret
     fi
-    # Excute the script to store the "<val>=<value>" pairs as shell variables.
+    # Execute the script to store the "<val>=<value>" pairs as shell variables.
     eval "$build_dicts_script"
     ret=$?
     unset build_dicts_script
@@ -99,8 +103,7 @@
         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
         return
     fi
-    (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
-      command make --no-print-directory -f build/core/config.mk dumpvar-abs-$1)
+    (\cd $T; build/soong/soong_ui.bash --dumpvar-mode --abs $1)
 }
 
 # Get the exact value of a build variable.
@@ -117,8 +120,7 @@
         echo "Couldn't locate the top of the tree.  Try setting TOP." >&2
         return
     fi
-    (\cd $T; CALLED_FROM_SETUP=true BUILD_SYSTEM=build/core \
-      command make --no-print-directory -f build/core/config.mk dumpvar-$1)
+    (\cd $T; build/soong/soong_ui.bash --dumpvar-mode $1)
 }
 
 # check to see if the supplied product is one we can build
@@ -235,7 +237,12 @@
             ;;
     esac
 
-    export ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN:$ANDROID_TOOLCHAIN_2ND_ARCH:$ANDROID_DEV_SCRIPTS:
+    ANDROID_BUILD_PATHS=$(get_build_var ANDROID_BUILD_PATHS):$ANDROID_TOOLCHAIN
+    if [ -n "$ANDROID_TOOLCHAIN_2ND_ARCH" ]; then
+        ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_TOOLCHAIN_2ND_ARCH
+    fi
+    ANDROID_BUILD_PATHS=$ANDROID_BUILD_PATHS:$ANDROID_DEV_SCRIPTS:
+    export ANDROID_BUILD_PATHS
 
     # If prebuilts/android-emulator/<system>/ exists, prepend it to our PATH
     # to ensure that the corresponding 'emulator' binaries are used.
@@ -258,13 +265,11 @@
     export PATH=$ANDROID_BUILD_PATHS$PATH
     export PYTHONPATH=$T/development/python-packages:$PYTHONPATH
 
-    unset ANDROID_JAVA_TOOLCHAIN
-    unset ANDROID_PRE_BUILD_PATHS
-    if [ -n "$JAVA_HOME" ]; then
-        export ANDROID_JAVA_TOOLCHAIN=$JAVA_HOME/bin
-        export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
-        export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
-    fi
+    export ANDROID_JAVA_HOME=$(get_abs_build_var ANDROID_JAVA_HOME)
+    export JAVA_HOME=$ANDROID_JAVA_HOME
+    export ANDROID_JAVA_TOOLCHAIN=$(get_abs_build_var ANDROID_JAVA_TOOLCHAIN)
+    export ANDROID_PRE_BUILD_PATHS=$ANDROID_JAVA_TOOLCHAIN:
+    export PATH=$ANDROID_PRE_BUILD_PATHS$PATH
 
     unset ANDROID_PRODUCT_OUT
     export ANDROID_PRODUCT_OUT=$(get_abs_build_var PRODUCT_OUT)
@@ -297,7 +302,6 @@
 function set_stuff_for_environment()
 {
     settitle
-    set_java_home
     setpaths
     set_sequence_number
 
@@ -314,7 +318,12 @@
 
 function settitle()
 {
-    if [ "$STAY_OFF_MY_LAWN" = "" ]; then
+    # This used to be opt-out with STAY_OFF_MY_LAWN, but this breaks folks
+    # actually using PROMPT_COMMAND (https://issuetracker.google.com/38402256),
+    # and the attempt to set the title doesn't do anything for the default
+    # window manager in debian right now, so switch it to opt-in for anyone
+    # who actually wants this.
+    if [ "$ANDROID_BUILD_SET_WINDOW_TITLE" = "true" ]; then
         local arch=$(gettargetarch)
         local product=$TARGET_PRODUCT
         local variant=$TARGET_BUILD_VARIANT
@@ -651,11 +660,17 @@
 # Run tapas with one or more app names (from LOCAL_PACKAGE_NAME)
 function tapas()
 {
+    local showHelp="$(echo $* | xargs -n 1 echo | \grep -E '^(help)$' | xargs)"
     local arch="$(echo $* | xargs -n 1 echo | \grep -E '^(arm|x86|mips|armv5|arm64|x86_64|mips64)$' | xargs)"
     local variant="$(echo $* | xargs -n 1 echo | \grep -E '^(user|userdebug|eng)$' | xargs)"
     local density="$(echo $* | xargs -n 1 echo | \grep -E '^(ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
     local apps="$(echo $* | xargs -n 1 echo | \grep -E -v '^(user|userdebug|eng|arm|x86|mips|armv5|arm64|x86_64|mips64|ldpi|mdpi|tvdpi|hdpi|xhdpi|xxhdpi|xxxhdpi|alldpi)$' | xargs)"
 
+    if [ "$showHelp" != "" ]; then
+      $(gettop)/build/make/tapasHelp.sh
+      return
+    fi
+
     if [ $(echo $arch | wc -w) -gt 1 ]; then
         echo "tapas: Error: Multiple build archs supplied: $arch"
         return
@@ -702,7 +717,7 @@
 
 function gettop
 {
-    local TOPFILE=build/core/envsetup.mk
+    local TOPFILE=build/make/core/envsetup.mk
     if [ -n "$TOP" -a -f "$TOP/$TOPFILE" ] ; then
         # The following circumlocution ensures we remove symlinks from TOP.
         (cd $TOP; PWD= /bin/pwd)
@@ -736,7 +751,7 @@
         # Use scan-build to collect all static analyzer reports into directory
         # /tmp/scan-build-yyyy-mm-dd-hhmmss-*
         # The clang compiler passed by --use-analyzer here is not important.
-        # build/core/binary.mk will set CLANG_CXX and CLANG before calling
+        # build/make/core/binary.mk will set CLANG_CXX and CLANG before calling
         # c++-analyzer and ccc-analyzer.
         local CLANG_VERSION=$(get_build_var LLVM_PREBUILTS_VERSION)
         local BUILD_OS=$(get_build_var BUILD_OS)
@@ -762,7 +777,7 @@
 
 function findmakefile()
 {
-    local TOPFILE=build/core/envsetup.mk
+    local TOPFILE=build/make/core/envsetup.mk
     local HERE=$PWD
     local T=
     while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
@@ -972,7 +987,7 @@
 
 function cproj()
 {
-    local TOPFILE=build/core/envsetup.mk
+    local TOPFILE=build/make/core/envsetup.mk
     local HERE=$PWD
     local T=
     while [ \( ! \( -f $TOPFILE \) \) -a \( $PWD != "/" \) ]; do
@@ -1540,46 +1555,6 @@
     \cd $T/$pathname
 }
 
-# Force JAVA_HOME to point to java 1.7/1.8 if it isn't already set.
-function set_java_home() {
-    # Clear the existing JAVA_HOME value if we set it ourselves, so that
-    # we can reset it later, depending on the version of java the build
-    # system needs.
-    #
-    # If we don't do this, the JAVA_HOME value set by the first call to
-    # build/envsetup.sh will persist forever.
-    if [ -n "$ANDROID_SET_JAVA_HOME" ]; then
-      export JAVA_HOME=""
-    fi
-
-    if [ ! "$JAVA_HOME" ]; then
-      if [ -n "$LEGACY_USE_JAVA7" ]; then
-        echo Warning: Support for JDK 7 will be dropped. Switch to JDK 8.
-        case `uname -s` in
-            Darwin)
-                export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
-                ;;
-            *)
-                export JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64
-                ;;
-        esac
-      else
-        case `uname -s` in
-            Darwin)
-                export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
-                ;;
-            *)
-                export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
-                ;;
-        esac
-      fi
-
-      # Keep track of the fact that we set JAVA_HOME ourselves, so that
-      # we can change it on the next envsetup.sh, if required.
-      export ANDROID_SET_JAVA_HOME=true
-    fi
-}
-
 # Print colored exit condition
 function pez {
     "$@"
@@ -1597,6 +1572,13 @@
 {
     # If we're in the top of an Android tree, use soong_ui.bash instead of make
     if [ -f build/soong/soong_ui.bash ]; then
+        # Always use the real make if -C is passed in
+        for arg in "$@"; do
+            if [[ $arg == -C* ]]; then
+                echo command make
+                return
+            fi
+        done
         echo build/soong/soong_ui.bash --make-mode
     else
         echo command make
@@ -1643,7 +1625,7 @@
 
 function make()
 {
-    _wrap_build $(get_make_command) "$@"
+    _wrap_build $(get_make_command "$@") "$@"
 }
 
 function provision()
@@ -1675,6 +1657,14 @@
     "$ANDROID_PRODUCT_OUT/provision-device" "$@"
 }
 
+function atest()
+{
+    # TODO (sbasi): Replace this to be a destination in the build out when & if
+    # atest is built by the build system. (This will be necessary if it ever
+    # depends on external pip projects).
+    "$(gettop)"/tools/tradefederation/core/atest/atest.py "$@"
+}
+
 if [ "x$SHELL" != "x/bin/bash" ]; then
     case `ps -o command -p $$` in
         *bash*)
diff --git a/help.sh b/help.sh
new file mode 100755
index 0000000..3f39c77
--- /dev/null
+++ b/help.sh
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+# locate some directories
+cd "$(dirname $0)"
+SCRIPT_DIR="${PWD}"
+cd ../..
+TOP="${PWD}"
+
+message='The basic Android build process is:
+
+cd '"${TOP}"'
+source build/envsetup.sh    # Add "lunch" (and other utilities and variables)
+                            # to the shell environment.
+lunch [<product>-<variant>] # Choose the device to target.
+m -j [<goals>]              # Execute the configured build.
+
+Usage of "m" imitates usage of the program "make".
+See '"${SCRIPT_DIR}"'/Usage.txt for more info about build usage and concepts.
+
+Common goals are:
+
+    clean                   (aka clobber) equivalent to rm -rf out/
+    checkbuild              Build every module defined in the source tree
+    droid                   Default target
+    nothing                 Do not build anything, just parse and validate the build structure
+
+    java                    Build all the java code in the source tree
+    native                  Build all the native code in the source tree
+
+    host                    Build all the host code (not to be run on a device) in the source tree
+    target                  Build all the target code (to be run on the device) in the source tree
+
+    (java|native)-(host|target)
+    (host|target)-(java|native)
+                            Build the intersection of the two given arguments
+
+    snod                    Quickly rebuild the system image from built packages
+                            Stands for "System, NO Dependencies"
+    vnod                    Quickly rebuild the vendor image from built packages
+                            Stands for "Vendor, NO Dependencies"
+
+
+So, for example, you could run:
+
+cd '"${TOP}"'
+source build/envsetup.sh
+lunch aosp_arm-userdebug
+m -j java
+
+to build all of the java code for the userdebug variant of the aosp_arm device.
+'
+
+echo "$message"
diff --git a/navbar.md b/navbar.md
new file mode 100644
index 0000000..e218a78
--- /dev/null
+++ b/navbar.md
@@ -0,0 +1,4 @@
+* [Home](/README.md)
+* [Usage](/Usage.txt)
+* [Changes](/Changes.md)
+* [Outdated Reference](/core/build-system.html)
diff --git a/tapasHelp.sh b/tapasHelp.sh
new file mode 100755
index 0000000..058ac1d
--- /dev/null
+++ b/tapasHelp.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+# locate some directories
+cd "$(dirname $0)"
+SCRIPT_DIR="${PWD}"
+cd ../..
+TOP="${PWD}"
+
+message='usage: tapas [<App1> <App2> ...] [arm|x86|mips|armv5|arm64|x86_64|mips64] [eng|userdebug|user]
+
+tapas selects individual apps to be built by the Android build system. Unlike
+"lunch", "tapas" does not request the building of images for a device.
+Additionally, an app built with "tapas" will have its dex file inside its apk,
+which should cause it to be suitable for installing on any api-compatible
+device. In other words, "tapas" configures the build of unbundled apps.
+
+The names <App1> <App2> ... should match LOCAL_PACKAGE_NAME as defined in an
+Android.mk
+
+The usage of the other arguments matches that of the rest of the platform
+build system and can be found by running `m help`'
+
+echo "$message"
diff --git a/tools/signtos/Android.mk b/target/Android.mk
similarity index 60%
rename from tools/signtos/Android.mk
rename to target/Android.mk
index 94ab944..9929b00 100644
--- a/tools/signtos/Android.mk
+++ b/target/Android.mk
@@ -1,5 +1,5 @@
 #
-# Copyright (C) 2014 The Android Open Source Project
+# Copyright (C) 2017 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.
@@ -13,13 +13,11 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+
 LOCAL_PATH := $(call my-dir)
 
-# the signtos tool - signs Trusty images
-# ============================================================
-include $(CLEAR_VARS)
-LOCAL_MODULE := signtos
-LOCAL_SRC_FILES := SignTos.java
-LOCAL_JAR_MANIFEST := SignTos.mf
-LOCAL_STATIC_JAVA_LIBRARIES := bouncycastle-host bouncycastle-bcpkix-host
-include $(BUILD_HOST_JAVA_LIBRARY)
+# Only if this Android.mk was included not by a symlink should it be used.
+# This facilitates the transition away from symlinks: b/64397960
+ifeq ($(LOCAL_PATH),build/make/target)
+include $(call first-makefiles-under,$(LOCAL_PATH))
+endif
diff --git a/target/board/Android.mk b/target/board/Android.mk
index 5504c52..4b5af3c 100644
--- a/target/board/Android.mk
+++ b/target/board/Android.mk
@@ -78,9 +78,7 @@
 
 $(GEN): PRIVATE_FLAGS :=
 
-# TODO(b/37954458), (b/37321309) remove check of PRODUCT_FULL_TREBLE after
-# putting device compatibility matrices for non-treble devices.
-ifeq ($(PRODUCT_FULL_TREBLE),true)
+ifeq ($(PRODUCT_ENFORCE_VINTF_MANIFEST),true)
 ifdef BUILT_VENDOR_MATRIX
 $(GEN): $(BUILT_VENDOR_MATRIX)
 $(GEN): PRIVATE_FLAGS += -c "$(BUILT_VENDOR_MATRIX)"
@@ -105,9 +103,7 @@
 
 $(GEN): PRIVATE_FLAGS :=
 
-# TODO(b/37954458), (b/37321309) remove check of PRODUCT_FULL_TREBLE after
-# putting complete HAL manifests on non-treble devices.
-ifeq ($(PRODUCT_FULL_TREBLE),true)
+ifeq ($(PRODUCT_ENFORCE_VINTF_MANIFEST),true)
 ifdef BUILT_VENDOR_MANIFEST
 $(GEN): $(BUILT_VENDOR_MANIFEST)
 $(GEN): PRIVATE_FLAGS += -c "$(BUILT_VENDOR_MANIFEST)"
@@ -132,7 +128,7 @@
 
 # All kernel versions that the system image works with.
 KERNEL_VERSIONS := 3.18 4.4 4.9
-KERNEL_CONFIG_DATA := test/vts-testcase/kernel/config/data
+KERNEL_CONFIG_DATA := kernel/configs
 
 $(GEN): $(foreach version,$(KERNEL_VERSIONS),\
 	$(wildcard $(KERNEL_CONFIG_DATA)/android-$(version)/android-base*.cfg))
diff --git a/target/board/generic/BoardConfig.mk b/target/board/generic/BoardConfig.mk
index 331f082..8d93f75 100644
--- a/target/board/generic/BoardConfig.mk
+++ b/target/board/generic/BoardConfig.mk
@@ -57,11 +57,13 @@
 # when finalizing them.
 BOARD_VENDORIMAGE_PARTITION_SIZE := 100000000
 BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE := ext4
-BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
-BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
 BOARD_FLASH_BLOCK_SIZE := 512
 TARGET_USERIMAGES_SPARSE_EXT_DISABLED := true
 DEVICE_MATRIX_FILE   := device/generic/goldfish/compatibility_matrix.xml
 
 BOARD_SEPOLICY_DIRS += build/target/board/generic/sepolicy
 BOARD_PROPERTY_OVERRIDES_SPLIT_ENABLED := true
+
+# Enable A/B update
+TARGET_NO_RECOVERY := true
+BOARD_BUILD_SYSTEM_ROOT_IMAGE := true
diff --git a/target/board/generic/sepolicy/OWNERS b/target/board/generic/sepolicy/OWNERS
new file mode 100644
index 0000000..4bd7e34
--- /dev/null
+++ b/target/board/generic/sepolicy/OWNERS
@@ -0,0 +1,6 @@
+nnk@google.com
+jeffv@google.com
+klyubin@google.com
+dcashman@google.com
+jbires@google.com
+sspatil@google.com
diff --git a/target/board/generic/sepolicy/bootanim.te b/target/board/generic/sepolicy/bootanim.te
index 4be1c8a..b23e1ca 100644
--- a/target/board/generic/sepolicy/bootanim.te
+++ b/target/board/generic/sepolicy/bootanim.te
@@ -2,4 +2,8 @@
 allow bootanim ashmem_device:chr_file execute;
 #TODO: This can safely be ignored until b/62954877 is fixed
 dontaudit bootanim system_data_file:dir read;
+
+allow bootanim vendor_file:file { execute getattr open read };
+allow bootanim graphics_device:chr_file { read ioctl open };
+
 set_prop(bootanim, qemu_prop)
diff --git a/target/board/generic/sepolicy/domain.te b/target/board/generic/sepolicy/domain.te
index 5d5e4ac..3706dba 100644
--- a/target/board/generic/sepolicy/domain.te
+++ b/target/board/generic/sepolicy/domain.te
@@ -1,6 +1,3 @@
-# For /sys/qemu_trace files in the emulator.
-allow domain sysfs_writable:dir search;
-allow domain sysfs_writable:file rw_file_perms;
 allow domain qemu_device:chr_file rw_file_perms;
 
 get_prop(domain, qemu_prop)
diff --git a/target/board/generic/sepolicy/file.te b/target/board/generic/sepolicy/file.te
deleted file mode 100644
index f4ae9e4..0000000
--- a/target/board/generic/sepolicy/file.te
+++ /dev/null
@@ -1 +0,0 @@
-type sysfs_writable, fs_type, sysfs_type, mlstrustedobject;
diff --git a/target/board/generic/sepolicy/file_contexts b/target/board/generic/sepolicy/file_contexts
index f550f4d..521c65e 100644
--- a/target/board/generic/sepolicy/file_contexts
+++ b/target/board/generic/sepolicy/file_contexts
@@ -15,7 +15,6 @@
 /dev/qemu_.*                 u:object_r:qemu_device:s0
 /dev/ttyGF[0-9]*             u:object_r:serial_device:s0
 /dev/ttyS2                   u:object_r:console_device:s0
-/sys/qemu_trace(/.*)?        u:object_r:sysfs_writable:s0
 /vendor/bin/init\.ranchu-core\.sh u:object_r:goldfish_setup_exec:s0
 /vendor/bin/init\.ranchu-net\.sh u:object_r:goldfish_setup_exec:s0
 /vendor/bin/qemu-props       u:object_r:qemu_props_exec:s0
@@ -23,6 +22,7 @@
 /vendor/bin/hw/android\.hardware\.drm@1\.0-service\.widevine          u:object_r:hal_drm_widevine_exec:s0
 
 /vendor/lib(64)?/hw/gralloc\.ranchu\.so   u:object_r:same_process_hal_file:s0
+/vendor/lib(64)?/hw/gralloc\.goldfish\.default\.so   u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/libEGL_emulation\.so          u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/libGLESv1_CM_emulation\.so    u:object_r:same_process_hal_file:s0
 /vendor/lib(64)?/libGLESv2_emulation\.so       u:object_r:same_process_hal_file:s0
diff --git a/target/board/generic/sepolicy/genfs_contexts b/target/board/generic/sepolicy/genfs_contexts
new file mode 100644
index 0000000..bdcead1
--- /dev/null
+++ b/target/board/generic/sepolicy/genfs_contexts
@@ -0,0 +1,4 @@
+# On the emulator, device tree dir is configured to be
+# /sys/bus/platform/devices/ANDR0001:00/properties/android/ which is a symlink to
+# /sys/devices/platform/ANDR0001:00/properties/android/
+genfscon sysfs /devices/platform/ANDR0001:00/properties/android u:object_r:sysfs_dt_firmware_android:s0
diff --git a/target/board/generic/sepolicy/goldfish_setup.te b/target/board/generic/sepolicy/goldfish_setup.te
index bcd49bd..eb913e9 100644
--- a/target/board/generic/sepolicy/goldfish_setup.te
+++ b/target/board/generic/sepolicy/goldfish_setup.te
@@ -4,6 +4,7 @@
 
 init_daemon_domain(goldfish_setup)
 
+set_prop(goldfish_setup, debug_prop);
 allow goldfish_setup self:capability { net_admin net_raw };
 allow goldfish_setup self:udp_socket { create ioctl };
 allow goldfish_setup vendor_toolbox_exec:file execute_no_trans;
diff --git a/target/board/generic/sepolicy/hal_graphics_allocator_default.te b/target/board/generic/sepolicy/hal_graphics_allocator_default.te
new file mode 100644
index 0000000..0c8e27d
--- /dev/null
+++ b/target/board/generic/sepolicy/hal_graphics_allocator_default.te
@@ -0,0 +1,2 @@
+allow hal_graphics_allocator_default graphics_device:dir search;
+allow hal_graphics_allocator_default graphics_device:chr_file { ioctl open read write };
diff --git a/target/board/generic_arm64/BoardConfig.mk b/target/board/generic_arm64/BoardConfig.mk
index 46c8865..9d2ccbd 100644
--- a/target/board/generic_arm64/BoardConfig.mk
+++ b/target/board/generic_arm64/BoardConfig.mk
@@ -25,7 +25,7 @@
 TARGET_2ND_CPU_ABI := armeabi-v7a
 TARGET_2ND_CPU_ABI2 := armeabi
 
-ifneq ($(TARGET_BUILD_APPS)$(filter cts,$(MAKECMDGOALS)),)
+ifneq ($(TARGET_BUILD_APPS)$(filter cts sdk,$(MAKECMDGOALS)),)
 # DO NOT USE
 # DO NOT USE
 #
@@ -35,11 +35,11 @@
 # platforms.
 #
 # If you're building a 64 bit platform (and not an application) the
-# ARM-v8 specification allows you to assume NEON and all the features
-# available in a cortex-A15 CPU. You should be able to set :
+# ARM-v8 specification allows you to assume all the features available in an
+# armv7-a-neon CPU. You should set the following as 2nd arch/cpu variant:
 #
-# TARGET_2ND_ARCH_VARIANT := armv7-a-neon
-# TARGET_2ND_CPU_VARIANT := cortex-a15
+# TARGET_2ND_ARCH_VARIANT := armv8-a
+# TARGET_2ND_CPU_VARIANT := generic
 #
 # DO NOT USE
 # DO NOT USE
@@ -50,8 +50,8 @@
 # DO NOT USE
 # DO NOT USE
 else
-TARGET_2ND_ARCH_VARIANT := armv7-a-neon
-TARGET_2ND_CPU_VARIANT := cortex-a15
+TARGET_2ND_ARCH_VARIANT := armv8-a
+TARGET_2ND_CPU_VARIANT := generic
 endif
 
 
@@ -88,11 +88,13 @@
 # when finalizing them.
 BOARD_VENDORIMAGE_PARTITION_SIZE := 100000000
 BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE := ext4
-BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
-BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
 BOARD_FLASH_BLOCK_SIZE := 512
 TARGET_USERIMAGES_SPARSE_EXT_DISABLED := true
 DEVICE_MATRIX_FILE   := device/generic/goldfish/compatibility_matrix.xml
 
 BOARD_PROPERTY_OVERRIDES_SPLIT_ENABLED := true
 BOARD_SEPOLICY_DIRS += build/target/board/generic/sepolicy
+
+# Enable A/B update
+TARGET_NO_RECOVERY := true
+BOARD_BUILD_SYSTEM_ROOT_IMAGE := true
diff --git a/target/board/generic_arm64_ab/BoardConfig.mk b/target/board/generic_arm64_ab/BoardConfig.mk
index 6744289..e0d7372 100644
--- a/target/board/generic_arm64_ab/BoardConfig.mk
+++ b/target/board/generic_arm64_ab/BoardConfig.mk
@@ -39,3 +39,5 @@
 # TODO(b/36764215): remove this setting when the generic system image
 # no longer has QCOM-specific directories under /.
 BOARD_SEPOLICY_DIRS += build/target/board/generic_arm64_ab/sepolicy
+
+BOARD_VNDK_VERSION := current
diff --git a/target/board/generic_arm64_ab/sepolicy/OWNERS b/target/board/generic_arm64_ab/sepolicy/OWNERS
new file mode 100644
index 0000000..4bd7e34
--- /dev/null
+++ b/target/board/generic_arm64_ab/sepolicy/OWNERS
@@ -0,0 +1,6 @@
+nnk@google.com
+jeffv@google.com
+klyubin@google.com
+dcashman@google.com
+jbires@google.com
+sspatil@google.com
diff --git a/target/board/generic_mips/BoardConfig.mk b/target/board/generic_mips/BoardConfig.mk
index fb66d21..523408b 100644
--- a/target/board/generic_mips/BoardConfig.mk
+++ b/target/board/generic_mips/BoardConfig.mk
@@ -65,10 +65,12 @@
 # when finalizing them.
 BOARD_VENDORIMAGE_PARTITION_SIZE := 100000000
 BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE := ext4
-BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
-BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
 BOARD_FLASH_BLOCK_SIZE := 512
 TARGET_USERIMAGES_SPARSE_EXT_DISABLED := true
 DEVICE_MATRIX_FILE   := device/generic/goldfish/compatibility_matrix.xml
 
 BOARD_SEPOLICY_DIRS += build/target/board/generic/sepolicy
+
+# Enable A/B update
+TARGET_NO_RECOVERY := true
+BOARD_BUILD_SYSTEM_ROOT_IMAGE := true
diff --git a/target/board/generic_mips64/BoardConfig.mk b/target/board/generic_mips64/BoardConfig.mk
index 67bb51f..2052d7b 100644
--- a/target/board/generic_mips64/BoardConfig.mk
+++ b/target/board/generic_mips64/BoardConfig.mk
@@ -80,8 +80,6 @@
 # when finalizing them.
 BOARD_VENDORIMAGE_PARTITION_SIZE := 100000000
 BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE := ext4
-BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
-BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
 BOARD_FLASH_BLOCK_SIZE := 512
 TARGET_USERIMAGES_SPARSE_EXT_DISABLED := true
 DEVICE_MATRIX_FILE   := device/generic/goldfish/compatibility_matrix.xml
@@ -89,3 +87,7 @@
 BOARD_SEPOLICY_DIRS += build/target/board/generic/sepolicy
 
 DEX_PREOPT_DEFAULT := nostripping
+
+# Enable A/B update
+TARGET_NO_RECOVERY := true
+BOARD_BUILD_SYSTEM_ROOT_IMAGE := true
diff --git a/target/board/generic_x86/BoardConfig.mk b/target/board/generic_x86/BoardConfig.mk
index 000a9a3..0b39af1 100644
--- a/target/board/generic_x86/BoardConfig.mk
+++ b/target/board/generic_x86/BoardConfig.mk
@@ -48,8 +48,6 @@
 # when finalizing them.
 BOARD_VENDORIMAGE_PARTITION_SIZE := 100000000
 BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE := ext4
-BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
-BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
 BOARD_FLASH_BLOCK_SIZE := 512
 TARGET_USERIMAGES_SPARSE_EXT_DISABLED := true
 DEVICE_MATRIX_FILE   := device/generic/goldfish/compatibility_matrix.xml
@@ -57,3 +55,7 @@
 BOARD_SEPOLICY_DIRS += \
         build/target/board/generic/sepolicy \
         build/target/board/generic_x86/sepolicy
+
+# Enable A/B update
+TARGET_NO_RECOVERY := true
+BOARD_BUILD_SYSTEM_ROOT_IMAGE := true
diff --git a/target/board/generic_x86/sepolicy/OWNERS b/target/board/generic_x86/sepolicy/OWNERS
new file mode 100644
index 0000000..4bd7e34
--- /dev/null
+++ b/target/board/generic_x86/sepolicy/OWNERS
@@ -0,0 +1,6 @@
+nnk@google.com
+jeffv@google.com
+klyubin@google.com
+dcashman@google.com
+jbires@google.com
+sspatil@google.com
diff --git a/target/board/generic_x86_64/BoardConfig.mk b/target/board/generic_x86_64/BoardConfig.mk
index 883dd2e..3a4bde6 100755
--- a/target/board/generic_x86_64/BoardConfig.mk
+++ b/target/board/generic_x86_64/BoardConfig.mk
@@ -50,8 +50,6 @@
 # when finalizing them.
 BOARD_VENDORIMAGE_PARTITION_SIZE := 100000000
 BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE := ext4
-BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
-BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
 BOARD_FLASH_BLOCK_SIZE := 512
 TARGET_USERIMAGES_SPARSE_EXT_DISABLED := true
 BOARD_PROPERTY_OVERRIDES_SPLIT_ENABLED := true
@@ -60,3 +58,7 @@
 BOARD_SEPOLICY_DIRS += \
         build/target/board/generic/sepolicy \
         build/target/board/generic_x86/sepolicy
+
+# Enable A/B update
+TARGET_NO_RECOVERY := true
+BOARD_BUILD_SYSTEM_ROOT_IMAGE := true
diff --git a/target/board/treble_common.mk b/target/board/treble_common.mk
index fd3363c..1ba6726 100644
--- a/target/board/treble_common.mk
+++ b/target/board/treble_common.mk
@@ -57,8 +57,6 @@
 # Set emulator framebuffer display device buffer count to 3
 NUM_FRAMEBUFFER_SURFACE_BUFFERS := 3
 
-# TODO(b/35790399): remove when b/35790399 is fixed.
-BOARD_NAND_SPARE_SIZE := 0
 BOARD_FLASH_BLOCK_SIZE := 512
 
 # b/64700195: add minimum support for odm.img
diff --git a/target/product/aosp_mips.mk b/target/product/aosp_mips.mk
index a76b93a..5ee6185 100644
--- a/target/product/aosp_mips.mk
+++ b/target/product/aosp_mips.mk
@@ -1,5 +1,5 @@
 #
-# Copyright 2013 The Android Open-Source Project
+# Copyright 2017 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.
@@ -13,6 +13,19 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
+
+PRODUCT_PROPERTY_OVERRIDES += \
+	rild.libpath=/vendor/lib/libreference-ril.so
+
+# Note: the following lines need to stay at the beginning so that it can
+# take priority  and override the rules it inherit from other mk files
+# see copy file rules in core/Makefile
+PRODUCT_COPY_FILES += \
+    development/sys-img/advancedFeatures.ini.arm:advancedFeatures.ini \
+    prebuilts/qemu-kernel/mips/3.18/kernel-qemu2:kernel-ranchu \
+    device/generic/goldfish/fstab.ranchu.mips:root/fstab.ranchu \
+    device/generic/goldfish/fstab.ranchu.early.arm:root/fstab.ranchu.early
+
 include $(SRC_TARGET_DIR)/product/full_mips.mk
 
 PRODUCT_NAME := aosp_mips
diff --git a/target/product/aosp_mips64.mk b/target/product/aosp_mips64.mk
index f606858..73d3731 100644
--- a/target/product/aosp_mips64.mk
+++ b/target/product/aosp_mips64.mk
@@ -1,5 +1,5 @@
 #
-# Copyright 2013 The Android Open-Source Project
+# Copyright 2017 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.
@@ -14,14 +14,22 @@
 # limitations under the License.
 #
 
+PRODUCT_PROPERTY_OVERRIDES += \
+	rild.libpath=/vendor/lib64/libreference-ril.so
+
 # This is a build configuration for a full-featured build of the
 # Open-Source part of the tree. It's geared toward a US-centric
-# mips64 build quite specifically for the emulator, and might not be
+# build quite specifically for the emulator, and might not be
 # entirely appropriate to inherit from for on-device configurations.
 
-# This is for enabling ethernet support for ranchu.
-# Consider removing this after RIL support is provided in ranchu.
-PRODUCT_COPY_FILES += frameworks/native/data/etc/android.hardware.ethernet.xml:system/etc/permissions/android.hardware.ethernet.xml
+# Note: the following lines need to stay at the beginning so that it can
+# take priority  and override the rules it inherit from other mk files
+# see copy file rules in core/Makefile
+PRODUCT_COPY_FILES += \
+    development/sys-img/advancedFeatures.ini.arm:advancedFeatures.ini \
+    prebuilts/qemu-kernel/mips64/3.18/kernel-qemu2:kernel-ranchu \
+    device/generic/goldfish/fstab.ranchu.mips:root/fstab.ranchu \
+    device/generic/goldfish/fstab.ranchu.early.arm:root/fstab.ranchu.early
 
 $(call inherit-product, $(SRC_TARGET_DIR)/product/core_64_bit.mk)
 $(call inherit-product, $(SRC_TARGET_DIR)/product/aosp_base_telephony.mk)
diff --git a/target/product/base.mk b/target/product/base.mk
index 9e38623..c3eb3b2 100644
--- a/target/product/base.mk
+++ b/target/product/base.mk
@@ -147,8 +147,10 @@
 
 # Packages included only for eng or userdebug builds, previously debug tagged
 PRODUCT_PACKAGES_DEBUG := \
+    logpersist.start \
     perfprofd \
-    sqlite3
+    sqlite3 \
+    strace
 
 PRODUCT_COPY_FILES := $(call add-to-product-copy-files-if-exists,\
     frameworks/base/config/preloaded-classes:system/etc/preloaded-classes)
diff --git a/target/product/core.mk b/target/product/core.mk
index a2b0f1c..cab8d97 100644
--- a/target/product/core.mk
+++ b/target/product/core.mk
@@ -45,7 +45,6 @@
     Launcher2 \
     ManagedProvisioning \
     MtpDocumentsProvider \
-    PicoTts \
     PacProcessor \
     libpac \
     PrintSpooler \
diff --git a/target/product/core_minimal.mk b/target/product/core_minimal.mk
index ab65f5b..b252349 100644
--- a/target/product/core_minimal.mk
+++ b/target/product/core_minimal.mk
@@ -60,7 +60,6 @@
     iptables \
     gatekeeperd \
     keystore \
-    keystore.default \
     ld.config.txt \
     ld.mc \
     libaaudio \
@@ -77,7 +76,7 @@
     libwebviewchromium_plat_support \
     libwilhelm \
     logd \
-    make_ext4fs \
+    mke2fs \
     e2fsck \
     resize2fs \
     tune2fs \
@@ -135,7 +134,7 @@
 # Adoptable external storage supports both ext4 and f2fs
 PRODUCT_PACKAGES += \
     e2fsck \
-    make_ext4fs \
+    mke2fs \
     fsck.f2fs \
     make_f2fs \
 
@@ -157,9 +156,13 @@
 
 # On userdebug builds, collect more tombstones by default.
 ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
-PRODUCT_DEFAULT_PROPERTY_OVERRIDES += \
+PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
     tombstoned.max_tombstone_count=50
 endif
 
+PRODUCT_DEFAULT_PROPERTY_OVERRIDES += \
+    ro.logd.size.stats=64K \
+    log.tag.stats_log=I
+
 $(call inherit-product, $(SRC_TARGET_DIR)/product/runtime_libart.mk)
 $(call inherit-product, $(SRC_TARGET_DIR)/product/base.mk)
diff --git a/target/product/core_tiny.mk b/target/product/core_tiny.mk
index e473413..c86b862 100644
--- a/target/product/core_tiny.mk
+++ b/target/product/core_tiny.mk
@@ -59,7 +59,6 @@
     iptables \
     gatekeeperd \
     keystore \
-    keystore.default \
     ld.mc \
     libaaudio \
     libOpenMAXAL \
@@ -71,7 +70,7 @@
     libwilhelm \
     libdrmframework_jni \
     libdrmframework \
-    make_ext4fs \
+    mke2fs \
     e2fsck \
     resize2fs \
     tune2fs \
diff --git a/target/product/embedded.mk b/target/product/embedded.mk
index 7efa686..246a553 100644
--- a/target/product/embedded.mk
+++ b/target/product/embedded.mk
@@ -86,20 +86,10 @@
     system_manifest.xml \
     system_compatibility_matrix.xml \
 
-# SELinux packages
+# SELinux packages are added as dependencies of the selinux_policy
+# phony package.
 PRODUCT_PACKAGES += \
-    nonplat_mac_permissions.xml \
-    nonplat_property_contexts \
-    nonplat_seapp_contexts \
-    nonplat_service_contexts \
-    nonplat_hwservice_contexts \
-    plat_mac_permissions.xml \
-    plat_property_contexts \
-    plat_seapp_contexts \
-    plat_service_contexts \
-    plat_hwservice_contexts \
     selinux_policy \
-    vndservice_contexts
 
 # AID Generation for
 # <pwd.h> and <grp.h>
@@ -109,9 +99,14 @@
     fs_config_files \
     fs_config_dirs
 
+# If there are product-specific adb keys defined, install them on debuggable
+# builds.
+PRODUCT_PACKAGES_DEBUG += \
+    adb_keys
+
 # Ensure that this property is always defined so that bionic_systrace.cpp
 # can rely on it being initially set by init.
-PRODUCT_DEFAULT_PROPERTY_OVERRIDES += \
+PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
     debug.atrace.tags.enableflags=0
 
 PRODUCT_COPY_FILES += \
diff --git a/target/product/emulator.mk b/target/product/emulator.mk
index 0e1e581..856ba1a 100644
--- a/target/product/emulator.mk
+++ b/target/product/emulator.mk
@@ -26,6 +26,7 @@
 PRODUCT_PACKAGES += \
     egl.cfg \
     gralloc.goldfish \
+    gralloc.goldfish.default \
     gralloc.ranchu \
     libGLESv1_CM_emulation \
     lib_renderControl_enc \
@@ -35,6 +36,9 @@
     libOpenglSystemCommon \
     libGLESv2_emulation \
     libGLESv1_enc \
+    libEGL_swiftshader \
+    libGLESv1_CM_swiftshader \
+    libGLESv2_swiftshader \
     qemu-props \
     camera.goldfish \
     camera.goldfish.jpeg \
@@ -107,31 +111,7 @@
 # need this for gles libraries to load properly
 # after moving to /vendor/lib/
 PRODUCT_PACKAGES += \
-    android.hardware.renderscript@1.0.vndk-sp\
-    android.hardware.graphics.allocator@2.0.vndk-sp\
-    android.hardware.graphics.mapper@2.0.vndk-sp\
-    android.hardware.graphics.common@1.0.vndk-sp\
-    libhwbinder.vndk-sp\
-    libbase.vndk-sp\
-    libcutils.vndk-sp\
-    libhardware.vndk-sp\
-    libhidlbase.vndk-sp\
-    libhidltransport.vndk-sp\
-    libutils.vndk-sp\
-    libc++.vndk-sp\
-    libRS_internal.vndk-sp\
-    libRSDriver.vndk-sp\
-    libRSCpuRef.vndk-sp\
-    libbcinfo.vndk-sp\
-    libblas.vndk-sp\
-    libft2.vndk-sp\
-    libpng.vndk-sp\
-    libcompiler_rt.vndk-sp\
-    libbacktrace.vndk-sp\
-    libunwind.vndk-sp\
-    liblzma.vndk-sp\
-    libz.vndk-sp\
-
+    vndk-sp
 
 PRODUCT_COPY_FILES += \
     device/generic/goldfish/init.ranchu-core.sh:vendor/bin/init.ranchu-core.sh \
@@ -140,11 +120,11 @@
     device/generic/goldfish/fstab.ranchu:root/fstab.ranchu \
     device/generic/goldfish/fstab.ranchu.early:root/fstab.ranchu.early \
     device/generic/goldfish/ueventd.ranchu.rc:root/ueventd.ranchu.rc \
-    device/generic/goldfish/input/goldfish_rotary.idc:system/usr/idc/goldfish_rotary.idc \
+    device/generic/goldfish/input/goldfish_rotary.idc:$(TARGET_COPY_OUT_VENDOR)/usr/idc/goldfish_rotary.idc \
     device/generic/goldfish/manifest.xml:$(TARGET_COPY_OUT_VENDOR)/manifest.xml \
-    device/generic/goldfish/data/etc/permissions/privapp-permissions-goldfish.xml:system/etc/permissions/privapp-permissions-goldfish.xml \
+    device/generic/goldfish/data/etc/permissions/privapp-permissions-goldfish.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/privapp-permissions-goldfish.xml \
     device/generic/goldfish/data/etc/config.ini:config.ini \
-    frameworks/native/data/etc/android.hardware.usb.accessory.xml:system/etc/permissions/android.hardware.usb.accessory.xml
+    frameworks/native/data/etc/android.hardware.usb.accessory.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.usb.accessory.xml
 
 PRODUCT_PACKAGE_OVERLAYS := device/generic/goldfish/overlay
 
diff --git a/target/product/full_base.mk b/target/product/full_base.mk
index 65bdf0f..7aac435 100644
--- a/target/product/full_base.mk
+++ b/target/product/full_base.mk
@@ -48,11 +48,11 @@
 # Get some sounds
 $(call inherit-product-if-exists, frameworks/base/data/sounds/AllAudio.mk)
 
-# Get the TTS language packs
-$(call inherit-product-if-exists, external/svox/pico/lang/all_pico_languages.mk)
-
 # Get a list of languages.
 $(call inherit-product, $(SRC_TARGET_DIR)/product/locales_full.mk)
 
 # Get everything else from the parent package
 $(call inherit-product, $(SRC_TARGET_DIR)/product/generic_no_telephony.mk)
+
+# Add adb keys to debuggable AOSP builds (if they exist)
+$(call inherit-product-if-exists, vendor/google/security/adb/vendor_key.mk)
diff --git a/target/product/full_base_telephony.mk b/target/product/full_base_telephony.mk
index 9a2c63a..375c679 100644
--- a/target/product/full_base_telephony.mk
+++ b/target/product/full_base_telephony.mk
@@ -25,7 +25,7 @@
 
 PRODUCT_COPY_FILES := \
     device/generic/goldfish/data/etc/apns-conf.xml:system/etc/apns-conf.xml \
-    frameworks/native/data/etc/handheld_core_hardware.xml:system/etc/permissions/handheld_core_hardware.xml
+    frameworks/native/data/etc/handheld_core_hardware.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/handheld_core_hardware.xml
 
 $(call inherit-product, $(SRC_TARGET_DIR)/product/aosp_base.mk)
 $(call inherit-product, $(SRC_TARGET_DIR)/product/telephony.mk)
diff --git a/target/product/runtime_libart.mk b/target/product/runtime_libart.mk
index 8aedee7..6e7038e 100644
--- a/target/product/runtime_libart.mk
+++ b/target/product/runtime_libart.mk
@@ -56,32 +56,38 @@
 PRODUCT_PACKAGES += \
     cacerts \
 
-PRODUCT_DEFAULT_PROPERTY_OVERRIDES += \
+PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
     dalvik.vm.image-dex2oat-Xms=64m \
     dalvik.vm.image-dex2oat-Xmx=64m \
     dalvik.vm.dex2oat-Xms=64m \
     dalvik.vm.dex2oat-Xmx=512m \
-    ro.dalvik.vm.native.bridge=0 \
     dalvik.vm.usejit=true \
     dalvik.vm.usejitprofiles=true \
     dalvik.vm.dexopt.secondary=true \
     dalvik.vm.appimageformat=lz4
 
+PRODUCT_PROPERTY_OVERRIDES += \
+    ro.dalvik.vm.native.bridge=0
+
 # Different dexopt types for different package update/install times.
 # On eng builds, make "boot" reasons only extract for faster turnaround.
 ifeq (eng,$(TARGET_BUILD_VARIANT))
-    PRODUCT_DEFAULT_PROPERTY_OVERRIDES += \
+    PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
         pm.dexopt.first-boot=extract \
         pm.dexopt.boot=extract
 else
-    PRODUCT_DEFAULT_PROPERTY_OVERRIDES += \
+    PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
         pm.dexopt.first-boot=quicken \
         pm.dexopt.boot=verify
 endif
 
-PRODUCT_DEFAULT_PROPERTY_OVERRIDES += \
+PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
     pm.dexopt.install=quicken \
     pm.dexopt.bg-dexopt=speed-profile \
     pm.dexopt.ab-ota=speed-profile \
     pm.dexopt.inactive=verify \
     pm.dexopt.shared=speed
+
+# Enable minidebuginfo generation unless overridden.
+PRODUCT_SYSTEM_DEFAULT_PROPERTIES += \
+    dalvik.vm.dex2oat-minidebuginfo=true
diff --git a/target/product/sdk_base.mk b/target/product/sdk_base.mk
index 6d4d59f..75a63e6 100644
--- a/target/product/sdk_base.mk
+++ b/target/product/sdk_base.mk
@@ -91,19 +91,19 @@
 	frameworks/base/data/sounds/effects/camera_click.ogg:system/media/audio/ui/camera_click.ogg \
 	frameworks/base/data/sounds/effects/VideoRecord.ogg:system/media/audio/ui/VideoRecord.ogg \
 	frameworks/base/data/sounds/effects/VideoStop.ogg:system/media/audio/ui/VideoStop.ogg \
-	device/generic/goldfish/data/etc/handheld_core_hardware.xml:system/etc/permissions/handheld_core_hardware.xml \
-	device/generic/goldfish/camera/media_profiles.xml:system/etc/media_profiles.xml \
-	frameworks/av/media/libstagefright/data/media_codecs_google_audio.xml:system/etc/media_codecs_google_audio.xml \
-	frameworks/av/media/libstagefright/data/media_codecs_google_telephony.xml:system/etc/media_codecs_google_telephony.xml \
-	device/generic/goldfish/camera/media_codecs_google_video.xml:system/etc/media_codecs_google_video.xml \
-	device/generic/goldfish/camera/media_codecs.xml:system/etc/media_codecs.xml \
-	device/generic/goldfish/camera/media_codecs_performance.xml:system/etc/media_codecs_performance.xml \
-	frameworks/native/data/etc/android.hardware.touchscreen.multitouch.jazzhand.xml:system/etc/permissions/android.hardware.touchscreen.multitouch.jazzhand.xml \
-	frameworks/native/data/etc/android.hardware.camera.xml:system/etc/permissions/android.hardware.camera.xml \
-	frameworks/native/data/etc/android.hardware.fingerprint.xml:system/etc/permissions/android.hardware.fingerprint.xml \
-	frameworks/native/data/etc/android.software.autofill.xml:system/etc/permissions/android.software.autofill.xml \
-	frameworks/av/media/libeffects/data/audio_effects.conf:system/etc/audio_effects.conf \
-	device/generic/goldfish/audio_policy.conf:system/etc/audio_policy.conf
+	device/generic/goldfish/data/etc/handheld_core_hardware.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/handheld_core_hardware.xml \
+	device/generic/goldfish/camera/media_profiles.xml:$(TARGET_COPY_OUT_VENDOR)/etc/media_profiles_V1_0.xml \
+	frameworks/av/media/libstagefright/data/media_codecs_google_audio.xml:$(TARGET_COPY_OUT_VENDOR)/etc/media_codecs_google_audio.xml \
+	frameworks/av/media/libstagefright/data/media_codecs_google_telephony.xml:$(TARGET_COPY_OUT_VENDOR)/etc/media_codecs_google_telephony.xml \
+	device/generic/goldfish/camera/media_codecs_google_video.xml:$(TARGET_COPY_OUT_VENDOR)/etc/media_codecs_google_video.xml \
+	device/generic/goldfish/camera/media_codecs.xml:$(TARGET_COPY_OUT_VENDOR)/etc/media_codecs.xml \
+	device/generic/goldfish/camera/media_codecs_performance.xml:$(TARGET_COPY_OUT_VENDOR)/etc/media_codecs_performance.xml \
+	frameworks/native/data/etc/android.hardware.touchscreen.multitouch.jazzhand.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.touchscreen.multitouch.jazzhand.xml \
+	frameworks/native/data/etc/android.hardware.camera.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.camera.xml \
+	frameworks/native/data/etc/android.hardware.fingerprint.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.hardware.fingerprint.xml \
+	frameworks/native/data/etc/android.software.autofill.xml:$(TARGET_COPY_OUT_VENDOR)/etc/permissions/android.software.autofill.xml \
+	frameworks/av/media/libeffects/data/audio_effects.conf:$(TARGET_COPY_OUT_VENDOR)/etc/audio_effects.conf \
+	device/generic/goldfish/audio_policy.conf:$(TARGET_COPY_OUT_VENDOR)/etc/audio_policy.conf
 
 include $(SRC_TARGET_DIR)/product/emulator.mk
 
@@ -119,14 +119,6 @@
 $(call inherit-product-if-exists, frameworks/webview/chromium/chromium.mk)
 $(call inherit-product, $(SRC_TARGET_DIR)/product/core.mk)
 
-# include available languages for TTS in the system image
--include external/svox/pico/lang/PicoLangDeDeInSystem.mk
--include external/svox/pico/lang/PicoLangEnGBInSystem.mk
--include external/svox/pico/lang/PicoLangEnUsInSystem.mk
--include external/svox/pico/lang/PicoLangEsEsInSystem.mk
--include external/svox/pico/lang/PicoLangFrFrInSystem.mk
--include external/svox/pico/lang/PicoLangItItInSystem.mk
-
 # locale. en_US is both first and in alphabetical order to
 # ensure this is the default locale.
 PRODUCT_LOCALES := \
diff --git a/target/product/security/Android.mk b/target/product/security/Android.mk
index 5a40397..4142ea9 100644
--- a/target/product/security/Android.mk
+++ b/target/product/security/Android.mk
@@ -10,3 +10,16 @@
 LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)
 
 include $(BUILD_PREBUILT)
+
+#######################################
+# adb key, if configured via PRODUCT_ADB_KEYS
+ifdef PRODUCT_ADB_KEYS
+  ifneq ($(filter eng userdebug,$(TARGET_BUILD_VARIANT)),)
+    include $(CLEAR_VARS)
+    LOCAL_MODULE := adb_keys
+    LOCAL_MODULE_CLASS := ETC
+    LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)
+    LOCAL_PREBUILT_MODULE_FILE := $(PRODUCT_ADB_KEYS)
+    include $(BUILD_PREBUILT)
+  endif
+endif
diff --git a/target/product/security/README b/target/product/security/README
index 15f2e93..6a6e62d 100644
--- a/target/product/security/README
+++ b/target/product/security/README
@@ -34,5 +34,5 @@
     $ java -jar out/host/linux-x86/framework/dumpkey.jar build/target/product/security/testkey.x509.pem
     {64,0xc926ad21,{1795090719,2141396315,950055447,2581568430,4268923165,1920809988,546586521,3498997798,1776797858,3740060814,1805317999,1429410244,129622599,1422441418,1783893377,1222374759,2563319927,323993566,28517732,609753416,1826472888,215237850,4261642700,4049082591,3228462402,774857746,154822455,2497198897,2758199418,3019015328,2794777644,87251430,2534927978,120774784,571297800,3695899472,2479925187,3811625450,3401832990,2394869647,3267246207,950095497,555058928,414729973,1136544882,3044590084,465547824,4058146728,2731796054,1689838846,3890756939,1048029507,895090649,247140249,178744550,3547885223,3165179243,109881576,3944604415,1044303212,3772373029,2985150306,3737520932,3599964420},{3437017481,3784475129,2800224972,3086222688,251333580,2131931323,512774938,325948880,2657486437,2102694287,3820568226,792812816,1026422502,2053275343,2800889200,3113586810,165549746,4273519969,4065247892,1902789247,772932719,3941848426,3652744109,216871947,3164400649,1942378755,3996765851,1055777370,964047799,629391717,2232744317,3910558992,191868569,2758883837,3682816752,2997714732,2702529250,3570700455,3776873832,3924067546,3555689545,2758825434,1323144535,61311905,1997411085,376844204,213777604,4077323584,9135381,1625809335,2804742137,2952293945,1117190829,4237312782,1825108855,3013147971,1111251351,2568837572,1684324211,2520978805,367251975,810756730,2353784344,1175080310}}
 
-This is called by build/core/Makefile to incorporate the OTA signing keys
+This is called by build/make/core/Makefile to incorporate the OTA signing keys
 into the recovery image.
diff --git a/target/product/verity.mk b/target/product/verity.mk
index 0badb9f..d954159 100644
--- a/target/product/verity.mk
+++ b/target/product/verity.mk
@@ -14,20 +14,16 @@
 # limitations under the License.
 #
 
-# Provides dependencies necessary for verified boot (only for user and
-# userdebug builds)
+# Provides dependencies necessary for verified boot.
 
-user_variant := $(filter user userdebug,$(TARGET_BUILD_VARIANT))
-ifneq (,$(user_variant))
-    PRODUCT_SUPPORTS_BOOT_SIGNER := true
-    PRODUCT_SUPPORTS_VERITY := true
-    PRODUCT_SUPPORTS_VERITY_FEC := true
+PRODUCT_SUPPORTS_BOOT_SIGNER := true
+PRODUCT_SUPPORTS_VERITY := true
+PRODUCT_SUPPORTS_VERITY_FEC := true
 
-    # The dev key is used to sign boot and recovery images, and the verity
-    # metadata table. Actual product deliverables will be re-signed by hand.
-    # We expect this file to exist with the suffixes ".x509.pem" and ".pk8".
-    PRODUCT_VERITY_SIGNING_KEY := build/target/product/security/verity
+# The dev key is used to sign boot and recovery images, and the verity
+# metadata table. Actual product deliverables will be re-signed by hand.
+# We expect this file to exist with the suffixes ".x509.pem" and ".pk8".
+PRODUCT_VERITY_SIGNING_KEY := build/target/product/security/verity
 
-    PRODUCT_PACKAGES += \
-            verity_key
-endif
+PRODUCT_PACKAGES += \
+        verity_key
diff --git a/target/product/vndk/Android.mk b/target/product/vndk/Android.mk
index c8ed873..d2d9562 100644
--- a/target/product/vndk/Android.mk
+++ b/target/product/vndk/Android.mk
@@ -1,8 +1,102 @@
+ifneq ($(BOARD_VNDK_VERSION),)
 LOCAL_PATH:= $(call my-dir)
 
-include $(CLEAR_VARS)
+#####################################################################
+# Setting the VNDK version. Version is 10000.0 for not-yet-published
+# platform and xx.y for released platform.
+vndk_major_ver := 10000
+vndk_minor_ver := 0
 
-LOCAL_MODULE := vndk_package
+#TODO(b/68027291): Revive this check when we have stable VNDK in P or later.
+#ifneq ($(vndk_major_ver), $(PLATFORM_SDK_VERSION))
+#$(error vndk_major_version does not match PLATFORM_SDK_VERSION, please update.)
+#endif
+
+ifneq (REL,$(PLATFORM_VERSION_CODENAME))
+    vndk_major_ver := 10000
+    vndk_minor_ver := 0
+endif
+PLATFORM_VNDK_VERSION := $(vndk_major_ver).$(vndk_minor_ver)
+vndk_major_ver :=
+vndk_minor_ver :=
+
+#####################################################################
+# Create the list of vndk libraries from the source code.
+INTERNAL_VNDK_LIB_LIST := $(call intermediates-dir-for,PACKAGING,vndk)/libs.txt
+$(INTERNAL_VNDK_LIB_LIST):
+	@echo "Generate: $@"
+	@mkdir -p $(dir $@)
+	$(hide) echo -n > $@
+	$(hide) $(foreach lib, $(LLNDK_LIBRARIES), \
+	  echo LLNDK: $(lib).so >> $@;)
+	$(hide) $(foreach lib, $(VNDK_SAMEPROCESS_LIBRARIES), \
+	  echo VNDK-SP: $(lib).so >> $@;)
+	$(hide) $(foreach lib, $(VNDK_CORE_LIBRARIES), \
+	  echo VNDK-core: $(lib).so >> $@;)
+	$(hide) $(foreach lib, $(VNDK_PRIVATE_LIBRARIES), \
+	  echo VNDK-private: $(lib).so >> $@;)
+
+#####################################################################
+# This is the up-to-date list of vndk libs.
+# TODO(b/62012285): the lib list should be stored somewhere under
+# /prebuilts/vndk
+LATEST_VNDK_LIB_LIST := $(LOCAL_PATH)/$(PLATFORM_VNDK_VERSION).txt
+
+#####################################################################
+# Check the generate list against the latest list stored in the
+# source tree
+.PHONY: check-vndk-list
+
+ifeq (REL,$(PLATFORM_VERSION_CODENAME))
+# The check is enforced in release branches
+droidcore: check-vndk-list
+endif
+
+check-vndk-list-timestamp := $(call intermediates-dir-for,PACKAGING,vndk)/check-list-timestamp
+check-vndk-list: $(check-vndk-list-timestamp)
+
+_vndk_check_failure_message := "VNDK library list has changed."
+ifeq (REL,$(PLATFORM_VERSION_CODENAME)
+_vndk_check_failure_message += "This isn't allowed in API locked branches."
+else
+_vndk_check_failure_message += "Run update-vndk-list.sh to update the list."
+endif
+
+$(check-vndk-list-timestamp): $(INTERNAL_VNDK_LIB_LIST) $(LATEST_VNDK_LIB_LIST) $(HOST_OUT_EXECUTABLES)/update-vndk-list.sh
+	$(hide) ( diff --old-line-format="Removed %L" \
+	  --new-line-format="Added %L" \
+	  --unchanged-line-format="" \
+	  $(LATEST_VNDK_LIB_LIST) $(INTERNAL_VNDK_LIB_LIST) \
+	  || ( echo $(_vndk_check_failure_message); exit 1 ))
+	$(hide) mkdir -p $(dir $@)
+	$(hide) touch $@
+
+#####################################################################
+# Script to update the latest VNDK lib list
+include $(CLEAR_VARS)
+LOCAL_MODULE := update-vndk-list.sh
+LOCAL_MODULE_CLASS := EXECUTABLES
+LOCAL_MODULE_STEM := $(LOCAL_MODULE)
+LOCAL_IS_HOST_MODULE := true
+include $(BUILD_SYSTEM)/base_rules.mk
+$(LOCAL_BUILT_MODULE): PRIVATE_INTERNAL_VNDK_LIB_LIST := $(INTERNAL_VNDK_LIB_LIST)
+$(LOCAL_BUILT_MODULE): PRIVATE_LATEST_VNDK_LIB_LIST := $(LATEST_VNDK_LIB_LIST)
+$(LOCAL_BUILT_MODULE):
+	@echo "Generate: $@"
+	@mkdir -p $(dir $@)
+	@rm -f $@
+	$(hide) echo "#!/bin/bash" > $@
+ifeq (REL,$(PLATFORM_VERSION_CODENAME))
+	$(hide) echo "echo Updating VNDK library list is NOT allowed in API locked branches." >> $@; \
+	        echo "exit 1" >> $@
+else
+	$(hide) echo "cp $(PRIVATE_INTERNAL_VNDK_LIB_LIST) $(PRIVATE_LATEST_VNDK_LIB_LIST)" >> $@; \
+	        echo "echo $(PRIVATE_LATEST_VNDK_LIB_LIST) updated." >> $@
+endif
+	@chmod a+x $@
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := vndk_current
 LOCAL_REQUIRED_MODULES := \
     $(addsuffix .vendor,$(VNDK_CORE_LIBRARIES)) \
     $(addsuffix .vendor,$(VNDK_SAMEPROCESS_LIBRARIES)) \
@@ -11,3 +105,17 @@
     vndksp.libraries.txt
 
 include $(BUILD_PHONY_PACKAGE)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := vndk_package
+ifeq (current,$(BOARD_VNDK_VERSION))
+LOCAL_REQUIRED_MODULES := \
+    vndk_current
+else
+LOCAL_REQUIRED_MODULES := \
+    vndk_v$(BOARD_VNDK_VERSION)
+endif
+LOCAL_REQUIRED_MODULES += \
+    $(foreach vndk_ver,$(PRODUCT_EXTRA_VNDK_VERSIONS),vndk_v$(vndk_ver))
+include $(BUILD_PHONY_PACKAGE)
+endif # BOARD_VNDK_VERSION is set
diff --git a/tools/Android.mk b/tools/Android.mk
index 9073ac3..c05d681 100644
--- a/tools/Android.mk
+++ b/tools/Android.mk
@@ -16,4 +16,8 @@
 
 LOCAL_PATH := $(call my-dir)
 
+# Only if this Android.mk was included not by a symlink should it be used.
+# This facilitates the transition away from symlinks: b/64397960
+ifeq ($(LOCAL_PATH),build/make/tools)
 include $(call all-makefiles-under,$(LOCAL_PATH))
+endif
diff --git a/tools/acp/Android.bp b/tools/acp/Android.bp
index faf2034..64f5a10 100644
--- a/tools/acp/Android.bp
+++ b/tools/acp/Android.bp
@@ -5,6 +5,7 @@
 cc_binary_host {
 
     srcs: ["acp.c"],
+    cflags: ["-Wall", "-Werror"],
 
     static_libs: ["libhost"],
     name: "acp",
diff --git a/tools/acp/acp.c b/tools/acp/acp.c
index eb1de1f..d4a9fbc 100644
--- a/tools/acp/acp.c
+++ b/tools/acp/acp.c
@@ -41,10 +41,9 @@
 int process(int argc, char* const argv[], unsigned int options)
 {
     int retVal = 0;
-    int i, cc;
+    int i;
     char* stripDest = NULL;
     int stripDestLen;
-    struct stat destStat;
     bool destMustBeDir = false;
     struct stat sb;
 
diff --git a/tools/apicheck/Android.mk b/tools/apicheck/Android.mk
index b547058..ab3493d 100644
--- a/tools/apicheck/Android.mk
+++ b/tools/apicheck/Android.mk
@@ -12,7 +12,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-ifneq ($(TARGET_BUILD_PDK),true)
 LOCAL_PATH := $(call my-dir)
 
 # the hat script
@@ -26,4 +25,3 @@
 include $(BUILD_PREBUILT)
 
 # Apicheck is now part of Doclava -- See external/doclava.
-endif
diff --git a/tools/apicheck/etc/apicheck b/tools/apicheck/etc/apicheck
index c710c46..e4ffb77 100644
--- a/tools/apicheck/etc/apicheck
+++ b/tools/apicheck/etc/apicheck
@@ -18,7 +18,7 @@
 # and set up progdir to be the fully-qualified pathname of its directory.
 #
 # The classpath and other java options used in apicheck are specified in
-# build/core/tasks/apicheck.mk.
+# build/make/core/tasks/apicheck.mk.
 
 prog="$0"
 while [ -h "${prog}" ]; do
diff --git a/tools/atree/Android.bp b/tools/atree/Android.bp
new file mode 100644
index 0000000..5fbe042
--- /dev/null
+++ b/tools/atree/Android.bp
@@ -0,0 +1,14 @@
+// Copyright 2007 The Android Open Source Project
+//
+// Copies files into the directory structure described by a manifest
+
+cc_binary_host {
+    name: "atree",
+    srcs: [
+        "atree.cpp",
+        "files.cpp",
+        "fs.cpp",
+    ],
+    cflags: ["-Wall", "-Werror"],
+    static_libs: ["libhost"],
+}
diff --git a/tools/atree/Android.mk b/tools/atree/Android.mk
deleted file mode 100644
index f598db5..0000000
--- a/tools/atree/Android.mk
+++ /dev/null
@@ -1,19 +0,0 @@
-# Copyright 2007 The Android Open Source Project
-#
-# Copies files into the directory structure described by a manifest
-
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
-	atree.cpp \
-	files.cpp \
-	fs.cpp
-
-LOCAL_STATIC_LIBRARIES := \
-	libhost
-
-LOCAL_MODULE := atree
-
-include $(BUILD_HOST_EXECUTABLE)
-
diff --git a/tools/atree/files.cpp b/tools/atree/files.cpp
index d945f58..d5c8a97 100644
--- a/tools/atree/files.cpp
+++ b/tools/atree/files.cpp
@@ -425,8 +425,6 @@
                 const vector<string>& excludes,
                 vector<FileRecord>* more)
 {
-    int err;
-
     string full = path_append(rec.sourceBase, rec.sourceName);
     full = path_append(full, path);
 
diff --git a/tools/checkowners.py b/tools/checkowners.py
index b874955..1190d30 100755
--- a/tools/checkowners.py
+++ b/tools/checkowners.py
@@ -34,8 +34,8 @@
                + urllib.quote(address))
     echo('Checking email address: ' + address)
     result = urllib2.urlopen(request).read()
-    expected = '"email": "' + address + '"'
-    checked_addresses[address] = (result.find(expected) >= 0)
+    checked_addresses[address] = (
+        result.find('"email":') >= 0 and result.find('"_account_id":') >= 0)
   return checked_addresses[address]
 
 
diff --git a/tools/fs_config/Android.bp b/tools/fs_config/Android.bp
new file mode 100644
index 0000000..797cfe2
--- /dev/null
+++ b/tools/fs_config/Android.bp
@@ -0,0 +1,63 @@
+// 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.
+
+cc_binary_host {
+    name: "fs_config",
+    srcs: ["fs_config.c"],
+    shared_libs: [
+        "libcutils",
+        "libselinux",
+    ],
+    cflags: ["-Werror"],
+}
+
+// -----------------------------------------------------------------------------
+// Unit tests.
+// -----------------------------------------------------------------------------
+
+test_c_flags = [
+    "-fstack-protector-all",
+    "-g",
+    "-Wall",
+    "-Wextra",
+    "-Werror",
+    "-fno-builtin",
+    "-DANDROID_FILESYSTEM_CONFIG=\"android_filesystem_config_test_data.h\"",
+]
+
+//#################################
+// test executable
+cc_test_host {
+    name: "fs_config_generate_test",
+    srcs: ["fs_config_generate.c"],
+    shared_libs: ["libcutils"],
+    cflags: test_c_flags,
+    relative_install_path: "fs_config-unit-tests",
+    no_named_install_directory: true,
+    gtest: false,
+
+}
+
+//#################################
+// gTest tool
+cc_test_host {
+    name: "fs_config-unit-tests",
+    cflags: test_c_flags + ["-DHOST"],
+    shared_libs: [
+        "liblog",
+        "libcutils",
+        "libbase",
+    ],
+    srcs: ["fs_config_test.cpp"],
+}
diff --git a/tools/fs_config/Android.mk b/tools/fs_config/Android.mk
index ab7f92d..3773d38 100644
--- a/tools/fs_config/Android.mk
+++ b/tools/fs_config/Android.mk
@@ -14,15 +14,6 @@
 
 LOCAL_PATH := $(call my-dir)
 
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := fs_config.c
-LOCAL_MODULE := fs_config
-LOCAL_SHARED_LIBRARIES := libcutils libselinux
-LOCAL_CFLAGS := -Werror
-
-include $(BUILD_HOST_EXECUTABLE)
-
 # One can override the default android_filesystem_config.h file in one of two ways:
 #
 # 1. The old way:
@@ -306,36 +297,3 @@
 fs_config_generate_bin :=
 my_gen_oem_aid :=
 fs_config_generate_extra_partition_list :=
-
-# -----------------------------------------------------------------------------
-# Unit tests.
-# -----------------------------------------------------------------------------
-
-test_c_flags := \
-    -fstack-protector-all \
-    -g \
-    -Wall \
-    -Wextra \
-    -Werror \
-    -fno-builtin \
-    -DANDROID_FILESYSTEM_CONFIG='"android_filesystem_config_test_data.h"'
-
-##################################
-# test executable
-include $(CLEAR_VARS)
-LOCAL_MODULE := fs_config_generate_test
-LOCAL_SRC_FILES := fs_config_generate.c
-LOCAL_SHARED_LIBRARIES := libcutils
-LOCAL_CFLAGS := $(test_c_flags)
-LOCAL_MODULE_RELATIVE_PATH := fs_config-unit-tests
-LOCAL_GTEST := false
-include $(BUILD_HOST_NATIVE_TEST)
-
-##################################
-# gTest tool
-include $(CLEAR_VARS)
-LOCAL_MODULE := fs_config-unit-tests
-LOCAL_CFLAGS += $(test_c_flags) -DHOST
-LOCAL_SHARED_LIBRARIES := liblog libcutils libbase
-LOCAL_SRC_FILES := fs_config_test.cpp
-include $(BUILD_HOST_NATIVE_TEST)
diff --git a/tools/fs_get_stats/Android.bp b/tools/fs_get_stats/Android.bp
new file mode 100644
index 0000000..67742b8
--- /dev/null
+++ b/tools/fs_get_stats/Android.bp
@@ -0,0 +1,9 @@
+cc_binary_host {
+    name: "fs_get_stats",
+    srcs: ["fs_get_stats.c"],
+    cflags: ["-Wall", "-Werror"],
+    shared_libs: [
+        "libcutils",
+        "liblog",
+    ],
+}
diff --git a/tools/fs_get_stats/Android.mk b/tools/fs_get_stats/Android.mk
deleted file mode 100644
index 4501c1f..0000000
--- a/tools/fs_get_stats/Android.mk
+++ /dev/null
@@ -1,11 +0,0 @@
-LOCAL_PATH:= $(call my-dir)
-
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := fs_get_stats.c
-
-LOCAL_MODULE := fs_get_stats
-
-LOCAL_SHARED_LIBRARIES := libcutils liblog
-
-include $(BUILD_HOST_EXECUTABLE)
diff --git a/tools/generate-notice-files.py b/tools/generate-notice-files.py
index adbf7c2..b754174 100755
--- a/tools/generate-notice-files.py
+++ b/tools/generate-notice-files.py
@@ -253,15 +253,12 @@
 
     filesets = [sorted(files_with_same_hash[md5]) for md5 in sorted(files_with_same_hash.keys())]
 
-    print "Combining NOTICE files into text"
     combine_notice_files_text(filesets, input_dir, txt_output_file, file_title)
 
     if html_output_file is not None:
-        print "Combining NOTICE files into HTML"
         combine_notice_files_html(filesets, input_dir, html_output_file)
 
     if xml_output_file is not None:
-        print "Combining NOTICE files into XML"
         combine_notice_files_xml(files_with_same_hash, input_dir, xml_output_file)
 
 if __name__ == "__main__":
diff --git a/tools/ijar/Android.bp b/tools/ijar/Android.bp
deleted file mode 100644
index a244a2d..0000000
--- a/tools/ijar/Android.bp
+++ /dev/null
@@ -1,18 +0,0 @@
-// Copyright 2015 The Android Open Source Project
-//
-// The rest of files in this directory comes from
-// https://github.com/bazelbuild/bazel/tree/master/third_party/ijar
-
-cc_binary_host {
-    srcs: [
-        "classfile.cc",
-        "ijar.cc",
-        "zip.cc",
-    ],
-    cflags: [
-        "-Wall",
-        "-Werror",
-    ],
-    host_ldlibs: ["-lz"],
-    name: "ijar",
-}
diff --git a/tools/ijar/LICENSE b/tools/ijar/LICENSE
deleted file mode 100644
index 6b0b127..0000000
--- a/tools/ijar/LICENSE
+++ /dev/null
@@ -1,203 +0,0 @@
-
-                                 Apache License
-                           Version 2.0, January 2004
-                        http://www.apache.org/licenses/
-
-   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
-
-   1. Definitions.
-
-      "License" shall mean the terms and conditions for use, reproduction,
-      and distribution as defined by Sections 1 through 9 of this document.
-
-      "Licensor" shall mean the copyright owner or entity authorized by
-      the copyright owner that is granting the License.
-
-      "Legal Entity" shall mean the union of the acting entity and all
-      other entities that control, are controlled by, or are under common
-      control with that entity. For the purposes of this definition,
-      "control" means (i) the power, direct or indirect, to cause the
-      direction or management of such entity, whether by contract or
-      otherwise, or (ii) ownership of fifty percent (50%) or more of the
-      outstanding shares, or (iii) beneficial ownership of such entity.
-
-      "You" (or "Your") shall mean an individual or Legal Entity
-      exercising permissions granted by this License.
-
-      "Source" form shall mean the preferred form for making modifications,
-      including but not limited to software source code, documentation
-      source, and configuration files.
-
-      "Object" form shall mean any form resulting from mechanical
-      transformation or translation of a Source form, including but
-      not limited to compiled object code, generated documentation,
-      and conversions to other media types.
-
-      "Work" shall mean the work of authorship, whether in Source or
-      Object form, made available under the License, as indicated by a
-      copyright notice that is included in or attached to the work
-      (an example is provided in the Appendix below).
-
-      "Derivative Works" shall mean any work, whether in Source or Object
-      form, that is based on (or derived from) the Work and for which the
-      editorial revisions, annotations, elaborations, or other modifications
-      represent, as a whole, an original work of authorship. For the purposes
-      of this License, Derivative Works shall not include works that remain
-      separable from, or merely link (or bind by name) to the interfaces of,
-      the Work and Derivative Works thereof.
-
-      "Contribution" shall mean any work of authorship, including
-      the original version of the Work and any modifications or additions
-      to that Work or Derivative Works thereof, that is intentionally
-      submitted to Licensor for inclusion in the Work by the copyright owner
-      or by an individual or Legal Entity authorized to submit on behalf of
-      the copyright owner. For the purposes of this definition, "submitted"
-      means any form of electronic, verbal, or written communication sent
-      to the Licensor or its representatives, including but not limited to
-      communication on electronic mailing lists, source code control systems,
-      and issue tracking systems that are managed by, or on behalf of, the
-      Licensor for the purpose of discussing and improving the Work, but
-      excluding communication that is conspicuously marked or otherwise
-      designated in writing by the copyright owner as "Not a Contribution."
-
-      "Contributor" shall mean Licensor and any individual or Legal Entity
-      on behalf of whom a Contribution has been received by Licensor and
-      subsequently incorporated within the Work.
-
-   2. Grant of Copyright License. Subject to the terms and conditions of
-      this License, each Contributor hereby grants to You a perpetual,
-      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
-      copyright license to reproduce, prepare Derivative Works of,
-      publicly display, publicly perform, sublicense, and distribute the
-      Work and such Derivative Works in Source or Object form.
-
-   3. Grant of Patent License. Subject to the terms and conditions of
-      this License, each Contributor hereby grants to You a perpetual,
-      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
-      (except as stated in this section) patent license to make, have made,
-      use, offer to sell, sell, import, and otherwise transfer the Work,
-      where such license applies only to those patent claims licensable
-      by such Contributor that are necessarily infringed by their
-      Contribution(s) alone or by combination of their Contribution(s)
-      with the Work to which such Contribution(s) was submitted. If You
-      institute patent litigation against any entity (including a
-      cross-claim or counterclaim in a lawsuit) alleging that the Work
-      or a Contribution incorporated within the Work constitutes direct
-      or contributory patent infringement, then any patent licenses
-      granted to You under this License for that Work shall terminate
-      as of the date such litigation is filed.
-
-   4. Redistribution. You may reproduce and distribute copies of the
-      Work or Derivative Works thereof in any medium, with or without
-      modifications, and in Source or Object form, provided that You
-      meet the following conditions:
-
-      (a) You must give any other recipients of the Work or
-          Derivative Works a copy of this License; and
-
-      (b) You must cause any modified files to carry prominent notices
-          stating that You changed the files; and
-
-      (c) You must retain, in the Source form of any Derivative Works
-          that You distribute, all copyright, patent, trademark, and
-          attribution notices from the Source form of the Work,
-          excluding those notices that do not pertain to any part of
-          the Derivative Works; and
-
-      (d) If the Work includes a "NOTICE" text file as part of its
-          distribution, then any Derivative Works that You distribute must
-          include a readable copy of the attribution notices contained
-          within such NOTICE file, excluding those notices that do not
-          pertain to any part of the Derivative Works, in at least one
-          of the following places: within a NOTICE text file distributed
-          as part of the Derivative Works; within the Source form or
-          documentation, if provided along with the Derivative Works; or,
-          within a display generated by the Derivative Works, if and
-          wherever such third-party notices normally appear. The contents
-          of the NOTICE file are for informational purposes only and
-          do not modify the License. You may add Your own attribution
-          notices within Derivative Works that You distribute, alongside
-          or as an addendum to the NOTICE text from the Work, provided
-          that such additional attribution notices cannot be construed
-          as modifying the License.
-
-      You may add Your own copyright statement to Your modifications and
-      may provide additional or different license terms and conditions
-      for use, reproduction, or distribution of Your modifications, or
-      for any such Derivative Works as a whole, provided Your use,
-      reproduction, and distribution of the Work otherwise complies with
-      the conditions stated in this License.
-
-   5. Submission of Contributions. Unless You explicitly state otherwise,
-      any Contribution intentionally submitted for inclusion in the Work
-      by You to the Licensor shall be under the terms and conditions of
-      this License, without any additional terms or conditions.
-      Notwithstanding the above, nothing herein shall supersede or modify
-      the terms of any separate license agreement you may have executed
-      with Licensor regarding such Contributions.
-
-   6. Trademarks. This License does not grant permission to use the trade
-      names, trademarks, service marks, or product names of the Licensor,
-      except as required for reasonable and customary use in describing the
-      origin of the Work and reproducing the content of the NOTICE file.
-
-   7. Disclaimer of Warranty. Unless required by applicable law or
-      agreed to in writing, Licensor provides the Work (and each
-      Contributor provides its Contributions) on an "AS IS" BASIS,
-      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
-      implied, including, without limitation, any warranties or conditions
-      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
-      PARTICULAR PURPOSE. You are solely responsible for determining the
-      appropriateness of using or redistributing the Work and assume any
-      risks associated with Your exercise of permissions under this License.
-
-   8. Limitation of Liability. In no event and under no legal theory,
-      whether in tort (including negligence), contract, or otherwise,
-      unless required by applicable law (such as deliberate and grossly
-      negligent acts) or agreed to in writing, shall any Contributor be
-      liable to You for damages, including any direct, indirect, special,
-      incidental, or consequential damages of any character arising as a
-      result of this License or out of the use or inability to use the
-      Work (including but not limited to damages for loss of goodwill,
-      work stoppage, computer failure or malfunction, or any and all
-      other commercial damages or losses), even if such Contributor
-      has been advised of the possibility of such damages.
-
-   9. Accepting Warranty or Additional Liability. While redistributing
-      the Work or Derivative Works thereof, You may choose to offer,
-      and charge a fee for, acceptance of support, warranty, indemnity,
-      or other liability obligations and/or rights consistent with this
-      License. However, in accepting such obligations, You may act only
-      on Your own behalf and on Your sole responsibility, not on behalf
-      of any other Contributor, and only if You agree to indemnify,
-      defend, and hold each Contributor harmless for any liability
-      incurred by, or claims asserted against, such Contributor by reason
-      of your accepting any such warranty or additional liability.
-
-   END OF TERMS AND CONDITIONS
-
-   APPENDIX: How to apply the Apache License to your work.
-
-      To apply the Apache License to your work, attach the following
-      boilerplate notice, with the fields enclosed by brackets "[]"
-      replaced with your own identifying information. (Don't include
-      the brackets!)  The text should be enclosed in the appropriate
-      comment syntax for the file format. We also recommend that a
-      file or class name and description of purpose be included on the
-      same "printed page" as the copyright notice for easier
-      identification within third-party archives.
-
-   Copyright [yyyy] [name of copyright owner]
-
-   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.
-
diff --git a/tools/ijar/README.txt b/tools/ijar/README.txt
deleted file mode 100644
index d5a6a0f..0000000
--- a/tools/ijar/README.txt
+++ /dev/null
@@ -1,120 +0,0 @@
-
-ijar: A tool for generating interface .jars from normal .jars
-=============================================================
-
-Alan Donovan, 26 May 2007.
-
-Rationale:
-
-  In order to improve the speed of compilation of Java programs in
-  Bazel, the output of build steps is cached.
-
-  This works very nicely for C++ compilation: a compilation unit
-  includes a .cc source file and typically dozens of header files.
-  Header files change relatively infrequently, so the need for a
-  rebuild is usually driven by a change in the .cc file.  Even after
-  syncing a slightly newer version of the tree and doing a rebuild,
-  many hits in the cache are still observed.
-
-  In Java, by contrast, a compilation unit involves a set of .java
-  source files, plus a set of .jar files containing already-compiled
-  JVM .class files.  Class files serve a dual purpose: from the JVM's
-  perspective, they are containers of executable code, but from the
-  compiler's perspective, they are interface definitions.  The problem
-  here is that .jar files are very much more sensitive to change than
-  C++ header files, so even a change that is insignificant to the
-  compiler (such as the addition of a print statement to a method in a
-  prerequisite class) will cause the jar to change, and any code that
-  depends on this jar's interface will be recompiled unnecessarily.
-
-  The purpose of ijar is to produce, from a .jar file, a much smaller,
-  simpler .jar file containing only the parts that are significant for
-  the purposes of compilation.  In other words, an interface .jar
-  file.  By changing ones compilation dependencies to be the interface
-  jar files, unnecessary recompilation is avoided when upstream
-  changes don't affect the interface.
-
-Details:
-
-  ijar is a tool that reads a .jar file and emits a .jar file
-  containing only the parts that are relevant to Java compilation.
-  For example, it throws away:
-
-  - Files whose name does not end in ".class".
-  - All executable method code.
-  - All private methods and fields.
-  - All constants and attributes except the minimal set necessary to
-    describe the class interface.
-  - All debugging information
-    (LineNumberTable, SourceFile, LocalVariableTables attributes).
-
-  It also sets to zero the file modification times in the index of the
-  .jar file.
-
-Implementation:
-
-  ijar is implemented in C++, and runs very quickly.  For example
-  (when optimized) it takes only 530ms to process a 42MB
-  .jar file containing 5878 classe, resulting in an interface .jar
-  file of only 11.4MB in size.  For more usual .jar sizes of a few
-  megabytes, a runtime of 50ms is typical.
-
-  The implementation strategy is to mmap both the input jar and the
-  newly-created _interface.jar, and to scan through the former and
-  emit the latter in a single pass. There are a couple of locations
-  where some kind of "backpatching" is required:
-
-  - in the .zip file format, for each file, the size field precedes
-    the data.  We emit a zero but note its location, generate and emit
-    the stripped classfile, then poke the correct size into the
-    location.
-
-  - for JVM .class files, the header (including the constant table)
-    precedes the body, but cannot be emitted before it because it's
-    not until we emit the body that we know which constants are
-    referenced and which are garbage.  So we emit the body into a
-    temporary buffer, then emit the header to the output jar, followed
-    by the contents of the temp buffer.
-
-  Also note that the zip file format has unnecessary duplication of
-  the index metadata: it has header+data for each file, then another
-  set of (similar) headers at the end.  Rather than save the metadata
-  explicitly in some datastructure, we just record the addresses of
-  the already-emitted zip metadata entries in the output file, and
-  then read from there as necessary.
-
-Notes:
-
-  This code has no dependency except on the STL and on zlib.
-
-  Almost all of the getX/putX/ReadX/WriteX functions in the code
-  advance their first argument pointer, which is passed by reference.
-
-  It's tempting to discard package-private classes and class members.
-  However, this would be incorrect because they are a necessary part
-  of the package interface, as a Java package is often compiled in
-  multiple stages.  For example: in Bazel, both java tests and java
-  code inhabit the same Java package but are compiled separately.
-
-Assumptions:
-
-  We assume that jar files are uncompressed v1.0 zip files (created
-  with 'jar c0f') with a zero general_purpose_bit_flag.
-
-  We assume that javap/javac don't need the correct CRC checksums in
-  the .jar file.
-
-  We assume that it's better simply to abort in the face of unknown
-  input than to risk leaving out something important from the output
-  (although in the case of annotations, it should be safe to ignore
-  ones we don't understand).
-
-TODO:
-  Maybe: ensure a canonical sort order is used for every list (jar
-  entries, class members, attributes, etc.)  This isn't essential
-  because we can assume the compiler is deterministic and the order in
-  the source files changes little.  Also, it would require two passes. :(
-
-  Maybe: delete dynamically-allocated memory.
-
-  Add (a lot) more tests.  Include a test of idempotency.
diff --git a/tools/ijar/classfile.cc b/tools/ijar/classfile.cc
deleted file mode 100644
index d33e0db..0000000
--- a/tools/ijar/classfile.cc
+++ /dev/null
@@ -1,1788 +0,0 @@
-// Copyright 2001,2007 Alan Donovan. All rights reserved.
-//
-// Author: Alan Donovan <adonovan@google.com>
-//
-// 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.
-//
-// classfile.cc -- classfile parsing and stripping.
-//
-
-// TODO(adonovan) don't pass pointers by reference; this is not
-// compatible with Google C++ style.
-
-// See README.txt for details.
-//
-// For definition of JVM class file format, see:
-// Java SE 8 Edition:
-// http://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4
-
-#define __STDC_FORMAT_MACROS 1
-#define __STDC_LIMIT_MACROS 1
-#include <inttypes.h> // for PRIx32
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <set>
-#include <string>
-#include <vector>
-
-#include "common.h"
-
-namespace devtools_ijar {
-
-// See Table 4.3 in JVM Spec.
-enum CONSTANT {
-  CONSTANT_Class              = 7,
-  CONSTANT_FieldRef           = 9,
-  CONSTANT_Methodref          = 10,
-  CONSTANT_Interfacemethodref = 11,
-  CONSTANT_String             = 8,
-  CONSTANT_Integer            = 3,
-  CONSTANT_Float              = 4,
-  CONSTANT_Long               = 5,
-  CONSTANT_Double             = 6,
-  CONSTANT_NameAndType        = 12,
-  CONSTANT_Utf8               = 1,
-  CONSTANT_MethodHandle       = 15,
-  CONSTANT_MethodType         = 16,
-  CONSTANT_InvokeDynamic      = 18
-};
-
-// See Tables 4.1, 4.4, 4.5 in JVM Spec.
-enum ACCESS  {
-  ACC_PUBLIC          = 0x0001,
-  ACC_PRIVATE         = 0x0002,
-  ACC_PROTECTED       = 0x0004,
-  ACC_STATIC          = 0x0008,
-  ACC_FINAL           = 0x0010,
-  ACC_SYNCHRONIZED    = 0x0020,
-  ACC_VOLATILE        = 0x0040,
-  ACC_TRANSIENT       = 0x0080,
-  ACC_INTERFACE       = 0x0200,
-  ACC_ABSTRACT        = 0x0400
-};
-
-// See Table 4.7.20-A in Java 8 JVM Spec.
-enum TARGET_TYPE {
-  // Targets for type parameter declarations (ElementType.TYPE_PARAMETER):
-  CLASS_TYPE_PARAMETER        = 0x00,
-  METHOD_TYPE_PARAMETER       = 0x01,
-
-  // Targets for type uses that may be externally visible in classes and members
-  // (ElementType.TYPE_USE):
-  CLASS_EXTENDS               = 0x10,
-  CLASS_TYPE_PARAMETER_BOUND  = 0x11,
-  METHOD_TYPE_PARAMETER_BOUND = 0x12,
-  FIELD                       = 0x13,
-  METHOD_RETURN               = 0x14,
-  METHOD_RECEIVER             = 0x15,
-  METHOD_FORMAL_PARAMETER     = 0x16,
-  THROWS                      = 0x17,
-
-  // TARGET_TYPE >= 0x40 is reserved for type uses that occur only within code
-  // blocks. Ijar doesn't need to know about these.
-};
-
-struct Constant;
-
-// TODO(adonovan) these globals are unfortunate
-static std::vector<Constant*>        const_pool_in; // input constant pool
-static std::vector<Constant*>        const_pool_out; // output constant_pool
-static std::set<std::string>         used_class_names;
-static Constant *                    class_name;
-
-// Returns the Constant object, given an index into the input constant pool.
-// Note: constant(0) == NULL; this invariant is exploited by the
-// InnerClassesAttribute, inter alia.
-inline Constant *constant(int idx) {
-  if (idx < 0 || (unsigned)idx >= const_pool_in.size()) {
-    fprintf(stderr, "Illegal constant pool index: %d\n", idx);
-    abort();
-  }
-  return const_pool_in[idx];
-}
-
-/**********************************************************************
- *                                                                    *
- *                             Constants                              *
- *                                                                    *
- **********************************************************************/
-
-// See sec.4.4 of JVM spec.
-struct Constant {
-
-  explicit Constant(u1 tag) :
-      slot_(0),
-      tag_(tag) {}
-
-  virtual ~Constant() {}
-
-  // For UTF-8 string constants, returns the encoded string.
-  // Otherwise, returns an undefined string value suitable for debugging.
-  virtual std::string Display() = 0;
-
-  virtual void Write(u1 *&p) = 0;
-
-  // Called by slot() when a constant has been identified as required
-  // in the output classfile's constant pool.  This is a hook allowing
-  // constants to register their dependency on other constants, by
-  // calling slot() on them in turn.
-  virtual void Keep() {}
-
-  bool Kept() {
-    return slot_ != 0;
-  }
-
-  // Returns the index of this constant in the output class's constant
-  // pool, assigning a slot if not already done.
-  u2 slot() {
-    if (slot_ == 0) {
-      Keep();
-      slot_ = const_pool_out.size(); // BugBot's "narrowing" warning
-                                     // is bogus.  The number of
-                                     // output constants can't exceed
-                                     // the number of input constants.
-      if (slot_ == 0) {
-        fprintf(stderr, "Constant::slot() called before output phase.\n");
-        abort();
-      }
-      const_pool_out.push_back(this);
-      if (tag_ == CONSTANT_Long || tag_ == CONSTANT_Double) {
-        const_pool_out.push_back(NULL);
-      }
-    }
-    return slot_;
-  }
-
-  u2 slot_; // zero => "this constant is unreachable garbage"
-  u1 tag_;
-};
-
-// Extracts class names from a signature and puts them into the global
-// variable used_class_names.
-//
-// desc: the descriptor class names should be extracted from.
-// p: the position where the extraction should tart.
-void ExtractClassNames(const std::string& desc, size_t* p);
-
-// See sec.4.4.1 of JVM spec.
-struct Constant_Class : Constant
-{
-  explicit Constant_Class(u2 name_index) :
-      Constant(CONSTANT_Class),
-      name_index_(name_index) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, constant(name_index_)->slot());
-  }
-
-  std::string Display() {
-    return constant(name_index_)->Display();
-  }
-
-  void Keep() { constant(name_index_)->slot(); }
-
-  u2 name_index_;
-};
-
-// See sec.4.4.2 of JVM spec.
-struct Constant_FMIref : Constant
-{
-  Constant_FMIref(u1 tag,
-                  u2 class_index,
-                  u2 name_type_index) :
-      Constant(tag),
-      class_index_(class_index),
-      name_type_index_(name_type_index) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, constant(class_index_)->slot());
-    put_u2be(p, constant(name_type_index_)->slot());
-  }
-
-  std::string Display() {
-    return constant(class_index_)->Display() + "::" +
-        constant(name_type_index_)->Display();
-  }
-
-  void Keep() {
-    constant(class_index_)->slot();
-    constant(name_type_index_)->slot();
-  }
-
-  u2 class_index_;
-  u2 name_type_index_;
-};
-
-// See sec.4.4.3 of JVM spec.
-struct Constant_String : Constant
-{
-  explicit Constant_String(u2 string_index) :
-      Constant(CONSTANT_String),
-      string_index_(string_index) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, constant(string_index_)->slot());
-  }
-
-  std::string Display() {
-    return "\"" + constant(string_index_)->Display() + "\"";
-  }
-
-  void Keep() { constant(string_index_)->slot(); }
-
-  u2 string_index_;
-};
-
-// See sec.4.4.4 of JVM spec.
-struct Constant_IntegerOrFloat : Constant
-{
-  Constant_IntegerOrFloat(u1 tag, u4 bytes) :
-      Constant(tag),
-      bytes_(bytes) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u4be(p, bytes_);
-  }
-
-  std::string Display() { return "int/float"; }
-
-  u4 bytes_;
-};
-
-// See sec.4.4.5 of JVM spec.
-struct Constant_LongOrDouble : Constant_IntegerOrFloat
-{
-  Constant_LongOrDouble(u1 tag, u4 high_bytes, u4 low_bytes) :
-      Constant_IntegerOrFloat(tag, high_bytes),
-      low_bytes_(low_bytes) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u4be(p, bytes_);
-    put_u4be(p, low_bytes_);
-  }
-
-  std::string Display() { return "long/double"; }
-
-  u4 low_bytes_;
-};
-
-// See sec.4.4.6 of JVM spec.
-struct Constant_NameAndType : Constant
-{
-  Constant_NameAndType(u2 name_index, u2 descr_index) :
-      Constant(CONSTANT_NameAndType),
-      name_index_(name_index),
-      descr_index_(descr_index) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, constant(name_index_)->slot());
-    put_u2be(p, constant(descr_index_)->slot());
-  }
-
-  std::string Display() {
-    return constant(name_index_)->Display() + "::" +
-        constant(descr_index_)->Display();
-  }
-
-  void Keep() {
-    constant(name_index_)->slot();
-    constant(descr_index_)->slot();
-  }
-
-  u2 name_index_;
-  u2 descr_index_;
-};
-
-// See sec.4.4.7 of JVM spec.
-struct Constant_Utf8 : Constant
-{
-  Constant_Utf8(u4 length, const u1 *utf8) :
-      Constant(CONSTANT_Utf8),
-      length_(length),
-      utf8_(utf8) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, length_);
-    put_n(p, utf8_, length_);
-  }
-
-  std::string Display() {
-    return std::string((const char*) utf8_, length_);
-  }
-
-  u4 length_;
-  const u1 *utf8_;
-};
-
-// See sec.4.4.8 of JVM spec.
-struct Constant_MethodHandle : Constant
-{
-  Constant_MethodHandle(u1 reference_kind, u2 reference_index) :
-      Constant(CONSTANT_MethodHandle),
-      reference_kind_(reference_kind),
-      reference_index_(reference_index) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u1(p, reference_kind_);
-    put_u2be(p, reference_index_);
-  }
-
-  std::string Display() {
-    return "Constant_MethodHandle::" + std::to_string(reference_kind_) + "::"
-        + constant(reference_index_)->Display();
-  }
-
-  u1 reference_kind_;
-  u2 reference_index_;
-};
-
-// See sec.4.4.9 of JVM spec.
-struct Constant_MethodType : Constant
-{
-  explicit Constant_MethodType(u2 descriptor_index) :
-      Constant(CONSTANT_MethodType),
-      descriptor_index_(descriptor_index) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, descriptor_index_);
-  }
-
-  std::string Display() {
-    return  "Constant_MethodType::" + constant(descriptor_index_)->Display();
-  }
-
-  u2 descriptor_index_;
-};
-
-// See sec.4.4.10 of JVM spec.
-struct Constant_InvokeDynamic : Constant
-{
-  Constant_InvokeDynamic(u2 bootstrap_method_attr_index, u2 name_and_type_index) :
-      Constant(CONSTANT_InvokeDynamic),
-      bootstrap_method_attr_index_(bootstrap_method_attr_index),
-      name_and_type_index_(name_and_type_index) {}
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, bootstrap_method_attr_index_);
-    put_u2be(p, name_and_type_index_);
-  }
-
-  std::string Display() {
-    return  "Constant_InvokeDynamic::"
-        + std::to_string(bootstrap_method_attr_index_) + "::"
-        + constant(name_and_type_index_)->Display();
-  }
-
-  u2 bootstrap_method_attr_index_;
-  u2 name_and_type_index_;
-};
-
-/**********************************************************************
- *                                                                    *
- *                             Attributes                             *
- *                                                                    *
- **********************************************************************/
-
-// See sec.4.7 of JVM spec.
-struct Attribute {
-
-  virtual ~Attribute() {}
-  virtual void Write(u1 *&p) = 0;
-  virtual void ExtractClassNames() {}
-
-  void WriteProlog(u1 *&p, u2 length) {
-    put_u2be(p, attribute_name_->slot());
-    put_u4be(p, length);
-  }
-
-  Constant *attribute_name_;
-};
-
-// See sec.4.7.5 of JVM spec.
-struct ExceptionsAttribute : Attribute {
-
-  static ExceptionsAttribute* Read(const u1 *&p, Constant *attribute_name) {
-    ExceptionsAttribute *attr = new ExceptionsAttribute;
-    attr->attribute_name_ = attribute_name;
-    u2 number_of_exceptions = get_u2be(p);
-    for (int ii = 0; ii < number_of_exceptions; ++ii) {
-      attr->exceptions_.push_back(constant(get_u2be(p)));
-    }
-    return attr;
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, exceptions_.size() * 2 + 2);
-    put_u2be(p, exceptions_.size());
-    for (size_t ii = 0; ii < exceptions_.size(); ++ii) {
-      put_u2be(p, exceptions_[ii]->slot());
-    }
-  }
-
-  std::vector<Constant*> exceptions_;
-};
-
-// See sec.4.7.6 of JVM spec.
-struct InnerClassesAttribute : Attribute {
-
-  struct Entry {
-    Constant *inner_class_info;
-    Constant *outer_class_info;
-    Constant *inner_name;
-    u2 inner_class_access_flags;
-  };
-
-  virtual ~InnerClassesAttribute() {
-    for (size_t i = 0; i < entries_.size(); i++) {
-      delete entries_[i];
-    }
-  }
-
-  static InnerClassesAttribute* Read(const u1 *&p, Constant *attribute_name) {
-    InnerClassesAttribute *attr = new InnerClassesAttribute;
-    attr->attribute_name_ = attribute_name;
-
-    u2 number_of_classes = get_u2be(p);
-    for (int ii = 0; ii < number_of_classes; ++ii) {
-      Entry *entry = new Entry;
-      entry->inner_class_info = constant(get_u2be(p));
-      entry->outer_class_info = constant(get_u2be(p));
-      entry->inner_name = constant(get_u2be(p));
-      entry->inner_class_access_flags = get_u2be(p);
-
-      attr->entries_.push_back(entry);
-    }
-    return attr;
-  }
-
-  void Write(u1 *&p) {
-    std::set<int> kept_entries;
-    // We keep an entry if the constant referring to the inner class is already
-    // kept. Then we mark its outer class and its class name as kept, too, then
-    // iterate until a fixed point is reached.
-    size_t entry_count;
-    int iteration = 0;
-
-    do {
-      entry_count = kept_entries.size();
-      for (size_t i_entry = 0; i_entry < entries_.size(); ++i_entry) {
-        Entry* entry = entries_[i_entry];
-        if (entry->inner_class_info->Kept() ||
-            used_class_names.find(entry->inner_class_info->Display())
-                != used_class_names.end() ||
-            entry->outer_class_info == class_name ||
-            entry->outer_class_info == NULL ||
-            entry->inner_name == NULL) {
-          kept_entries.insert(i_entry);
-
-          // These are zero for anonymous inner classes
-          if (entry->outer_class_info != NULL) {
-            entry->outer_class_info->slot();
-          }
-
-          if (entry->inner_name != NULL) {
-            entry->inner_name->slot();
-          }
-        }
-      }
-      iteration += 1;
-    } while (entry_count != kept_entries.size());
-
-    if (kept_entries.size() == 0) {
-      return;
-    }
-
-    WriteProlog(p, 2 + kept_entries.size() * 8);
-    put_u2be(p, kept_entries.size());
-
-    for (std::set<int>::iterator it = kept_entries.begin();
-         it != kept_entries.end();
-         ++it) {
-      Entry *entry = entries_[*it];
-      put_u2be(p, entry->inner_class_info == NULL
-               ? 0
-               : entry->inner_class_info->slot());
-      put_u2be(p, entry->outer_class_info == NULL
-               ? 0
-               : entry->outer_class_info->slot());
-      put_u2be(p, entry->inner_name == NULL
-               ? 0
-               : entry->inner_name->slot());
-      put_u2be(p, entry->inner_class_access_flags);
-    }
-  }
-
-  std::vector<Entry*> entries_;
-};
-
-// See sec.4.7.7 of JVM spec.
-// We preserve EnclosingMethod attributes to be able to identify local and
-// anonymous classes. These classes will be stripped of most content, as they
-// represent implementation details that shoudn't leak into the ijars. Omitting
-// EnclosingMethod attributes can lead to type-checking failures in the presence
-// of generics (see b/9070939).
-struct EnclosingMethodAttribute : Attribute {
-
-  static EnclosingMethodAttribute* Read(const u1 *&p,
-                                        Constant *attribute_name) {
-    EnclosingMethodAttribute *attr = new EnclosingMethodAttribute;
-    attr->attribute_name_ = attribute_name;
-    attr->class_ = constant(get_u2be(p));
-    attr->method_ = constant(get_u2be(p));
-    return attr;
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, 4);
-    put_u2be(p, class_->slot());
-    put_u2be(p, method_ == NULL ? 0 : method_->slot());
-  }
-
-  Constant *class_;
-  Constant *method_;
-};
-
-// See sec.4.7.16.1 of JVM spec.
-// Used by AnnotationDefault and other attributes.
-struct ElementValue {
-  virtual ~ElementValue() {}
-  virtual void Write(u1 *&p) = 0;
-  virtual void ExtractClassNames() {}
-  static ElementValue* Read(const u1 *&p);
-  u1 tag_;
-  u4 length_;
-};
-
-struct BaseTypeElementValue : ElementValue {
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, const_value_->slot());
-  }
-  static BaseTypeElementValue *Read(const u1 *&p) {
-    BaseTypeElementValue *value = new BaseTypeElementValue;
-    value->const_value_ = constant(get_u2be(p));
-    return value;
-  }
-  Constant *const_value_;
-};
-
-struct EnumTypeElementValue : ElementValue {
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, type_name_->slot());
-    put_u2be(p, const_name_->slot());
-  }
-  static EnumTypeElementValue *Read(const u1 *&p) {
-    EnumTypeElementValue *value = new EnumTypeElementValue;
-    value->type_name_ = constant(get_u2be(p));
-    value->const_name_ = constant(get_u2be(p));
-    return value;
-  }
-  Constant *type_name_;
-  Constant *const_name_;
-};
-
-struct ClassTypeElementValue : ElementValue {
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, class_info_->slot());
-  }
-
-  virtual void ExtractClassNames() {
-    size_t idx = 0;
-    devtools_ijar::ExtractClassNames(class_info_->Display(), &idx);
-  }
-
-  static ClassTypeElementValue *Read(const u1 *&p) {
-    ClassTypeElementValue *value = new ClassTypeElementValue;
-    value->class_info_ = constant(get_u2be(p));
-    return value;
-  }
-  Constant *class_info_;
-};
-
-struct ArrayTypeElementValue : ElementValue {
-  virtual ~ArrayTypeElementValue() {
-    for (size_t i = 0; i < values_.size(); i++) {
-      delete values_[i];
-    }
-  }
-
-  virtual void ExtractClassNames() {
-    for (size_t i = 0; i < values_.size(); i++) {
-      values_[i]->ExtractClassNames();
-    }
-  }
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    put_u2be(p, values_.size());
-    for (size_t ii = 0; ii < values_.size(); ++ii) {
-      values_[ii]->Write(p);
-    }
-  }
-  static ArrayTypeElementValue *Read(const u1 *&p) {
-    ArrayTypeElementValue *value = new ArrayTypeElementValue;
-    u2 num_values = get_u2be(p);
-    for (int ii = 0; ii < num_values; ++ii) {
-      value->values_.push_back(ElementValue::Read(p));
-    }
-    return value;
-  }
-  std::vector<ElementValue*> values_;
-};
-
-// See sec.4.7.16 of JVM spec.
-struct Annotation {
-  virtual ~Annotation() {
-    for (size_t i = 0; i < element_value_pairs_.size(); i++) {
-      delete element_value_pairs_[i]->element_value_;
-      delete element_value_pairs_[i];
-    }
-  }
-
-  void ExtractClassNames() {
-    for (size_t i = 0; i < element_value_pairs_.size(); i++) {
-      element_value_pairs_[i]->element_value_->ExtractClassNames();
-    }
-  }
-
-  void Write(u1 *&p) {
-    put_u2be(p, type_->slot());
-    put_u2be(p, element_value_pairs_.size());
-    for (size_t ii = 0; ii < element_value_pairs_.size(); ++ii) {
-      put_u2be(p, element_value_pairs_[ii]->element_name_->slot());
-      element_value_pairs_[ii]->element_value_->Write(p);
-    }
-  }
-  static Annotation *Read(const u1 *&p) {
-    Annotation *value = new Annotation;
-    value->type_ = constant(get_u2be(p));
-    u2 num_element_value_pairs = get_u2be(p);
-    for (int ii = 0; ii < num_element_value_pairs; ++ii) {
-      ElementValuePair *pair = new ElementValuePair;
-      pair->element_name_ = constant(get_u2be(p));
-      pair->element_value_ = ElementValue::Read(p);
-      value->element_value_pairs_.push_back(pair);
-    }
-    return value;
-  }
-  Constant *type_;
-  struct ElementValuePair {
-    Constant *element_name_;
-    ElementValue *element_value_;
-  };
-  std::vector<ElementValuePair*> element_value_pairs_;
-};
-
-// See sec 4.7.20 of Java 8 JVM Spec
-//
-// Each entry in the annotations table represents a single run-time visible
-// annotation on a type used in a declaration or expression. The type_annotation
-// structure has the following format:
-//
-// type_annotation {
-//   u1 target_type;
-//   union {
-//     type_parameter_target;
-//     supertype_target;
-//     type_parameter_bound_target;
-//     empty_target;
-//     method_formal_parameter_target;
-//     throws_target;
-//     localvar_target;
-//     catch_target;
-//     offset_target;
-//     type_argument_target;
-//   } target_info;
-//   type_path target_path;
-//   u2        type_index;
-//   u2        num_element_value_pairs;
-//   {
-//     u2            element_name_index;
-//     element_value value;
-//   }
-//   element_value_pairs[num_element_value_pairs];
-// }
-//
-struct TypeAnnotation {
-  virtual ~TypeAnnotation() {
-    delete target_info_;
-    delete type_path_;
-    delete annotation_;
-  }
-
-  void ExtractClassNames() {
-    annotation_->ExtractClassNames();
-  }
-
-  void Write(u1 *&p) {
-    put_u1(p, target_type_);
-    target_info_->Write(p);
-    type_path_->Write(p);
-    annotation_->Write(p);
-  }
-
-  static TypeAnnotation *Read(const u1 *&p) {
-    TypeAnnotation *value = new TypeAnnotation;
-    value->target_type_ = get_u1(p);
-    value->target_info_ = ReadTargetInfo(p, value->target_type_);
-    value->type_path_ = TypePath::Read(p);
-    value->annotation_ = Annotation::Read(p);
-    return value;
-  }
-
-  struct TargetInfo {
-    virtual ~TargetInfo() {}
-    virtual void Write(u1 *&p) = 0;
-  };
-
-  struct TypeParameterTargetInfo : TargetInfo {
-    void Write(u1 *&p) {
-      put_u1(p, type_parameter_index_);
-    }
-    static TypeParameterTargetInfo *Read(const u1 *&p) {
-      TypeParameterTargetInfo *value = new TypeParameterTargetInfo;
-      value->type_parameter_index_ = get_u1(p);
-      return value;
-    }
-    u1 type_parameter_index_;
-  };
-
-  struct ClassExtendsInfo : TargetInfo {
-    void Write(u1 *&p) {
-      put_u2be(p, supertype_index_);
-    }
-    static ClassExtendsInfo *Read(const u1 *&p) {
-      ClassExtendsInfo *value = new ClassExtendsInfo;
-      value->supertype_index_ = get_u2be(p);
-      return value;
-    }
-    u2 supertype_index_;
-  };
-
-  struct TypeParameterBoundInfo : TargetInfo {
-    void Write(u1 *&p) {
-      put_u1(p, type_parameter_index_);
-      put_u1(p, bound_index_);
-    }
-    static TypeParameterBoundInfo *Read(const u1 *&p) {
-      TypeParameterBoundInfo *value = new TypeParameterBoundInfo;
-      value->type_parameter_index_ = get_u1(p);
-      value->bound_index_ = get_u1(p);
-      return value;
-    }
-    u1 type_parameter_index_;
-    u1 bound_index_;
-  };
-
-  struct EmptyInfo : TargetInfo {
-    void Write(u1 *&) {}
-    static EmptyInfo *Read(const u1 *&) {
-      return new EmptyInfo;
-    }
-  };
-
-  struct MethodFormalParameterInfo : TargetInfo {
-    void Write(u1 *&p) {
-      put_u1(p, method_formal_parameter_index_);
-    }
-    static MethodFormalParameterInfo *Read(const u1 *&p) {
-      MethodFormalParameterInfo *value = new MethodFormalParameterInfo;
-      value->method_formal_parameter_index_ = get_u1(p);
-      return value;
-    }
-    u1 method_formal_parameter_index_;
-  };
-
-  struct ThrowsTypeInfo : TargetInfo {
-    void Write(u1 *&p) {
-      put_u2be(p, throws_type_index_);
-    }
-    static ThrowsTypeInfo *Read(const u1 *&p) {
-      ThrowsTypeInfo *value = new ThrowsTypeInfo;
-      value->throws_type_index_ = get_u2be(p);
-      return value;
-    }
-    u2 throws_type_index_;
-  };
-
-  static TargetInfo *ReadTargetInfo(const u1 *&p, u1 target_type) {
-    switch (target_type) {
-      case CLASS_TYPE_PARAMETER:
-      case METHOD_TYPE_PARAMETER:
-        return TypeParameterTargetInfo::Read(p);
-      case CLASS_EXTENDS:
-        return ClassExtendsInfo::Read(p);
-      case CLASS_TYPE_PARAMETER_BOUND:
-      case METHOD_TYPE_PARAMETER_BOUND:
-        return TypeParameterBoundInfo::Read(p);
-      case FIELD:
-      case METHOD_RETURN:
-      case METHOD_RECEIVER:
-        return new EmptyInfo;
-      case METHOD_FORMAL_PARAMETER:
-        return MethodFormalParameterInfo::Read(p);
-      case THROWS:
-        return ThrowsTypeInfo::Read(p);
-      default:
-        fprintf(stderr, "Illegal type annotation target type: %d\n",
-                target_type);
-        abort();
-    }
-  }
-
-  struct TypePath {
-    void Write(u1 *&p) {
-      put_u1(p, path_.size());
-      for (TypePathEntry entry : path_) {
-        put_u1(p, entry.type_path_kind_);
-        put_u1(p, entry.type_argument_index_);
-      }
-    }
-    static TypePath *Read(const u1 *&p) {
-      TypePath *value = new TypePath;
-      u1 path_length = get_u1(p);
-      for (int ii = 0; ii < path_length; ++ii) {
-        TypePathEntry entry;
-        entry.type_path_kind_ = get_u1(p);
-        entry.type_argument_index_ = get_u1(p);
-        value->path_.push_back(entry);
-      }
-      return value;
-    }
-
-    struct TypePathEntry {
-      u1 type_path_kind_;
-      u1 type_argument_index_;
-    };
-    std::vector<TypePathEntry> path_;
-  };
-
-  u1 target_type_;
-  TargetInfo *target_info_;
-  TypePath *type_path_;
-  Annotation *annotation_;
-};
-
-struct AnnotationTypeElementValue : ElementValue {
-  virtual ~AnnotationTypeElementValue() {
-    delete annotation_;
-  }
-
-  void Write(u1 *&p) {
-    put_u1(p, tag_);
-    annotation_->Write(p);
-  }
-  static AnnotationTypeElementValue *Read(const u1 *&p) {
-    AnnotationTypeElementValue *value = new AnnotationTypeElementValue;
-    value->annotation_ = Annotation::Read(p);
-    return value;
-  }
-
-  Annotation *annotation_;
-};
-
-ElementValue* ElementValue::Read(const u1 *&p) {
-  const u1* start = p;
-  ElementValue *result;
-  u1 tag = get_u1(p);
-  if (tag != 0 && strchr("BCDFIJSZs", (char) tag) != NULL) {
-    result = BaseTypeElementValue::Read(p);
-  } else if ((char) tag == 'e') {
-    result = EnumTypeElementValue::Read(p);
-  } else if ((char) tag == 'c') {
-    result = ClassTypeElementValue::Read(p);
-  } else if ((char) tag == '[') {
-    result = ArrayTypeElementValue::Read(p);
-  } else if ((char) tag == '@') {
-    result = AnnotationTypeElementValue::Read(p);
-  } else {
-    fprintf(stderr, "Illegal element_value::tag: %d\n", tag);
-    abort();
-  }
-  result->tag_ = tag;
-  result->length_ = p - start;
-  return result;
-}
-
-// See sec.4.7.20 of JVM spec.
-// We preserve AnnotationDefault attributes because they are required
-// in order to make use of an annotation in new code.
-struct AnnotationDefaultAttribute : Attribute {
-  virtual ~AnnotationDefaultAttribute() {
-    delete default_value_;
-  }
-
-  static AnnotationDefaultAttribute* Read(const u1 *&p,
-                                          Constant *attribute_name) {
-    AnnotationDefaultAttribute *attr = new AnnotationDefaultAttribute;
-    attr->attribute_name_ = attribute_name;
-    attr->default_value_ = ElementValue::Read(p);
-    return attr;
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, default_value_->length_);
-    default_value_->Write(p);
-  }
-
-  virtual void ExtractClassNames() {
-    default_value_->ExtractClassNames();
-  }
-
-  ElementValue *default_value_;
-};
-
-// See sec.4.7.2 of JVM spec.
-// We preserve ConstantValue attributes because they are required for
-// compile-time constant propagation.
-struct ConstantValueAttribute : Attribute {
-
-  static ConstantValueAttribute* Read(const u1 *&p, Constant *attribute_name) {
-    ConstantValueAttribute *attr = new ConstantValueAttribute;
-    attr->attribute_name_ = attribute_name;
-    attr->constantvalue_ = constant(get_u2be(p));
-    return attr;
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, 2);
-    put_u2be(p, constantvalue_->slot());
-  }
-
-  Constant *constantvalue_;
-};
-
-// See sec.4.7.9 of JVM spec.
-// We preserve Signature attributes because they are required by the
-// compiler for type-checking of generics.
-struct SignatureAttribute : Attribute {
-
-  static SignatureAttribute* Read(const u1 *&p, Constant *attribute_name) {
-    SignatureAttribute *attr = new SignatureAttribute;
-    attr->attribute_name_ = attribute_name;
-    attr->signature_  = constant(get_u2be(p));
-    return attr;
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, 2);
-    put_u2be(p, signature_->slot());
-  }
-
-  virtual void ExtractClassNames() {
-    size_t signature_idx = 0;
-    devtools_ijar::ExtractClassNames(signature_->Display(), &signature_idx);
-  }
-
-  Constant *signature_;
-};
-
-// See sec.4.7.15 of JVM spec.
-// We preserve Deprecated attributes because they are required by the
-// compiler to generate warning messages.
-struct DeprecatedAttribute : Attribute {
-
-  static DeprecatedAttribute* Read(const u1 *&, Constant *attribute_name) {
-    DeprecatedAttribute *attr = new DeprecatedAttribute;
-    attr->attribute_name_ = attribute_name;
-    return attr;
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, 0);
-  }
-};
-
-// See sec.4.7.16-17 of JVM spec v3.  Includes RuntimeVisible and
-// RuntimeInvisible.
-//
-// We preserve all annotations.
-struct AnnotationsAttribute : Attribute {
-  virtual ~AnnotationsAttribute() {
-    for (size_t i = 0; i < annotations_.size(); i++) {
-      delete annotations_[i];
-    }
-  }
-
-  static AnnotationsAttribute* Read(const u1 *&p, Constant *attribute_name) {
-    AnnotationsAttribute *attr = new AnnotationsAttribute;
-    attr->attribute_name_ = attribute_name;
-    u2 num_annotations = get_u2be(p);
-    for (int ii = 0; ii < num_annotations; ++ii) {
-      Annotation *annotation = Annotation::Read(p);
-      attr->annotations_.push_back(annotation);
-    }
-    return attr;
-  }
-
-  virtual void ExtractClassNames() {
-    for (size_t i = 0; i < annotations_.size(); i++) {
-      annotations_[i]->ExtractClassNames();
-    }
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, -1);
-    u1 *payload_start = p - 4;
-    put_u2be(p, annotations_.size());
-    for (size_t ii = 0; ii < annotations_.size(); ++ii) {
-      annotations_[ii]->Write(p);
-    }
-    put_u4be(payload_start, p - 4 - payload_start);  // backpatch length
-  }
-
-  std::vector<Annotation*> annotations_;
-};
-
-// See sec.4.7.18-19 of JVM spec.  Includes RuntimeVisible and
-// RuntimeInvisible.
-//
-// We preserve all annotations.
-struct ParameterAnnotationsAttribute : Attribute {
-
-  static ParameterAnnotationsAttribute* Read(const u1 *&p,
-                                             Constant *attribute_name) {
-    ParameterAnnotationsAttribute *attr = new ParameterAnnotationsAttribute;
-    attr->attribute_name_ = attribute_name;
-    u1 num_parameters = get_u1(p);
-    for (int ii = 0; ii < num_parameters; ++ii) {
-      std::vector<Annotation*> annotations;
-      u2 num_annotations = get_u2be(p);
-      for (int ii = 0; ii < num_annotations; ++ii) {
-        Annotation *annotation = Annotation::Read(p);
-        annotations.push_back(annotation);
-      }
-      attr->parameter_annotations_.push_back(annotations);
-    }
-    return attr;
-  }
-
-  virtual void ExtractClassNames() {
-    for (size_t i = 0; i < parameter_annotations_.size(); i++) {
-      const std::vector<Annotation*>& annotations = parameter_annotations_[i];
-      for (size_t j = 0; j < annotations.size(); j++) {
-        annotations[j]->ExtractClassNames();
-      }
-    }
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, -1);
-    u1 *payload_start = p - 4;
-    put_u1(p, parameter_annotations_.size());
-    for (size_t ii = 0; ii < parameter_annotations_.size(); ++ii) {
-      std::vector<Annotation *> &annotations = parameter_annotations_[ii];
-      put_u2be(p, annotations.size());
-      for (size_t jj = 0; jj < annotations.size(); ++jj) {
-        annotations[jj]->Write(p);
-      }
-    }
-    put_u4be(payload_start, p - 4 - payload_start);  // backpatch length
-  }
-
-  std::vector<std::vector<Annotation*> > parameter_annotations_;
-};
-
-// See sec.4.7.20 of Java 8 JVM spec. Includes RuntimeVisibleTypeAnnotations
-// and RuntimeInvisibleTypeAnnotations.
-struct TypeAnnotationsAttribute : Attribute {
-  static TypeAnnotationsAttribute* Read(const u1 *&p, Constant *attribute_name,
-                                        u4) {
-    auto attr = new TypeAnnotationsAttribute;
-    attr->attribute_name_ = attribute_name;
-    u2 num_annotations = get_u2be(p);
-    for (int ii = 0; ii < num_annotations; ++ii) {
-      TypeAnnotation *annotation = TypeAnnotation::Read(p);
-      attr->type_annotations_.push_back(annotation);
-    }
-    return attr;
-  }
-
-  virtual void ExtractClassNames() {
-    for (size_t i = 0; i < type_annotations_.size(); i++) {
-      type_annotations_[i]->ExtractClassNames();
-    }
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, -1);
-    u1 *payload_start = p - 4;
-    put_u2be(p, type_annotations_.size());
-    for (TypeAnnotation *annotation : type_annotations_) {
-      annotation->Write(p);
-    }
-    put_u4be(payload_start, p - 4 - payload_start);  // backpatch length
-  }
-
-  std::vector<TypeAnnotation*> type_annotations_;
-};
-
-struct GeneralAttribute : Attribute {
-  static GeneralAttribute* Read(const u1 *&p, Constant *attribute_name,
-                                u4 attribute_length) {
-    auto attr = new GeneralAttribute;
-    attr->attribute_name_ = attribute_name;
-    attr->attribute_length_ = attribute_length;
-    attr->attribute_content_ = p;
-    p += attribute_length;
-    return attr;
-  }
-
-  void Write(u1 *&p) {
-    WriteProlog(p, attribute_length_);
-    put_n(p, attribute_content_, attribute_length_);
-  }
-
-  u4 attribute_length_;
-  const u1 *attribute_content_;
-};
-
-/**********************************************************************
- *                                                                    *
- *                             ClassFile                              *
- *                                                                    *
- **********************************************************************/
-
-struct HasAttrs {
-  std::vector<Attribute*> attributes;
-
-  void WriteAttrs(u1 *&p);
-  void ReadAttrs(const u1 *&p);
-
-  virtual ~HasAttrs() {
-    for (size_t i = 0; i < attributes.size(); i++) {
-      delete attributes[i];
-    }
-  }
-
-  void ExtractClassNames() {
-    for (size_t i = 0; i < attributes.size(); i++) {
-      attributes[i]->ExtractClassNames();
-    }
-  }
-};
-
-// A field or method.
-// See sec.4.5 and 4.6 of JVM spec.
-struct Member : HasAttrs {
-  u2 access_flags;
-  Constant *name;
-  Constant *descriptor;
-
-  static Member* Read(const u1 *&p) {
-    Member *m = new Member;
-    m->access_flags = get_u2be(p);
-    m->name = constant(get_u2be(p));
-    m->descriptor = constant(get_u2be(p));
-    m->ReadAttrs(p);
-    return m;
-  }
-
-  void Write(u1 *&p) {
-    put_u2be(p, access_flags);
-    put_u2be(p, name->slot());
-    put_u2be(p, descriptor->slot());
-    WriteAttrs(p);
-  }
-};
-
-// See sec.4.1 of JVM spec.
-struct ClassFile : HasAttrs {
-
-  size_t length;
-
-  // Header:
-  u4 magic;
-  u2 major;
-  u2 minor;
-
-  // Body:
-  u2 access_flags;
-  Constant *this_class;
-  Constant *super_class;
-  std::vector<Constant*> interfaces;
-  std::vector<Member*> fields;
-  std::vector<Member*> methods;
-
-  virtual ~ClassFile() {
-    for (size_t i = 0; i < fields.size(); i++) {
-      delete fields[i];
-    }
-
-    for (size_t i = 0; i < methods.size(); i++) {
-      delete methods[i];
-    }
-
-    // Constants do not need to be deleted; they are owned by the constant pool.
-  }
-
-  void WriteClass(u1 *&p);
-
-  bool ReadConstantPool(const u1 *&p);
-
-  void StripIfAnonymous();
-
-  void WriteHeader(u1 *&p) {
-    put_u4be(p, magic);
-    put_u2be(p, major);
-    put_u2be(p, minor);
-
-    put_u2be(p, const_pool_out.size());
-    for (u2 ii = 1; ii < const_pool_out.size(); ++ii) {
-      if (const_pool_out[ii] != NULL) { // NB: NULLs appear after long/double.
-        const_pool_out[ii]->Write(p);
-      }
-    }
-  }
-
-  void WriteBody(u1 *&p) {
-    put_u2be(p, access_flags);
-    put_u2be(p, this_class->slot());
-    put_u2be(p, super_class == NULL ? 0 : super_class->slot());
-    put_u2be(p, interfaces.size());
-    for (size_t ii = 0; ii < interfaces.size(); ++ii) {
-      put_u2be(p, interfaces[ii]->slot());
-    }
-    put_u2be(p, fields.size());
-    for (size_t ii = 0; ii < fields.size(); ++ii) {
-      fields[ii]->Write(p);
-    }
-    put_u2be(p, methods.size());
-    for (size_t ii = 0; ii < methods.size(); ++ii) {
-      methods[ii]->Write(p);
-    }
-
-    Attribute* inner_classes = NULL;
-
-    // Make the inner classes attribute the last, so that it can know which
-    // constants were needed
-    for (size_t ii = 0; ii < attributes.size(); ii++) {
-      if (attributes[ii]->attribute_name_->Display() == "InnerClasses") {
-        inner_classes = attributes[ii];
-        attributes.erase(attributes.begin() + ii);
-        break;
-      }
-    }
-
-    if (inner_classes != NULL) {
-      attributes.push_back(inner_classes);
-    }
-
-    WriteAttrs(p);
-  }
-
-};
-
-void HasAttrs::ReadAttrs(const u1 *&p) {
-  u2 attributes_count = get_u2be(p);
-  for (int ii = 0; ii < attributes_count; ii++) {
-    Constant *attribute_name = constant(get_u2be(p));
-    u4 attribute_length = get_u4be(p);
-
-    std::string attr_name = attribute_name->Display();
-    if (attr_name == "SourceFile" ||
-        attr_name == "LineNumberTable" ||
-        attr_name == "LocalVariableTable" ||
-        attr_name == "LocalVariableTypeTable" ||
-        attr_name == "Code" ||
-        attr_name == "Synthetic" ||
-        attr_name == "BootstrapMethods") {
-      p += attribute_length; // drop these attributes
-    } else if (attr_name == "Exceptions") {
-      attributes.push_back(ExceptionsAttribute::Read(p, attribute_name));
-    } else if (attr_name == "Signature") {
-      attributes.push_back(SignatureAttribute::Read(p, attribute_name));
-    } else if (attr_name == "Deprecated") {
-      attributes.push_back(DeprecatedAttribute::Read(p, attribute_name));
-    } else if (attr_name == "EnclosingMethod") {
-      attributes.push_back(EnclosingMethodAttribute::Read(p, attribute_name));
-    } else if (attr_name == "InnerClasses") {
-      // TODO(bazel-team): omit private inner classes
-      attributes.push_back(InnerClassesAttribute::Read(p, attribute_name));
-    } else if (attr_name == "AnnotationDefault") {
-      attributes.push_back(AnnotationDefaultAttribute::Read(p, attribute_name));
-    } else if (attr_name == "ConstantValue") {
-      attributes.push_back(ConstantValueAttribute::Read(p, attribute_name));
-    } else if (attr_name == "RuntimeVisibleAnnotations" ||
-               attr_name == "RuntimeInvisibleAnnotations") {
-      attributes.push_back(AnnotationsAttribute::Read(p, attribute_name));
-    } else if (attr_name == "RuntimeVisibleParameterAnnotations" ||
-               attr_name == "RuntimeInvisibleParameterAnnotations") {
-      attributes.push_back(
-          ParameterAnnotationsAttribute::Read(p, attribute_name));
-    } else if (attr_name == "Scala" ||
-               attr_name == "ScalaSig" ||
-               attr_name == "ScalaInlineInfo") {
-      // These are opaque blobs, so can be handled with a general
-      // attribute handler
-      attributes.push_back(GeneralAttribute::Read(p, attribute_name,
-                                                  attribute_length));
-    } else if (attr_name == "RuntimeVisibleTypeAnnotations" ||
-               attr_name == "RuntimeInvisibleTypeAnnotations") {
-      // JSR 308: annotations on types. JDK 7 has no use for these yet, but the
-      // Checkers Framework relies on them.
-      attributes.push_back(TypeAnnotationsAttribute::Read(p, attribute_name,
-                                                          attribute_length));
-    } else {
-      // Skip over unknown attributes with a warning.  The JVM spec
-      // says this is ok, so long as we handle the mandatory attributes.
-      fprintf(stderr, "ijar: skipping unknown attribute: \"%s\".\n",
-              attr_name.c_str());
-      p += attribute_length;
-    }
-  }
-}
-
-void HasAttrs::WriteAttrs(u1 *&p) {
-  u1* p_size = p;
-
-  put_u2be(p, 0);
-  int n_written_attrs = 0;
-  for (size_t ii = 0; ii < attributes.size(); ii++) {
-    u1* before = p;
-    attributes[ii]->Write(p);
-    if (p != before) {
-      n_written_attrs++;
-    }
-  }
-
-  put_u2be(p_size, n_written_attrs);
-}
-
-// See sec.4.4 of JVM spec.
-bool ClassFile::ReadConstantPool(const u1 *&p) {
-
-  const_pool_in.clear();
-  const_pool_in.push_back(NULL); // dummy first item
-
-  u2 cp_count = get_u2be(p);
-  for (int ii = 1; ii < cp_count; ++ii) {
-    u1 tag = get_u1(p);
-
-    if (devtools_ijar::verbose) {
-      fprintf(stderr, "cp[%d/%d] = tag %d\n", ii, cp_count, tag);
-    }
-
-    switch(tag) {
-      case CONSTANT_Class: {
-        u2 name_index = get_u2be(p);
-        const_pool_in.push_back(new Constant_Class(name_index));
-        break;
-      }
-      case CONSTANT_FieldRef:
-      case CONSTANT_Methodref:
-      case CONSTANT_Interfacemethodref: {
-        u2 class_index = get_u2be(p);
-        u2 nti = get_u2be(p);
-        const_pool_in.push_back(new Constant_FMIref(tag, class_index, nti));
-        break;
-      }
-      case CONSTANT_String: {
-        u2 string_index = get_u2be(p);
-        const_pool_in.push_back(new Constant_String(string_index));
-        break;
-      }
-      case CONSTANT_NameAndType: {
-        u2 name_index = get_u2be(p);
-        u2 descriptor_index = get_u2be(p);
-        const_pool_in.push_back(
-            new Constant_NameAndType(name_index, descriptor_index));
-        break;
-      }
-      case CONSTANT_Utf8: {
-        u2 length = get_u2be(p);
-        if (devtools_ijar::verbose) {
-          fprintf(stderr, "Utf8: \"%s\" (%d)\n",
-                  std::string((const char*) p, length).c_str(), length);
-        }
-
-        const_pool_in.push_back(new Constant_Utf8(length, p));
-        p += length;
-        break;
-      }
-      case CONSTANT_Integer:
-      case CONSTANT_Float: {
-        u4 bytes = get_u4be(p);
-        const_pool_in.push_back(new Constant_IntegerOrFloat(tag, bytes));
-        break;
-      }
-      case CONSTANT_Long:
-      case CONSTANT_Double: {
-        u4 high_bytes = get_u4be(p);
-        u4 low_bytes = get_u4be(p);
-        const_pool_in.push_back(
-            new Constant_LongOrDouble(tag, high_bytes, low_bytes));
-        // Longs and doubles occupy two constant pool slots.
-        // ("In retrospect, making 8-byte constants take two "constant
-        // pool entries was a poor choice." --JVM Spec.)
-        const_pool_in.push_back(NULL);
-        ii++;
-        break;
-      }
-      case CONSTANT_MethodHandle: {
-        u1 reference_kind = get_u1(p);
-        u2 reference_index = get_u2be(p);
-        const_pool_in.push_back(
-            new Constant_MethodHandle(reference_kind, reference_index));
-        break;
-      }
-      case CONSTANT_MethodType: {
-        u2 descriptor_index = get_u2be(p);
-        const_pool_in.push_back(new Constant_MethodType(descriptor_index));
-        break;
-      }
-      case CONSTANT_InvokeDynamic: {
-        u2 bootstrap_method_attr = get_u2be(p);
-        u2 name_name_type_index = get_u2be(p);
-        const_pool_in.push_back(new Constant_InvokeDynamic(
-            bootstrap_method_attr, name_name_type_index));
-        break;
-      }
-      default: {
-        fprintf(stderr, "Unknown constant: %02x. Passing class through.\n",
-                tag);
-        return false;
-      }
-    }
-  }
-
-  return true;
-}
-
-// Anonymous inner classes are stripped to opaque classes that only extend
-// Object. None of their methods or fields are accessible anyway.
-void ClassFile::StripIfAnonymous() {
-  int enclosing_index = -1;
-  int inner_classes_index = -1;
-
-  for (size_t ii = 0; ii < attributes.size(); ++ii) {
-    if (attributes[ii]->attribute_name_->Display() == "EnclosingMethod") {
-      enclosing_index = ii;
-    } else if (attributes[ii]->attribute_name_->Display() == "InnerClasses") {
-      inner_classes_index = ii;
-    }
-  }
-
-  // Presence of an EnclosingMethod attribute indicates a local or anonymous
-  // class, which can be stripped.
-  if (enclosing_index > -1) {
-    // Clear the signature to only extend java.lang.Object.
-    super_class = NULL;
-    interfaces.clear();
-
-    // Clear away all fields (implementation details).
-    for (size_t ii = 0; ii < fields.size(); ++ii) {
-      delete fields[ii];
-    }
-    fields.clear();
-
-    // Clear away all methods (implementation details).
-    for (size_t ii = 0; ii < methods.size(); ++ii) {
-      delete methods[ii];
-    }
-    methods.clear();
-
-    // Only preserve the InnerClasses attribute to comply with the spec.
-    Attribute *attr = NULL;
-    for (size_t ii = 0; ii < attributes.size(); ++ii) {
-      if (static_cast<int>(ii) != inner_classes_index) {
-        delete attributes[ii];
-      } else {
-        attr = attributes[ii];
-      }
-    }
-    attributes.clear();
-    if (attr != NULL) {
-      attributes.push_back(attr);
-    }
-  }
-}
-
-static ClassFile *ReadClass(const void *classdata, size_t length) {
-  const u1 *p = (u1*) classdata;
-
-  ClassFile *clazz = new ClassFile;
-
-  clazz->length = length;
-
-  clazz->magic = get_u4be(p);
-  if (clazz->magic != 0xCAFEBABE) {
-    fprintf(stderr, "Bad magic %" PRIx32 "\n", clazz->magic);
-    abort();
-  }
-  clazz->major = get_u2be(p);
-  clazz->minor = get_u2be(p);
-
-  if (!clazz->ReadConstantPool(p)) {
-    delete clazz;
-    return NULL;
-  }
-
-  clazz->access_flags = get_u2be(p);
-  clazz->this_class = constant(get_u2be(p));
-  class_name = clazz->this_class;
-
-  u2 super_class_id = get_u2be(p);
-  clazz->super_class = super_class_id == 0 ? NULL : constant(super_class_id);
-
-  u2 interfaces_count = get_u2be(p);
-  for (int ii = 0; ii < interfaces_count; ++ii) {
-    clazz->interfaces.push_back(constant(get_u2be(p)));
-  }
-
-  u2 fields_count = get_u2be(p);
-  for (int ii = 0; ii < fields_count; ++ii) {
-    Member *field = Member::Read(p);
-
-    if (!(field->access_flags & ACC_PRIVATE)) { // drop private fields
-      clazz->fields.push_back(field);
-    }
-  }
-
-  u2 methods_count = get_u2be(p);
-  for (int ii = 0; ii < methods_count; ++ii) {
-    Member *method = Member::Read(p);
-
-    // drop class initializers
-    if (method->name->Display() == "<clinit>") continue;
-
-    if (!(method->access_flags & ACC_PRIVATE)) { // drop private methods
-      clazz->methods.push_back(method);
-    }
-  }
-
-  clazz->ReadAttrs(p);
-  clazz->StripIfAnonymous();
-
-  return clazz;
-}
-
-// In theory, '/' is also reserved, but it's okay if we just parse package
-// identifiers as part of the class name. Note that signatures are UTF-8, but
-// this works just as well as in plain ASCII.
-static const char *SIGNATURE_NON_IDENTIFIER_CHARS = ".;[<>:";
-
-void Expect(const std::string& desc, size_t* p, char expected) {
-  if (desc[*p] != expected) {
-    fprintf(stderr, "Expected '%c' in '%s' at %zd in signature\n",
-            expected, desc.substr(*p).c_str(), *p);
-    exit(1);
-  }
-
-  *p += 1;
-}
-
-// These functions form a crude recursive descent parser for descriptors and
-// signatures in class files (see JVM spec 4.3).
-//
-// This parser is a bit more liberal than the spec, but this should be fine,
-// because it accepts all valid class files and croaks only on invalid ones.
-void ParseFromClassTypeSignature(const std::string& desc, size_t* p);
-void ParseSimpleClassTypeSignature(const std::string& desc, size_t* p);
-void ParseClassTypeSignatureSuffix(const std::string& desc, size_t* p);
-void ParseIdentifier(const std::string& desc, size_t* p);
-void ParseTypeArgumentsOpt(const std::string& desc, size_t* p);
-void ParseMethodDescriptor(const std::string& desc, size_t* p);
-
-void ParseClassTypeSignature(const std::string& desc, size_t* p) {
-  Expect(desc, p, 'L');
-  ParseSimpleClassTypeSignature(desc, p);
-  ParseClassTypeSignatureSuffix(desc, p);
-  Expect(desc, p, ';');
-}
-
-void ParseSimpleClassTypeSignature(const std::string& desc, size_t* p) {
-  ParseIdentifier(desc, p);
-  ParseTypeArgumentsOpt(desc, p);
-}
-
-void ParseClassTypeSignatureSuffix(const std::string& desc, size_t* p) {
-  while (desc[*p] == '.') {
-    *p += 1;
-    ParseSimpleClassTypeSignature(desc, p);
-  }
-}
-
-void ParseIdentifier(const std::string& desc, size_t* p) {
-  size_t next = desc.find_first_of(SIGNATURE_NON_IDENTIFIER_CHARS, *p);
-  std::string id = desc.substr(*p, next - *p);
-  used_class_names.insert(id);
-  *p = next;
-}
-
-void ParseTypeArgumentsOpt(const std::string& desc, size_t* p) {
-  if (desc[*p] != '<') {
-    return;
-  }
-
-  *p += 1;
-  while (desc[*p] != '>') {
-    switch (desc[*p]) {
-      case '*':
-        *p += 1;
-        break;
-
-      case '+':
-      case '-':
-        *p += 1;
-        ExtractClassNames(desc, p);
-        break;
-
-      default:
-        ExtractClassNames(desc, p);
-        break;
-    }
-  }
-
-  *p += 1;
-}
-
-void ParseMethodDescriptor(const std::string& desc, size_t* p) {
-  Expect(desc, p, '(');
-  while (desc[*p] != ')') {
-    ExtractClassNames(desc, p);
-  }
-
-  Expect(desc, p, ')');
-  ExtractClassNames(desc, p);
-}
-
-void ParseFormalTypeParameters(const std::string& desc, size_t* p) {
-  Expect(desc, p, '<');
-  while (desc[*p] != '>') {
-    ParseIdentifier(desc, p);
-    Expect(desc, p, ':');
-    if (desc[*p] != ':' && desc[*p] != '>') {
-      ExtractClassNames(desc, p);
-    }
-
-    while (desc[*p] == ':') {
-      Expect(desc, p, ':');
-      ExtractClassNames(desc, p);
-    }
-  }
-
-  Expect(desc, p, '>');
-}
-
-void ExtractClassNames(const std::string& desc, size_t* p) {
-  switch (desc[*p]) {
-    case '<':
-      ParseFormalTypeParameters(desc, p);
-      ExtractClassNames(desc, p);
-      break;
-
-    case 'L':
-      ParseClassTypeSignature(desc, p);
-      break;
-
-    case '[':
-      *p += 1;
-      ExtractClassNames(desc, p);
-      break;
-
-    case 'T':
-      *p += 1;
-      ParseIdentifier(desc, p);
-      Expect(desc, p, ';');
-      break;
-
-    case '(':
-      ParseMethodDescriptor(desc, p);
-      break;
-
-    case 'B':
-    case 'C':
-    case 'D':
-    case 'F':
-    case 'I':
-    case 'J':
-    case 'S':
-    case 'Z':
-    case 'V':
-      *p += 1;
-      break;
-
-    default:
-      fprintf(stderr, "Invalid signature %s\n", desc.substr(*p).c_str());
-  }
-}
-
-void ClassFile::WriteClass(u1 *&p) {
-  used_class_names.clear();
-  std::vector<Member *> members;
-  members.insert(members.end(), fields.begin(), fields.end());
-  members.insert(members.end(), methods.begin(), methods.end());
-  ExtractClassNames();
-  for (size_t i = 0; i < members.size(); i++) {
-    Member *member = members[i];
-    size_t idx = 0;
-    devtools_ijar::ExtractClassNames(member->descriptor->Display(), &idx);
-    member->ExtractClassNames();
-  }
-
-  // We have to write the body out before the header in order to reference
-  // the essential constants and populate the output constant pool:
-  u1 *body = new u1[length];
-  u1 *q = body;
-  WriteBody(q); // advances q
-  u4 body_length = q - body;
-
-  WriteHeader(p); // advances p
-  put_n(p, body, body_length);
-  delete[] body;
-}
-
-
-void StripClass(u1 *&classdata_out, const u1 *classdata_in, size_t in_length) {
-  ClassFile *clazz = ReadClass(classdata_in, in_length);
-  if (clazz == NULL) {
-    // Class is invalid. Simply copy it to the output and call it a day.
-    put_n(classdata_out, classdata_in, in_length);
-  } else {
-
-    // Constant pool item zero is a dummy entry.  Setting it marks the
-    // beginning of the output phase; calls to Constant::slot() will
-    // fail if called prior to this.
-    const_pool_out.push_back(NULL);
-    clazz->WriteClass(classdata_out);
-
-    delete clazz;
-  }
-
-  // Now clean up all the mess we left behind.
-
-  for (size_t i = 0; i < const_pool_in.size(); i++) {
-    delete const_pool_in[i];
-  }
-
-  const_pool_in.clear();
-  const_pool_out.clear();
-}
-
-}  // namespace devtools_ijar
diff --git a/tools/ijar/common.h b/tools/ijar/common.h
deleted file mode 100644
index 118041b..0000000
--- a/tools/ijar/common.h
+++ /dev/null
@@ -1,102 +0,0 @@
-// Copyright 2001,2007 Alan Donovan. All rights reserved.
-//
-// Author: Alan Donovan <adonovan@google.com>
-//
-// 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.
-//
-// common.h -- common definitions.
-//
-
-#ifndef INCLUDED_DEVTOOLS_IJAR_COMMON_H
-#define INCLUDED_DEVTOOLS_IJAR_COMMON_H
-
-#include <stddef.h>
-#include <stdint.h>
-#include <string.h>
-
-namespace devtools_ijar {
-
-typedef unsigned long long u8;
-typedef uint32_t u4;
-typedef uint16_t u2;
-typedef uint8_t  u1;
-
-// be = big endian, le = little endian
-
-inline u1 get_u1(const u1 *&p) {
-    return *p++;
-}
-
-inline u2 get_u2be(const u1 *&p) {
-    u4 x = (p[0] << 8) | p[1];
-    p += 2;
-    return x;
-}
-
-inline u2 get_u2le(const u1 *&p) {
-    u4 x = (p[1] << 8) | p[0];
-    p += 2;
-    return x;
-}
-
-inline u4 get_u4be(const u1 *&p) {
-    u4 x = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
-    p += 4;
-    return x;
-}
-
-inline u4 get_u4le(const u1 *&p) {
-    u4 x = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
-    p += 4;
-    return x;
-}
-
-inline void put_u1(u1 *&p, u1 x) {
-    *p++ = x;
-}
-
-inline void put_u2be(u1 *&p, u2 x) {
-    *p++ = x >> 8;
-    *p++ = x & 0xff;
-}
-
-inline void put_u2le(u1 *&p, u2 x) {
-    *p++ = x & 0xff;
-    *p++ = x >> 8;;
-}
-
-inline void put_u4be(u1 *&p, u4 x) {
-    *p++ = x >> 24;
-    *p++ = (x >> 16) & 0xff;
-    *p++ = (x >> 8) & 0xff;
-    *p++ = x & 0xff;
-}
-
-inline void put_u4le(u1 *&p, u4 x) {
-    *p++ = x & 0xff;
-    *p++ = (x >> 8) & 0xff;
-    *p++ = (x >> 16) & 0xff;
-    *p++ = x >> 24;
-}
-
-// Copy n bytes from src to p, and advance p.
-inline void put_n(u1 *&p, const u1 *src, size_t n) {
-  memcpy(p, src, n);
-  p += n;
-}
-
-extern bool verbose;
-
-}  // namespace devtools_ijar
-
-#endif // INCLUDED_DEVTOOLS_IJAR_COMMON_H
diff --git a/tools/ijar/ijar.cc b/tools/ijar/ijar.cc
deleted file mode 100644
index 1925b48..0000000
--- a/tools/ijar/ijar.cc
+++ /dev/null
@@ -1,182 +0,0 @@
-// Copyright 2001,2007 Alan Donovan. All rights reserved.
-//
-// Author: Alan Donovan <adonovan@google.com>
-//
-// 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.
-//
-// ijar.cpp -- .jar -> _interface.jar tool.
-//
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <limits.h>
-#include <errno.h>
-#include <memory>
-
-#include "zip.h"
-
-namespace devtools_ijar {
-
-bool verbose = false;
-
-// Reads a JVM class from classdata_in (of the specified length), and
-// writes out a simplified class to classdata_out, advancing the
-// pointer.
-void StripClass(u1 *&classdata_out, const u1 *classdata_in, size_t in_length);
-
-const char* CLASS_EXTENSION = ".class";
-const size_t CLASS_EXTENSION_LENGTH = strlen(CLASS_EXTENSION);
-
-// ZipExtractorProcessor that select only .class file and use
-// StripClass to generate an interface class, storing as a new file
-// in the specified ZipBuilder.
-class JarStripperProcessor : public ZipExtractorProcessor {
- public:
-  JarStripperProcessor() {}
-  virtual ~JarStripperProcessor() {}
-
-  virtual void Process(const char* filename, const u4 attr,
-                       const u1* data, const size_t size);
-  virtual bool Accept(const char* filename, const u4 attr);
-
- private:
-  // Not owned by JarStripperProcessor, see SetZipBuilder().
-  ZipBuilder* builder;
-
- public:
-  // Set the ZipBuilder to add the ijar class to the output zip file.
-  // This pointer should not be deleted while this class is still in use and
-  // it should be set before any call to the Process() method.
-  void SetZipBuilder(ZipBuilder* builder) {
-    this->builder = builder;
-  }
-};
-
-bool JarStripperProcessor::Accept(const char* filename, const u4) {
-  ssize_t offset = strlen(filename) - CLASS_EXTENSION_LENGTH;
-  if (offset >= 0) {
-    return strcmp(filename + offset, CLASS_EXTENSION) == 0;
-  }
-  return false;
-}
-
-void JarStripperProcessor::Process(const char* filename, const u4,
-                                   const u1* data, const size_t size) {
-  if (verbose) {
-    fprintf(stderr, "INFO: StripClass: %s\n", filename);
-  }
-  u1 *q = builder->NewFile(filename, 0);
-  u1 *classdata_out = q;
-  StripClass(q, data, size);  // actually process it
-  size_t out_length = q - classdata_out;
-  builder->FinishFile(out_length);
-}
-
-// Opens "file_in" (a .jar file) for reading, and writes an interface
-// .jar to "file_out".
-void OpenFilesAndProcessJar(const char *file_out, const char *file_in) {
-  JarStripperProcessor processor;
-  std::unique_ptr<ZipExtractor> in(ZipExtractor::Create(file_in, &processor));
-  if (in.get() == NULL) {
-    fprintf(stderr, "Unable to open Zip file %s: %s\n", file_in,
-            strerror(errno));
-    abort();
-  }
-  u8 output_length = in->CalculateOutputLength();
-  std::unique_ptr<ZipBuilder> out(ZipBuilder::Create(file_out, output_length));
-  if (out.get() == NULL) {
-    fprintf(stderr, "Unable to open output file %s: %s\n", file_out,
-            strerror(errno));
-    abort();
-  }
-  processor.SetZipBuilder(out.get());
-
-  // Process all files in the zip
-  if (in->ProcessAll() < 0) {
-    fprintf(stderr, "%s\n", in->GetError());
-    abort();
-  }
-
-  // Add dummy file, since javac doesn't like truly empty jars.
-  if (out->GetNumberFiles() == 0) {
-    out->WriteEmptyFile("dummy");
-  }
-  // Finish writing the output file
-  if (out->Finish() < 0) {
-    fprintf(stderr, "%s\n", out->GetError());
-    abort();
-  }
-  // Get all file size
-  size_t in_length = in->GetSize();
-  size_t out_length = out->GetSize();
-  if (verbose) {
-    fprintf(stderr, "INFO: produced interface jar: %s -> %s (%d%%).\n",
-            file_in, file_out,
-            static_cast<int>(100.0 * out_length / in_length));
-  }
-}
-
-}  // namespace devtools_ijar
-
-//
-// main method
-//
-static void usage() {
-  fprintf(stderr, "Usage: ijar [-v] x.jar [x_interface.jar>]\n");
-  fprintf(stderr, "Creates an interface jar from the specified jar file.\n");
-  exit(1);
-}
-
-int main(int argc, char **argv) {
-  const char *filename_in = NULL;
-  const char *filename_out = NULL;
-
-  for (int ii = 1; ii < argc; ++ii) {
-    if (strcmp(argv[ii], "-v") == 0) {
-      devtools_ijar::verbose = true;
-    } else if (filename_in == NULL) {
-      filename_in = argv[ii];
-    } else if (filename_out == NULL) {
-      filename_out = argv[ii];
-    } else {
-      usage();
-    }
-  }
-
-  if (filename_in == NULL) {
-    usage();
-  }
-
-  // Guess output filename from input:
-  char filename_out_buf[PATH_MAX];
-  if (filename_out == NULL) {
-    size_t len = strlen(filename_in);
-    if (len > 4 && strncmp(filename_in + len - 4, ".jar", 4) == 0) {
-      strcpy(filename_out_buf, filename_in);
-      strcpy(filename_out_buf + len - 4, "-interface.jar");
-      filename_out = filename_out_buf;
-    } else {
-      fprintf(stderr, "Can't determine output filename since input filename "
-              "doesn't end with '.jar'.\n");
-      return 1;
-    }
-  }
-
-  if (devtools_ijar::verbose) {
-    fprintf(stderr, "INFO: writing to '%s'.\n", filename_out);
-  }
-
-  devtools_ijar::OpenFilesAndProcessJar(filename_out, filename_in);
-  return 0;
-}
diff --git a/tools/ijar/zip.cc b/tools/ijar/zip.cc
deleted file mode 100644
index 3aa06db..0000000
--- a/tools/ijar/zip.cc
+++ /dev/null
@@ -1,1032 +0,0 @@
-// Copyright 2007 Alan Donovan. All rights reserved.
-//
-// Author: Alan Donovan <adonovan@google.com>
-//
-// 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.
-//
-// zip.cc -- .zip (.jar) file reading/writing routines.
-//
-
-// See README.txt for details.
-//
-// See http://www.pkware.com/documents/casestudies/APPNOTE.TXT
-// for definition of PKZIP file format.
-
-#define _FILE_OFFSET_BITS 64  // Support zip files larger than 2GB
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stddef.h>
-#include <stdint.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/mman.h>
-#include <unistd.h>
-#include <limits.h>
-#include <limits>
-#include <vector>
-
-#include "zip.h"
-#include <zlib.h>
-
-#define LOCAL_FILE_HEADER_SIGNATURE           0x04034b50
-#define CENTRAL_FILE_HEADER_SIGNATURE         0x02014b50
-#define END_OF_CENTRAL_DIR_SIGNATURE          0x06054b50
-#define DATA_DESCRIPTOR_SIGNATURE             0x08074b50
-
-// version to extract: 1.0 - default value from APPNOTE.TXT.
-// Output JAR files contain no extra ZIP features, so this is enough.
-#define ZIP_VERSION_TO_EXTRACT                10
-#define COMPRESSION_METHOD_STORED             0   // no compression
-#define COMPRESSION_METHOD_DEFLATED           8
-
-#define GENERAL_PURPOSE_BIT_FLAG_COMPRESSED (1 << 3)
-#define GENERAL_PURPOSE_BIT_FLAG_UTF8_ENCODED (1 << 11)
-#define GENERAL_PURPOSE_BIT_FLAG_COMPRESSION_SPEED ((1 << 2) | (1 << 1))
-#define GENERAL_PURPOSE_BIT_FLAG_SUPPORTED \
-  (GENERAL_PURPOSE_BIT_FLAG_COMPRESSED \
-  | GENERAL_PURPOSE_BIT_FLAG_UTF8_ENCODED \
-  | GENERAL_PURPOSE_BIT_FLAG_COMPRESSION_SPEED)
-
-namespace devtools_ijar {
-// In the absence of ZIP64 support, zip files are limited to 4GB.
-// http://www.info-zip.org/FAQ.html#limits
-static const u8 kMaximumOutputSize = std::numeric_limits<uint32_t>::max();
-
-static bool ProcessCentralDirEntry(const u1 *&p,
-                                   size_t *compressed_size,
-                                   size_t *uncompressed_size,
-                                   char *filename,
-                                   size_t filename_size,
-                                   u4 *attr,
-                                   u4 *offset);
-
-//
-// A class representing a ZipFile for reading. Its public API is exposed
-// using the ZipExtractor abstract class.
-//
-class InputZipFile : public ZipExtractor {
- public:
-  InputZipFile(ZipExtractorProcessor *processor, int fd, off_t in_length,
-               off_t in_offset, const u1* zipdata_in, const u1* central_dir);
-  virtual ~InputZipFile();
-
-  virtual const char* GetError() {
-    if (errmsg[0] == 0) {
-      return NULL;
-    }
-    return errmsg;
-  }
-
-  virtual bool ProcessNext();
-  virtual void Reset();
-  virtual size_t GetSize() {
-    return in_length_;
-  }
-
-  virtual u8 CalculateOutputLength();
-
- private:
-  ZipExtractorProcessor *processor;
-
-  int fd_in;  // Input file descripor
-
-  // InputZipFile is responsible for maintaining the following
-  // pointers. They are allocated by the Create() method before
-  // the object is actually created using mmap.
-  const u1 * const zipdata_in_;   // start of input file mmap
-  const u1 * zipdata_in_mapped_;  // start of still mapped region
-  const u1 * const central_dir_;  // central directory in input file
-
-  size_t in_length_;  // size of the input file
-  size_t in_offset_;  // offset  the input file
-
-  const u1 *p;  // input cursor
-
-  const u1* central_dir_current_;  // central dir input cursor
-
-  // Buffer size is initially INITIAL_BUFFER_SIZE. It doubles in size every
-  // time it is found too small, until it reaches MAX_BUFFER_SIZE. If that is
-  // not enough, we bail out. We only decompress class files, so they should
-  // be smaller than 64K anyway, but we give a little leeway.
-  // MAX_BUFFER_SIZE must be bigger than the size of the biggest file in the
-  // ZIP. It is set to 128M here so we can uncompress the Bazel server with
-  // this library.
-  static const size_t INITIAL_BUFFER_SIZE = 256 * 1024;  // 256K
-  static const size_t MAX_BUFFER_SIZE = 128 * 1024 * 1024;
-  static const size_t MAX_MAPPED_REGION = 32 * 1024 * 1024;
-
-  // These metadata fields are the fields of the ZIP header of the file being
-  // processed.
-  u2 extract_version_;
-  u2 general_purpose_bit_flag_;
-  u2 compression_method_;
-  u4 uncompressed_size_;
-  u4 compressed_size_;
-  u2 file_name_length_;
-  u2 extra_field_length_;
-  const u1 *file_name_;
-  const u1 *extra_field_;
-
-  // Administration of memory reserved for decompressed data. We use the same
-  // buffer for each file to avoid some malloc()/free() calls and free the
-  // memory only in the dtor. C-style memory management is used so that we
-  // can call realloc.
-  u1 *uncompressed_data_;
-  size_t uncompressed_data_allocated_;
-
-  // Copy of the last filename entry - Null-terminated.
-  char filename[PATH_MAX];
-  // The external file attribute field
-  u4 attr;
-
-  // last error
-  char errmsg[4*PATH_MAX];
-
-  int error(const char *fmt, ...) {
-    va_list ap;
-    va_start(ap, fmt);
-    vsnprintf(errmsg, 4*PATH_MAX, fmt, ap);
-    va_end(ap);
-    return -1;
-  }
-
-  // Check that at least n bytes remain in the input file, otherwise
-  // abort with an error message.  "state" is the name of the field
-  // we're about to read, for diagnostics.
-  int EnsureRemaining(size_t n, const char *state) {
-    size_t in_offset = p - zipdata_in_;
-    size_t remaining = in_length_ - in_offset;
-    if (n > remaining) {
-      return error("Premature end of file (at offset %zd, state=%s); "
-                   "expected %zd more bytes but found %zd.\n",
-                   in_offset, state, n, remaining);
-    }
-    return 0;
-  }
-
-  // Read one entry from input zip file
-  int ProcessLocalFileEntry(size_t compressed_size, size_t uncompressed_size);
-
-  // Uncompress a file from the archive using zlib. The pointer returned
-  // is owned by InputZipFile, so it must not be freed. Advances the input
-  // cursor to the first byte after the compressed data.
-  u1* UncompressFile();
-
-  // Skip a file
-  int SkipFile(const bool compressed);
-
-  // Process a file
-  int ProcessFile(const bool compressed);
-};
-
-//
-// A class implementing ZipBuilder that represent an open zip file for writing.
-//
-class OutputZipFile : public ZipBuilder {
- public:
-  OutputZipFile(int fd, u1 * const zipdata_out) :
-      fd_out(fd),
-      zipdata_out_(zipdata_out),
-      q(zipdata_out) {
-    errmsg[0] = 0;
-  }
-
-  virtual const char* GetError() {
-    if (errmsg[0] == 0) {
-      return NULL;
-    }
-    return errmsg;
-  }
-
-  virtual ~OutputZipFile() { Finish(); }
-  virtual u1* NewFile(const char* filename, const u4 attr);
-  virtual int FinishFile(size_t filelength, bool compress = false,
-                         bool compute_crc = false);
-  virtual int WriteEmptyFile(const char *filename);
-  virtual size_t GetSize() {
-    return Offset(q);
-  }
-  virtual int GetNumberFiles() {
-    return entries_.size();
-  }
-  virtual int Finish();
-
- private:
-  struct LocalFileEntry {
-    // Start of the local header (in the output buffer).
-    size_t local_header_offset;
-
-    // Sizes of the file entry
-    size_t uncompressed_length;
-    size_t compressed_length;
-
-    // Compression method
-    u2 compression_method;
-
-    // CRC32
-    u4 crc32;
-
-    // external attributes field
-    u4 external_attr;
-
-    // Start/length of the file_name in the local header.
-    u1 *file_name;
-    u2 file_name_length;
-
-    // Start/length of the extra_field in the local header.
-    const u1 *extra_field;
-    u2 extra_field_length;
-  };
-
-  int fd_out;  // file descriptor for the output file
-
-  // OutputZipFile is responsible for maintaining the following
-  // pointers. They are allocated by the Create() method before
-  // the object is actually created using mmap.
-  u1 * const zipdata_out_;        // start of output file mmap
-  u1 *q;  // output cursor
-
-  u1 *header_ptr;  // Current pointer to "compression method" entry.
-
-  // List of entries to write the central directory
-  std::vector<LocalFileEntry*> entries_;
-
-  // last error
-  char errmsg[4*PATH_MAX];
-
-  int error(const char *fmt, ...) {
-    va_list ap;
-    va_start(ap, fmt);
-    vsnprintf(errmsg, 4*PATH_MAX, fmt, ap);
-    va_end(ap);
-    return -1;
-  }
-
-  // Write the ZIP central directory structure for each local file
-  // entry in "entries".
-  void WriteCentralDirectory();
-
-  // Returns the offset of the pointer relative to the start of the
-  // output zip file.
-  size_t Offset(const u1 *const x) {
-    return x - zipdata_out_;
-  }
-
-  // Write ZIP file header in the output. Since the compressed size is not
-  // known in advance, it must be recorded later. This method returns a pointer
-  // to "compressed size" in the file header that should be passed to
-  // WriteFileSizeInLocalFileHeader() later.
-  u1* WriteLocalFileHeader(const char *filename, const u4 attr);
-
-  // Fill in the "compressed size" and "uncompressed size" fields in a local
-  // file header previously written by WriteLocalFileHeader().
-  size_t WriteFileSizeInLocalFileHeader(u1 *header_ptr,
-                                        size_t out_length,
-                                        bool compress = false,
-                                        const u4 crc = 0);
-};
-
-//
-// Implementation of InputZipFile
-//
-bool InputZipFile::ProcessNext() {
-  // Process the next entry in the central directory. Also make sure that the
-  // content pointer is in sync.
-  size_t compressed, uncompressed;
-  u4 offset;
-  if (!ProcessCentralDirEntry(central_dir_current_, &compressed, &uncompressed,
-                              filename, PATH_MAX, &attr, &offset)) {
-    return false;
-  }
-
-  // There might be an offset specified in the central directory that does
-  // not match the file offset, if so, correct the pointer.
-  if (offset != 0 && (p != (zipdata_in_ + in_offset_ + offset))) {
-    p = zipdata_in_ + offset;
-  }
-
-  if (EnsureRemaining(4, "signature") < 0) {
-    return false;
-  }
-  u4 signature = get_u4le(p);
-  if (signature == LOCAL_FILE_HEADER_SIGNATURE) {
-    if (ProcessLocalFileEntry(compressed, uncompressed) < 0) {
-      return false;
-    }
-  } else {
-    error("local file header signature for file %s not found\n", filename);
-    return false;
-  }
-
-  return true;
-}
-
-int InputZipFile::ProcessLocalFileEntry(
-    size_t compressed_size, size_t uncompressed_size) {
-  if (EnsureRemaining(26, "extract_version") < 0) {
-    return -1;
-  }
-  extract_version_ = get_u2le(p);
-  general_purpose_bit_flag_ = get_u2le(p);
-
-  if ((general_purpose_bit_flag_ & ~GENERAL_PURPOSE_BIT_FLAG_SUPPORTED) != 0) {
-    return error("Unsupported value (0x%04x) in general purpose bit flag.\n",
-                 general_purpose_bit_flag_);
-  }
-
-  compression_method_ = get_u2le(p);
-
-  if (compression_method_ != COMPRESSION_METHOD_DEFLATED &&
-      compression_method_ != COMPRESSION_METHOD_STORED) {
-    return error("Unsupported compression method (%d).\n",
-                 compression_method_);
-  }
-
-  // skip over: last_mod_file_time, last_mod_file_date, crc32
-  p += 2 + 2 + 4;
-  compressed_size_ = get_u4le(p);
-  uncompressed_size_ = get_u4le(p);
-  file_name_length_ = get_u2le(p);
-  extra_field_length_ = get_u2le(p);
-
-  if (EnsureRemaining(file_name_length_, "file_name") < 0) {
-    return -1;
-  }
-  file_name_ = p;
-  p += file_name_length_;
-
-  if (EnsureRemaining(extra_field_length_, "extra_field") < 0) {
-    return -1;
-  }
-  extra_field_ = p;
-  p += extra_field_length_;
-
-  bool is_compressed = compression_method_ == COMPRESSION_METHOD_DEFLATED;
-
-  // If the zip is compressed, compressed and uncompressed size members are
-  // zero in the local file header. If not, check that they are the same as the
-  // lengths from the central directory, otherwise, just believe the central
-  // directory
-  if (compressed_size_ == 0) {
-    compressed_size_ = compressed_size;
-  } else {
-    if (compressed_size_ != compressed_size) {
-      return error("central directory and file header inconsistent\n");
-    }
-  }
-
-  if (uncompressed_size_ == 0) {
-    uncompressed_size_ = uncompressed_size;
-  } else {
-    if (uncompressed_size_ != uncompressed_size) {
-      return error("central directory and file header inconsistent\n");
-    }
-  }
-
-  if (processor->Accept(filename, attr)) {
-    if (ProcessFile(is_compressed) < 0) {
-      return -1;
-    }
-  } else {
-    if (SkipFile(is_compressed) < 0) {
-      return -1;
-    }
-  }
-
-  if (general_purpose_bit_flag_ & GENERAL_PURPOSE_BIT_FLAG_COMPRESSED) {
-    // Skip the data descriptor. Some implementations do not put the signature
-    // here, so check if the next 4 bytes are a signature, and if so, skip the
-    // next 12 bytes (for CRC, compressed/uncompressed size), otherwise skip
-    // the next 8 bytes (because the value just read was the CRC).
-    u4 signature = get_u4le(p);
-    if (signature == DATA_DESCRIPTOR_SIGNATURE) {
-      p += 4 * 3;
-    } else {
-      p += 4 * 2;
-    }
-  }
-
-  if (p > zipdata_in_mapped_ + MAX_MAPPED_REGION) {
-    munmap(const_cast<u1 *>(zipdata_in_mapped_), MAX_MAPPED_REGION);
-    zipdata_in_mapped_ += MAX_MAPPED_REGION;
-  }
-
-  return 0;
-}
-
-int InputZipFile::SkipFile(const bool compressed) {
-  if (!compressed) {
-    // In this case, compressed_size_ == uncompressed_size_ (since the file is
-    // uncompressed), so we can use either.
-    if (compressed_size_ != uncompressed_size_) {
-      return error("compressed size != uncompressed size, although the file "
-                   "is uncompressed.\n");
-    }
-  }
-
-  if (EnsureRemaining(compressed_size_, "file_data") < 0) {
-    return -1;
-  }
-  p += compressed_size_;
-  return 0;
-}
-
-u1* InputZipFile::UncompressFile() {
-  size_t in_offset = p - zipdata_in_;
-  size_t remaining = in_length_ - in_offset;
-  z_stream stream;
-
-  stream.zalloc = Z_NULL;
-  stream.zfree = Z_NULL;
-  stream.opaque = Z_NULL;
-  stream.avail_in = remaining;
-  stream.next_in = (Bytef *) p;
-
-  int ret = inflateInit2(&stream, -MAX_WBITS);
-  if (ret != Z_OK) {
-    error("inflateInit: %d\n", ret);
-    return NULL;
-  }
-
-  int uncompressed_until_now = 0;
-
-  while (true) {
-    stream.avail_out = uncompressed_data_allocated_ - uncompressed_until_now;
-    stream.next_out = uncompressed_data_ + uncompressed_until_now;
-    int old_avail_out = stream.avail_out;
-
-    ret = inflate(&stream, Z_SYNC_FLUSH);
-    int uncompressed_now = old_avail_out - stream.avail_out;
-    uncompressed_until_now += uncompressed_now;
-
-    switch (ret) {
-      case Z_STREAM_END: {
-        // zlib said that there is no more data to decompress.
-
-        u1 *new_p = reinterpret_cast<u1*>(stream.next_in);
-        compressed_size_ = new_p - p;
-        uncompressed_size_ = uncompressed_until_now;
-        p = new_p;
-        inflateEnd(&stream);
-        return uncompressed_data_;
-      }
-
-      case Z_OK: {
-        // zlib said that there is no more room in the buffer allocated for
-        // the decompressed data. Enlarge that buffer and try again.
-
-        if (uncompressed_data_allocated_ == MAX_BUFFER_SIZE) {
-          error("ijar does not support decompressing files "
-                "larger than %dMB.\n",
-                (int) (MAX_BUFFER_SIZE/(1024*1024)));
-          return NULL;
-        }
-
-        uncompressed_data_allocated_ *= 2;
-        if (uncompressed_data_allocated_ > MAX_BUFFER_SIZE) {
-          uncompressed_data_allocated_ = MAX_BUFFER_SIZE;
-        }
-
-        uncompressed_data_ = reinterpret_cast<u1*>(
-            realloc(uncompressed_data_, uncompressed_data_allocated_));
-        break;
-      }
-
-      case Z_DATA_ERROR:
-      case Z_BUF_ERROR:
-      case Z_STREAM_ERROR:
-      case Z_NEED_DICT:
-      default: {
-        error("zlib returned error code %d during inflate.\n", ret);
-        return NULL;
-      }
-    }
-  }
-}
-
-int InputZipFile::ProcessFile(const bool compressed) {
-  const u1 *file_data;
-  if (compressed) {
-    file_data = UncompressFile();
-    if (file_data == NULL) {
-      return -1;
-    }
-  } else {
-    // In this case, compressed_size_ == uncompressed_size_ (since the file is
-    // uncompressed), so we can use either.
-    if (compressed_size_ != uncompressed_size_) {
-      return error("compressed size != uncompressed size, although the file "
-                   "is uncompressed.\n");
-    }
-
-    if (EnsureRemaining(compressed_size_, "file_data") < 0) {
-      return -1;
-    }
-    file_data = p;
-    p += compressed_size_;
-  }
-  processor->Process(filename, attr, file_data, uncompressed_size_);
-  return 0;
-}
-
-
-// Reads and returns some metadata of the next file from the central directory:
-// - compressed size
-// - uncompressed size
-// - whether the entry is a class file (to be included in the output).
-// Precondition: p points to the beginning of an entry in the central dir
-// Postcondition: p points to the beginning of the next entry in the central dir
-// Returns true if the central directory contains another file and false if not.
-// Of course, in the latter case, the size output variables are not changed.
-// Note that the central directory is always followed by another data structure
-// that has a signature, so parsing it this way is safe.
-static bool ProcessCentralDirEntry(
-    const u1 *&p, size_t *compressed_size, size_t *uncompressed_size,
-    char *filename, size_t filename_size, u4 *attr, u4 *offset) {
-  u4 signature = get_u4le(p);
-  if (signature != CENTRAL_FILE_HEADER_SIGNATURE) {
-    return false;
-  }
-
-  p += 16;  // skip to 'compressed size' field
-  *compressed_size = get_u4le(p);
-  *uncompressed_size = get_u4le(p);
-  u2 file_name_length = get_u2le(p);
-  u2 extra_field_length = get_u2le(p);
-  u2 file_comment_length = get_u2le(p);
-  p += 4;  // skip to external file attributes field
-  *attr = get_u4le(p);
-  *offset = get_u4le(p);
-  {
-    size_t len = (file_name_length < filename_size)
-      ? file_name_length
-      : (filename_size - 1);
-    memcpy(reinterpret_cast<void*>(filename), p, len);
-    filename[len] = 0;
-  }
-  p += file_name_length;
-  p += extra_field_length;
-  p += file_comment_length;
-  return true;
-}
-
-// Gives a maximum bound on the size of the interface JAR. Basically, adds
-// the difference between the compressed and uncompressed sizes to the size
-// of the input file.
-u8 InputZipFile::CalculateOutputLength() {
-  const u1* current = central_dir_;
-
-  u8 compressed_size = 0;
-  u8 uncompressed_size = 0;
-  u8 skipped_compressed_size = 0;
-  u4 attr;
-  u4 offset;
-  char filename[PATH_MAX];
-
-  while (true) {
-    size_t file_compressed, file_uncompressed;
-    if (!ProcessCentralDirEntry(current,
-                                &file_compressed, &file_uncompressed,
-                                filename, PATH_MAX, &attr, &offset)) {
-      break;
-    }
-
-    if (processor->Accept(filename, attr)) {
-      compressed_size += (u8) file_compressed;
-      uncompressed_size += (u8) file_uncompressed;
-    } else {
-      skipped_compressed_size += file_compressed;
-    }
-  }
-
-  // The worst case is when the output is simply the input uncompressed. The
-  // metadata in the zip file will stay the same, so the file will grow by the
-  // difference between the compressed and uncompressed sizes.
-  return (u8) in_length_ - skipped_compressed_size
-      + (uncompressed_size - compressed_size);
-}
-
-// Given the data in the zip file, returns the offset of the central directory
-// and the number of files contained in it.
-bool FindZipCentralDirectory(const u1* bytes, size_t in_length,
-                             u4* offset, const u1** central_dir) {
-  static const int MAX_COMMENT_LENGTH = 0xffff;
-  static const int CENTRAL_DIR_LOCATOR_SIZE = 22;
-  // Maximum distance of start of central dir locator from end of file
-  static const int MAX_DELTA = MAX_COMMENT_LENGTH + CENTRAL_DIR_LOCATOR_SIZE;
-  const u1* last_pos_to_check = in_length < MAX_DELTA
-      ? bytes
-      : bytes + (in_length - MAX_DELTA);
-  const u1* current;
-  bool found = false;
-
-  for (current = bytes + in_length - CENTRAL_DIR_LOCATOR_SIZE;
-       current >= last_pos_to_check;
-       current-- ) {
-    const u1* p = current;
-    if (get_u4le(p) != END_OF_CENTRAL_DIR_SIGNATURE) {
-      continue;
-    }
-
-    p += 16;  // skip to comment length field
-    u2 comment_length = get_u2le(p);
-
-    // Does the comment go exactly till the end of the file?
-    if (current + comment_length + CENTRAL_DIR_LOCATOR_SIZE
-        != bytes + in_length) {
-      continue;
-    }
-
-    // Hooray, we found it!
-    found = true;
-    break;
-  }
-
-  if (!found) {
-    fprintf(stderr, "file is invalid or corrupted (missing end of central "
-                    "directory record)\n");
-    return false;
-  }
-
-  const u1* end_of_central_dir = current;
-  get_u4le(current);  // central directory locator signature, already checked
-  u2 number_of_this_disk = get_u2le(current);
-  u2 disk_with_central_dir = get_u2le(current);
-  u2 central_dir_entries_on_this_disk = get_u2le(current);
-  u2 central_dir_entries = get_u2le(current);
-  u4 central_dir_size = get_u4le(current);
-  u4 central_dir_offset = get_u4le(current);
-  u2 file_comment_length = get_u2le(current);
-  current += file_comment_length;  // set current to the end of the central dir
-
-  if (number_of_this_disk != 0
-    || disk_with_central_dir != 0
-    || central_dir_entries_on_this_disk != central_dir_entries) {
-    fprintf(stderr, "multi-disk JAR files are not supported\n");
-    return false;
-  }
-
-  // Do not change output values before determining that they are OK.
-  *offset = central_dir_offset;
-  // Central directory start can then be used to determine the actual
-  // starts of the zip file (which can be different in case of a non-zip
-  // header like for auto-extractable binaries).
-  *central_dir = end_of_central_dir - central_dir_size;
-  return true;
-}
-
-void InputZipFile::Reset() {
-  central_dir_current_ = central_dir_;
-  zipdata_in_mapped_ = zipdata_in_;
-  p = zipdata_in_ + in_offset_;
-}
-
-int ZipExtractor::ProcessAll() {
-  while (ProcessNext()) {}
-  if (GetError() != NULL) {
-    return -1;
-  }
-  return 0;
-}
-
-ZipExtractor* ZipExtractor::Create(const char* filename,
-                                   ZipExtractorProcessor *processor) {
-  int fd_in = open(filename, O_RDONLY);
-  if (fd_in < 0) {
-    return NULL;
-  }
-
-  off_t length = lseek(fd_in, 0, SEEK_END);
-  if (length < 0) {
-    return NULL;
-  }
-
-  void *zipdata_in = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd_in, 0);
-  if (zipdata_in == MAP_FAILED) {
-    return NULL;
-  }
-
-  u4 central_dir_offset;
-  const u1 *central_dir = NULL;
-
-  if (!devtools_ijar::FindZipCentralDirectory(
-          static_cast<const u1*>(zipdata_in), length,
-          &central_dir_offset, &central_dir)) {
-    errno = EIO;  // we don't really have a good error number
-    return NULL;
-  }
-  const u1 *zipdata_start = static_cast<const u1*>(zipdata_in);
-  off_t offset = - static_cast<off_t>(zipdata_start
-                                      + central_dir_offset
-                                      - central_dir);
-
-  return new InputZipFile(processor, fd_in, length, offset,
-                          zipdata_start, central_dir);
-}
-
-InputZipFile::InputZipFile(ZipExtractorProcessor *processor, int fd,
-                           off_t in_length, off_t in_offset,
-                           const u1* zipdata_in, const u1* central_dir)
-  : processor(processor), fd_in(fd),
-    zipdata_in_(zipdata_in), zipdata_in_mapped_(zipdata_in),
-    central_dir_(central_dir), in_length_(in_length), in_offset_(in_offset),
-    p(zipdata_in + in_offset), central_dir_current_(central_dir) {
-  uncompressed_data_allocated_ = INITIAL_BUFFER_SIZE;
-  uncompressed_data_ =
-    reinterpret_cast<u1*>(malloc(uncompressed_data_allocated_));
-  errmsg[0] = 0;
-}
-
-InputZipFile::~InputZipFile() {
-  free(uncompressed_data_);
-  close(fd_in);
-}
-
-
-//
-// Implementation of OutputZipFile
-//
-int OutputZipFile::WriteEmptyFile(const char *filename) {
-  const u1* file_name = (const u1*) filename;
-  size_t file_name_length = strlen(filename);
-
-  LocalFileEntry *entry = new LocalFileEntry;
-  entry->local_header_offset = Offset(q);
-  entry->external_attr = 0;
-  entry->crc32 = 0;
-
-  // Output the ZIP local_file_header:
-  put_u4le(q, LOCAL_FILE_HEADER_SIGNATURE);
-  put_u2le(q, 10);  // extract_version
-  put_u2le(q, 0);  // general_purpose_bit_flag
-  put_u2le(q, 0);  // compression_method
-  put_u2le(q, 0);  // last_mod_file_time
-  put_u2le(q, 0);  // last_mod_file_date
-  put_u4le(q, entry->crc32);  // crc32
-  put_u4le(q, 0);  // compressed_size
-  put_u4le(q, 0);  // uncompressed_size
-  put_u2le(q, file_name_length);
-  put_u2le(q, 0);  // extra_field_length
-  put_n(q, file_name, file_name_length);
-
-  entry->file_name_length = file_name_length;
-  entry->extra_field_length = 0;
-  entry->compressed_length = 0;
-  entry->uncompressed_length = 0;
-  entry->compression_method = 0;
-  entry->extra_field = (const u1 *)"";
-  entry->file_name = (u1*) strdup((const char *) file_name);
-  entries_.push_back(entry);
-
-  return 0;
-}
-
-void OutputZipFile::WriteCentralDirectory() {
-  // central directory:
-  const u1 *central_directory_start = q;
-  for (size_t ii = 0; ii < entries_.size(); ++ii) {
-    LocalFileEntry *entry = entries_[ii];
-    put_u4le(q, CENTRAL_FILE_HEADER_SIGNATURE);
-    put_u2le(q, 0);  // version made by
-
-    put_u2le(q, ZIP_VERSION_TO_EXTRACT);  // version to extract
-    put_u2le(q, 0);  // general purpose bit flag
-    put_u2le(q, entry->compression_method);  // compression method:
-    put_u2le(q, 0);                          // last_mod_file_time
-    put_u2le(q, 0);  // last_mod_file_date
-    put_u4le(q, entry->crc32);  // crc32
-    put_u4le(q, entry->compressed_length);    // compressed_size
-    put_u4le(q, entry->uncompressed_length);  // uncompressed_size
-    put_u2le(q, entry->file_name_length);
-    put_u2le(q, entry->extra_field_length);
-
-    put_u2le(q, 0);  // file comment length
-    put_u2le(q, 0);  // disk number start
-    put_u2le(q, 0);  // internal file attributes
-    put_u4le(q, entry->external_attr);  // external file attributes
-    // relative offset of local header:
-    put_u4le(q, entry->local_header_offset);
-
-    put_n(q, entry->file_name, entry->file_name_length);
-    put_n(q, entry->extra_field, entry->extra_field_length);
-  }
-  u4 central_directory_size = q - central_directory_start;
-
-  put_u4le(q, END_OF_CENTRAL_DIR_SIGNATURE);
-  put_u2le(q, 0);  // number of this disk
-  put_u2le(q, 0);  // number of the disk with the start of the central directory
-  put_u2le(q, entries_.size());  // # central dir entries on this disk
-  put_u2le(q, entries_.size());  // total # entries in the central directory
-  put_u4le(q, central_directory_size);  // size of the central directory
-  put_u4le(q, Offset(central_directory_start));  // offset of start of central
-                                                 // directory wrt starting disk
-  put_u2le(q, 0);  // .ZIP file comment length
-}
-
-u1* OutputZipFile::WriteLocalFileHeader(const char* filename, const u4 attr) {
-  off_t file_name_length_ = strlen(filename);
-  LocalFileEntry *entry = new LocalFileEntry;
-  entry->local_header_offset = Offset(q);
-  entry->file_name_length = file_name_length_;
-  entry->file_name = new u1[file_name_length_];
-  entry->external_attr = attr;
-  memcpy(entry->file_name, filename, file_name_length_);
-  entry->extra_field_length = 0;
-  entry->extra_field = (const u1 *)"";
-  entry->crc32 = 0;
-
-  // Output the ZIP local_file_header:
-  put_u4le(q, LOCAL_FILE_HEADER_SIGNATURE);
-  put_u2le(q, ZIP_VERSION_TO_EXTRACT);     // version to extract
-  put_u2le(q, 0);                          // general purpose bit flag
-  u1 *header_ptr = q;
-  put_u2le(q, COMPRESSION_METHOD_STORED);  // compression method = placeholder
-  put_u2le(q, 0);                          // last_mod_file_time
-  put_u2le(q, 0);                          // last_mod_file_date
-  put_u4le(q, entry->crc32);               // crc32
-  put_u4le(q, 0);  // compressed_size = placeholder
-  put_u4le(q, 0);  // uncompressed_size = placeholder
-  put_u2le(q, entry->file_name_length);
-  put_u2le(q, entry->extra_field_length);
-
-  put_n(q, entry->file_name, entry->file_name_length);
-  put_n(q, entry->extra_field, entry->extra_field_length);
-  entries_.push_back(entry);
-
-  return header_ptr;
-}
-
-// Try to compress a file entry in memory using the deflate algorithm.
-// It will compress buf (of size length) unless the compressed size is bigger
-// than the input size. The result will overwrite the content of buf and the
-// final size is returned.
-size_t TryDeflate(u1 *buf, size_t length) {
-  u1 *outbuf = reinterpret_cast<u1 *>(malloc(length));
-  z_stream stream;
-
-  // Initialize the z_stream strcut for reading from buf and wrinting in outbuf.
-  stream.zalloc = Z_NULL;
-  stream.zfree = Z_NULL;
-  stream.opaque = Z_NULL;
-  stream.total_in = length;
-  stream.avail_in = length;
-  stream.total_out = length;
-  stream.avail_out = length;
-  stream.next_in = buf;
-  stream.next_out = outbuf;
-
-  // deflateInit2 negative windows size prevent the zlib wrapper to be used.
-  if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED,
-                  -MAX_WBITS, 8, Z_DEFAULT_STRATEGY) != Z_OK) {
-    // Failure to compress => return the buffer uncompressed
-    free(outbuf);
-    return length;
-  }
-
-  if (deflate(&stream, Z_FINISH) == Z_STREAM_END) {
-    // Compression successful and fits in outbuf, let's copy the result in buf.
-    length = stream.total_out;
-    memcpy(buf, outbuf, length);
-  }
-
-  deflateEnd(&stream);
-  free(outbuf);
-
-  // Return the length of the resulting buffer
-  return length;
-}
-
-size_t OutputZipFile::WriteFileSizeInLocalFileHeader(u1 *header_ptr,
-                                                     size_t out_length,
-                                                     bool compress,
-                                                     const u4 crc) {
-  size_t compressed_size = out_length;
-  if (compress) {
-    compressed_size = TryDeflate(q, out_length);
-  }
-  // compression method
-  if (compressed_size < out_length) {
-    put_u2le(header_ptr, COMPRESSION_METHOD_DEFLATED);
-  } else {
-    put_u2le(header_ptr, COMPRESSION_METHOD_STORED);
-  }
-  header_ptr += 4;
-  put_u4le(header_ptr, crc);              // crc32
-  put_u4le(header_ptr, compressed_size);  // compressed_size
-  put_u4le(header_ptr, out_length);       // uncompressed_size
-  return compressed_size;
-}
-
-int OutputZipFile::Finish() {
-  if (fd_out > 0) {
-    WriteCentralDirectory();
-    if (ftruncate(fd_out, GetSize()) < 0) {
-      return error("ftruncate(fd_out, GetSize()): %s", strerror(errno));
-    }
-    if (close(fd_out) < 0) {
-      return error("close(fd_out): %s", strerror(errno));
-    }
-    fd_out = -1;
-  }
-  return 0;
-}
-
-u1* OutputZipFile::NewFile(const char* filename, const u4 attr) {
-  header_ptr = WriteLocalFileHeader(filename, attr);
-  return q;
-}
-
-int OutputZipFile::FinishFile(size_t filelength, bool compress,
-                              bool compute_crc) {
-  u4 crc = 0;
-  if (compute_crc) {
-    crc = crc32(crc, q, filelength);
-  }
-  size_t compressed_size =
-      WriteFileSizeInLocalFileHeader(header_ptr, filelength, compress, crc);
-  entries_.back()->crc32 = crc;
-  entries_.back()->compressed_length = compressed_size;
-  entries_.back()->uncompressed_length = filelength;
-  if (compressed_size < filelength) {
-    entries_.back()->compression_method = COMPRESSION_METHOD_DEFLATED;
-  } else {
-    entries_.back()->compression_method = COMPRESSION_METHOD_STORED;
-  }
-  q += compressed_size;
-  return 0;
-}
-
-ZipBuilder* ZipBuilder::Create(const char* zip_file, u8 estimated_size) {
-  if (estimated_size > kMaximumOutputSize) {
-    fprintf(stderr,
-            "Uncompressed input jar has size %llu, "
-            "which exceeds the maximum supported output size %llu.\n"
-            "Assuming that ijar will be smaller and hoping for the best.\n",
-            estimated_size, kMaximumOutputSize);
-    estimated_size = kMaximumOutputSize;
-  }
-
-  int fd_out = open(zip_file, O_CREAT|O_RDWR|O_TRUNC, 0644);
-  if (fd_out < 0) {
-    return NULL;
-  }
-
-  // Create mmap-able sparse file
-  if (ftruncate(fd_out, estimated_size) < 0) {
-    return NULL;
-  }
-
-  // Ensure that any buffer overflow in JarStripper will result in
-  // SIGSEGV or SIGBUS by over-allocating beyond the end of the file.
-  size_t mmap_length = std::min(estimated_size + sysconf(_SC_PAGESIZE),
-                                (u8) std::numeric_limits<size_t>::max());
-
-  void *zipdata_out = mmap(NULL, mmap_length, PROT_WRITE,
-                           MAP_SHARED, fd_out, 0);
-  if (zipdata_out == MAP_FAILED) {
-    fprintf(stderr, "output_length=%llu\n", estimated_size);
-    return NULL;
-  }
-
-  return new OutputZipFile(fd_out, (u1*) zipdata_out);
-}
-
-u8 ZipBuilder::EstimateSize(char **files) {
-  struct stat statst;
-  // Digital signature field size = 6, End of central directory = 22, Total = 28
-  u8 size = 28;
-  // Count the size of all the files in the input to estimate the size of the
-  // output.
-  for (int i = 0; files[i] != NULL; i++) {
-    if (stat(files[i], &statst) != 0) {
-      fprintf(stderr, "File %s does not seem to exist.", files[i]);
-      return 0;
-    }
-    size += statst.st_size;
-    // Add sizes of Zip meta data
-    // local file header = 30 bytes
-    // data descriptor = 12 bytes
-    // central directory descriptor = 46 bytes
-    //    Total: 88bytes
-    size += 88;
-    // The filename is stored twice (once in the central directory
-    // and once in the local file header).
-    size += strlen(files[i]) * 2;
-  }
-  return size;
-}
-
-}  // namespace devtools_ijar
diff --git a/tools/ijar/zip.h b/tools/ijar/zip.h
deleted file mode 100644
index dda2c6e..0000000
--- a/tools/ijar/zip.h
+++ /dev/null
@@ -1,173 +0,0 @@
-// Copyright 2015 Google Inc. All rights reserved.
-//
-// 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.
-//
-// zip.h -- .zip (.jar) file reading/writing routines.
-//
-// This file specifies the interface to use the ZIP implementation of ijar.
-//
-
-#ifndef INCLUDED_THIRD_PARTY_IJAR_ZIP_H
-#define INCLUDED_THIRD_PARTY_IJAR_ZIP_H
-
-#include <sys/stat.h>
-
-#include "common.h"
-
-namespace devtools_ijar {
-
-// Tells if this is a directory entry from the mode. This method
-// is safer than zipattr_to_mode(attr) & S_IFDIR because the unix
-// mode might not be set in DOS zip files.
-inline bool zipattr_is_dir(u4 attr) { return (attr & 0x10) != 0; }
-
-// Convert a Unix file mode to a ZIP file attribute
-inline u4 mode_to_zipattr(mode_t m) {
-  return (((u4) m) << 16) + ((m & S_IFDIR) != 0 ? 0x10 : 0);
-}
-
-// Convert a ZIP file attribute to a Unix file mode
-inline mode_t zipattr_to_mode(u4 attr) {
-  return ((mode_t) ((attr >> 16) & 0xffff));
-}
-
-//
-// Class interface for building ZIP files
-//
-class ZipBuilder {
- public:
-  virtual ~ZipBuilder() {}
-
-  // Returns the text for the last error, or null on no last error.
-  virtual const char* GetError() = 0;
-
-  // Add a new file to the ZIP, the file will have path "filename"
-  // and external attributes "attr". This function returns a pointer
-  // to a memory buffer to write the data of the file into. This buffer
-  // is owned by ZipBuilder and should not be free'd by the caller. The
-  // file length is then specified when the files is finished written
-  // using the FinishFile(size_t) function.
-  // On failure, returns NULL and GetError() will return an non-empty message.
-  virtual u1* NewFile(const char* filename, const u4 attr) = 0;
-
-  // Finish writing a file and specify its length. After calling this method
-  // one should not reuse the pointer given by NewFile. The file can be
-  // compressed using the deflate algorithm by setting `compress` to true.
-  // By default, CRC32 are not computed as java tooling doesn't care, but
-  // computing it can be activated by setting `compute_crc` to true.
-  // On failure, returns -1 and GetError() will return an non-empty message.
-  virtual int FinishFile(size_t filelength,
-                         bool compress = false,
-                         bool compute_crc = false) = 0;
-
-  // Write an empty file, it is equivalent to:
-  //   NewFile(filename, 0);
-  //   FinishFile(0);
-  // On failure, returns -1 and GetError() will return an non-empty message.
-  virtual int WriteEmptyFile(const char* filename) = 0;
-
-  // Finish writing the ZIP file. This method can be called only once
-  // (subsequent calls will do nothing) and none of
-  // NewFile/FinishFile/WriteEmptyFile should be called after calling Finish. If
-  // this method was not called when the object is destroyed, it will be called.
-  // It is here as a convenience to get information on the final generated ZIP
-  // file.
-  // On failure, returns -1 and GetError() will return an non-empty message.
-  virtual int Finish() = 0;
-
-  // Get the current size of the ZIP file. This size will not be matching the
-  // final ZIP file until Finish() has been called because Finish() is actually
-  // writing the central directory of the ZIP File.
-  virtual size_t GetSize() = 0;
-
-  // Returns the current number of files stored in the ZIP.
-  virtual int GetNumberFiles() = 0;
-
-  // Create a new ZipBuilder writing the file zip_file and the size of the
-  // output will be at most estimated_size. Use ZipBuilder::EstimateSize() or
-  // ZipExtractor::CalculateOuputLength() to have an estimated_size depending on
-  // a list of file to store.
-  // On failure, returns NULL. Refer to errno for error code.
-  static ZipBuilder* Create(const char* zip_file, u8 estimated_size);
-
-  // Estimate the maximum size of the ZIP files containing files in the "files"
-  // null-terminated array.
-  // Returns 0 on error.
-  static u8 EstimateSize(char **files);
-};
-
-//
-// An abstract class to process data from a ZipExtractor.
-// Derive from this class if you wish to process data from a ZipExtractor.
-//
-class ZipExtractorProcessor {
- public:
-  virtual ~ZipExtractorProcessor() {}
-
-  // Tells whether to skip or process the file "filename". "attr" is the
-  // external file attributes and can be converted to unix mode using the
-  // zipattr_to_mode() function. This method is suppoed to returns true
-  // if the file should be processed and false if it should be skipped.
-  virtual bool Accept(const char* filename, const u4 attr) = 0;
-
-  // Process a file accepted by Accept. The file "filename" has external
-  // attributes "attr" and length "size". The file content is accessible
-  // in the buffer pointed by "data".
-  virtual void Process(const char* filename, const u4 attr,
-                       const u1* data, const size_t size) = 0;
-};
-
-//
-// Class interface for reading ZIP files
-//
-class ZipExtractor {
- public:
-  virtual ~ZipExtractor() {}
-
-  // Returns the text for the last error, or null on no last error.
-  virtual const char* GetError() = 0;
-
-  // Process the next files, returns false if the end of ZIP file has been
-  // reached. The processor provided by the Create method will be called
-  // if a file is encountered. If false is returned, check the return value
-  // of GetError() for potential errors.
-  virtual bool ProcessNext() = 0;
-
-  // Process the all files, returns -1 on error (GetError() will be populated
-  // on error).
-  virtual int ProcessAll();
-
-  // Reset the file pointer to the beginning.
-  virtual void Reset() = 0;
-
-  // Return the size of the ZIP file.
-  virtual size_t GetSize() = 0;
-
-  // Return the size of the resulting zip file by keeping only file
-  // accepted by the processor and storing them uncompressed. This
-  // method can be used to create a ZipBuilder for storing a subset
-  // of the input files.
-  // On error, 0 is returned and GetError() returns a non-empty message.
-  virtual u8 CalculateOutputLength() = 0;
-
-  // Create a ZipExtractor that extract the zip file "filename" and process
-  // it with "processor".
-  // On error, a null pointer is returned and the value of errno should be
-  // checked.
-  static ZipExtractor* Create(const char* filename,
-                              ZipExtractorProcessor *processor);
-};
-
-}  // namespace devtools_ijar
-
-#endif  // INCLUDED_THIRD_PARTY_IJAR_ZIP_H
diff --git a/tools/ijar/zip_main.cc b/tools/ijar/zip_main.cc
deleted file mode 100644
index 3f4a50c..0000000
--- a/tools/ijar/zip_main.cc
+++ /dev/null
@@ -1,312 +0,0 @@
-// Copyright 2015 Google Inc. All rights reserved.
-//
-// Author: Alan Donovan <adonovan@google.com>
-//
-// 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.
-
-//
-// Zip / Unzip file using ijar zip implementation.
-//
-// Note that this Zip implementation intentionally don't compute CRC-32
-// because it is useless computation for jar because Java doesn't care.
-// CRC-32 of all files in the zip file will be set to 0.
-//
-
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <limits.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/mman.h>
-#include <errno.h>
-#include <memory>
-
-#include "zip.h"
-
-namespace devtools_ijar {
-
-#define SYSCALL(expr)  do { \
-                         if ((expr) < 0) { \
-                           perror(#expr); \
-                           abort(); \
-                         } \
-                       } while (0)
-
-//
-// A ZipExtractorProcessor that extract all files in the ZIP file.
-//
-class UnzipProcessor : public ZipExtractorProcessor {
- public:
-  // Create a processor who will extract the files into output_root
-  // if "extract" is set to true and will print the list of files and
-  // their unix modes if "verbose" is set to true.
-  UnzipProcessor(const char *output_root, bool verbose, bool extract)
-    : output_root_(output_root), verbose_(verbose), extract_(extract) {}
-  virtual ~UnzipProcessor() {}
-
-  virtual void Process(const char* filename, const u4 attr,
-                       const u1* data, const size_t size);
-  virtual bool Accept(const char* filename, const u4 attr) {
-    return true;
-  }
-
- private:
-  const char *output_root_;
-  const bool verbose_;
-  const bool extract_;
-};
-
-// Concatene 2 path, path1 and path2, using / as a directory separator and
-// puting the result in "out". "size" specify the size of the output buffer
-void concat_path(char* out, const size_t size,
-                 const char *path1, const char *path2) {
-  int len1 = strlen(path1);
-  size_t l = len1;
-  strncpy(out, path1, size - 1);
-  out[size-1] = 0;
-  if (l < size - 1 && path1[len1] != '/' && path2[0] != '/') {
-    out[l] = '/';
-    l++;
-    out[l] = 0;
-  }
-  if (l < size - 1) {
-    strncat(out, path2, size - 1 - l);
-  }
-}
-
-// Do a recursive mkdir of all folders of path except the last path
-// segment (if path ends with a / then the last path segment is empty).
-// All folders are created using "mode" for creation mode.
-void mkdirs(const char *path, mode_t mode) {
-  char path_[PATH_MAX];
-  struct stat statst;
-  strncpy(path_, path, PATH_MAX);
-  path_[PATH_MAX-1] = 0;
-  char *pointer = path_;
-  while ((pointer = strchr(pointer, '/')) != NULL) {
-    if (path_ != pointer) {  // skip leading slash
-      *pointer = 0;
-      if (stat(path_, &statst) != 0) {
-        if (mkdir(path_, mode) < 0) {
-          fprintf(stderr, "Cannot create folder %s: %s\n",
-                  path_, strerror(errno));
-          abort();
-        }
-      }
-      *pointer = '/';
-    }
-    pointer++;
-  }
-}
-
-void UnzipProcessor::Process(const char* filename, const u4 attr,
-                             const u1* data, const size_t size) {
-  mode_t mode = zipattr_to_mode(attr);
-  mode_t perm = mode & 0777;
-  bool isdir = (mode & S_IFDIR) != 0;
-  if (attr == 0) {
-    // Fallback when the external attribute is not set.
-    isdir = filename[strlen(filename)-1] == '/';
-    perm = 0777;
-  }
-  if (verbose_) {
-    printf("%c %o %s\n", isdir ? 'd' : 'f', perm, filename);
-  }
-  if (extract_) {
-    char path[PATH_MAX];
-    int fd;
-    concat_path(path, PATH_MAX, output_root_, filename);
-    mkdirs(path, perm);
-    if (!isdir) {
-      fd = open(path, O_CREAT | O_WRONLY, perm);
-      if (fd < 0) {
-        fprintf(stderr, "Cannot open file %s for writing: %s\n",
-                path, strerror(errno));
-        abort();
-      }
-      SYSCALL(write(fd, data, size));
-      SYSCALL(close(fd));
-    }
-  }
-}
-
-// Get the basename of path and store it in output. output_size
-// is the size of the output buffer.
-void basename(const char *path, char *output, size_t output_size) {
-  const char *pointer = strrchr(path, '/');
-  if (pointer == NULL) {
-    pointer = path;
-  } else {
-    pointer++;  // Skip the leading slash.
-  }
-  strncpy(output, pointer, output_size);
-  output[output_size-1] = 0;
-}
-
-
-// Execute the extraction (or just listing if just v is provided)
-int extract(char *zipfile, bool verbose, bool extract) {
-  char output_root[PATH_MAX];
-  getcwd(output_root, PATH_MAX);
-
-  UnzipProcessor processor(output_root, verbose, extract);
-  std::unique_ptr<ZipExtractor> extractor(ZipExtractor::Create(zipfile,
-                                                               &processor));
-  if (extractor.get() == NULL) {
-    fprintf(stderr, "Unable to open zip file %s: %s.\n", zipfile,
-            strerror(errno));
-    return -1;
-  }
-
-  if (extractor->ProcessAll() < 0) {
-    fprintf(stderr, "%s.\n", extractor->GetError());
-    return -1;
-  }
-  return 0;
-}
-
-// Execute the create operation
-int create(char *zipfile, char **files, bool flatten, bool verbose,
-           bool compress) {
-  struct stat statst;
-  u8 size = ZipBuilder::EstimateSize(files);
-  if (size == 0) {
-    return -1;
-  }
-  std::unique_ptr<ZipBuilder> builder(ZipBuilder::Create(zipfile, size));
-  if (builder.get() == NULL) {
-    fprintf(stderr, "Unable to create zip file %s: %s.\n",
-            zipfile, strerror(errno));
-    return -1;
-  }
-  for (int i = 0; files[i] != NULL; i++) {
-    stat(files[i], &statst);
-    char path[PATH_MAX];
-    bool isdir = (statst.st_mode & S_IFDIR) != 0;
-
-    if (flatten && isdir) {
-      continue;
-    }
-
-    // Compute the path, flattening it if requested
-    if (flatten) {
-      basename(files[i], path, PATH_MAX);
-    } else {
-      strncpy(path, files[i], PATH_MAX);
-      path[PATH_MAX-1] = 0;
-      size_t len = strlen(path);
-      if (isdir && len < PATH_MAX - 1) {
-        // Add the trailing slash for folders
-        path[len] = '/';
-        path[len+1] = 0;
-      }
-    }
-
-    if (verbose) {
-      mode_t perm = statst.st_mode & 0777;
-      printf("%c %o %s\n", isdir ? 'd' : 'f', perm, path);
-    }
-
-    u1 *buffer = builder->NewFile(path, mode_to_zipattr(statst.st_mode));
-    if (isdir || statst.st_size == 0) {
-      builder->FinishFile(0);
-    } else {
-      // mmap the input file and memcpy
-      int fd = open(files[i], O_RDONLY);
-      if (fd < 0) {
-        fprintf(stderr, "Can't open file %s for reading: %s.\n",
-                files[i], strerror(errno));
-        return -1;
-      }
-      void *data = mmap(NULL, statst.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
-      if (data == MAP_FAILED) {
-        fprintf(stderr, "Can't mmap file %s for reading: %s.\n",
-                files[i], strerror(errno));
-        return -1;
-      }
-      memcpy(buffer, data, statst.st_size);
-      munmap(data, statst.st_size);
-      builder->FinishFile(statst.st_size, compress, true);
-    }
-  }
-  if (builder->Finish() < 0) {
-    fprintf(stderr, "%s\n", builder->GetError());
-    return -1;
-  }
-  return 0;
-}
-
-}  // namespace devtools_ijar
-
-//
-// main method
-//
-static void usage(char *progname) {
-  fprintf(stderr, "Usage: %s [vxc[fC]] x.zip [file1...filen]\n", progname);
-  fprintf(stderr, "  v verbose - list all file in x.zip\n");
-  fprintf(stderr, "  x extract - extract file in x.zip in current directory\n");
-  fprintf(stderr, "  c create  - add files to x.zip\n");
-  fprintf(stderr, "  f flatten - flatten files to use with create operation\n");
-  fprintf(stderr,
-          "  C compress - compress files when using the create operation\n");
-  fprintf(stderr, "x and c cannot be used in the same command-line.\n");
-  exit(1);
-}
-
-int main(int argc, char **argv) {
-  bool extract = false;
-  bool verbose = false;
-  bool create = false;
-  bool compress = false;
-  bool flatten = false;
-
-  if (argc < 3) {
-    usage(argv[0]);
-  }
-
-  for (int i = 0; argv[1][i] != 0; i++) {
-    switch (argv[1][i]) {
-    case 'x':
-      extract = true;
-      break;
-    case 'v':
-      verbose = true;
-      break;
-    case 'c':
-      create = true;
-      break;
-    case 'f':
-      flatten = true;
-      break;
-    case 'C':
-      compress = true;
-      break;
-    default:
-      usage(argv[0]);
-    }
-  }
-  if (create) {
-    if (extract) {
-      usage(argv[0]);
-    }
-    // Create a zip
-    return devtools_ijar::create(argv[2], argv + 3, flatten, verbose, compress);
-  } else {
-    if (flatten) {
-      usage(argv[0]);
-    }
-    // Extraction / list mode
-    return devtools_ijar::extract(argv[2], verbose, extract);
-  }
-}
diff --git a/tools/java-event-log-tags.py b/tools/java-event-log-tags.py
index f364751..37cd712 100755
--- a/tools/java-event-log-tags.py
+++ b/tools/java-event-log-tags.py
@@ -51,30 +51,37 @@
     print >> sys.stderr, "unhandled option %s" % (o,)
     sys.exit(1)
 
-if len(args) != 2:
-  print "need exactly two input files, not %d" % (len(args),)
+if len(args) != 1 and len(args) != 2:
+  print "need one or two input files, not %d" % (len(args),)
   print __doc__
   sys.exit(1)
 
 fn = args[0]
 tagfile = event_log_tags.TagFile(fn)
 
-# Load the merged tag file (which should have numbers assigned for all
-# tags.  Use the numbers from the merged file to fill in any missing
-# numbers from the input file.
-merged_fn = args[1]
-merged_tagfile = event_log_tags.TagFile(merged_fn)
-merged_by_name = dict([(t.tagname, t) for t in merged_tagfile.tags])
-for t in tagfile.tags:
-  if t.tagnum is None:
-    if t.tagname in merged_by_name:
-      t.tagnum = merged_by_name[t.tagname].tagnum
-    else:
-      # We're building something that's not being included in the
-      # product, so its tags don't appear in the merged file.  Assign
-      # them all an arbitrary number so we can emit the java and
-      # compile the (unused) package.
-      t.tagnum = 999999
+if len(args) > 1:
+  # Load the merged tag file (which should have numbers assigned for all
+  # tags.  Use the numbers from the merged file to fill in any missing
+  # numbers from the input file.
+  merged_fn = args[1]
+  merged_tagfile = event_log_tags.TagFile(merged_fn)
+  merged_by_name = dict([(t.tagname, t) for t in merged_tagfile.tags])
+  for t in tagfile.tags:
+    if t.tagnum is None:
+      if t.tagname in merged_by_name:
+        t.tagnum = merged_by_name[t.tagname].tagnum
+      else:
+        # We're building something that's not being included in the
+        # product, so its tags don't appear in the merged file.  Assign
+        # them all an arbitrary number so we can emit the java and
+        # compile the (unused) package.
+        t.tagnum = 999999
+else:
+  # Not using the merged tag file, so all tags must have manually assigned
+  # numbers
+  for t in tagfile.tags:
+    if t.tagnum is None:
+      tagfilef.AddError("tag \"%s\" has no number" % (tagname,), tag.linenum)
 
 if "java_package" not in tagfile.options:
   tagfile.AddError("java_package option not specified", linenum=0)
diff --git a/tools/libhost/Android.bp b/tools/libhost/Android.bp
index e5a5ecf..4c9100f 100644
--- a/tools/libhost/Android.bp
+++ b/tools/libhost/Android.bp
@@ -10,6 +10,7 @@
     name: "libhost",
     target: {
         windows: {
+            cflags: ["-Wno-unused-parameter"],
             enabled: true,
         },
     },
diff --git a/tools/libhost/CopyFile.c b/tools/libhost/CopyFile.c
index bd65f1e..f9bda86 100644
--- a/tools/libhost/CopyFile.c
+++ b/tools/libhost/CopyFile.c
@@ -352,7 +352,12 @@
  * need to trash it so we can create one.
  */
 #if defined(_WIN32)
-extern int copySymlink(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options) __attribute__((error("no symlinks on Windows")));
+extern int copySymlink(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
+#ifdef __clang__
+  __attribute__((unavailable("no symlinks on Windows")));
+#else
+  __attribute__((error("no symlinks on Windows")));
+#endif
 #else
 static int copySymlink(const char* src, const char* dst, const struct stat* pSrcStat, unsigned int options)
 {
@@ -574,8 +579,10 @@
         } else {
             retVal = copyDirectory(src, dst, &srcStat, options);
         }
+#if !defined(_WIN32)
     } else if (S_ISLNK(srcStat.st_mode)) {
         retVal = copySymlink(src, dst, &srcStat, options);
+#endif
     } else if (S_ISREG(srcStat.st_mode)) {
         retVal = copyRegular(src, dst, &srcStat, options);
     } else {
diff --git a/tools/makeparallel/Android.bp b/tools/makeparallel/Android.bp
index cb81817..898db68 100644
--- a/tools/makeparallel/Android.bp
+++ b/tools/makeparallel/Android.bp
@@ -18,9 +18,4 @@
         "makeparallel.cpp",
     ],
     cflags: ["-Wall", "-Werror"],
-    target: {
-        linux: {
-            host_ldlibs: ["-lrt", "-lpthread"],
-        },
-    },
 }
diff --git a/tools/post_process_props.py b/tools/post_process_props.py
index 9355e4b..9ddd5d7 100755
--- a/tools/post_process_props.py
+++ b/tools/post_process_props.py
@@ -64,7 +64,7 @@
   buildprops = prop.to_dict()
   for key, value in buildprops.iteritems():
     # Check build properties' length.
-    if len(value) > PROP_VALUE_MAX:
+    if len(value) > PROP_VALUE_MAX and not key.startswith("ro."):
       check_pass = False
       sys.stderr.write("error: %s cannot exceed %d bytes: " %
                        (key, PROP_VALUE_MAX))
diff --git a/tools/releasetools/add_img_to_target_files.py b/tools/releasetools/add_img_to_target_files.py
index 1a08cb6..e295760 100755
--- a/tools/releasetools/add_img_to_target_files.py
+++ b/tools/releasetools/add_img_to_target_files.py
@@ -45,19 +45,13 @@
 
 from __future__ import print_function
 
-import sys
-
-if sys.hexversion < 0x02070000:
-  print("Python 2.7 or newer is required.", file=sys.stderr)
-  sys.exit(1)
-
 import datetime
-import errno
 import hashlib
 import os
 import shlex
 import shutil
 import subprocess
+import sys
 import tempfile
 import uuid
 import zipfile
@@ -67,6 +61,10 @@
 import rangelib
 import sparse_img
 
+if sys.hexversion < 0x02070000:
+  print("Python 2.7 or newer is required.", file=sys.stderr)
+  sys.exit(1)
+
 OPTIONS = common.OPTIONS
 
 OPTIONS.add_missing = False
@@ -215,22 +213,6 @@
 def CreateImage(input_dir, info_dict, what, output_file, block_list=None):
   print("creating " + what + ".img...")
 
-  # The name of the directory it is making an image out of matters to
-  # mkyaffs2image.  It wants "system" but we have a directory named
-  # "SYSTEM", so create a symlink.
-  temp_dir = tempfile.mkdtemp()
-  OPTIONS.tempfiles.append(temp_dir)
-  try:
-    os.symlink(os.path.join(input_dir, what.upper()),
-               os.path.join(temp_dir, what))
-  except OSError as e:
-    # bogus error on my mac version?
-    #   File "./build/tools/releasetools/img_from_target_files"
-    #     os.path.join(OPTIONS.input_tmp, "system"))
-    # OSError: [Errno 17] File exists
-    if e.errno == errno.EEXIST:
-      pass
-
   image_props = build_image.ImagePropFromGlobalDict(info_dict, what)
   fstab = info_dict["fstab"]
   mount_point = "/" + what
@@ -272,7 +254,7 @@
   hash_seed = "hash_seed-" + uuid_seed
   image_props["hash_seed"] = str(uuid.uuid5(uuid.NAMESPACE_URL, hash_seed))
 
-  succ = build_image.BuildImage(os.path.join(temp_dir, what),
+  succ = build_image.BuildImage(os.path.join(input_dir, what.upper()),
                                 image_props, output_file.name)
   assert succ, "build " + what + ".img image failed"
 
@@ -322,21 +304,11 @@
   timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
   image_props["timestamp"] = int(timestamp)
 
-  # The name of the directory it is making an image out of matters to
-  # mkyaffs2image.  So we create a temp dir, and within it we create an
-  # empty dir named "data", or a symlink to the DATA dir,
-  # and build the image from that.
-  temp_dir = tempfile.mkdtemp()
-  OPTIONS.tempfiles.append(temp_dir)
-  user_dir = os.path.join(temp_dir, "data")
-  empty = (OPTIONS.info_dict.get("userdata_img_with_data") != "true")
-  if empty:
-    # Create an empty dir.
-    os.mkdir(user_dir)
+  if OPTIONS.info_dict.get("userdata_img_with_data") == "true":
+    user_dir = os.path.join(OPTIONS.input_tmp, "DATA")
   else:
-    # Symlink to the DATA dir.
-    os.symlink(os.path.join(OPTIONS.input_tmp, "DATA"),
-               user_dir)
+    user_dir = tempfile.mkdtemp()
+    OPTIONS.tempfiles.append(user_dir)
 
   fstab = OPTIONS.info_dict["fstab"]
   if fstab:
@@ -373,10 +345,20 @@
     cmd.extend(["--include_descriptors_from_image", img_path])
 
 
-def AddVBMeta(output_zip, boot_img_path, system_img_path, vendor_img_path,
-              dtbo_img_path, prefix="IMAGES/"):
-  """Create a VBMeta image and store it in output_zip."""
+def AddVBMeta(output_zip, partitions, prefix="IMAGES/"):
+  """Creates a VBMeta image and store it in output_zip.
+
+  Args:
+    output_zip: The output zip file, which needs to be already open.
+    partitions: A dict that's keyed by partition names with image paths as
+        values. Only valid partition names are accepted, which include 'boot',
+        'recovery', 'system', 'vendor', 'dtbo'.
+  """
   img = OutputFile(output_zip, OPTIONS.input_tmp, prefix, "vbmeta.img")
+  if os.path.exists(img.input_name):
+    print("vbmeta.img already exists in %s; not rebuilding..." % (prefix,))
+    return img.input_name
+
   avbtool = os.getenv('AVBTOOL') or OPTIONS.info_dict["avb_avbtool"]
   cmd = [avbtool, "make_vbmeta_image", "--output", img.name]
   common.AppendAVBSigningArgs(cmd, "vbmeta")
@@ -384,10 +366,12 @@
   public_key_dir = tempfile.mkdtemp(prefix="avbpubkey-")
   OPTIONS.tempfiles.append(public_key_dir)
 
-  AppendVBMetaArgsForPartition(cmd, "boot", boot_img_path, public_key_dir)
-  AppendVBMetaArgsForPartition(cmd, "system", system_img_path, public_key_dir)
-  AppendVBMetaArgsForPartition(cmd, "vendor", vendor_img_path, public_key_dir)
-  AppendVBMetaArgsForPartition(cmd, "dtbo", dtbo_img_path, public_key_dir)
+  for partition, path in partitions.items():
+    assert partition in common.AVB_PARTITIONS, 'Unknown partition: %s' % (
+        partition,)
+    assert os.path.exists(path), 'Failed to find %s for partition %s' % (
+        path, partition)
+    AppendVBMetaArgsForPartition(cmd, partition, path, public_key_dir)
 
   args = OPTIONS.info_dict.get("avb_vbmeta_args")
   if args and args.strip():
@@ -403,9 +387,9 @@
         if os.path.exists(image_path):
           continue
         found = False
-        for dir in ['IMAGES', 'RADIO', 'VENDOR_IMAGES', 'PREBUILT_IMAGES']:
+        for dir_name in ['IMAGES', 'RADIO', 'VENDOR_IMAGES', 'PREBUILT_IMAGES']:
           alt_path = os.path.join(
-              OPTIONS.input_tmp, dir, os.path.basename(image_path))
+              OPTIONS.input_tmp, dir_name, os.path.basename(image_path))
           if os.path.exists(alt_path):
             split_args[index + 1] = alt_path
             found = True
@@ -469,13 +453,8 @@
   timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds()
   image_props["timestamp"] = int(timestamp)
 
-  # The name of the directory it is making an image out of matters to
-  # mkyaffs2image.  So we create a temp dir, and within it we create an
-  # empty dir named "cache", and build the image from that.
-  temp_dir = tempfile.mkdtemp()
-  OPTIONS.tempfiles.append(temp_dir)
-  user_dir = os.path.join(temp_dir, "cache")
-  os.mkdir(user_dir)
+  user_dir = tempfile.mkdtemp()
+  OPTIONS.tempfiles.append(user_dir)
 
   fstab = OPTIONS.info_dict["fstab"]
   if fstab:
@@ -509,6 +488,17 @@
 
 
 def AddImagesToTargetFiles(filename):
+  """Creates and adds images (boot/recovery/system/...) to a target_files.zip.
+
+  It works with either a zip file (zip mode), or a directory that contains the
+  files to be packed into a target_files.zip (dir mode). The latter is used when
+  being called from build/make/core/Makefile.
+
+  The images will be created under IMAGES/ in the input target_files.zip.
+
+  Args:
+      filename: the target_files.zip, or the zip root directory.
+  """
   if os.path.isdir(filename):
     OPTIONS.input_tmp = os.path.abspath(filename)
     input_zip = None
@@ -540,10 +530,13 @@
   else:
     OPTIONS.info_dict = common.LoadInfoDict(filename, filename)
     output_zip = None
-    images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES")
-    if not os.path.isdir(images_dir):
-      os.makedirs(images_dir)
-    images_dir = None
+
+  # Always make input_tmp/IMAGES available, since we may stage boot / recovery
+  # images there even under zip mode. The directory will be cleaned up as part
+  # of OPTIONS.input_tmp.
+  images_dir = os.path.join(OPTIONS.input_tmp, "IMAGES")
+  if not os.path.isdir(images_dir):
+    os.makedirs(images_dir)
 
   has_recovery = (OPTIONS.info_dict.get("no_recovery") != "true")
 
@@ -558,67 +551,63 @@
     if fp:
       OPTIONS.info_dict["avb_salt"] = hashlib.sha256(fp).hexdigest()
 
+  # A map between partition names and their paths, which could be used when
+  # generating AVB vbmeta image.
+  partitions = dict()
+
   def banner(s):
     print("\n\n++++ " + s + " ++++\n\n")
 
-  prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
-  boot_image = None
-  if os.path.exists(prebuilt_path):
-    banner("boot")
-    print("boot.img already exists in IMAGES/, no need to rebuild...")
-    if OPTIONS.rebuild_recovery:
-      boot_image = common.GetBootableImage(
-          "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
-  else:
-    banner("boot")
-    boot_image = common.GetBootableImage(
-        "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
-    if boot_image:
+  banner("boot")
+  # common.GetBootableImage() returns the image directly if present.
+  boot_image = common.GetBootableImage(
+      "IMAGES/boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
+  # boot.img may be unavailable in some targets (e.g. aosp_arm64).
+  if boot_image:
+    partitions['boot'] = os.path.join(OPTIONS.input_tmp, "IMAGES", "boot.img")
+    if not os.path.exists(partitions['boot']):
+      boot_image.WriteToDir(OPTIONS.input_tmp)
       if output_zip:
         boot_image.AddToZip(output_zip)
-      else:
-        boot_image.WriteToDir(OPTIONS.input_tmp)
 
   recovery_image = None
   if has_recovery:
     banner("recovery")
-    prebuilt_path = os.path.join(OPTIONS.input_tmp, "IMAGES", "recovery.img")
-    if os.path.exists(prebuilt_path):
-      print("recovery.img already exists in IMAGES/, no need to rebuild...")
-      if OPTIONS.rebuild_recovery:
-        recovery_image = common.GetBootableImage(
-            "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp,
-            "RECOVERY")
-    else:
-      recovery_image = common.GetBootableImage(
-          "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
-      if recovery_image:
-        if output_zip:
-          recovery_image.AddToZip(output_zip)
-        else:
-          recovery_image.WriteToDir(OPTIONS.input_tmp)
+    recovery_image = common.GetBootableImage(
+        "IMAGES/recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
+    assert recovery_image, "Failed to create recovery.img."
+    partitions['recovery'] = os.path.join(
+        OPTIONS.input_tmp, "IMAGES", "recovery.img")
+    if not os.path.exists(partitions['recovery']):
+      recovery_image.WriteToDir(OPTIONS.input_tmp)
+      if output_zip:
+        recovery_image.AddToZip(output_zip)
 
       banner("recovery (two-step image)")
       # The special recovery.img for two-step package use.
       recovery_two_step_image = common.GetBootableImage(
           "IMAGES/recovery-two-step.img", "recovery-two-step.img",
           OPTIONS.input_tmp, "RECOVERY", two_step_image=True)
-      if recovery_two_step_image:
+      assert recovery_two_step_image, "Failed to create recovery-two-step.img."
+      recovery_two_step_image_path = os.path.join(
+          OPTIONS.input_tmp, "IMAGES", "recovery-two-step.img")
+      if not os.path.exists(recovery_two_step_image_path):
+        recovery_two_step_image.WriteToDir(OPTIONS.input_tmp)
         if output_zip:
           recovery_two_step_image.AddToZip(output_zip)
-        else:
-          recovery_two_step_image.WriteToDir(OPTIONS.input_tmp)
 
   banner("system")
-  system_img_path = AddSystem(
+  partitions['system'] = system_img_path = AddSystem(
       output_zip, recovery_img=recovery_image, boot_img=boot_image)
-  vendor_img_path = None
+
   if has_vendor:
     banner("vendor")
-    vendor_img_path = AddVendor(output_zip)
+    partitions['vendor'] = vendor_img_path = AddVendor(output_zip)
+
   if has_system_other:
     banner("system_other")
     AddSystemOther(output_zip)
+
   if not OPTIONS.is_signing:
     banner("userdata")
     AddUserdata(output_zip)
@@ -629,16 +618,13 @@
     banner("partition-table")
     AddPartitionTable(output_zip)
 
-  dtbo_img_path = None
   if OPTIONS.info_dict.get("has_dtbo") == "true":
     banner("dtbo")
-    dtbo_img_path = AddDtbo(output_zip)
+    partitions['dtbo'] = AddDtbo(output_zip)
 
   if OPTIONS.info_dict.get("avb_enable") == "true":
     banner("vbmeta")
-    boot_contents = boot_image.WriteToTemp()
-    AddVBMeta(output_zip, boot_contents.name, system_img_path,
-              vendor_img_path, dtbo_img_path)
+    AddVBMeta(output_zip, partitions)
 
   # For devices using A/B update, copy over images from RADIO/ and/or
   # VENDOR_IMAGES/ to IMAGES/ and make sure we have all the needed
@@ -670,8 +656,6 @@
         continue
 
       img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
-      img_vendor_dir = os.path.join(
-        OPTIONS.input_tmp, "VENDOR_IMAGES")
       if os.path.exists(img_radio_path):
         if output_zip:
           common.ZipWrite(output_zip, img_radio_path,
@@ -679,11 +663,12 @@
         else:
           shutil.copy(img_radio_path, prebuilt_path)
       else:
+        img_vendor_dir = os.path.join(OPTIONS.input_tmp, "VENDOR_IMAGES")
         for root, _, files in os.walk(img_vendor_dir):
           if img_name in files:
             if output_zip:
               common.ZipWrite(output_zip, os.path.join(root, img_name),
-                os.path.join("IMAGES", img_name))
+                              os.path.join("IMAGES", img_name))
             else:
               shutil.copy(os.path.join(root, img_name), prebuilt_path)
             break
diff --git a/tools/releasetools/blockimgdiff.py b/tools/releasetools/blockimgdiff.py
index b8123c0..8f06b95 100644
--- a/tools/releasetools/blockimgdiff.py
+++ b/tools/releasetools/blockimgdiff.py
@@ -16,6 +16,7 @@
 
 import array
 import common
+import copy
 import functools
 import heapq
 import itertools
@@ -204,6 +205,18 @@
     self.id = len(by_id)
     by_id.append(self)
 
+    self._patch = None
+
+  @property
+  def patch(self):
+    return self._patch
+
+  @patch.setter
+  def patch(self, patch):
+    if patch:
+      assert self.style == "diff"
+    self._patch = patch
+
   def NetStashChange(self):
     return (sum(sr.size() for (_, sr) in self.stash_before) -
             sum(sr.size() for (_, sr) in self.use_stash))
@@ -213,6 +226,7 @@
     self.use_stash = []
     self.style = "new"
     self.src_ranges = RangeSet()
+    self.patch = None
 
   def __str__(self):
     return (str(self.id) + ": <" + str(self.src_ranges) + " " + self.style +
@@ -675,6 +689,7 @@
             # These are identical; we don't need to generate a patch,
             # just issue copy commands on the device.
             xf.style = "move"
+            xf.patch = None
             tgt_size = xf.tgt_ranges.size() * self.tgt.blocksize
             if xf.src_ranges != xf.tgt_ranges:
               print("%10d %10d (%6.2f%%) %7s %s %s (from %s)" % (
@@ -683,24 +698,33 @@
                       xf.tgt_name + " (from " + xf.src_name + ")"),
                   str(xf.tgt_ranges), str(xf.src_ranges)))
           else:
-            # For files in zip format (eg, APKs, JARs, etc.) we would
-            # like to use imgdiff -z if possible (because it usually
-            # produces significantly smaller patches than bsdiff).
-            # This is permissible if:
-            #
-            #  - imgdiff is not disabled, and
-            #  - the source and target files are monotonic (ie, the
-            #    data is stored with blocks in increasing order), and
-            #  - we haven't removed any blocks from the source set.
-            #
-            # If these conditions are satisfied then appending all the
-            # blocks in the set together in order will produce a valid
-            # zip file (plus possibly extra zeros in the last block),
-            # which is what imgdiff needs to operate.  (imgdiff is
-            # fine with extra zeros at the end of the file.)
-            imgdiff = (not self.disable_imgdiff and xf.intact and
-                       xf.tgt_name.split(".")[-1].lower()
-                       in ("apk", "jar", "zip"))
+            if xf.patch:
+              # We have already generated the patch with imgdiff. Check if the
+              # transfer is intact.
+              assert not self.disable_imgdiff
+              imgdiff = True
+              if not xf.intact:
+                imgdiff = False
+                xf.patch = None
+            else:
+              # For files in zip format (eg, APKs, JARs, etc.) we would
+              # like to use imgdiff -z if possible (because it usually
+              # produces significantly smaller patches than bsdiff).
+              # This is permissible if:
+              #
+              #  - imgdiff is not disabled, and
+              #  - the source and target files are monotonic (ie, the
+              #    data is stored with blocks in increasing order), and
+              #  - we haven't removed any blocks from the source set.
+              #
+              # If these conditions are satisfied then appending all the
+              # blocks in the set together in order will produce a valid
+              # zip file (plus possibly extra zeros in the last block),
+              # which is what imgdiff needs to operate.  (imgdiff is
+              # fine with extra zeros at the end of the file.)
+              imgdiff = (not self.disable_imgdiff and xf.intact and
+                         xf.tgt_name.split(".")[-1].lower()
+                         in ("apk", "jar", "zip"))
             xf.style = "imgdiff" if imgdiff else "bsdiff"
             diff_queue.append((index, imgdiff, patch_num))
             patch_num += 1
@@ -717,6 +741,7 @@
       diff_total = len(diff_queue)
       patches = [None] * diff_total
       error_messages = []
+      warning_messages = []
       if sys.stdout.isatty():
         global diff_done
         diff_done = 0
@@ -737,29 +762,51 @@
             xf_index, imgdiff, patch_index = diff_queue.pop()
 
           xf = self.transfers[xf_index]
-          src_ranges = xf.src_ranges
-          tgt_ranges = xf.tgt_ranges
+          patch = xf.patch
+          if not patch:
+            src_ranges = xf.src_ranges
+            tgt_ranges = xf.tgt_ranges
 
-          # Needs lock since WriteRangeDataToFd() is stateful (calling seek).
-          with lock:
-            src_file = common.MakeTempFile(prefix="src-")
-            with open(src_file, "wb") as fd:
-              self.src.WriteRangeDataToFd(src_ranges, fd)
-
-            tgt_file = common.MakeTempFile(prefix="tgt-")
-            with open(tgt_file, "wb") as fd:
-              self.tgt.WriteRangeDataToFd(tgt_ranges, fd)
-
-          try:
-            patch = compute_patch(src_file, tgt_file, imgdiff)
-          except ValueError as e:
+            # Needs lock since WriteRangeDataToFd() is stateful (calling seek).
             with lock:
-              error_messages.append(
+              src_file = common.MakeTempFile(prefix="src-")
+              with open(src_file, "wb") as fd:
+                self.src.WriteRangeDataToFd(src_ranges, fd)
+
+              tgt_file = common.MakeTempFile(prefix="tgt-")
+              with open(tgt_file, "wb") as fd:
+                self.tgt.WriteRangeDataToFd(tgt_ranges, fd)
+
+            message = []
+            try:
+              patch = compute_patch(src_file, tgt_file, imgdiff)
+            except ValueError as e:
+              message.append(
                   "Failed to generate %s for %s: tgt=%s, src=%s:\n%s" % (
-                      "imgdiff" if imgdiff else "bsdiff",
-                      xf.tgt_name if xf.tgt_name == xf.src_name else
-                          xf.tgt_name + " (from " + xf.src_name + ")",
-                      xf.tgt_ranges, xf.src_ranges, e.message))
+                  "imgdiff" if imgdiff else "bsdiff",
+                  xf.tgt_name if xf.tgt_name == xf.src_name else
+                      xf.tgt_name + " (from " + xf.src_name + ")",
+                  xf.tgt_ranges, xf.src_ranges, e.message))
+              # TODO(b/68016761): Better handle the holes in mke2fs created
+              # images.
+              if imgdiff:
+                try:
+                  patch = compute_patch(src_file, tgt_file, imgdiff=False)
+                  message.append(
+                      "Fell back and generated with bsdiff instead for %s" % (
+                      xf.tgt_name,))
+                  xf.style = "bsdiff"
+                  with lock:
+                    warning_messages.extend(message)
+                  del message[:]
+                except ValueError as e:
+                  message.append(
+                      "Also failed to generate with bsdiff for %s:\n%s" % (
+                      xf.tgt_name, e.message))
+
+            if message:
+              with lock:
+                error_messages.extend(message)
 
           with lock:
             patches[patch_index] = (xf_index, patch)
@@ -781,8 +828,15 @@
       if sys.stdout.isatty():
         print('\n')
 
+      if warning_messages:
+        print('WARNING:')
+        print('\n'.join(warning_messages))
+        print('\n\n\n')
+
       if error_messages:
+        print('ERROR:')
         print('\n'.join(error_messages))
+        print('\n\n\n')
         sys.exit(1)
     else:
       patches = []
@@ -1124,8 +1178,8 @@
   def FindTransfers(self):
     """Parse the file_map to generate all the transfers."""
 
-    def AddSplitTransfers(tgt_name, src_name, tgt_ranges, src_ranges,
-                          style, by_id):
+    def AddSplitTransfersWithFixedSizeChunks(tgt_name, src_name, tgt_ranges,
+                                             src_ranges, style, by_id):
       """Add one or multiple Transfer()s by splitting large files.
 
       For BBOTA v3, we need to stash source blocks for resumable feature.
@@ -1140,21 +1194,7 @@
       Compared to the fixed 1024-block limit, it reduces the overall package
       size by 30% for volantis, and 20% for angler and bullhead."""
 
-      # Possibly split large files into smaller chunks.
       pieces = 0
-      cache_size = common.OPTIONS.cache_size
-      split_threshold = 0.125
-      max_blocks_per_transfer = int(cache_size * split_threshold /
-                                    self.tgt.blocksize)
-
-      # Change nothing for small files.
-      if (tgt_ranges.size() <= max_blocks_per_transfer and
-          src_ranges.size() <= max_blocks_per_transfer):
-        Transfer(tgt_name, src_name, tgt_ranges, src_ranges,
-                 self.tgt.RangeSha1(tgt_ranges), self.src.RangeSha1(src_ranges),
-                 style, by_id)
-        return
-
       while (tgt_ranges.size() > max_blocks_per_transfer and
              src_ranges.size() > max_blocks_per_transfer):
         tgt_split_name = "%s-%d" % (tgt_name, pieces)
@@ -1180,6 +1220,37 @@
                  self.tgt.RangeSha1(tgt_ranges), self.src.RangeSha1(src_ranges),
                  style, by_id)
 
+    def AddSplitTransfers(tgt_name, src_name, tgt_ranges, src_ranges, style,
+                          by_id):
+      """Find all the zip files and split the others with a fixed chunk size.
+
+      This function will construct a list of zip archives, which will later be
+      split by imgdiff to reduce the final patch size. For the other files,
+      we will plainly split them based on a fixed chunk size with the potential
+      patch size penalty.
+      """
+
+      assert style == "diff"
+
+      # Change nothing for small files.
+      if (tgt_ranges.size() <= max_blocks_per_transfer and
+          src_ranges.size() <= max_blocks_per_transfer):
+        Transfer(tgt_name, src_name, tgt_ranges, src_ranges,
+                 self.tgt.RangeSha1(tgt_ranges), self.src.RangeSha1(src_ranges),
+                 style, by_id)
+        return
+
+      if tgt_name.split(".")[-1].lower() in ("apk", "jar", "zip"):
+        split_enable = (not self.disable_imgdiff and src_ranges.monotonic and
+                        tgt_ranges.monotonic)
+        if split_enable and (self.tgt.RangeSha1(tgt_ranges) !=
+                             self.src.RangeSha1(src_ranges)):
+          large_apks.append((tgt_name, src_name, tgt_ranges, src_ranges))
+          return
+
+      AddSplitTransfersWithFixedSizeChunks(tgt_name, src_name, tgt_ranges,
+                                           src_ranges, style, by_id)
+
     def AddTransfer(tgt_name, src_name, tgt_ranges, src_ranges, style, by_id,
                     split=False):
       """Wrapper function for adding a Transfer()."""
@@ -1248,8 +1319,145 @@
       AddSplitTransfers(
           tgt_name, src_name, tgt_ranges, src_ranges, style, by_id)
 
+    def ParseAndValidateSplitInfo(patch_size, tgt_ranges, src_ranges,
+                                  split_info):
+      """Parse the split_info and return a list of info tuples.
+
+      Args:
+        patch_size: total size of the patch file.
+        tgt_ranges: Ranges of the target file within the original image.
+        src_ranges: Ranges of the source file within the original image.
+        split_info format:
+          imgdiff version#
+          count of pieces
+          <patch_size_1> <tgt_size_1> <src_ranges_1>
+          ...
+          <patch_size_n> <tgt_size_n> <src_ranges_n>
+
+      Returns:
+        [patch_start, patch_len, split_tgt_ranges, split_src_ranges]
+      """
+
+      version = int(split_info[0])
+      assert version == 2
+      count = int(split_info[1])
+      assert len(split_info) - 2 == count
+
+      split_info_list = []
+      patch_start = 0
+      tgt_remain = copy.deepcopy(tgt_ranges)
+      # each line has the format <patch_size>, <tgt_size>, <src_ranges>
+      for line in split_info[2:]:
+        info = line.split()
+        assert len(info) == 3
+        patch_length = int(info[0])
+
+        split_tgt_size = int(info[1])
+        assert split_tgt_size % 4096 == 0
+        assert split_tgt_size / 4096 <= tgt_remain.size()
+        split_tgt_ranges = tgt_remain.first(split_tgt_size / 4096)
+        tgt_remain = tgt_remain.subtract(split_tgt_ranges)
+
+        # Find the split_src_ranges within the image file from its relative
+        # position in file.
+        split_src_indices = RangeSet.parse_raw(info[2])
+        split_src_ranges = RangeSet()
+        for r in split_src_indices:
+          curr_range = src_ranges.first(r[1]).subtract(src_ranges.first(r[0]))
+          assert not split_src_ranges.overlaps(curr_range)
+          split_src_ranges = split_src_ranges.union(curr_range)
+
+        split_info_list.append((patch_start, patch_length,
+                                split_tgt_ranges, split_src_ranges))
+        patch_start += patch_length
+
+      # Check that the sizes of all the split pieces add up to the final file
+      # size for patch and target.
+      assert tgt_remain.size() == 0
+      assert patch_start == patch_size
+      return split_info_list
+
+    def AddSplitTransferForLargeApks():
+      """Create split transfers for large apk files.
+
+      Example: Chrome.apk will be split into
+        src-0: Chrome.apk-0, tgt-0: Chrome.apk-0
+        src-1: Chrome.apk-1, tgt-1: Chrome.apk-1
+        ...
+
+      After the split, the target pieces are continuous and block aligned; and
+      the source pieces are mutually exclusive. During the split, we also
+      generate and save the image patch between src-X & tgt-X. This patch will
+      be valid because the block ranges of src-X & tgt-X will always stay the
+      same afterwards; but there's a chance we don't use the patch if we
+      convert the "diff" command into "new" or "move" later.
+
+      The split will be attempted by calling imgdiff, which expects the input
+      files to be valid zip archives. If imgdiff fails for some reason (i.e.
+      holes in the APK file), we will fall back to split the failed APKs into
+      fixed size chunks.
+      """
+
+      while True:
+        with transfer_lock:
+          if not large_apks:
+            return
+          tgt_name, src_name, tgt_ranges, src_ranges = large_apks.pop(0)
+
+        src_file = common.MakeTempFile(prefix="src-")
+        tgt_file = common.MakeTempFile(prefix="tgt-")
+        with transfer_lock:
+          with open(src_file, "wb") as src_fd:
+            self.src.WriteRangeDataToFd(src_ranges, src_fd)
+          with open(tgt_file, "wb") as tgt_fd:
+            self.tgt.WriteRangeDataToFd(tgt_ranges, tgt_fd)
+
+        patch_file = common.MakeTempFile(prefix="patch-")
+        patch_info_file = common.MakeTempFile(prefix="split_info-")
+        cmd = ["imgdiff", "-z",
+               "--block-limit={}".format(max_blocks_per_transfer),
+               "--split-info=" + patch_info_file,
+               src_file, tgt_file, patch_file]
+        p = common.Run(cmd, stdout=subprocess.PIPE)
+        p.communicate()
+        if p.returncode != 0:
+          print("Failed to create patch between {} and {},"
+                " falling back to bsdiff".format(src_name, tgt_name))
+          with transfer_lock:
+            AddSplitTransfersWithFixedSizeChunks(tgt_name, src_name,
+                                                 tgt_ranges, src_ranges,
+                                                 "diff", self.transfers)
+          continue
+
+        with open(patch_info_file) as patch_info:
+          lines = patch_info.readlines()
+
+        patch_size_total = os.path.getsize(patch_file)
+        split_info_list = ParseAndValidateSplitInfo(patch_size_total,
+                                                    tgt_ranges, src_ranges,
+                                                    lines)
+        for index, (patch_start, patch_length, split_tgt_ranges,
+            split_src_ranges) in enumerate(split_info_list):
+          with open(patch_file) as f:
+            f.seek(patch_start)
+            patch_content = f.read(patch_length)
+
+          split_src_name = "{}-{}".format(src_name, index)
+          split_tgt_name = "{}-{}".format(tgt_name, index)
+          transfer_split = Transfer(split_tgt_name, split_src_name,
+                                    split_tgt_ranges, split_src_ranges,
+                                    self.tgt.RangeSha1(split_tgt_ranges),
+                                    self.src.RangeSha1(split_src_ranges),
+                                    "diff", self.transfers)
+          transfer_split.patch = patch_content
+
     print("Finding transfers...")
 
+    large_apks = []
+    cache_size = common.OPTIONS.cache_size
+    split_threshold = 0.125
+    max_blocks_per_transfer = int(cache_size * split_threshold /
+                                  self.tgt.blocksize)
     empty = RangeSet()
     for tgt_fn, tgt_ranges in self.tgt.file_map.items():
       if tgt_fn == "__ZERO":
@@ -1294,6 +1502,14 @@
 
       AddTransfer(tgt_fn, None, tgt_ranges, empty, "new", self.transfers)
 
+    transfer_lock = threading.Lock()
+    threads = [threading.Thread(target=AddSplitTransferForLargeApks)
+               for _ in range(self.threads)]
+    for th in threads:
+      th.start()
+    while threads:
+      threads.pop().join()
+
   def AbbreviateSourceNames(self):
     for k in self.src.file_map.keys():
       b = os.path.basename(k)
diff --git a/tools/releasetools/build_image.py b/tools/releasetools/build_image.py
index ccfa35f..e4e4138 100755
--- a/tools/releasetools/build_image.py
+++ b/tools/releasetools/build_image.py
@@ -36,30 +36,36 @@
 FIXED_SALT = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7"
 BLOCK_SIZE = 4096
 
-def RunCommand(cmd):
+def RunCommand(cmd, verbose=None):
   """Echo and run the given command.
 
   Args:
     cmd: the command represented as a list of strings.
+    verbose: show commands being executed.
   Returns:
     A tuple of the output and the exit code.
   """
-  print "Running: ", " ".join(cmd)
+  if verbose is None:
+    verbose = OPTIONS.verbose
+  if verbose:
+    print("Running: " + " ".join(cmd))
   p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
   output, _ = p.communicate()
-  print "%s" % (output.rstrip(),)
+
+  if verbose:
+    print(output.rstrip())
   return (output, p.returncode)
 
 def GetVerityFECSize(partition_size):
   cmd = ["fec", "-s", str(partition_size)]
-  output, exit_code = RunCommand(cmd)
+  output, exit_code = RunCommand(cmd, False)
   if exit_code != 0:
     return False, 0
   return True, int(output)
 
 def GetVerityTreeSize(partition_size):
   cmd = ["build_verity_tree", "-s", str(partition_size)]
-  output, exit_code = RunCommand(cmd)
+  output, exit_code = RunCommand(cmd, False)
   if exit_code != 0:
     return False, 0
   return True, int(output)
@@ -67,7 +73,7 @@
 def GetVerityMetadataSize(partition_size):
   cmd = ["system/extras/verity/build_verity_metadata.py", "size",
          str(partition_size)]
-  output, exit_code = RunCommand(cmd)
+  output, exit_code = RunCommand(cmd, False)
   if exit_code != 0:
     return False, 0
   return True, int(output)
@@ -189,6 +195,9 @@
     else:
       hi = i
 
+  if OPTIONS.verbose:
+    print("Adjusted partition size for verity, partition_size: {},"
+          " verity_size: {}".format(result, verity_size))
   AdjustPartitionSizeForVerity.results[key] = (result, verity_size)
   return (result, verity_size)
 
@@ -217,12 +226,15 @@
   return True
 
 def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
-                        block_device, signer_path, key, signer_args):
+                        block_device, signer_path, key, signer_args,
+                        verity_disable):
   cmd = ["system/extras/verity/build_verity_metadata.py", "build",
          str(image_size), verity_metadata_path, root_hash, salt, block_device,
          signer_path, key]
   if signer_args:
     cmd.append("--signer_args=\"%s\"" % (' '.join(signer_args),))
+  if verity_disable:
+    cmd.append("--verity_disable")
   output, exit_code = RunCommand(cmd)
   if exit_code != 0:
     print "Could not build verity metadata! Error: %s" % output
@@ -246,7 +258,7 @@
   return True
 
 def Append(target, file_to_append, error_message):
-  print "appending %s to %s" % (file_to_append, target)
+  # appending file_to_append to target
   with open(target, "a") as out_file:
     with open(file_to_append, "r") as input_file:
       for line in input_file:
@@ -326,8 +338,10 @@
   # build the metadata blocks
   root_hash = prop_dict["verity_root_hash"]
   salt = prop_dict["verity_salt"]
+  verity_disable = "verity_disable" in prop_dict
   if not BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt,
-                             block_dev, signer_path, signer_key, signer_args):
+                             block_dev, signer_path, signer_key, signer_args,
+                             verity_disable):
     shutil.rmtree(tempdir_name, ignore_errors=True)
     return False
 
@@ -503,6 +517,17 @@
   elif fs_type.startswith("f2fs"):
     build_command = ["mkf2fsuserimg.sh"]
     build_command.extend([out_file, prop_dict["partition_size"]])
+    if fs_config:
+      build_command.extend(["-C", fs_config])
+    build_command.extend(["-f", in_dir])
+    if target_out:
+      build_command.extend(["-D", target_out])
+    if "selinux_fc" in prop_dict:
+      build_command.extend(["-s", prop_dict["selinux_fc"]])
+    build_command.extend(["-t", prop_dict["mount_point"]])
+    if "timestamp" in prop_dict:
+      build_command.extend(["-T", str(prop_dict["timestamp"])])
+    build_command.extend(["-L", prop_dict["mount_point"]])
   else:
     print("Error: unknown filesystem type '%s'" % (fs_type))
     return False
@@ -534,6 +559,7 @@
     if base_fs_file is not None:
       os.remove(base_fs_file)
   if exit_code != 0:
+    print("Error: '%s' failed with exit code %d" % (build_command, exit_code))
     return False
 
   # Bug: 21522719, 22023465
@@ -611,7 +637,11 @@
 
     os.remove(unsparse_image)
 
-  return exit_code == 0
+    if exit_code != 0:
+      print("Error: '%s' failed with exit code %d" % (e2fsck_command, exit_code))
+      return False
+
+  return True
 
 
 def ImagePropFromGlobalDict(glob_dict, mount_point):
@@ -642,6 +672,7 @@
       "verity_key",
       "verity_signer_cmd",
       "verity_fec",
+      "verity_disable",
       "avb_enable",
       "avb_avbtool",
       "avb_salt",
diff --git a/tools/releasetools/check_ota_package_signature.py b/tools/releasetools/check_ota_package_signature.py
index 548b619..8106d06 100755
--- a/tools/releasetools/check_ota_package_signature.py
+++ b/tools/releasetools/check_ota_package_signature.py
@@ -22,15 +22,30 @@
 
 import argparse
 import common
+import os
+import os.path
 import re
+import site
 import subprocess
 import sys
+import tempfile
+import zipfile
 
 from hashlib import sha1
 from hashlib import sha256
 
+# 'update_payload' package is under 'system/update_engine/scripts/', which
+# should be included in PYTHONPATH. Try to set it up automatically if
+# if ANDROID_BUILD_TOP is available.
+top = os.getenv('ANDROID_BUILD_TOP')
+if top:
+  site.addsitedir(os.path.join(top, 'system', 'update_engine', 'scripts'))
 
-def cert_uses_sha256(cert):
+from update_payload.payload import Payload
+from update_payload.update_metadata_pb2 import Signatures
+
+
+def CertUsesSha256(cert):
   """Check if the cert uses SHA-256 hashing algorithm."""
 
   cmd = ['openssl', 'x509', '-text', '-noout', '-in', cert]
@@ -46,7 +61,7 @@
   return algorithm.group(1).startswith('sha256')
 
 
-def verify_package(cert, package):
+def VerifyPackage(cert, package):
   """Verify the given package with the certificate.
 
   (Comments from bootable/recovery/verifier.cpp:)
@@ -90,7 +105,7 @@
   print('Signed data length: %d' % (signed_len,))
   print('Signature start: %d' % (signature_start,))
 
-  use_sha256 = cert_uses_sha256(cert)
+  use_sha256 = CertUsesSha256(cert)
   print('Use SHA-256: %s' % (use_sha256,))
 
   if use_sha256:
@@ -100,7 +115,7 @@
   h.update(package_bytes[:signed_len])
   package_digest = h.hexdigest().lower()
 
-  print('Digest: %s\n' % (package_digest,))
+  print('Digest: %s' % (package_digest,))
 
   # Get the signature from the input package.
   signature = package_bytes[signature_start:-6]
@@ -141,7 +156,87 @@
   assert package_digest == digest_string, "Verification failed."
 
   # Verified successfully upon reaching here.
-  print('VERIFIED\n')
+  print('\nWhole package signature VERIFIED\n')
+
+
+def VerifyAbOtaPayload(cert, package):
+  """Verifies the payload and metadata signatures in an A/B OTA payload."""
+
+  def VerifySignatureBlob(hash_file, blob):
+    """Verifies the input hash_file against the signature blob."""
+    signatures = Signatures()
+    signatures.ParseFromString(blob)
+
+    extracted_sig_file = common.MakeTempFile(
+        prefix='extracted-sig-', suffix='.bin')
+    # In Android, we only expect one signature.
+    assert len(signatures.signatures) == 1, \
+        'Invalid number of signatures: %d' % len(signatures.signatures)
+    signature = signatures.signatures[0]
+    length = len(signature.data)
+    assert length == 256, 'Invalid signature length %d' % (length,)
+    with open(extracted_sig_file, 'w') as f:
+      f.write(signature.data)
+
+    # Verify the signature file extracted from the payload, by reversing the
+    # signing operation. Alternatively, this can be done by calling 'openssl
+    # rsautl -verify -certin -inkey <cert.pem> -in <extracted_sig_file> -out
+    # <output>', then to assert that
+    # <output> == SHA-256 DigestInfo prefix || <hash_file>.
+    cmd = ['openssl', 'pkeyutl', '-verify', '-certin', '-inkey', cert,
+           '-pkeyopt', 'digest:sha256', '-in', hash_file,
+           '-sigfile', extracted_sig_file]
+    p = common.Run(cmd, stdout=subprocess.PIPE)
+    result, _ = p.communicate()
+
+    # https://github.com/openssl/openssl/pull/3213
+    # 'openssl pkeyutl -verify' (prior to 1.1.0) returns non-zero return code,
+    # even on successful verification. To avoid the false alarm with older
+    # openssl, check the output directly.
+    assert result.strip() == 'Signature Verified Successfully', result.strip()
+
+  package_zip = zipfile.ZipFile(package, 'r')
+  if 'payload.bin' not in package_zip.namelist():
+    common.ZipClose(package_zip)
+    return
+
+  print('Verifying A/B OTA payload signatures...')
+
+  package_dir = tempfile.mkdtemp(prefix='package-')
+  common.OPTIONS.tempfiles.append(package_dir)
+
+  payload_file = package_zip.extract('payload.bin', package_dir)
+  payload = Payload(open(payload_file, 'rb'))
+  payload.Init()
+
+  # Extract the payload hash and metadata hash from the payload.bin.
+  payload_hash_file = common.MakeTempFile(prefix='hash-', suffix='.bin')
+  metadata_hash_file = common.MakeTempFile(prefix='hash-', suffix='.bin')
+  cmd = ['brillo_update_payload', 'hash',
+         '--unsigned_payload', payload_file,
+         '--signature_size', '256',
+         '--metadata_hash_file', metadata_hash_file,
+         '--payload_hash_file', payload_hash_file]
+  p = common.Run(cmd, stdout=subprocess.PIPE)
+  p.communicate()
+  assert p.returncode == 0, 'brillo_update_payload hash failed'
+
+  # Payload signature verification.
+  assert payload.manifest.HasField('signatures_offset')
+  payload_signature = payload.ReadDataBlob(
+      payload.manifest.signatures_offset, payload.manifest.signatures_size)
+  VerifySignatureBlob(payload_hash_file, payload_signature)
+
+  # Metadata signature verification.
+  metadata_signature = payload.ReadDataBlob(
+      -payload.header.metadata_signature_len,
+      payload.header.metadata_signature_len)
+  VerifySignatureBlob(metadata_hash_file, metadata_signature)
+
+  common.ZipClose(package_zip)
+
+  # Verified successfully upon reaching here.
+  print('\nPayload signatures VERIFIED\n\n')
 
 
 def main():
@@ -150,7 +245,8 @@
   parser.add_argument('package', help='The OTA package to be verified.')
   args = parser.parse_args()
 
-  verify_package(args.certificate, args.package)
+  VerifyPackage(args.certificate, args.package)
+  VerifyAbOtaPayload(args.certificate, args.package)
 
 
 if __name__ == '__main__':
@@ -159,3 +255,5 @@
   except AssertionError as err:
     print('\n    ERROR: %s\n' % (err,))
     sys.exit(1)
+  finally:
+    common.Cleanup()
diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py
index f3096c9..fc1f52a 100644
--- a/tools/releasetools/common.py
+++ b/tools/releasetools/common.py
@@ -76,6 +76,11 @@
 # Values for "certificate" in apkcerts that mean special things.
 SPECIAL_CERT_STRINGS = ("PRESIGNED", "EXTERNAL")
 
+
+# The partitions allowed to be signed by AVB (Android verified boot 2.0).
+AVB_PARTITIONS = ('boot', 'recovery', 'system', 'vendor', 'dtbo')
+
+
 class ErrorCode(object):
   """Define error_codes for failures that happen during the actual
   update package installation.
@@ -252,15 +257,16 @@
   else:
     d["fstab"] = None
 
-  d["build.prop"] = LoadBuildProp(read_helper)
+  d["build.prop"] = LoadBuildProp(read_helper, 'SYSTEM/build.prop')
+  d["vendor.build.prop"] = LoadBuildProp(read_helper, 'VENDOR/build.prop')
   return d
 
 
-def LoadBuildProp(read_helper):
+def LoadBuildProp(read_helper, prop_file):
   try:
-    data = read_helper("SYSTEM/build.prop")
+    data = read_helper(prop_file)
   except KeyError:
-    print("Warning: could not find SYSTEM/build.prop in %s" % (zip,))
+    print("Warning: could not read %s" % (prop_file,))
     data = ""
   return LoadDictionaryFromLines(data.split("\n"))
 
@@ -447,10 +453,12 @@
   else:
     cmd.extend(["--output", img.name])
 
+  # "boot" or "recovery", without extension.
+  partition_name = os.path.basename(sourcedir).lower()
+
   p = Run(cmd, stdout=subprocess.PIPE)
   p.communicate()
-  assert p.returncode == 0, "mkbootimg of %s image failed" % (
-      os.path.basename(sourcedir),)
+  assert p.returncode == 0, "mkbootimg of %s image failed" % (partition_name,)
 
   if (info_dict.get("boot_signer", None) == "true" and
       info_dict.get("verity_key", None)):
@@ -459,7 +467,7 @@
     if two_step_image:
       path = "/boot"
     else:
-      path = "/" + os.path.basename(sourcedir).lower()
+      path = "/" + partition_name
     cmd = [OPTIONS.boot_signer_path]
     cmd.extend(OPTIONS.boot_signer_args)
     cmd.extend([path, img.name,
@@ -471,7 +479,7 @@
 
   # Sign the image if vboot is non-empty.
   elif info_dict.get("vboot", None):
-    path = "/" + os.path.basename(sourcedir).lower()
+    path = "/" + partition_name
     img_keyblock = tempfile.NamedTemporaryFile()
     # We have switched from the prebuilt futility binary to using the tool
     # (futility-host) built from the source. Override the setting in the old
@@ -493,20 +501,21 @@
     img_unsigned.close()
     img_keyblock.close()
 
-  # AVB: if enabled, calculate and add hash to boot.img.
+  # AVB: if enabled, calculate and add hash to boot.img or recovery.img.
   if info_dict.get("avb_enable") == "true":
     avbtool = os.getenv('AVBTOOL') or info_dict["avb_avbtool"]
-    part_size = info_dict["boot_size"]
+    part_size = info_dict[partition_name + "_size"]
     cmd = [avbtool, "add_hash_footer", "--image", img.name,
-           "--partition_size", str(part_size), "--partition_name", "boot"]
-    AppendAVBSigningArgs(cmd, "boot")
-    args = info_dict.get("avb_boot_add_hash_footer_args")
+           "--partition_size", str(part_size), "--partition_name",
+           partition_name]
+    AppendAVBSigningArgs(cmd, partition_name)
+    args = info_dict.get("avb_" + partition_name + "_add_hash_footer_args")
     if args and args.strip():
       cmd.extend(shlex.split(args))
     p = Run(cmd, stdout=subprocess.PIPE)
     p.communicate()
     assert p.returncode == 0, "avbtool add_hash_footer of %s failed" % (
-        os.path.basename(OPTIONS.input_tmp))
+        partition_name,)
 
   img.seek(os.SEEK_SET, 0)
   data = img.read()
@@ -734,10 +743,18 @@
 
 
 def CheckSize(data, target, info_dict):
-  """Check the data string passed against the max size limit, if
-  any, for the given target.  Raise exception if the data is too big.
-  Print a warning if the data is nearing the maximum size."""
+  """Checks the data string passed against the max size limit.
 
+  For non-AVB images, raise exception if the data is too big. Print a warning
+  if the data is nearing the maximum size.
+
+  For AVB images, the actual image size should be identical to the limit.
+
+  Args:
+    data: A string that contains all the data for the partition.
+    target: The partition name. The ".img" suffix is optional.
+    info_dict: The dict to be looked up for relevant info.
+  """
   if target.endswith(".img"):
     target = target[:-4]
   mount_point = "/" + target
@@ -757,14 +774,22 @@
     return
 
   size = len(data)
-  pct = float(size) * 100.0 / limit
-  msg = "%s size (%d) is %.2f%% of limit (%d)" % (target, size, pct, limit)
-  if pct >= 99.0:
-    raise ExternalError(msg)
-  elif pct >= 95.0:
-    print("\n  WARNING: %s\n" % (msg,))
-  elif OPTIONS.verbose:
-    print("  ", msg)
+  # target could be 'userdata' or 'cache'. They should follow the non-AVB image
+  # path.
+  if info_dict.get("avb_enable") == "true" and target in AVB_PARTITIONS:
+    if size != limit:
+      raise ExternalError(
+          "Mismatching image size for %s: expected %d actual %d" % (
+              target, limit, size))
+  else:
+    pct = float(size) * 100.0 / limit
+    msg = "%s size (%d) is %.2f%% of limit (%d)" % (target, size, pct, limit)
+    if pct >= 99.0:
+      raise ExternalError(msg)
+    elif pct >= 95.0:
+      print("\n  WARNING: %s\n" % (msg,))
+    elif OPTIONS.verbose:
+      print("  ", msg)
 
 
 def ReadApkCerts(tf_zip):
@@ -1570,11 +1595,11 @@
     #   decompression_time: 15s  | 25s                | 25s
 
     if not self.src:
-      bro_cmd = ['bro', '--quality', '6',
-                 '--input', '{}.new.dat'.format(self.path),
-                 '--output', '{}.new.dat.br'.format(self.path)]
+      brotli_cmd = ['brotli', '--quality=6',
+                    '--output={}.new.dat.br'.format(self.path),
+                    '{}.new.dat'.format(self.path)]
       print("Compressing {}.new.dat with brotli".format(self.partition))
-      p = Run(bro_cmd, stdout=subprocess.PIPE)
+      p = Run(brotli_cmd, stdout=subprocess.PIPE)
       p.communicate()
       assert p.returncode == 0,\
           'compression of {}.new.dat failed'.format(self.partition)
diff --git a/tools/releasetools/ota_from_target_files.py b/tools/releasetools/ota_from_target_files.py
index 1b0f68b..63af8b0 100755
--- a/tools/releasetools/ota_from_target_files.py
+++ b/tools/releasetools/ota_from_target_files.py
@@ -21,9 +21,6 @@
 
 Usage:  ota_from_target_files [flags] input_target_files output_ota_package
 
-  --board_config  <file>
-      Deprecated.
-
   -k (--package_key) <key> Key to use to sign the package (default is
       the value of default_system_dev_certificate from the input
       target-files's META/misc_info.txt, or
@@ -46,9 +43,9 @@
       Similar to --full_radio. When generating an incremental OTA, always
       include a full copy of bootloader image.
 
-  -v  (--verify)
-      Remount and verify the checksums of the files written to the
-      system and vendor (if used) partitions.  Incremental builds only.
+  --verify
+      Remount and verify the checksums of the files written to the system and
+      vendor (if used) partitions. Non-A/B incremental OTAs only.
 
   -o  (--oem_settings)  <main_file[,additional_files...]>
       Comma seperated list of files used to specify the expected OEM-specific
@@ -275,6 +272,11 @@
     return False
 
 
+def HasTrebleEnabled(target_files_zip, info_dict):
+  return (HasVendorPartition(target_files_zip) and
+          GetBuildProp("ro.treble.enabled", info_dict) == "true")
+
+
 def GetOemProperty(name, oem_props, oem_dict, info_dict):
   if oem_props is not None and name in oem_props:
     return oem_dict[name]
@@ -316,57 +318,93 @@
   return sparse_img.SparseImage(path, mappath, clobbered_blocks)
 
 
-def AddCompatibilityArchive(target_zip, output_zip, system_included=True,
-                            vendor_included=True):
-  """Adds compatibility info from target files into the output zip.
+def AddCompatibilityArchiveIfTrebleEnabled(target_zip, output_zip,
+                                           target_info_dict,
+                                           source_info_dict=None):
+  """Adds compatibility info into the output zip if it's Treble-enabled target.
 
   Metadata used for on-device compatibility verification is retrieved from
   target_zip then added to compatibility.zip which is added to the output_zip
   archive.
 
-  Compatibility archive should only be included for devices with a vendor
-  partition as checking provides value when system and vendor are independently
-  versioned.
+  Compatibility archive should only be included for devices that have enabled
+  Treble support.
 
   Args:
     target_zip: Zip file containing the source files to be included for OTA.
     output_zip: Zip file that will be sent for OTA.
-    system_included: If True, the system image will be updated and therefore
-        its metadata should be included.
-    vendor_included: If True, the vendor image will be updated and therefore
-        its metadata should be included.
+    target_info_dict: The dict that holds the target build info.
+    source_info_dict: The dict that holds the source build info, if generating
+        an incremental OTA; None otherwise.
   """
 
-  # Determine what metadata we need. Files are names relative to META/.
-  compatibility_files = []
-  vendor_metadata = ("vendor_manifest.xml", "vendor_matrix.xml")
-  system_metadata = ("system_manifest.xml", "system_matrix.xml")
-  if vendor_included:
-    compatibility_files += vendor_metadata
-  if system_included:
-    compatibility_files += system_metadata
+  def AddCompatibilityArchive(system_updated, vendor_updated):
+    """Adds compatibility info based on system/vendor update status.
 
-  # Create new archive.
-  compatibility_archive = tempfile.NamedTemporaryFile()
-  compatibility_archive_zip = zipfile.ZipFile(compatibility_archive, "w",
-      compression=zipfile.ZIP_DEFLATED)
+    Args:
+      system_updated: If True, the system image will be updated and therefore
+          its metadata should be included.
+      vendor_updated: If True, the vendor image will be updated and therefore
+          its metadata should be included.
+    """
+    # Determine what metadata we need. Files are names relative to META/.
+    compatibility_files = []
+    vendor_metadata = ("vendor_manifest.xml", "vendor_matrix.xml")
+    system_metadata = ("system_manifest.xml", "system_matrix.xml")
+    if vendor_updated:
+      compatibility_files += vendor_metadata
+    if system_updated:
+      compatibility_files += system_metadata
 
-  # Add metadata.
-  for file_name in compatibility_files:
-    target_file_name = "META/" + file_name
+    # Create new archive.
+    compatibility_archive = tempfile.NamedTemporaryFile()
+    compatibility_archive_zip = zipfile.ZipFile(compatibility_archive, "w",
+        compression=zipfile.ZIP_DEFLATED)
 
-    if target_file_name in target_zip.namelist():
-      data = target_zip.read(target_file_name)
-      common.ZipWriteStr(compatibility_archive_zip, file_name, data)
+    # Add metadata.
+    for file_name in compatibility_files:
+      target_file_name = "META/" + file_name
 
-  # Ensure files are written before we copy into output_zip.
-  compatibility_archive_zip.close()
+      if target_file_name in target_zip.namelist():
+        data = target_zip.read(target_file_name)
+        common.ZipWriteStr(compatibility_archive_zip, file_name, data)
 
-  # Only add the archive if we have any compatibility info.
-  if compatibility_archive_zip.namelist():
-    common.ZipWrite(output_zip, compatibility_archive.name,
-                    arcname="compatibility.zip",
-                    compress_type=zipfile.ZIP_STORED)
+    # Ensure files are written before we copy into output_zip.
+    compatibility_archive_zip.close()
+
+    # Only add the archive if we have any compatibility info.
+    if compatibility_archive_zip.namelist():
+      common.ZipWrite(output_zip, compatibility_archive.name,
+                      arcname="compatibility.zip",
+                      compress_type=zipfile.ZIP_STORED)
+
+  # Will only proceed if the target has enabled the Treble support (as well as
+  # having a /vendor partition).
+  if not HasTrebleEnabled(target_zip, target_info_dict):
+    return
+
+  # We don't support OEM thumbprint in Treble world (which calculates
+  # fingerprints in a different way as shown in CalculateFingerprint()).
+  assert not target_info_dict.get("oem_fingerprint_properties")
+
+  # Full OTA carries the info for system/vendor both.
+  if source_info_dict is None:
+    AddCompatibilityArchive(True, True)
+    return
+
+  assert not source_info_dict.get("oem_fingerprint_properties")
+
+  source_fp = GetBuildProp("ro.build.fingerprint", source_info_dict)
+  target_fp = GetBuildProp("ro.build.fingerprint", target_info_dict)
+  system_updated = source_fp != target_fp
+
+  source_fp_vendor = GetVendorBuildProp("ro.vendor.build.fingerprint",
+                                        source_info_dict)
+  target_fp_vendor = GetVendorBuildProp("ro.vendor.build.fingerprint",
+                                        target_info_dict)
+  vendor_updated = source_fp_vendor != target_fp_vendor
+
+  AddCompatibilityArchive(system_updated, vendor_updated)
 
 
 def WriteFullOTAPackage(input_zip, output_zip):
@@ -470,11 +508,6 @@
   if HasVendorPartition(input_zip):
     system_progress -= 0.1
 
-  # Place a copy of file_contexts.bin into the OTA package which will be used
-  # by the recovery program.
-  if "selinux_fc" in OPTIONS.info_dict:
-    WritePolicyConfig(OPTIONS.info_dict["selinux_fc"], output_zip)
-
   recovery_mount_options = OPTIONS.info_dict.get("recovery_mount_options")
 
   script.ShowProgress(system_progress, 0)
@@ -499,6 +532,9 @@
     vendor_diff = common.BlockDifference("vendor", vendor_tgt)
     vendor_diff.WriteScript(script, output_zip)
 
+  AddCompatibilityArchiveIfTrebleEnabled(input_zip, output_zip,
+                                         OPTIONS.info_dict)
+
   common.CheckSize(boot_img.data, "boot.img", OPTIONS.info_dict)
   common.ZipWriteStr(output_zip, "boot.img", boot_img.data)
 
@@ -540,10 +576,6 @@
   WriteMetadata(metadata, output_zip)
 
 
-def WritePolicyConfig(file_name, output_zip):
-  common.ZipWrite(output_zip, file_name, os.path.basename(file_name))
-
-
 def WriteMetadata(metadata, output_zip):
   value = "".join(["%s=%s\n" % kv for kv in sorted(metadata.iteritems())])
   common.ZipWriteStr(output_zip, METADATA_NAME, value,
@@ -551,13 +583,22 @@
 
 
 def GetBuildProp(prop, info_dict):
-  """Return the fingerprint of the build of a given target-files info_dict."""
+  """Returns the inquired build property from a given info_dict."""
   try:
     return info_dict.get("build.prop", {})[prop]
   except KeyError:
     raise common.ExternalError("couldn't find %s in build.prop" % (prop,))
 
 
+def GetVendorBuildProp(prop, info_dict):
+  """Returns the inquired vendor build property from a given info_dict."""
+  try:
+    return info_dict.get("vendor.build.prop", {})[prop]
+  except KeyError:
+    raise common.ExternalError(
+        "couldn't find %s in vendor.build.prop" % (prop,))
+
+
 def HandleDowngradeMetadata(metadata):
   # Only incremental OTAs are allowed to reach here.
   assert OPTIONS.incremental_source is not None
@@ -688,6 +729,10 @@
   else:
     vendor_diff = None
 
+  AddCompatibilityArchiveIfTrebleEnabled(
+      target_zip, output_zip, OPTIONS.target_info_dict,
+      OPTIONS.source_info_dict)
+
   AppendAssertions(script, OPTIONS.target_info_dict, oem_dicts)
   device_specific.IncrementalOTA_Assertions()
 
@@ -1077,6 +1122,11 @@
          "--target_image", target_file]
   if source_file is not None:
     cmd.extend(["--source_image", source_file])
+  if OPTIONS.downgrade:
+    max_timestamp = GetBuildProp("ro.build.date.utc", OPTIONS.source_info_dict)
+  else:
+    max_timestamp = metadata["post-timestamp"]
+  cmd.extend(["--max_timestamp", max_timestamp])
   p1 = common.Run(cmd, stdout=log_file, stderr=subprocess.STDOUT)
   p1.communicate()
   assert p1.returncode == 0, "brillo_update_payload generate failed"
@@ -1178,32 +1228,13 @@
     else:
       print("Warning: cannot find care map file in target_file package")
 
-  if HasVendorPartition(target_zip):
-    update_vendor = True
-    update_system = True
+  # OPTIONS.source_info_dict must be None for incrementals.
+  if source_file is None:
+    assert OPTIONS.source_info_dict is None
 
-    # If incremental then figure out what is being updated so metadata only for
-    # the updated image is included.
-    if source_file is not None:
-      input_tmp, input_zip = common.UnzipTemp(
-          target_file, UNZIP_PATTERN)
-      source_tmp, source_zip = common.UnzipTemp(
-          source_file, UNZIP_PATTERN)
+  AddCompatibilityArchiveIfTrebleEnabled(
+      target_zip, output_zip, OPTIONS.info_dict, OPTIONS.source_info_dict)
 
-      vendor_src = GetImage("vendor", source_tmp)
-      vendor_tgt = GetImage("vendor", input_tmp)
-      system_src = GetImage("system", source_tmp)
-      system_tgt = GetImage("system", input_tmp)
-
-      update_system = system_src.TotalSha1() != system_tgt.TotalSha1()
-      update_vendor = vendor_src.TotalSha1() != vendor_tgt.TotalSha1()
-
-      input_zip.close()
-      source_zip.close()
-
-    target_zip = zipfile.ZipFile(target_file, "r")
-    AddCompatibilityArchive(target_zip, output_zip, update_system,
-                            update_vendor)
   common.ZipClose(target_zip)
 
   # Write the current metadata entry with placeholders.
@@ -1261,9 +1292,7 @@
 def main(argv):
 
   def option_handler(o, a):
-    if o == "--board_config":
-      pass   # deprecated
-    elif o in ("-k", "--package_key"):
+    if o in ("-k", "--package_key"):
       OPTIONS.package_key = a
     elif o in ("-i", "--incremental_from"):
       OPTIONS.incremental_source = a
@@ -1325,7 +1354,6 @@
   args = common.ParseOptions(argv, __doc__,
                              extra_opts="b:k:i:d:we:t:2o:",
                              extra_long_opts=[
-                                 "board_config=",
                                  "package_key=",
                                  "incremental_from=",
                                  "full_radio",
diff --git a/tools/releasetools/pylintrc b/tools/releasetools/pylintrc
index 90de1af..7b3405c 100644
--- a/tools/releasetools/pylintrc
+++ b/tools/releasetools/pylintrc
@@ -144,9 +144,6 @@
 
 [BASIC]
 
-# Required attributes for module, separated by a comma
-required-attributes=
-
 # List of builtins function names that should not be used, separated by a comma
 bad-functions=map,filter,input
 
@@ -357,10 +354,6 @@
 
 [CLASSES]
 
-# List of interface methods to ignore, separated by a comma. This is used for
-# instance to not check methods defines in Zope's Interface base class.
-ignore-iface-methods=isImplementedBy,deferred,extends,names,namesAndDescriptions,queryDescriptionFor,getBases,getDescriptionFor,getDoc,getName,getTaggedValue,getTaggedValueTags,isEqualOrExtendedBy,setTaggedValue,isImplementedByInstancesOf,adaptWith,is_implemented_by
-
 # List of method names used to declare (i.e. assign) instance attributes.
 defining-attr-methods=__init__,__new__,setUp
 
diff --git a/tools/releasetools/rangelib.py b/tools/releasetools/rangelib.py
index fa6eec1..87380a5 100644
--- a/tools/releasetools/rangelib.py
+++ b/tools/releasetools/rangelib.py
@@ -320,7 +320,7 @@
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     >>> list(RangeSet("10-19 3-5").next_item())
     [3, 4, 5, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
-    >>> list(rangelib.RangeSet("10-19 3 5 7").next_item())
+    >>> list(RangeSet("10-19 3 5 7").next_item())
     [3, 5, 7, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
     """
     for s, e in self:
diff --git a/tools/releasetools/sign_target_files_apks.py b/tools/releasetools/sign_target_files_apks.py
index 83c2487..f559b29 100755
--- a/tools/releasetools/sign_target_files_apks.py
+++ b/tools/releasetools/sign_target_files_apks.py
@@ -444,7 +444,7 @@
   except KeyError:
     raise common.ExternalError("can't read META/otakeys.txt from input")
 
-  extra_recovery_keys = misc_info.get("extra_recovery_keys", None)
+  extra_recovery_keys = misc_info.get("extra_recovery_keys")
   if extra_recovery_keys:
     extra_recovery_keys = [OPTIONS.key_map.get(k, k) + ".x509.pem"
                            for k in extra_recovery_keys.split()]
@@ -468,8 +468,10 @@
   else:
     devkey = misc_info.get("default_system_dev_certificate",
                            "build/target/product/security/testkey")
-    mapped_keys.append(
-        OPTIONS.key_map.get(devkey, devkey) + ".x509.pem")
+    mapped_devkey = OPTIONS.key_map.get(devkey, devkey)
+    if mapped_devkey != devkey:
+      misc_info["default_system_dev_certificate"] = mapped_devkey
+    mapped_keys.append(mapped_devkey + ".x509.pem")
     print("META/otakeys.txt has no keys; using %s for OTA package"
           " verification." % (mapped_keys[0],))
 
@@ -587,6 +589,7 @@
   AVB_FOOTER_ARGS_BY_PARTITION = {
     'boot' : 'avb_boot_add_hash_footer_args',
     'dtbo' : 'avb_dtbo_add_hash_footer_args',
+    'recovery' : 'avb_recovery_add_hash_footer_args',
     'system' : 'avb_system_add_hashtree_footer_args',
     'vendor' : 'avb_vendor_add_hashtree_footer_args',
     'vbmeta' : 'avb_vbmeta_args',
diff --git a/tools/releasetools/test_common.py b/tools/releasetools/test_common.py
index 36f256d..10ec0d3 100644
--- a/tools/releasetools/test_common.py
+++ b/tools/releasetools/test_common.py
@@ -20,32 +20,27 @@
 import unittest
 import zipfile
 
+from hashlib import sha1
+
 import common
 import validate_target_files
 
-
-def random_string_with_holes(size, block_size, step_size):
-  data = ["\0"] * size
-  for begin in range(0, size, step_size):
-    end = begin + block_size
-    data[begin:end] = os.urandom(block_size)
-  return "".join(data)
+KiB = 1024
+MiB = 1024 * KiB
+GiB = 1024 * MiB
 
 def get_2gb_string():
-  kilobytes = 1024
-  megabytes = 1024 * kilobytes
-  gigabytes = 1024 * megabytes
-
-  size = int(2 * gigabytes + 1)
-  block_size = 4 * kilobytes
-  step_size = 4 * megabytes
-  two_gb_string = random_string_with_holes(
-        size, block_size, step_size)
-  return two_gb_string
+  size = int(2 * GiB + 1)
+  block_size = 4 * KiB
+  step_size = 4 * MiB
+  # Generate a long string with holes, e.g. 'xyz\x00abc\x00...'.
+  for _ in range(0, size, step_size):
+    yield os.urandom(block_size)
+    yield '\0' * (step_size - block_size)
 
 
 class CommonZipTest(unittest.TestCase):
-  def _verify(self, zip_file, zip_file_name, arcname, contents,
+  def _verify(self, zip_file, zip_file_name, arcname, expected_hash,
               test_file_name=None, expected_stat=None, expected_mode=0o644,
               expected_compress_type=zipfile.ZIP_STORED):
     # Verify the stat if present.
@@ -69,7 +64,11 @@
     self.assertEqual(info.compress_type, expected_compress_type)
 
     # Verify the zip contents.
-    self.assertEqual(zip_file.read(arcname), contents)
+    entry = zip_file.open(arcname)
+    sha1_hash = sha1()
+    for chunk in iter(lambda: entry.read(4 * MiB), ''):
+      sha1_hash.update(chunk)
+    self.assertEqual(expected_hash, sha1_hash.hexdigest())
     self.assertIsNone(zip_file.testzip())
 
   def _test_ZipWrite(self, contents, extra_zipwrite_args=None):
@@ -90,7 +89,10 @@
     zip_file = zipfile.ZipFile(zip_file_name, "w")
 
     try:
-      test_file.write(contents)
+      sha1_hash = sha1()
+      for data in contents:
+        sha1_hash.update(data)
+        test_file.write(data)
       test_file.close()
 
       expected_stat = os.stat(test_file_name)
@@ -102,8 +104,9 @@
       common.ZipWrite(zip_file, test_file_name, **extra_zipwrite_args)
       common.ZipClose(zip_file)
 
-      self._verify(zip_file, zip_file_name, arcname, contents, test_file_name,
-                   expected_stat, expected_mode, expected_compress_type)
+      self._verify(zip_file, zip_file_name, arcname, sha1_hash.hexdigest(),
+                   test_file_name, expected_stat, expected_mode,
+                   expected_compress_type)
     finally:
       os.remove(test_file_name)
       os.remove(zip_file_name)
@@ -133,7 +136,7 @@
       common.ZipWriteStr(zip_file, zinfo_or_arcname, contents, **extra_args)
       common.ZipClose(zip_file)
 
-      self._verify(zip_file, zip_file_name, arcname, contents,
+      self._verify(zip_file, zip_file_name, arcname, sha1(contents).hexdigest(),
                    expected_mode=expected_mode,
                    expected_compress_type=expected_compress_type)
     finally:
@@ -159,7 +162,10 @@
     zip_file = zipfile.ZipFile(zip_file_name, "w")
 
     try:
-      test_file.write(large)
+      sha1_hash = sha1()
+      for data in large:
+        sha1_hash.update(data)
+        test_file.write(data)
       test_file.close()
 
       expected_stat = os.stat(test_file_name)
@@ -173,12 +179,13 @@
       common.ZipClose(zip_file)
 
       # Verify the contents written by ZipWrite().
-      self._verify(zip_file, zip_file_name, arcname_large, large,
-                   test_file_name, expected_stat, expected_mode,
-                   expected_compress_type)
+      self._verify(zip_file, zip_file_name, arcname_large,
+                   sha1_hash.hexdigest(), test_file_name, expected_stat,
+                   expected_mode, expected_compress_type)
 
       # Verify the contents written by ZipWriteStr().
-      self._verify(zip_file, zip_file_name, arcname_small, small,
+      self._verify(zip_file, zip_file_name, arcname_small,
+                   sha1(small).hexdigest(),
                    expected_compress_type=expected_compress_type)
     finally:
       os.remove(zip_file_name)
@@ -287,13 +294,17 @@
       common.ZipWriteStr(zip_file, zinfo, random_string, perms=0o400)
       common.ZipClose(zip_file)
 
-      self._verify(zip_file, zip_file_name, "foo", random_string,
+      self._verify(zip_file, zip_file_name, "foo",
+                   sha1(random_string).hexdigest(),
                    expected_mode=0o644)
-      self._verify(zip_file, zip_file_name, "bar", random_string,
+      self._verify(zip_file, zip_file_name, "bar",
+                   sha1(random_string).hexdigest(),
                    expected_mode=0o755)
-      self._verify(zip_file, zip_file_name, "baz", random_string,
+      self._verify(zip_file, zip_file_name, "baz",
+                   sha1(random_string).hexdigest(),
                    expected_mode=0o740)
-      self._verify(zip_file, zip_file_name, "qux", random_string,
+      self._verify(zip_file, zip_file_name, "qux",
+                   sha1(random_string).hexdigest(),
                    expected_mode=0o400)
     finally:
       os.remove(zip_file_name)
@@ -310,8 +321,18 @@
     dummy_fstab = \
         ["/dev/soc.0/by-name/boot /boot emmc defaults defaults",
          "/dev/soc.0/by-name/recovery /recovery emmc defaults defaults"]
-    self._info["fstab"] = common.LoadRecoveryFSTab(lambda x : "\n".join(x),
-                                                   2, dummy_fstab)
+    self._info["fstab"] = common.LoadRecoveryFSTab("\n".join, 2, dummy_fstab)
+    # Construct the gzipped recovery.img and boot.img
+    self.recovery_data = bytearray([
+        0x1f, 0x8b, 0x08, 0x00, 0x81, 0x11, 0x02, 0x5a, 0x00, 0x03, 0x2b, 0x4a,
+        0x4d, 0xce, 0x2f, 0x4b, 0x2d, 0xaa, 0x04, 0x00, 0xc9, 0x93, 0x43, 0xf3,
+        0x08, 0x00, 0x00, 0x00
+    ])
+    # echo -n "boot" | gzip -f | hd
+    self.boot_data = bytearray([
+        0x1f, 0x8b, 0x08, 0x00, 0x8c, 0x12, 0x02, 0x5a, 0x00, 0x03, 0x4b, 0xca,
+        0xcf, 0x2f, 0x01, 0x00, 0xc4, 0xae, 0xed, 0x46, 0x04, 0x00, 0x00, 0x00
+    ])
 
   def _out_tmp_sink(self, name, data, prefix="SYSTEM"):
     loc = os.path.join(self._tempdir, prefix, name)
@@ -321,8 +342,8 @@
       f.write(data)
 
   def test_full_recovery(self):
-    recovery_image = common.File("recovery.img", "recovery");
-    boot_image = common.File("boot.img", "boot");
+    recovery_image = common.File("recovery.img", self.recovery_data)
+    boot_image = common.File("boot.img", self.boot_data)
     self._info["full_recovery_image"] = "true"
 
     common.MakeRecoveryPatch(self._tempdir, self._out_tmp_sink,
@@ -331,9 +352,9 @@
                                                         self._info)
 
   def test_recovery_from_boot(self):
-    recovery_image = common.File("recovery.img", "recovery");
+    recovery_image = common.File("recovery.img", self.recovery_data)
     self._out_tmp_sink("recovery.img", recovery_image.data, "IMAGES")
-    boot_image = common.File("boot.img", "boot");
+    boot_image = common.File("boot.img", self.boot_data)
     self._out_tmp_sink("boot.img", boot_image.data, "IMAGES")
 
     common.MakeRecoveryPatch(self._tempdir, self._out_tmp_sink,
diff --git a/tools/signapk/Android.bp b/tools/signapk/Android.bp
new file mode 100644
index 0000000..4954cf5
--- /dev/null
+++ b/tools/signapk/Android.bp
@@ -0,0 +1,31 @@
+//
+// 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.
+//
+
+// the signapk tool (a .jar application used to sign packages)
+// ============================================================
+java_library_host {
+    name: "signapk",
+    srcs: ["src/**/*.java"],
+    manifest: "SignApk.mf",
+    static_libs: [
+        "apksig",
+        "bouncycastle",
+        "bouncycastle-bcpkix",
+        "conscrypt",
+    ],
+
+    required: ["libconscrypt_openjdk_jni"],
+}
diff --git a/tools/signapk/Android.mk b/tools/signapk/Android.mk
index 051a51d..ff54d6d 100644
--- a/tools/signapk/Android.mk
+++ b/tools/signapk/Android.mk
@@ -15,24 +15,10 @@
 #
 LOCAL_PATH := $(call my-dir)
 
-# the signapk tool (a .jar application used to sign packages)
-# ============================================================
-include $(CLEAR_VARS)
-LOCAL_MODULE := signapk
-LOCAL_SRC_FILES := $(call all-java-files-under, src)
-LOCAL_JAR_MANIFEST := SignApk.mf
-LOCAL_STATIC_JAVA_LIBRARIES := \
-    apksig \
-    bouncycastle-host \
-    bouncycastle-bcpkix-host \
-    conscrypt-host
-LOCAL_REQUIRED_MODULES := libconscrypt_openjdk_jni
-include $(BUILD_HOST_JAVA_LIBRARY)
-
 ifeq ($(TARGET_BUILD_APPS),)
 # The post-build signing tools need signapk.jar and its shared libraries,
 # but we don't need this if we're just doing unbundled apps.
-my_dist_files := $(LOCAL_INSTALLED_MODULE) \
+my_dist_files := $(HOST_OUT_JAVA_LIBRARIES)/signapk.jar \
     $(HOST_OUT_SHARED_LIBRARIES)/libconscrypt_openjdk_jni$(HOST_SHLIB_SUFFIX)
 
 $(call dist-for-goals,droidcore,$(my_dist_files))
diff --git a/tools/signapk/OWNERS b/tools/signapk/OWNERS
new file mode 100644
index 0000000..0b8d398
--- /dev/null
+++ b/tools/signapk/OWNERS
@@ -0,0 +1,2 @@
+cbrubaker@google.com
+klyubin@google.com
diff --git a/tools/signapk/src/com/android/signapk/SignApk.java b/tools/signapk/src/com/android/signapk/SignApk.java
index 3b00599..fdf6283 100644
--- a/tools/signapk/src/com/android/signapk/SignApk.java
+++ b/tools/signapk/src/com/android/signapk/SignApk.java
@@ -1082,14 +1082,15 @@
                     ByteBuffer[] outputChunks = new ByteBuffer[] {v1SignedApk};
 
                     ZipSections zipSections = findMainZipSections(v1SignedApk);
-                    ApkSignerEngine.OutputApkSigningBlockRequest addV2SignatureRequest =
-                            apkSigner.outputZipSections(
+                    ApkSignerEngine.OutputApkSigningBlockRequest2 addV2SignatureRequest =
+                            apkSigner.outputZipSections2(
                                     DataSources.asDataSource(zipSections.beforeCentralDir),
                                     DataSources.asDataSource(zipSections.centralDir),
                                     DataSources.asDataSource(zipSections.eocd));
                     if (addV2SignatureRequest != null) {
                         // Need to insert the returned APK Signing Block before ZIP Central
                         // Directory.
+                        int padding = addV2SignatureRequest.getPaddingSizeBeforeApkSigningBlock();
                         byte[] apkSigningBlock = addV2SignatureRequest.getApkSigningBlock();
                         // Because the APK Signing Block is inserted before the Central Directory,
                         // we need to adjust accordingly the offset of Central Directory inside the
@@ -1100,10 +1101,12 @@
                         modifiedEocd.order(ByteOrder.LITTLE_ENDIAN);
                         ApkUtils.setZipEocdCentralDirectoryOffset(
                                 modifiedEocd,
-                                zipSections.beforeCentralDir.remaining() + apkSigningBlock.length);
+                                zipSections.beforeCentralDir.remaining() + padding +
+                                apkSigningBlock.length);
                         outputChunks =
                                 new ByteBuffer[] {
                                         zipSections.beforeCentralDir,
+                                        ByteBuffer.allocate(padding),
                                         ByteBuffer.wrap(apkSigningBlock),
                                         zipSections.centralDir,
                                         modifiedEocd};
diff --git a/tools/signtos/Android.bp b/tools/signtos/Android.bp
new file mode 100644
index 0000000..b26631f
--- /dev/null
+++ b/tools/signtos/Android.bp
@@ -0,0 +1,27 @@
+//
+// Copyright (C) 2014 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.
+//
+
+// the signtos tool - signs Trusty images
+// ============================================================
+java_library_host {
+    name: "signtos",
+    srcs: ["SignTos.java"],
+    manifest: "SignTos.mf",
+    static_libs: [
+        "bouncycastle",
+        "bouncycastle-bcpkix",
+    ],
+}
diff --git a/tools/soong_to_convert.py b/tools/soong_to_convert.py
index 3d62d43..083f6f7 100755
--- a/tools/soong_to_convert.py
+++ b/tools/soong_to_convert.py
@@ -76,8 +76,10 @@
     problems = dict()
     deps = dict()
     reverse_deps = dict()
+    module_types = dict()
 
-    for (module, problem, dependencies) in reader:
+    for (module, module_type, problem, dependencies) in reader:
+        module_types[module] = module_type
         problems[module] = problem
         deps[module] = [d for d in dependencies.strip().split(' ') if d != ""]
         for dep in deps[module]:
@@ -94,16 +96,19 @@
         extra = ""
         if len(problems[module]) > 0:
             extra = " ({})".format(problems[module])
-        results.append((count_deps(reverse_deps, module, []), module + extra))
+        results.append((count_deps(reverse_deps, module, []), module + extra, module_types[module]))
 
     return sorted(results, key=lambda result: (-result[0], result[1]))
 
+def filter(results, module_type):
+    return [x for x in results if x[2] == module_type]
+
 def display(results):
     """Displays the results"""
     count_header = "# Blocked on"
     count_width = len(count_header)
     print("{} Module (potential problems)".format(count_header))
-    for (count, module) in results:
+    for (count, module, module_type) in results:
         print("{:>{}} {}".format(count, count_width, module))
 
 def main(filename):
@@ -111,7 +116,15 @@
     with open(filename, 'rb') as csvfile:
         results = process(csv.reader(csvfile))
 
-    display(results)
+    native_results = filter(results, "native")
+    java_results = filter(results, "java")
+
+    print("native modules ready to convert")
+    display(native_results)
+
+    print("")
+    print("java modules ready to convert")
+    display(java_results)
 
 if __name__ == "__main__":
     if len(sys.argv) != 2:
diff --git a/tools/vendor_buildinfo.sh b/tools/vendor_buildinfo.sh
index 485ada3..c12f7cb 100755
--- a/tools/vendor_buildinfo.sh
+++ b/tools/vendor_buildinfo.sh
@@ -6,10 +6,10 @@
 echo "ro.product.board=$TARGET_BOOTLOADER_BOARD_NAME"
 echo "ro.board.platform=$TARGET_BOARD_PLATFORM"
 
-echo "ro.vendor.product.manufacturer=$PRODUCT_MANUFACTURER"
-echo "ro.vendor.product.model=$PRODUCT_MODEL"
-echo "ro.vendor.product.brand=$PRODUCT_BRAND"
-echo "ro.vendor.product.name=$PRODUCT_NAME"
-echo "ro.vendor.product.device=$TARGET_DEVICE"
+echo "ro.product.vendor.manufacturer=$PRODUCT_MANUFACTURER"
+echo "ro.product.vendor.model=$PRODUCT_MODEL"
+echo "ro.product.vendor.brand=$PRODUCT_BRAND"
+echo "ro.product.vendor.name=$PRODUCT_NAME"
+echo "ro.product.vendor.device=$TARGET_DEVICE"
 
 echo "# end build properties"
diff --git a/tools/warn.py b/tools/warn.py
index 44ad368..cc63de4 100755
--- a/tools/warn.py
+++ b/tools/warn.py
@@ -1807,6 +1807,7 @@
      'patterns': [r".*: warning: In file included from .+,"]},
 
     # warnings from clang-tidy
+    group_tidy_warn_pattern('android'),
     group_tidy_warn_pattern('cert'),
     group_tidy_warn_pattern('clang-diagnostic'),
     group_tidy_warn_pattern('cppcoreguidelines'),
diff --git a/tools/zipalign/Android.bp b/tools/zipalign/Android.bp
new file mode 100644
index 0000000..8e6196d
--- /dev/null
+++ b/tools/zipalign/Android.bp
@@ -0,0 +1,36 @@
+//
+// Copyright 2008 The Android Open Source Project
+//
+// Zip alignment tool
+//
+
+cc_binary_host {
+    name: "zipalign",
+
+    srcs: [
+        "ZipAlign.cpp",
+        "ZipEntry.cpp",
+        "ZipFile.cpp",
+    ],
+
+    cflags: ["-Wall", "-Werror"],
+
+    // NOTE: Do not add any shared_libs dependencies because they will break the
+    // static_sdk_tools target.
+    static_libs: [
+        "libutils",
+        "libcutils",
+        "liblog",
+        "libziparchive",
+        "libz",
+        "libbase",
+        "libzopfli",
+    ],
+
+    target: {
+        windows: {
+            host_ldlibs: ["-lpthread"],
+            enabled: true,
+        },
+    },
+}
diff --git a/tools/zipalign/Android.mk b/tools/zipalign/Android.mk
deleted file mode 100644
index 8c0240a..0000000
--- a/tools/zipalign/Android.mk
+++ /dev/null
@@ -1,38 +0,0 @@
-# 
-# Copyright 2008 The Android Open Source Project
-#
-# Zip alignment tool
-#
-
-LOCAL_PATH:= $(call my-dir)
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
-	ZipAlign.cpp \
-	ZipEntry.cpp \
-	ZipFile.cpp
-
-LOCAL_C_INCLUDES += external/zlib \
-	external/zopfli/src
-
-LOCAL_STATIC_LIBRARIES := \
-	libandroidfw \
-	libutils \
-	libcutils \
-	liblog \
-	libzopfli
-
-LOCAL_LDLIBS_linux += -lrt
-
-LOCAL_STATIC_LIBRARIES_windows += libz
-LOCAL_LDLIBS_linux += -lz
-LOCAL_LDLIBS_darwin += -lz
-
-ifneq ($(strip $(BUILD_HOST_static)),)
-LOCAL_LDLIBS += -lpthread
-endif # BUILD_HOST_static
-
-LOCAL_MODULE := zipalign
-LOCAL_MODULE_HOST_OS := darwin linux windows
-
-include $(BUILD_HOST_EXECUTABLE)
diff --git a/tools/zipalign/ZipFile.cpp b/tools/zipalign/ZipFile.cpp
index 1b39902..43bc9bf 100644
--- a/tools/zipalign/ZipFile.cpp
+++ b/tools/zipalign/ZipFile.cpp
@@ -20,13 +20,12 @@
 
 #define LOG_TAG "zip"
 
-#include <androidfw/ZipUtils.h>
 #include <utils/Log.h>
+#include <ziparchive/zip_archive.h>
 
 #include "ZipFile.h"
 
 #include <zlib.h>
-#define DEF_MEM_LEVEL 8                // normally in zutil.h?
 
 #include "zopfli/deflate.h"
 
@@ -135,7 +134,7 @@
 /*
  * Return the Nth entry in the archive.
  */
-ZipEntry* ZipFile::getEntryByIndex(int idx) const
+android::ZipEntry* ZipFile::getEntryByIndex(int idx) const
 {
     if (idx < 0 || idx >= (int) mEntries.size())
         return NULL;
@@ -146,7 +145,7 @@
 /*
  * Find an entry by name.
  */
-ZipEntry* ZipFile::getEntryByName(const char* fileName) const
+android::ZipEntry* ZipFile::getEntryByName(const char* fileName) const
 {
     /*
      * Do a stupid linear string-compare search.
@@ -785,8 +784,6 @@
 status_t ZipFile::copyDataToFp(FILE* dstFp,
     const void* data, size_t size, uint32_t* pCRC32)
 {
-    size_t count;
-
     *pCRC32 = crc32(0L, Z_NULL, 0);
     if (size > 0) {
         *pCRC32 = crc32(*pCRC32, (const unsigned char*)data, size);
@@ -1198,6 +1195,58 @@
 }
 #endif
 
+class BufferWriter : public zip_archive::Writer {
+  public:
+    BufferWriter(void* buf, size_t size) : Writer(),
+        buf_(reinterpret_cast<uint8_t*>(buf)), size_(size), bytes_written_(0) {}
+
+    bool Append(uint8_t* buf, size_t buf_size) override {
+        if (bytes_written_ + buf_size > size_) {
+            return false;
+        }
+
+        memcpy(buf_ + bytes_written_, buf, buf_size);
+        bytes_written_ += buf_size;
+        return true;
+    }
+
+  private:
+    uint8_t* const buf_;
+    const size_t size_;
+    size_t bytes_written_;
+};
+
+class FileReader : public zip_archive::Reader {
+  public:
+    FileReader(FILE* fp) : Reader(), fp_(fp), current_offset_(0) {
+    }
+
+    bool ReadAtOffset(uint8_t* buf, size_t len, uint32_t offset) const {
+        // Data is usually requested sequentially, so this helps avoid pointless
+        // fseeks every time we perform a read. There's an impedence mismatch
+        // here because the original API was designed around pread and pwrite.
+        if (offset != current_offset_) {
+            if (fseek(fp_, offset, SEEK_SET) != 0) {
+                return false;
+            }
+
+            current_offset_ = offset;
+        }
+
+        size_t read = fread(buf, 1, len, fp_);
+        if (read != len) {
+            return false;
+        }
+
+        current_offset_ += read;
+        return true;
+    }
+
+  private:
+    FILE* fp_;
+    mutable uint32_t current_offset_;
+};
+
 // free the memory when you're done
 void* ZipFile::uncompress(const ZipEntry* entry) const
 {
@@ -1240,11 +1289,13 @@
             }
             break;
         case ZipEntry::kCompressDeflated: {
-            if (!ZipUtils::inflateToBuffer(mZipFp, buf, unlen, clen)) {
+            const FileReader reader(mZipFp);
+            BufferWriter writer(buf, unlen);
+            if (zip_archive::Inflate(reader, clen, unlen, &writer, nullptr) != 0) {
                 goto bail;
             }
-            }
             break;
+        }
         default:
             goto bail;
     }
diff --git a/tools/ziptime/Android.bp b/tools/ziptime/Android.bp
index 874d346..5ef45ed 100644
--- a/tools/ziptime/Android.bp
+++ b/tools/ziptime/Android.bp
@@ -27,6 +27,7 @@
     ],
 
     name: "ziptime",
+    cflags: ["-Wall", "-Werror"],
     target: {
         windows: {
             enabled: true,
diff --git a/tools/ziptime/ZipEntry.cpp b/tools/ziptime/ZipEntry.cpp
index 51ce09f..e7b52ed 100644
--- a/tools/ziptime/ZipEntry.cpp
+++ b/tools/ziptime/ZipEntry.cpp
@@ -86,7 +86,6 @@
  */
 status_t ZipEntry::LocalFileHeader::rewrite(FILE* fp)
 {
-    status_t result = 0;
     uint8_t buf[kLFHLen];
 
     if (fread(buf, 1, kLFHLen, fp) != kLFHLen)
@@ -124,7 +123,6 @@
  */
 status_t ZipEntry::CentralDirEntry::rewrite(FILE* fp)
 {
-    status_t result = 0;
     uint8_t buf[kCDELen];
     uint16_t fileNameLength, extraFieldLength, fileCommentLength;