Merge "Add audioserver executable to base config"
diff --git a/CleanSpec.mk b/CleanSpec.mk
index a7c91b4..9cab929 100644
--- a/CleanSpec.mk
+++ b/CleanSpec.mk
@@ -366,6 +366,11 @@
 
 $(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/build.prop)
 
+# Change PLATFORM_VERSION from NYC to N
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/build.prop)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/system/app/*)
+$(call add-clean-step, rm -rf $(PRODUCT_OUT)/obj/APPS/*)
+
 # ************************************************
 # NEWER CLEAN STEPS MUST BE AT THE END OF THE LIST
 # ************************************************
diff --git a/core/LINUX_KERNEL_COPYING b/core/LINUX_KERNEL_COPYING
new file mode 100644
index 0000000..ca442d3
--- /dev/null
+++ b/core/LINUX_KERNEL_COPYING
@@ -0,0 +1,356 @@
+
+   NOTE! This copyright does *not* cover user programs that use kernel
+ services by normal system calls - this is merely considered normal use
+ of the kernel, and does *not* fall under the heading of "derived work".
+ Also note that the GPL below is copyrighted by the Free Software
+ Foundation, but the instance of code that it refers to (the Linux
+ kernel) is copyrighted by me and others who actually wrote it.
+
+ Also note that the only valid version of the GPL as far as the kernel
+ is concerned is _this_ particular version of the license (ie v2, not
+ v2.2 or v3.x or whatever), unless explicitly otherwise stated.
+
+			Linus Torvalds
+
+----------------------------------------
+
+		    GNU GENERAL PUBLIC LICENSE
+		       Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+                       51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+			    Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+		    GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+			    NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+		     END OF TERMS AND CONDITIONS
+
+	    How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) year name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  <signature of Ty Coon>, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Library General
+Public License instead of this License.
diff --git a/core/Makefile b/core/Makefile
index d499c16..bcb257d 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -122,7 +122,7 @@
   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 := $(USER)$(shell $(DATE) +%m%d%H%M)
+    BF_BUILD_NUMBER := $(shell echo $${USER:0:6})$(shell $(DATE) +%m%d%H%M)
   else
     BF_BUILD_NUMBER := $(BUILD_NUMBER)
   endif
@@ -177,6 +177,12 @@
 
 BUILDINFO_SH := build/tools/buildinfo.sh
 
+# TARGET_BUILD_FLAVOR and ro.build.flavor are used only by the test harness to distinguish builds.
+TARGET_BUILD_FLAVOR := $(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)
+ifdef SANITIZE_TARGET
+TARGET_BUILD_FLAVOR := $(TARGET_BUILD_FLAVOR)_asan
+endif
+
 ifdef TARGET_SYSTEM_PROP
 system_prop_file := $(TARGET_SYSTEM_PROP)
 else
@@ -194,7 +200,7 @@
 		echo "import /oem/oem.prop $(prop)" >> $@;)
 endif
 	$(hide) TARGET_BUILD_TYPE="$(TARGET_BUILD_VARIANT)" \
-			TARGET_BUILD_FLAVOR="$(TARGET_PRODUCT)-$(TARGET_BUILD_VARIANT)" \
+			TARGET_BUILD_FLAVOR="$(TARGET_BUILD_FLAVOR)" \
 			TARGET_DEVICE="$(TARGET_DEVICE)" \
 			PRODUCT_NAME="$(TARGET_PRODUCT)" \
 			PRODUCT_BRAND="$(PRODUCT_BRAND)" \
@@ -208,6 +214,7 @@
 			DATE="$(DATE_FROM_FILE)" \
 			BUILD_NUMBER="$(BUILD_NUMBER_FROM_FILE)" \
 			BOARD_BUILD_SYSTEM_ROOT_IMAGE="$(BOARD_BUILD_SYSTEM_ROOT_IMAGE)" \
+			AB_OTA_UPDATER="$(AB_OTA_UPDATER)" \
 			PLATFORM_VERSION="$(PLATFORM_VERSION)" \
 			PLATFORM_SECURITY_PATCH="$(PLATFORM_SECURITY_PATCH)" \
 			PLATFORM_BASE_OS="$(PLATFORM_BASE_OS)" \
@@ -314,7 +321,12 @@
 	@echo Package stats: $@
 	@mkdir -p $(dir $@)
 	$(hide) rm -f $@
+ifeq ($(PACKAGES_TO_STAT),)
+# Create empty package stats file if target builds no jar(s) or apk(s).
+	$(hide) touch $@
+else
 	$(hide) build/tools/dump-package-stats $^ > $@
+endif
 
 .PHONY: package-stats
 package-stats: $(PACKAGE_STATS_FILE)
@@ -652,7 +664,7 @@
 # make the target NOTICE files depend on this particular file too, which will
 # then be in the right directory for the find in combine-notice-files to work.
 $(kernel_notice_file): \
-	    prebuilts/qemu-kernel/arm/LINUX_KERNEL_COPYING \
+	    $(BUILD_SYSTEM)/LINUX_KERNEL_COPYING \
 	    | $(ACP)
 	@echo Copying: $@
 	$(hide) mkdir -p $(dir $@)
@@ -667,10 +679,11 @@
 # before the rules that use that variable to build the image.
 ALL_DEFAULT_INSTALLED_MODULES += $(TARGET_OUT_ETC)/security/otacerts.zip
 $(TARGET_OUT_ETC)/security/otacerts.zip: KEY_CERT_PAIR := $(DEFAULT_KEY_CERT_PAIR)
-$(TARGET_OUT_ETC)/security/otacerts.zip: $(addsuffix .x509.pem,$(DEFAULT_KEY_CERT_PAIR))
+$(TARGET_OUT_ETC)/security/otacerts.zip: $(addsuffix .x509.pem,$(DEFAULT_KEY_CERT_PAIR)) | $(ZIPTIME)
 	$(hide) rm -f $@
 	$(hide) mkdir -p $(dir $@)
-	$(hide) zip -qj $@ $<
+	$(hide) zip -qjX $@ $<
+	$(remove-timestamps-from-package)
 
 .PHONY: otacerts
 otacerts: $(TARGET_OUT_ETC)/security/otacerts.zip
@@ -928,9 +941,10 @@
 		$(call build-recoveryimage-target, $@)
 
 ifneq ($(BOARD_USES_FULL_RECOVERY_IMAGE),true)
-$(RECOVERY_RESOURCE_ZIP): $(INSTALLED_RECOVERYIMAGE_TARGET)
+$(RECOVERY_RESOURCE_ZIP): $(INSTALLED_RECOVERYIMAGE_TARGET) | $(ZIPTIME)
 	$(hide) mkdir -p $(dir $@)
-	$(hide) find $(TARGET_RECOVERY_ROOT_OUT)/res -type f | sort | zip -0qrj $@ -@
+	$(hide) find $(TARGET_RECOVERY_ROOT_OUT)/res -type f | sort | zip -0qrjX $@ -@
+	$(remove-timestamps-from-package)
 endif
 
 .PHONY: recoveryimage-nodeps
@@ -1121,19 +1135,19 @@
 $(INSTALLED_PLATFORM_ZIP) : $(INTERNAL_SYSTEMIMAGE_FILES)
 	$(call pretty,"Platform zip package: $(INSTALLED_PLATFORM_ZIP)")
 	$(hide) rm -f $@
-	$(hide) cd $(dir $@) && zip -qry $(notdir $@) \
+	$(hide) cd $(dir $@) && zip -qryX $(notdir $@) \
 		$(TARGET_COPY_OUT_SYSTEM) \
 		$(patsubst $(PRODUCT_OUT)/%, %, $(TARGET_OUT_NOTICE_FILES)) \
 		$(addprefix symbols/,$(PDK_SYMBOL_FILES_LIST))
 ifdef BOARD_VENDORIMAGE_FILE_SYSTEM_TYPE
-	$(hide) cd $(dir $@) && zip -qry $(notdir $@) \
+	$(hide) cd $(dir $@) && zip -qryX $(notdir $@) \
 		$(TARGET_COPY_OUT_VENDOR)
 endif
 ifneq ($(PDK_PLATFORM_JAVA_ZIP_CONTENTS),)
-	$(hide) cd $(OUT_DIR) && zip -qry $(patsubst $(OUT_DIR)/%,%,$@) $(PDK_PLATFORM_JAVA_ZIP_CONTENTS)
+	$(hide) cd $(OUT_DIR) && zip -qryX $(patsubst $(OUT_DIR)/%,%,$@) $(PDK_PLATFORM_JAVA_ZIP_CONTENTS)
 endif
 ifneq ($(PDK_PLATFORM_ZIP_PRODUCT_BINARIES),)
-	$(hide) zip -qry $@ $(PDK_PLATFORM_ZIP_PRODUCT_BINARIES)
+	$(hide) zip -qryX $@ $(PDK_PLATFORM_ZIP_PRODUCT_BINARIES)
 endif
 
 .PHONY: platform
@@ -1379,13 +1393,14 @@
   $(HOST_LIBRARY_PATH)/libcrypto-host$(HOST_SHLIB_SUFFIX) \
   $(HOST_LIBRARY_PATH)/libdivsufsort$(HOST_SHLIB_SUFFIX) \
   $(HOST_LIBRARY_PATH)/libdivsufsort64$(HOST_SHLIB_SUFFIX) \
-  $(HOST_LIBRARY_PATH)/libext2fs_host$(HOST_SHLIB_SUFFIX) \
-  $(HOST_LIBRARY_PATH)/libext2_blkid_host$(HOST_SHLIB_SUFFIX) \
-  $(HOST_LIBRARY_PATH)/libext2_com_err_host$(HOST_SHLIB_SUFFIX) \
-  $(HOST_LIBRARY_PATH)/libext2_e2p_host$(HOST_SHLIB_SUFFIX) \
-  $(HOST_LIBRARY_PATH)/libext2_profile_host$(HOST_SHLIB_SUFFIX) \
-  $(HOST_LIBRARY_PATH)/libext2_quota_host$(HOST_SHLIB_SUFFIX) \
-  $(HOST_LIBRARY_PATH)/libext2_uuid_host$(HOST_SHLIB_SUFFIX)
+  $(HOST_LIBRARY_PATH)/libext2fs-host$(HOST_SHLIB_SUFFIX) \
+  $(HOST_LIBRARY_PATH)/libext2_blkid-host$(HOST_SHLIB_SUFFIX) \
+  $(HOST_LIBRARY_PATH)/libext2_com_err-host$(HOST_SHLIB_SUFFIX) \
+  $(HOST_LIBRARY_PATH)/libext2_e2p-host$(HOST_SHLIB_SUFFIX) \
+  $(HOST_LIBRARY_PATH)/libext2_profile-host$(HOST_SHLIB_SUFFIX) \
+  $(HOST_LIBRARY_PATH)/libext2_quota-host$(HOST_SHLIB_SUFFIX) \
+  $(HOST_LIBRARY_PATH)/libext2_uuid-host$(HOST_SHLIB_SUFFIX) \
+  $(HOST_LIBRARY_PATH)/libconscrypt_openjdk_jni$(HOST_SHLIB_SUFFIX)
 
 .PHONY: otatools
 otatools: $(OTATOOLS)
@@ -1402,9 +1417,9 @@
 	$(hide) $(ACP) -p system/extras/verity/build_verity_metadata.py $(zip_root)/system/extras/verity/
 	$(hide) $(ACP) -r -d -p build/tools/releasetools/* $(zip_root)/releasetools
 	$(hide) rm -rf $@ $(zip_root)/releasetools/*.pyc
-	$(hide) (cd $(zip_root) && zip -qry $(abspath $@) *)
-	$(hide) zip -qry $(abspath $@) build/target/product/security/
-	$(hide) find device vendor -name \*.pk8 -o -name \*.x509.pem -o -name oem.prop | xargs zip -qry $(abspath $@)>/dev/null || true
+	$(hide) (cd $(zip_root) && zip -qryX $(abspath $@) *)
+	$(hide) zip -qryX $(abspath $@) build/target/product/security/
+	$(hide) find device vendor -name \*.pk8 -o -name \*.x509.pem -o -name oem.prop | xargs zip -qryX $(abspath $@)>/dev/null || true
 
 .PHONY: otatools-package
 otatools-package: $(BUILT_OTATOOLS_PACKAGE)
@@ -1438,15 +1453,11 @@
   fi
 endef
 
-built_ota_tools := \
-    $(call intermediates-dir-for,EXECUTABLES,applypatch,,,$(TARGET_PREFER_32_BIT))/applypatch \
-    $(call intermediates-dir-for,EXECUTABLES,sqlite3,,,$(TARGET_PREFER_32_BIT))/sqlite3
+built_ota_tools :=
 
 # We can't build static executables when SANITIZE_TARGET=address
 ifeq ($(strip $(SANITIZE_TARGET)),)
 built_ota_tools += \
-    $(call intermediates-dir-for,EXECUTABLES,check_prereq,,,$(TARGET_PREFER_32_BIT))/check_prereq \
-    $(call intermediates-dir-for,EXECUTABLES,applypatch_static,,,$(TARGET_PREFER_32_BIT))/applypatch_static \
     $(call intermediates-dir-for,EXECUTABLES,updater,,,$(TARGET_PREFER_32_BIT))/updater
 endif
 
@@ -1551,9 +1562,11 @@
 	$(hide) mkdir -p $(zip_root)/OTA
 	$(hide) $(ACP) $(INSTALLED_ANDROID_INFO_TXT_TARGET) $(zip_root)/OTA/
 ifneq ($(AB_OTA_UPDATER),true)
+ifneq ($(built_ota_tools),)
 	$(hide) mkdir -p $(zip_root)/OTA/bin
 	$(hide) $(ACP) $(PRIVATE_OTA_TOOLS) $(zip_root)/OTA/bin/
 endif
+endif
 	@# Files that do not end up in any images, but are necessary to
 	@# build them.
 	$(hide) mkdir -p $(zip_root)/META
@@ -1616,6 +1629,11 @@
 	$(hide) for part in $(AB_OTA_PARTITIONS); do \
 	  echo "$${part}" >> $(zip_root)/META/ab_partitions.txt; \
 	done
+	$(hide) for conf in $(AB_OTA_POSTINSTALL_CONFIG); do \
+	  echo "$${conf}" >> $(zip_root)/META/postinstall_config.txt; \
+	done
+	@# Include the build type in META/misc_info.txt so the server can easily differentiate production builds.
+	$(hide) echo "build_type=$(TARGET_BUILD_VARIANT)" >> $(zip_root)/META/misc_info.txt
 ifdef OSRELEASED_DIRECTORY
 	$(hide) $(ACP) $(TARGET_OUT_ETC)/$(OSRELEASED_DIRECTORY)/product_id $(zip_root)/META/product_id.txt
 	$(hide) $(ACP) $(TARGET_OUT_ETC)/$(OSRELEASED_DIRECTORY)/product_version $(zip_root)/META/product_version.txt
@@ -1626,7 +1644,7 @@
 	$(hide) $(ACP) -r $(TARGET_OUT_BREAKPAD) $(zip_root)/BREAKPAD
 endif
 	@# Zip everything up, preserving symlinks
-	$(hide) (cd $(zip_root) && zip -qry ../$(notdir $@) .)
+	$(hide) (cd $(zip_root) && zip -qryX ../$(notdir $@) .)
 	@# Run fs_config on all the system, vendor, boot ramdisk,
 	@# and recovery ramdisk files in the zip, and save the output
 	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="SYSTEM/" } /^SYSTEM\// {print "system/" $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config -C -D $(TARGET_OUT) -S $(SELINUX_FC) > $(zip_root)/META/filesystem_config.txt
@@ -1639,7 +1657,7 @@
 ifneq ($(INSTALLED_RECOVERYIMAGE_TARGET),)
 	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="RECOVERY/RAMDISK/" } /^RECOVERY\/RAMDISK\// {print $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config -C -D $(TARGET_OUT) -S $(SELINUX_FC) > $(zip_root)/META/recovery_filesystem_config.txt
 endif
-	$(hide) (cd $(zip_root) && zip -q ../$(notdir $@) META/*filesystem_config.txt)
+	$(hide) (cd $(zip_root) && zip -qX ../$(notdir $@) META/*filesystem_config.txt)
 	$(hide) PATH=$(foreach p,$(INTERNAL_USERIMAGES_BINARY_PATHS),$(p):)$$PATH MKBOOTIMG=$(MKBOOTIMG) \
 	    ./build/tools/releasetools/add_img_to_target_files -v -p $(HOST_OUT) $@
 
@@ -1719,7 +1737,7 @@
 	@echo "Package symbols: $@"
 	$(hide) rm -rf $@
 	$(hide) mkdir -p $(dir $@) $(TARGET_OUT_UNSTRIPPED)
-	$(hide) zip -qr $@ $(TARGET_OUT_UNSTRIPPED)
+	$(hide) zip -qrX $@ $(TARGET_OUT_UNSTRIPPED)
 
 # -----------------------------------------------------------------
 # A zip of the Android Apps. Not keeping full path so that we don't
@@ -1736,8 +1754,13 @@
 	@echo "Package apps: $@"
 	$(hide) rm -rf $@
 	$(hide) mkdir -p $(dir $@)
-	$(hide) zip -qj $@ $(TARGET_OUT_APPS)/*/*.apk $(TARGET_OUT_APPS_PRIVILEGED)/*/*.apk
-
+	$(hide) apps_to_zip=`find $(TARGET_OUT_APPS) $(TARGET_OUT_APPS_PRIVILEGED) -mindepth 2 -maxdepth 3 -name "*.apk"`; \
+	if [ -z "$$apps_to_zip" ]; then \
+		echo "No apps to zip up. Generating empty apps archive." ; \
+		a=$$(mktemp /tmp/XXXXXXX) && touch $$a && zip $@ $$a && zip -d $@ $$a; \
+	else \
+		zip -qjX $@ $$apps_to_zip; \
+	fi
 
 #------------------------------------------------------------------
 # A zip of emma code coverage meta files. Generated for fully emma
@@ -1749,7 +1772,7 @@
 $(EMMA_META_ZIP) :
 	@echo "Collecting Emma coverage meta files."
 	$(hide) find $(TARGET_COMMON_OUT_ROOT) $(HOST_COMMON_OUT_ROOT) -name "coverage.em" | \
-		zip -@ -q $@
+		zip -@ -qX $@
 
 endif # EMMA_INSTRUMENT=true
 
@@ -1765,7 +1788,7 @@
 	$(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 -q $@ $$dict_files $$unobfuscated_jars; \
+		  zip -qX $@ $$dict_files $$unobfuscated_jars; \
 		else \
 		  touch $(dir $@)/zipdummy; \
 		  (cd $(dir $@) && zip -q $(notdir $@) zipdummy); \
@@ -1796,7 +1819,7 @@
 
 $(INTERNAL_EMULATOR_PACKAGE_TARGET): $(INTERNAL_EMULATOR_PACKAGE_FILES)
 	@echo "Package: $@"
-	$(hide) zip -qj $@ $(INTERNAL_EMULATOR_PACKAGE_FILES)
+	$(hide) zip -qjX $@ $(INTERNAL_EMULATOR_PACKAGE_FILES)
 
 endif
 # -----------------------------------------------------------------
@@ -1931,7 +1954,7 @@
 		HOST_OUT_EXECUTABLES=$(HOST_OUT_EXECUTABLES) HOST_OS=$(HOST_OS) \
 			development/build/tools/sdk_clean.sh $(PRIVATE_DIR) && \
 		chmod -R ug+rwX $(PRIVATE_DIR) && \
-		cd $(dir $@) && zip -rq $(notdir $@) $(PRIVATE_NAME) \
+		cd $(dir $@) && zip -rqX $(notdir $@) $(PRIVATE_NAME) \
 	) || ( rm -rf $(PRIVATE_DIR) $@ && exit 44 )
 
 
@@ -1966,11 +1989,15 @@
 include $(sort $(wildcard $(BUILD_SYSTEM)/tasks/*.mk))
 -include $(sort $(wildcard vendor/*/build/tasks/*.mk))
 -include $(sort $(wildcard device/*/build/tasks/*.mk))
+-include $(sort $(wildcard product/*/build/tasks/*.mk))
 # Also the project-specific tasks
 -include $(sort $(wildcard vendor/*/*/build/tasks/*.mk))
 -include $(sort $(wildcard device/*/*/build/tasks/*.mk))
+-include $(sort $(wildcard product/*/*/build/tasks/*.mk))
 endif
 
+include $(BUILD_SYSTEM)/product-graph.mk
+
 # -----------------------------------------------------------------
 # Create SDK repository packages. Must be done after tasks/* since
 # we need the addon rules defined.
diff --git a/core/base_rules.mk b/core/base_rules.mk
index ac6cde8..5648f64 100644
--- a/core/base_rules.mk
+++ b/core/base_rules.mk
@@ -221,6 +221,23 @@
 LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_BUILT_MODULE)
 
 ###########################################################
+## Create .toc files from shared objects to reduce unnecessary rebuild
+# .toc files have the list of external dynamic symbols without their addresses.
+# As .KATI_RESTAT is specified to .toc files and commit-change-for-toc is used,
+# dependent binaries of a .toc file will be rebuilt only when the content of
+# the .toc file is changed.
+###########################################################
+ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
+LOCAL_INTERMEDIATE_TARGETS += $(LOCAL_BUILT_MODULE).toc
+$(LOCAL_BUILT_MODULE).toc: $(LOCAL_BUILT_MODULE)
+	$(call $(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)transform-shared-lib-to-toc,$<,$@.tmp)
+	$(call commit-change-for-toc,$@)
+
+# Kati adds restat=1 to ninja. GNU make does nothing for this.
+.KATI_RESTAT: $(LOCAL_BUILT_MODULE).toc
+endif
+
+###########################################################
 ## logtags: Add .logtags files to global list
 ###########################################################
 
@@ -364,7 +381,7 @@
 my_compat_dist += $(foreach f, $(LOCAL_COMPATIBILITY_SUPPORT_FILES),\
   $(eval p := $(subst :,$(space),$(f)))\
   $(eval s := $(LOCAL_PATH)/$(word 1,$(p)))\
-  $(eval d := $(COMPATIBILITY_TESTCASES_OUT_$(LOCAL_COMPATIBILITY_SUITE))/$(or $(word 2,$(p)),(word 1,$(p))))\
+  $(eval d := $(COMPATIBILITY_TESTCASES_OUT_$(LOCAL_COMPATIBILITY_SUITE))/$(or $(word 2,$(p)),$(word 1,$(p))))\
   $(s):$(d))
 
 ifneq (,$(wildcard $(LOCAL_PATH)/AndroidTest.xml))
diff --git a/core/binary.mk b/core/binary.mk
index e9b2265..36443fe 100644
--- a/core/binary.mk
+++ b/core/binary.mk
@@ -85,11 +85,16 @@
 
   # The bionic linker now has support for packed relocations and gnu style
   # hashes (which are much faster!), but shipping to older devices requires
-  # the old style hash and disabling packed relocations.
-  #ifeq ($(shell expr $(LOCAL_SDK_VERSION) >= FIRST_SUPPORTED_VERSION),0)
-    my_ldflags += -Wl,--hash-style=sysv
-    LOCAL_PACK_MODULE_RELOCATIONS := false
-  #endif
+  # the old style hash. Fortunately, we can build with both and it'll work
+  # anywhere.
+  #
+  # This is not currently supported on MIPS architectures.
+  ifeq (,$(filter mips mips64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)))
+    my_ldflags += -Wl,--hash-style=both
+  endif
+
+  # We don't want to expose the relocation packer to the NDK just yet.
+  LOCAL_PACK_MODULE_RELOCATIONS := false
 
   # Set up the NDK stl variant. Starting from NDK-r5 the c++ stl resides in a separate location.
   # See ndk/docs/CPLUSPLUS-SUPPORT.html
@@ -106,15 +111,16 @@
   ifeq (,$(LOCAL_NDK_STL_VARIANT))
     LOCAL_NDK_STL_VARIANT := system
   endif
-  ifneq (1,$(words $(filter system stlport_static stlport_shared c++_static c++_shared gnustl_static, $(LOCAL_NDK_STL_VARIANT))))
+  ifneq (1,$(words $(filter none system stlport_static stlport_shared c++_static c++_shared gnustl_static, $(LOCAL_NDK_STL_VARIANT))))
     $(error $(LOCAL_PATH): Unknown LOCAL_NDK_STL_VARIANT $(LOCAL_NDK_STL_VARIANT))
   endif
   ifeq (system,$(LOCAL_NDK_STL_VARIANT))
     my_ndk_stl_include_path := $(my_ndk_source_root)/cxx-stl/system/include
-    # for "system" variant, the shared library exists in the system library and -lstdc++ is added by default.
+    my_system_shared_libraries += libstdc++
   else # LOCAL_NDK_STL_VARIANT is not system
   ifneq (,$(filter stlport_%, $(LOCAL_NDK_STL_VARIANT)))
     my_ndk_stl_include_path := $(my_ndk_source_root)/cxx-stl/stlport/stlport
+    my_system_shared_libraries += libstdc++
     ifeq (stlport_static,$(LOCAL_NDK_STL_VARIANT))
       my_ndk_stl_static_lib := $(my_ndk_source_root)/cxx-stl/stlport/libs/$(my_cpu_variant)/libstlport_static.a
     else
@@ -133,11 +139,14 @@
       my_ndk_stl_shared_lib := -lc++_shared
     endif
     my_ndk_stl_cppflags := -std=c++11
-  else
-    # LOCAL_NDK_STL_VARIANT is gnustl_static
+  else # LOCAL_NDK_STL_VARIANT is not c++_* either
+  ifneq (,$(filter gnustl_%, $(LOCAL_NDK_STL_VARIANT)))
     my_ndk_stl_include_path := $(my_ndk_source_root)/cxx-stl/gnu-libstdc++/$($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET_NDK_GCC_VERSION)/libs/$(my_cpu_variant)/include \
                                $(my_ndk_source_root)/cxx-stl/gnu-libstdc++/$($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET_NDK_GCC_VERSION)/include
     my_ndk_stl_static_lib := $(my_ndk_source_root)/cxx-stl/gnu-libstdc++/$($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET_NDK_GCC_VERSION)/libs/$(my_cpu_variant)/libgnustl_static.a
+  else # LOCAL_NDK_STL_VARIANT must be none
+    # Do nothing.
+  endif
   endif
   endif
   endif
@@ -177,6 +186,11 @@
 my_c_includes += $(LOCAL_C_INCLUDES_$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_C_INCLUDES_$(my_32_64_bit_suffix))
 my_generated_sources += $(LOCAL_GENERATED_SOURCES_$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) $(LOCAL_GENERATED_SOURCES_$(my_32_64_bit_suffix))
 
+my_missing_exclude_files := $(filter-out $(my_src_files),$(my_src_files_exclude))
+ifneq ($(my_missing_exclude_files),)
+$(warning Files are listed in LOCAL_SRC_FILES_EXCLUDE but not LOCAL_SRC_FILES)
+$(error $(my_missing_exclude_files))
+endif
 my_src_files := $(filter-out $(my_src_files_exclude),$(my_src_files))
 
 my_clang := $(strip $(LOCAL_CLANG))
@@ -195,11 +209,13 @@
         my_clang := true
     endif
     endif
-# Add option to make clang the default for device build
-else ifeq ($(USE_CLANG_PLATFORM_BUILD),true)
+# Add option to make gcc the default for device build
+else ifeq ($(USE_CLANG_PLATFORM_BUILD),false)
     ifeq ($(my_clang),)
-        my_clang := true
+        my_clang := false
     endif
+else ifeq ($(my_clang),)
+    my_clang := true
 endif
 
 my_cpp_std_version := -std=gnu++14
@@ -500,7 +516,7 @@
 else
 ifneq (,$(LOCAL_SDK_VERSION))
 # Set target-api for LOCAL_SDK_VERSIONs other than current.
-ifneq (,$(filter-out current system_current, $(LOCAL_SDK_VERSION)))
+ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
 renderscript_target_api := $(LOCAL_SDK_VERSION)
 endif
 endif  # LOCAL_SDK_VERSION is set
@@ -730,8 +746,9 @@
 	$(transform-aidl-to-cpp)
 -include $(addsuffix .P,$(basename $(aidl_gen_cpp)))
 
-# Add generated headers to include path.
+# Add generated headers to include paths.
 my_c_includes += $(aidl_gen_include_root)
+my_export_c_include_dirs += $(aidl_gen_include_root)
 # Pick up the generated C++ files later for transformation to .o files.
 my_generated_sources += $(aidl_gen_cpp)
 
@@ -1133,7 +1150,11 @@
 # that custom build rules which generate .o files don't consume other generated
 # sources as input (or if they do they take care of that dependency themselves).
 $(normal_objects) : | $(my_generated_sources)
+ifeq ($(BUILDING_WITH_NINJA),true)
+$(all_objects) : $(import_includes)
+else
 $(all_objects) : | $(import_includes)
+endif
 ALL_C_CPP_ETC_OBJECTS += $(all_objects)
 
 
@@ -1175,6 +1196,7 @@
     $(addprefix $($(LOCAL_2ND_ARCH_VAR_PREFIX)$(my_prefix)OUT_INTERMEDIATE_LIBRARIES)/, \
       $(addsuffix $(so_suffix), \
         $(my_shared_libraries)))
+built_shared_library_tocs := $(addsuffix .toc, $(built_shared_libraries))
 
 # Add the NDK libraries to the built module dependency
 my_system_shared_libraries_fullpath := \
@@ -1188,6 +1210,8 @@
     $(addprefix $($(LOCAL_2ND_ARCH_VAR_PREFIX)$(my_prefix)OUT_INTERMEDIATE_LIBRARIES)/, \
       $(addsuffix $(so_suffix), \
         $(installed_shared_library_module_names)))
+built_shared_library_tocs := $(addsuffix .toc, $(built_shared_libraries))
+my_system_shared_libraries_fullpath :=
 endif
 
 built_static_libraries := \
@@ -1282,7 +1306,8 @@
 ###########################################################
 # all_libraries is used for the dependencies on LOCAL_BUILT_MODULE.
 all_libraries := \
-    $(built_shared_libraries) \
+    $(built_shared_library_tocs) \
+    $(my_system_shared_libraries_fullpath) \
     $(built_static_libraries) \
     $(built_whole_libraries)
 
@@ -1297,16 +1322,31 @@
 export_includes := $(intermediates)/export_includes
 $(export_includes): PRIVATE_EXPORT_C_INCLUDE_DIRS := $(my_export_c_include_dirs)
 # Make sure .pb.h are already generated before any dependent source files get compiled.
-$(export_includes) : $(LOCAL_MODULE_MAKEFILE_DEP) $(proto_generated_headers) $(dbus_generated_headers)
+# Similarly, the generated DBus headers need to exist before we export their location.
+# People are not going to consume the aidl generated cpp file, but the cpp file is
+# generated after the headers, so this is a convenient way to ensure the headers exist.
+$(export_includes) : $(LOCAL_MODULE_MAKEFILE_DEP) $(proto_generated_headers) $(dbus_generated_headers) $(aidl_gen_cpp)
 	@echo Export includes file: $< -- $@
-	$(hide) mkdir -p $(dir $@) && rm -f $@
+	$(hide) mkdir -p $(dir $@) && rm -f $@.tmp
 ifdef my_export_c_include_dirs
 	$(hide) for d in $(PRIVATE_EXPORT_C_INCLUDE_DIRS); do \
-	        echo "-I $$d" >> $@; \
+	        echo "-I $$d" >> $@.tmp; \
 	        done
 else
-	$(hide) touch $@
+	$(hide) touch $@.tmp
 endif
+ifeq ($(BUILDING_WITH_NINJA),true)
+	$(hide) if cmp -s $@.tmp $@ ; then \
+	  rm $@.tmp ; \
+	else \
+	  mv $@.tmp $@ ; \
+	fi
+else
+	mv $@.tmp $@ ;
+endif
+
+# Kati adds restat=1 to ninja. GNU make does nothing for this.
+.KATI_RESTAT: $(export_includes)
 
 # Make sure export_includes gets generated when you are running mm/mmm
 $(LOCAL_BUILT_MODULE) : | $(export_includes)
diff --git a/core/clang/TARGET_arm64.mk b/core/clang/TARGET_arm64.mk
index 5120a6d..532ca77 100644
--- a/core/clang/TARGET_arm64.mk
+++ b/core/clang/TARGET_arm64.mk
@@ -66,6 +66,6 @@
 TARGET_LIBPROFILE_RT := $(LLVM_RTLIB_PATH)/libclang_rt.profile-aarch64-android.a
 
 # Address sanitizer clang config
-ADDRESS_SANITIZER_RUNTIME_LIBRARY := libclang_rt.asan-arm64-android
+ADDRESS_SANITIZER_RUNTIME_LIBRARY := libclang_rt.asan-aarch64-android
 ADDRESS_SANITIZER_RPATH := /data/vendor/lib64:/$(TARGET_COPY_OUT_VENDOR)/lib64:/data/lib64
 ADDRESS_SANITIZER_LINKER := /system/bin/linker_asan64
diff --git a/core/clang/config.mk b/core/clang/config.mk
index 0c5e963..756bf49 100644
--- a/core/clang/config.mk
+++ b/core/clang/config.mk
@@ -1,8 +1,8 @@
 ## Clang configurations.
 
-LLVM_PREBUILTS_VERSION := 3.6
-FORCE_BUILD_SANITIZER_SHARED_OBJECTS := true
-LLVM_PREBUILTS_PATH := prebuilts/clang/host/$(BUILD_OS)-x86/$(LLVM_PREBUILTS_VERSION)/bin
+LLVM_PREBUILTS_VERSION ?= 3.8
+LLVM_PREBUILTS_BASE ?= prebuilts/clang/host
+LLVM_PREBUILTS_PATH := $(LLVM_PREBUILTS_BASE)/$(BUILD_OS)-x86/$(LLVM_PREBUILTS_VERSION)/bin
 LLVM_RTLIB_PATH := $(LLVM_PREBUILTS_PATH)/../lib/clang/$(LLVM_PREBUILTS_VERSION)/lib/linux/
 
 CLANG := $(LLVM_PREBUILTS_PATH)/clang$(BUILD_EXECUTABLE_SUFFIX)
diff --git a/core/combo/HOST_CROSS_windows-x86.mk b/core/combo/HOST_CROSS_windows-x86.mk
index c5a713c..1ac1059 100644
--- a/core/combo/HOST_CROSS_windows-x86.mk
+++ b/core/combo/HOST_CROSS_windows-x86.mk
@@ -32,14 +32,21 @@
 $(combo_var_prefix)GLOBAL_CFLAGS += -D__STDC_FORMAT_MACROS -D__STDC_CONSTANT_MACROS
 # Use C99-compliant printf functions (%zd).
 $(combo_var_prefix)GLOBAL_CFLAGS += -D__USE_MINGW_ANSI_STDIO=1
-# Admit to using >= Win2K.
-$(combo_var_prefix)GLOBAL_CFLAGS += -D_WIN32_WINNT=0x0500
+# Admit to using >= Win2K. Both are needed because of <_mingw.h>.
+$(combo_var_prefix)GLOBAL_CFLAGS += -D_WIN32_WINNT=0x0500 -DWINVER=0x0500
 # Get 64-bit off_t and related functions.
 $(combo_var_prefix)GLOBAL_CFLAGS += -D_FILE_OFFSET_BITS=64
 
 $(combo_var_prefix)CC := $(TOOLS_PREFIX)gcc
 $(combo_var_prefix)CXX := $(TOOLS_PREFIX)g++
 $(combo_var_prefix)AR := $(TOOLS_PREFIX)ar
+$(combo_var_prefix)NM := $(TOOLS_PREFIX)nm
+$(combo_var_prefix)OBJDUMP := $(TOOLS_PREFIX)objdump
+
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(hide) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)OBJDUMP) -x $(1) | grep "^Name" | cut -f3 -d" " > $(2)
+$(hide) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)NM) -g -f p $(1) | cut -f1-2 -d" " >> $(2)
+endef
 
 $(combo_var_prefix)GLOBAL_LDFLAGS += \
     --enable-stdcall-fixup
diff --git a/core/combo/HOST_darwin-x86.mk b/core/combo/HOST_darwin-x86.mk
index 85c883c..fc56e52 100644
--- a/core/combo/HOST_darwin-x86.mk
+++ b/core/combo/HOST_darwin-x86.mk
@@ -36,6 +36,10 @@
 $(combo_2nd_arch_prefix)HOST_CC  := $($(combo_2nd_arch_prefix)HOST_TOOLCHAIN_PREFIX)-gcc
 $(combo_2nd_arch_prefix)HOST_CXX := $($(combo_2nd_arch_prefix)HOST_TOOLCHAIN_PREFIX)-g++
 
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_macho,$(1),$(2))
+endef
+
 # gcc location for clang; to be updated when clang is updated
 # HOST_TOOLCHAIN_ROOT is a Darwin-specific define
 $(combo_2nd_arch_prefix)HOST_TOOLCHAIN_FOR_CLANG := $($(combo_2nd_arch_prefix)HOST_TOOLCHAIN_ROOT)
diff --git a/core/combo/HOST_darwin-x86_64.mk b/core/combo/HOST_darwin-x86_64.mk
index 93a1b3e..251455f 100644
--- a/core/combo/HOST_darwin-x86_64.mk
+++ b/core/combo/HOST_darwin-x86_64.mk
@@ -36,6 +36,10 @@
 HOST_CC  := $(HOST_TOOLCHAIN_PREFIX)-gcc
 HOST_CXX := $(HOST_TOOLCHAIN_PREFIX)-g++
 
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_macho,$(1),$(2))
+endef
+
 # gcc location for clang; to be updated when clang is updated
 # HOST_TOOLCHAIN_ROOT is a Darwin-specific define
 HOST_TOOLCHAIN_FOR_CLANG := $(HOST_TOOLCHAIN_ROOT)
diff --git a/core/combo/HOST_linux-x86.mk b/core/combo/HOST_linux-x86.mk
index 4caf607..b5efc71 100644
--- a/core/combo/HOST_linux-x86.mk
+++ b/core/combo/HOST_linux-x86.mk
@@ -23,6 +23,12 @@
 $(combo_2nd_arch_prefix)HOST_CC  := $($(combo_2nd_arch_prefix)HOST_TOOLCHAIN_PREFIX)gcc
 $(combo_2nd_arch_prefix)HOST_CXX := $($(combo_2nd_arch_prefix)HOST_TOOLCHAIN_PREFIX)g++
 $(combo_2nd_arch_prefix)HOST_AR  := $($(combo_2nd_arch_prefix)HOST_TOOLCHAIN_PREFIX)ar
+$(combo_2nd_arch_prefix)HOST_READELF  := $($(combo_2nd_arch_prefix)HOST_TOOLCHAIN_PREFIX)readelf
+$(combo_2nd_arch_prefix)HOST_NM  := $($(combo_2nd_arch_prefix)HOST_TOOLCHAIN_PREFIX)nm
+
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_elf,$(1),$(2))
+endef
 
 # gcc location for clang; to be updated when clang is updated
 $(combo_2nd_arch_prefix)HOST_TOOLCHAIN_FOR_CLANG := prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8
diff --git a/core/combo/HOST_linux-x86_64.mk b/core/combo/HOST_linux-x86_64.mk
index 3708137..9a77324 100644
--- a/core/combo/HOST_linux-x86_64.mk
+++ b/core/combo/HOST_linux-x86_64.mk
@@ -23,6 +23,12 @@
 HOST_CC  := $(HOST_TOOLCHAIN_PREFIX)gcc
 HOST_CXX := $(HOST_TOOLCHAIN_PREFIX)g++
 HOST_AR  := $(HOST_TOOLCHAIN_PREFIX)ar
+HOST_READELF  := $(HOST_TOOLCHAIN_PREFIX)readelf
+HOST_NM  := $(HOST_TOOLCHAIN_PREFIX)nm
+
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_elf,$(1),$(2))
+endef
 
 # gcc location for clang; to be updated when clang is updated
 HOST_TOOLCHAIN_FOR_CLANG := prebuilts/gcc/linux-x86/host/x86_64-linux-glibc2.15-4.8
diff --git a/core/combo/TARGET_linux-arm.mk b/core/combo/TARGET_linux-arm.mk
index 5f0f1d2..485e48c 100644
--- a/core/combo/TARGET_linux-arm.mk
+++ b/core/combo/TARGET_linux-arm.mk
@@ -63,6 +63,11 @@
 $(combo_2nd_arch_prefix)TARGET_LD := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)ld
 $(combo_2nd_arch_prefix)TARGET_READELF := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)readelf
 $(combo_2nd_arch_prefix)TARGET_STRIP := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)strip
+$(combo_2nd_arch_prefix)TARGET_NM := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)nm
+
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_elf,$(1),$(2))
+endef
 
 $(combo_2nd_arch_prefix)TARGET_NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined
 
diff --git a/core/combo/TARGET_linux-arm64.mk b/core/combo/TARGET_linux-arm64.mk
index b213ea7..1eb3240 100644
--- a/core/combo/TARGET_linux-arm64.mk
+++ b/core/combo/TARGET_linux-arm64.mk
@@ -63,6 +63,11 @@
 TARGET_LD := $(TARGET_TOOLS_PREFIX)ld
 TARGET_READELF := $(TARGET_TOOLS_PREFIX)readelf
 TARGET_STRIP := $(TARGET_TOOLS_PREFIX)strip
+TARGET_NM := $(TARGET_TOOLS_PREFIX)nm
+
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_elf,$(1),$(2))
+endef
 
 TARGET_NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined
 
diff --git a/core/combo/TARGET_linux-mips.mk b/core/combo/TARGET_linux-mips.mk
index 8e117eb..be05302 100644
--- a/core/combo/TARGET_linux-mips.mk
+++ b/core/combo/TARGET_linux-mips.mk
@@ -63,6 +63,11 @@
 $(combo_2nd_arch_prefix)TARGET_LD := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)ld
 $(combo_2nd_arch_prefix)TARGET_READELF := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)readelf
 $(combo_2nd_arch_prefix)TARGET_STRIP := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)strip
+$(combo_2nd_arch_prefix)TARGET_NM := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)nm
+
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_elf,$(1),$(2))
+endef
 
 $(combo_2nd_arch_prefix)TARGET_NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined
 
diff --git a/core/combo/TARGET_linux-mips64.mk b/core/combo/TARGET_linux-mips64.mk
index 565083a..9704b6b 100644
--- a/core/combo/TARGET_linux-mips64.mk
+++ b/core/combo/TARGET_linux-mips64.mk
@@ -63,6 +63,11 @@
 TARGET_LD := $(TARGET_TOOLS_PREFIX)ld
 TARGET_READELF := $(TARGET_TOOLS_PREFIX)readelf
 TARGET_STRIP := $(TARGET_TOOLS_PREFIX)strip
+TARGET_NM := $(TARGET_TOOLS_PREFIX)nm
+
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_elf,$(1),$(2))
+endef
 
 TARGET_NO_UNDEFINED_LDFLAGS := -Wl,--no-undefined
 
diff --git a/core/combo/TARGET_linux-x86.mk b/core/combo/TARGET_linux-x86.mk
index 5fff641..c4cb0f8 100644
--- a/core/combo/TARGET_linux-x86.mk
+++ b/core/combo/TARGET_linux-x86.mk
@@ -56,6 +56,11 @@
 $(combo_2nd_arch_prefix)TARGET_LD := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)ld
 $(combo_2nd_arch_prefix)TARGET_READELF := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)readelf
 $(combo_2nd_arch_prefix)TARGET_STRIP := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)strip
+$(combo_2nd_arch_prefix)TARGET_NM := $($(combo_2nd_arch_prefix)TARGET_TOOLS_PREFIX)nm
+
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_elf,$(1),$(2))
+endef
 
 ifneq ($(wildcard $($(combo_2nd_arch_prefix)TARGET_CC)),)
 $(combo_2nd_arch_prefix)TARGET_LIBGCC := \
diff --git a/core/combo/TARGET_linux-x86_64.mk b/core/combo/TARGET_linux-x86_64.mk
index cacfae1..b7ec50b 100644
--- a/core/combo/TARGET_linux-x86_64.mk
+++ b/core/combo/TARGET_linux-x86_64.mk
@@ -56,6 +56,11 @@
 TARGET_LD := $(TARGET_TOOLS_PREFIX)ld
 TARGET_READELF := $(TARGET_TOOLS_PREFIX)readelf
 TARGET_STRIP := $(TARGET_TOOLS_PREFIX)strip
+TARGET_NM := $(TARGET_TOOLS_PREFIX)nm
+
+define $(combo_var_prefix)transform-shared-lib-to-toc
+$(call _gen_toc_command_for_elf,$(1),$(2))
+endef
 
 ifneq ($(wildcard $(TARGET_CC)),)
 TARGET_LIBGCC := \
diff --git a/core/config.mk b/core/config.mk
index e3832a0..017d8b9 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -62,6 +62,7 @@
 SRC_TARGET_DIR := $(TOPDIR)build/target
 SRC_API_DIR := $(TOPDIR)prebuilts/sdk/api
 SRC_SYSTEM_API_DIR := $(TOPDIR)prebuilts/sdk/system-api
+SRC_TEST_API_DIR := $(TOPDIR)prebuilts/sdk/test-api
 
 # Some specific paths to tools
 SRC_DROIDDOC_DIR := $(TOPDIR)build/tools/droiddoc
@@ -286,6 +287,18 @@
 endif
 TARGET_CPU_ABI2 := $(strip $(TARGET_CPU_ABI2))
 
+# Commands to generate .toc file common to ELF .so files.
+define _gen_toc_command_for_elf
+$(hide) ($($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)READELF) -d $(1) | grep SONAME || echo "No SONAME for $1") > $(2)
+$(hide) $($(PRIVATE_2ND_ARCH_VAR_PREFIX)$(PRIVATE_PREFIX)NM) -gD -f p $(1) | cut -f1-2 -d" " >> $(2)
+endef
+
+# Commands to generate .toc file from Darwin dynamic library.
+define _gen_toc_command_for_macho
+$(hide) otool -l $(1) | grep LC_ID_DYLIB -A 5 > $(2)
+$(hide) nm -gP $(1) | cut -f1-2 -d" " | grep -v U$$ >> $(2)
+endef
+
 combo_target := HOST_
 combo_2nd_arch_prefix :=
 include $(BUILD_SYSTEM)/combo/select.mk
@@ -437,6 +450,45 @@
 endif
 endif
 
+#
+# Tools that are prebuilts for TARGET_BUILD_APPS
+#
+
+ACP := $(HOST_OUT_EXECUTABLES)/acp
+AIDL := $(HOST_OUT_EXECUTABLES)/aidl
+AAPT := $(HOST_OUT_EXECUTABLES)/aapt
+ZIPALIGN := $(HOST_OUT_EXECUTABLES)/zipalign
+SIGNAPK_JAR := $(HOST_OUT_JAVA_LIBRARIES)/signapk$(COMMON_JAVA_PACKAGE_SUFFIX)
+SIGNAPK_JNI_LIBRARY_PATH := $(HOST_OUT_SHARED_LIBRARIES)
+LLVM_RS_CC := $(HOST_OUT_EXECUTABLES)/llvm-rs-cc
+BCC_COMPAT := $(HOST_OUT_EXECUTABLES)/bcc_compat
+
+DX := $(HOST_OUT_EXECUTABLES)/dx
+MAINDEXCLASSES := $(HOST_OUT_EXECUTABLES)/mainDexClasses
+
+# Override the definitions above for unbundled and PDK builds
+ifneq (,$(TARGET_BUILD_APPS)$(filter true,$(TARGET_BUILD_PDK)))
+prebuilt_sdk_tools := prebuilts/sdk/tools
+prebuilt_sdk_tools_bin := $(prebuilt_sdk_tools)/$(HOST_OS)/bin
+
+ACP := $(prebuilt_sdk_tools_bin)/acp
+AIDL := $(prebuilt_sdk_tools_bin)/aidl
+AAPT := $(prebuilt_sdk_tools_bin)/aapt
+ZIPALIGN := $(prebuilt_sdk_tools_bin)/zipalign
+SIGNAPK_JAR := $(prebuilt_sdk_tools)/lib/signapk$(COMMON_JAVA_PACKAGE_SUFFIX)
+# Use 64-bit libraries unconditionally because 32-bit JVMs are no longer supported
+SIGNAPK_JNI_LIBRARY_PATH := $(prebuilt_sdk_tools)/$(HOST_OS)/lib64
+
+DX := $(prebuilt_sdk_tools)/dx
+MAINDEXCLASSES := $(prebuilt_sdk_tools)/mainDexClasses
+
+# Don't use prebuilts in PDK
+ifneq ($(TARGET_BUILD_PDK),true)
+LLVM_RS_CC := $(prebuilt_sdk_tools_bin)/llvm-rs-cc
+BCC_COMPAT := $(prebuilt_sdk_tools_bin)/bcc_compat
+endif # TARGET_BUILD_PDK
+endif # TARGET_BUILD_APPS || TARGET_BUILD_PDK
+
 
 # ---------------------------------------------------------------
 # Generic tools.
@@ -456,8 +508,6 @@
 YASM := prebuilts/misc/$(BUILD_OS)-$(HOST_PREBUILT_ARCH)/yasm/yasm
 
 DOXYGEN:= doxygen
-AAPT := $(HOST_OUT_EXECUTABLES)/aapt$(HOST_EXECUTABLE_SUFFIX)
-AIDL := $(HOST_OUT_EXECUTABLES)/aidl$(HOST_EXECUTABLE_SUFFIX)
 AIDL_CPP := $(HOST_OUT_EXECUTABLES)/aidl-cpp$(HOST_EXECUTABLE_SUFFIX)
 ifeq ($(HOST_OS),linux)
 BREAKPAD_DUMP_SYMS := $(HOST_OUT_EXECUTABLES)/dump_syms
@@ -467,7 +517,6 @@
 endif
 PROTOC := $(HOST_OUT_EXECUTABLES)/aprotoc$(HOST_EXECUTABLE_SUFFIX)
 DBUS_GENERATOR := $(HOST_OUT_EXECUTABLES)/dbus-binding-generator
-SIGNAPK_JAR := $(HOST_OUT_JAVA_LIBRARIES)/signapk$(COMMON_JAVA_PACKAGE_SUFFIX)
 MKBOOTFS := $(HOST_OUT_EXECUTABLES)/mkbootfs$(HOST_EXECUTABLE_SUFFIX)
 MINIGZIP := $(HOST_OUT_EXECUTABLES)/minigzip$(HOST_EXECUTABLE_SUFFIX)
 ifeq (,$(strip $(BOARD_CUSTOM_MKBOOTIMG)))
@@ -513,8 +562,6 @@
 JILL := java -jar $(JILL_JAR)
 PROGUARD := external/proguard/bin/proguard.sh
 JAVATAGS := build/tools/java-event-log-tags.py
-LLVM_RS_CC := $(HOST_OUT_EXECUTABLES)/llvm-rs-cc$(HOST_EXECUTABLE_SUFFIX)
-BCC_COMPAT := $(HOST_OUT_EXECUTABLES)/bcc_compat$(HOST_EXECUTABLE_SUFFIX)
 RMTYPEDEFS := $(HOST_OUT_EXECUTABLES)/rmtypedefs
 APPEND2SIMG := $(HOST_OUT_EXECUTABLES)/append2simg
 VERITY_SIGNER := $(HOST_OUT_EXECUTABLES)/verity_signer
@@ -524,12 +571,13 @@
 VBOOT_SIGNER := prebuilts/misc/scripts/vboot_signer/vboot_signer.sh
 FEC := $(HOST_OUT_EXECUTABLES)/fec
 
-# ACP is always for the build OS, not for the host OS
-ACP := $(BUILD_OUT_EXECUTABLES)/acp$(BUILD_EXECUTABLE_SUFFIX)
+ifndef TARGET_BUILD_APPS
+ZIPTIME := $(HOST_OUT_EXECUTABLES)/ziptime$(HOST_EXECUTABLE_SUFFIX)
+endif
 
-# dx is java behind a shell script; no .exe necessary.
-DX := $(HOST_OUT_EXECUTABLES)/dx
-ZIPALIGN := $(HOST_OUT_EXECUTABLES)/zipalign$(HOST_EXECUTABLE_SUFFIX)
+# ijar converts a .jar file to a smaller .jar file which only has its
+# interfaces.
+IJAR := $(HOST_OUT_EXECUTABLES)/ijar$(BUILD_EXECUTABLE_SUFFIX)
 
 # relocation packer
 RELOCATION_PACKER := prebuilts/misc/$(BUILD_OS)-$(HOST_PREBUILT_ARCH)/relocation_packer/relocation_packer
@@ -545,6 +593,8 @@
 
 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)
 HOST_JDK_TOOLS_JAR:= $(shell $(BUILD_SYSTEM)/find-jdk-tools-jar.sh)
 
 ifneq ($(HOST_JDK_TOOLS_JAR),)
@@ -558,6 +608,7 @@
 ifneq ($(filter 64-Bit, $(shell java -version 2>&1)),)
 HOST_JDK_IS_64BIT_VERSION := true
 endif
+endif  # CALLED_FROM_SETUP not true
 
 # It's called md5 on Mac OS and md5sum on Linux
 ifeq ($(HOST_OS),darwin)
@@ -649,7 +700,7 @@
 
 # allow overriding default Java libraries on a per-target basis
 ifeq ($(TARGET_DEFAULT_JAVA_LIBRARIES),)
-  TARGET_DEFAULT_JAVA_LIBRARIES := core-libart core-junit ext framework okhttp
+  TARGET_DEFAULT_JAVA_LIBRARIES := core-oj core-libart core-junit ext framework okhttp
 endif
 
 # Flags for DEX2OAT
@@ -702,13 +753,15 @@
     $(patsubst $(HISTORICAL_SDK_VERSIONS_ROOT)/%/android.jar,%, \
     $(wildcard $(HISTORICAL_SDK_VERSIONS_ROOT)/*/android.jar)))
 
-# We don't have prebuilt system_current SDK yet.
-TARGET_AVAILABLE_SDK_VERSIONS := $(TARGET_AVAILABLE_SDK_VERSIONS)
+# We don't have prebuilt test_current SDK yet.
+TARGET_AVAILABLE_SDK_VERSIONS := test_current $(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
 INTERNAL_PLATFORM_SYSTEM_REMOVED_API_FILE := $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/system-removed.txt
+INTERNAL_PLATFORM_TEST_API_FILE := $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/test-api.txt
+INTERNAL_PLATFORM_TEST_REMOVED_API_FILE := $(TARGET_OUT_COMMON_INTERMEDIATES)/PACKAGING/test-removed.txt
 
 # This is the standard way to name a directory containing prebuilt target
 # objects. E.g., prebuilt/$(TARGET_PREBUILT_TAG)/libc.so
diff --git a/core/config_sanitizers.mk b/core/config_sanitizers.mk
index 7189338..df52e72 100644
--- a/core/config_sanitizers.mk
+++ b/core/config_sanitizers.mk
@@ -84,7 +84,7 @@
 endif
 
 ifneq ($(my_sanitize),)
-  fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)),
+  fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
   my_cflags += -fsanitize=$(fsanitize_arg)
 
   ifdef LOCAL_IS_HOST_MODULE
@@ -92,8 +92,10 @@
     my_ldflags += -fsanitize=$(fsanitize_arg)
     my_ldlibs += -lrt -ldl
   else
-    my_cflags += -fsanitize-undefined-trap-on-error
-    my_cflags += -ftrap-function=abort
+    ifeq ($(filter address,$(my_sanitize)),)
+      my_cflags += -fsanitize-trap=all
+      my_cflags += -ftrap-function=abort
+    endif
     my_shared_libraries += libdl
   endif
 endif
diff --git a/core/cxx_stl_setup.mk b/core/cxx_stl_setup.mk
index be8a711..37be1f7 100644
--- a/core/cxx_stl_setup.mk
+++ b/core/cxx_stl_setup.mk
@@ -104,9 +104,6 @@
     endif
 else ifeq ($(my_cxx_stl),ndk)
     # Using an NDK STL. Handled in binary.mk.
-    ifndef LOCAL_IS_HOST_MODULE
-        my_system_shared_libraries += libstdc++
-    endif
 else ifeq ($(my_cxx_stl),libstdc++)
     # Using bionic's basic libstdc++. Not actually an STL. Only around until the
     # tree is in good enough shape to not need it.
diff --git a/core/definitions.mk b/core/definitions.mk
index ecc7253..336cae8 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -530,7 +530,7 @@
         $(error $(LOCAL_PATH): Name not defined in call to generated-sources-dir-for)) \
     $(eval _idfPrefix := $(if $(strip $(3)),HOST,TARGET)) \
     $(if $(filter $(_idfPrefix)-$(_idfClass),$(COMMON_MODULE_CLASSES))$(4), \
-        $(eval _idfIntBase := $($(_idfPrefix)_OUT_GEN_COMMON)) \
+        $(eval _idfIntBase := $($(_idfPrefix)_OUT_COMMON_GEN)) \
       , \
         $(eval _idfIntBase := $($(_idfPrefix)_OUT_GEN)) \
      ) \
@@ -1516,7 +1516,6 @@
 $(transform-o-to-shared-lib-inner)
 endef
 
-
 ###########################################################
 ## Commands for filtering a target executable or library
 ###########################################################
@@ -1757,11 +1756,13 @@
         @$(call emit-line,$(wordlist 4401,4600,$(1)),$(2))
         @$(call emit-line,$(wordlist 4601,4800,$(1)),$(2))
         @$(call emit-line,$(wordlist 4801,5000,$(1)),$(2))
-        @$(if $(wordlist 5001,5002,$(1)),$(error Too many words ($(words $(1)))))
+        @$(call emit-line,$(wordlist 5001,5200,$(1)),$(2))
+        @$(if $(wordlist 5201,5202,$(1)),$(error Too many words ($(words $(1)))))
 endef
 
 # For a list of jar files, unzip them to a specified directory,
-# but make sure that no META-INF files come along for the ride.
+# but make sure that no META-INF files come along for the ride,
+# unless PRIVATE_DONT_DELETE_JAR_META_INF is set.
 #
 # $(1): files to unzip
 # $(2): destination directory
@@ -1773,8 +1774,8 @@
       exit 1; \
     fi; \
     unzip -qo $$f -d $(2); \
-  done \
-  $(if $(PRIVATE_DONT_DELETE_JAR_META_INF),,;rm -rf $(2)/META-INF)
+  done
+  $(if $(PRIVATE_DONT_DELETE_JAR_META_INF),,$(hide) rm -rf $(2)/META-INF)
 endef
 
 # Call jack
@@ -1876,7 +1877,7 @@
     $(hide) mkdir -p $@.res.tmp
     $(hide) $(call create-empty-package-at,$@.res.tmp.zip)
     $(hide) $(call add-java-resources-to,$@.res.tmp.zip)
-    $(hide) $(call unzip-jar-files,$@.res.tmp.zip,$@.res.tmp)
+    $(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"; \
@@ -1916,7 +1917,7 @@
 	$(hide) mkdir -p $(dir $@)
 	$(JILL) $(PRIVATE_JILL_FLAGS) --output $@.tmpjill.jack $<
 	$(hide) mkdir -p $@.tmpjill.res
-	$(hide) $(call unzip-jar-files,$<,$@.tmpjill.res)
+	$(hide) unzip -qo $< -d $@.tmpjill.res
 	$(hide) find $@.tmpjill.res -iname "*.class" -delete
 	$(hide) $(call call-jack) \
         -D jack.import.resource.policy=keep-first \
@@ -1928,6 +1929,42 @@
 	$(hide) rm $@.tmpjill.jack
 endef
 
+# Moves $1.tmp to $1 if necessary. This is designed to be used with
+# .KATI_RESTAT. For kati, this function doesn't update the timestamp
+# of $1 when $1.tmp is identical to $1 so that ninja won't rebuild
+# targets which depend on $1. For GNU make, this function simply
+# copies $1.tmp to $1.
+ifeq ($(BUILDING_WITH_NINJA),true)
+define commit-change-for-toc
+$(hide) if cmp -s $1.tmp $1 ; then \
+ rm $1.tmp ; \
+else \
+ mv $1.tmp $1 ; \
+fi
+endef
+else
+define commit-change-for-toc
+@# make doesn't support restat. We always update .toc files so the dependents will always be updated too.
+$(hide) mv $1.tmp $1
+endef
+endif
+
+## Rule to creates 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) $$< $$@.tmp
+	$$(call commit-change-for-toc,$$@)
+endef
+
+## Define a rule which generates .jar.toc and mark it as .KATI_RESTAT.
+define define-jar-to-toc-rule
+$(eval $(call _transform-jar-to-toc,$1))\
+$(eval .KATI_RESTAT: $1.toc)
+endef
+
 
 # Invoke Jack to compile java from source to jack files without shrink or obfuscation.
 #
@@ -2094,13 +2131,13 @@
 $(foreach abi,$(PRIVATE_JNI_SHARED_LIBRARIES_ABI),\
   $(call _add-jni-shared-libs-to-package-per-abi,$(abi),\
     $(patsubst $(abi):%,%,$(filter $(abi):%,$(PRIVATE_JNI_SHARED_LIBRARIES)))))
-$(hide) (cd $(dir $@) && zip -qr $(JNI_COMPRESS_FLAGS) $(notdir $@) lib)
+$(hide) (cd $(dir $@) && zip -qrX $(JNI_COMPRESS_FLAGS) $(notdir $@) lib)
 $(hide) rm -rf $(dir $@)lib
 endef
 
 #TODO: update the manifest to point to the dex file
 define add-dex-to-package
-$(hide) zip -qj $@ $(dir $(PRIVATE_DEX_FILE))classes*.dex
+$(hide) find $(dir $(PRIVATE_DEX_FILE)) -maxdepth 1 -name "classes*.dex" | sort | xargs zip -qjX $@
 endef
 
 # Add java resources added by the current module.
@@ -2116,7 +2153,7 @@
 #
 define add-carried-jack-resources
  $(hide) if [ -d $(PRIVATE_JACK_INTERMEDIATES_DIR) ] ; then \
-    find $(PRIVATE_JACK_INTERMEDIATES_DIR) -type f \
+    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 \
@@ -2129,7 +2166,7 @@
 #
 define sign-package
 $(hide) mv $@ $@.unsigned
-$(hide) java -jar $(SIGNAPK_JAR) \
+$(hide) java -Djava.library.path=$(SIGNAPK_JNI_LIBRARY_PATH) -jar $(SIGNAPK_JAR) \
     $(PRIVATE_CERTIFICATE) $(PRIVATE_PRIVATE_KEY) \
     $(PRIVATE_ADDITIONAL_CERTIFICATES) $@.unsigned $@.signed
 $(hide) mv $@.signed $@
@@ -2147,6 +2184,14 @@
 $(hide) mv $@.aligned $@
 endef
 
+# Remove dynamic timestamps from packages
+#
+ifndef TARGET_BUILD_APPS
+define remove-timestamps-from-package
+$(hide) $(ZIPTIME) $@
+endef
+endif
+
 # Uncompress shared libraries embedded in an apk.
 #
 define uncompress-shared-libs
@@ -2154,7 +2199,7 @@
   rm -rf $(dir $@)uncompressedlibs && mkdir $(dir $@)uncompressedlibs; \
   unzip $@ $(PRIVATE_EMBEDDED_JNI_LIBS) -d $(dir $@)uncompressedlibs && \
   zip -d $@ 'lib/*.so' && \
-  ( cd $(dir $@)uncompressedlibs && zip -D -r -0 ../$(notdir $@) lib ) && \
+  ( cd $(dir $@)uncompressedlibs && find lib -type f | sort | zip -D -X -0 ../$(notdir $@) -@ ) && \
   rm -rf $(dir $@)uncompressedlibs; \
   fi
 endef
@@ -2547,6 +2592,7 @@
 # Include any vendor specific definitions.mk file
 -include $(TOPDIR)vendor/*/build/core/definitions.mk
 -include $(TOPDIR)device/*/build/core/definitions.mk
+-include $(TOPDIR)product/*/build/core/definitions.mk
 
 # broken:
 #	$(foreach file,$^,$(if $(findstring,.a,$(suffix $file)),-l$(file),$(file)))
diff --git a/core/distdir.mk b/core/distdir.mk
index 24beddc..51ec46e 100644
--- a/core/distdir.mk
+++ b/core/distdir.mk
@@ -22,6 +22,11 @@
 
 dist_goal := $(strip $(filter dist,$(MAKECMDGOALS)))
 MAKECMDGOALS := $(strip $(filter-out dist,$(MAKECMDGOALS)))
+ifeq (,$(strip $(filter-out $(INTERNAL_MODIFIER_TARGETS),$(MAKECMDGOALS))))
+# The commandline was something like "make dist" or "make dist showcommands".
+# Add a dependency on a real target.
+dist: $(DEFAULT_GOAL)
+endif
 
 ifdef dist_goal
 
diff --git a/core/dpi_specific_apk.mk b/core/dpi_specific_apk.mk
index 5d0b5bf..6bae25d 100644
--- a/core/dpi_specific_apk.mk
+++ b/core/dpi_specific_apk.mk
@@ -16,7 +16,7 @@
 $(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)
-ifneq (,$(filter-out current system_current, $(LOCAL_SDK_VERSION)))
+ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
 $(built_dpi_apk): PRIVATE_DEFAULT_APP_TARGET_SDK := $(LOCAL_SDK_VERSION)
 else
 $(built_dpi_apk): PRIVATE_DEFAULT_APP_TARGET_SDK := $(DEFAULT_APP_TARGET_SDK)
@@ -48,7 +48,7 @@
 $(built_dpi_apk) : $(R_file_stamp)
 $(built_dpi_apk) : $(all_library_res_package_export_deps)
 $(built_dpi_apk) : $(private_key) $(certificate) $(SIGNAPK_JAR)
-$(built_dpi_apk) : $(AAPT) | $(ZIPALIGN)
+$(built_dpi_apk) : $(AAPT)
 $(built_dpi_apk) : $(all_res_assets) $(jni_shared_libraries) $(full_android_manifest)
 	@echo "target Package: $(PRIVATE_MODULE) ($@)"
 	$(if $(PRIVATE_SOURCE_ARCHIVE),\
@@ -68,7 +68,6 @@
 endif
 endif
 	$(sign-package)
-	$(align-package)
 
 # Set up global variables to register this apk to the higher-level dependency graph.
 ALL_MODULES += $(dpi_apk_name)
diff --git a/core/droiddoc.mk b/core/droiddoc.mk
index 542e767..432ba11 100644
--- a/core/droiddoc.mk
+++ b/core/droiddoc.mk
@@ -68,13 +68,16 @@
   else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),system_current)
     LOCAL_JAVA_LIBRARIES := android_system_stubs_current $(LOCAL_JAVA_LIBRARIES)
     $(full_target): PRIVATE_BOOTCLASSPATH := $(call java-lib-files, android_system_stubs_current)
+  else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),test_current)
+    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))
   endif
 else
-  LOCAL_JAVA_LIBRARIES := core-libart ext framework $(LOCAL_JAVA_LIBRARIES)
-  $(full_target): PRIVATE_BOOTCLASSPATH := $(call java-lib-files, core-libart)
+  LOCAL_JAVA_LIBRARIES := core-oj core-libart ext framework $(LOCAL_JAVA_LIBRARIES)
+  $(full_target): PRIVATE_BOOTCLASSPATH := $(call java-lib-files, core-oj):$(call java-lib-files, core-libart)
 endif  # LOCAL_SDK_VERSION
 LOCAL_JAVA_LIBRARIES := $(sort $(LOCAL_JAVA_LIBRARIES))
 
@@ -173,7 +176,7 @@
 		javadoc \
                 -encoding UTF-8 \
                 \@$(PRIVATE_SRC_LIST_FILE) \
-                -J-Xmx1280m \
+                -J-Xmx1600m \
                 -XDignore.symbol.file \
                 $(PRIVATE_PROFILING_OPTIONS) \
                 -quiet \
@@ -212,6 +215,7 @@
                 \@$(PRIVATE_SRC_LIST_FILE) \
                 -J-Xmx1024m \
                 -XDignore.symbol.file \
+                $(if $(EXPERIMENTAL_USE_JAVA8),-Xdoclint:none) \
                 $(PRIVATE_PROFILING_OPTIONS) \
                 $(addprefix -classpath ,$(PRIVATE_CLASSPATH)) \
                 $(addprefix -bootclasspath ,$(PRIVATE_BOOTCLASSPATH)) \
@@ -244,7 +248,7 @@
 	@echo Package docs: $@
 	@rm -f $@
 	@mkdir -p $(dir $@)
-	$(hide) ( F=$$(pwd)/$@ ; cd $(PRIVATE_DOCS_DIR) && zip -rq $$F * )
+	$(hide) ( F=$$(pwd)/$@ ; cd $(PRIVATE_DOCS_DIR) && zip -rqX $$F * )
 
 $(LOCAL_MODULE)-docs.zip : $(out_zip)
 
diff --git a/core/dynamic_binary.mk b/core/dynamic_binary.mk
index 5b11724..d6f6cc9 100644
--- a/core/dynamic_binary.mk
+++ b/core/dynamic_binary.mk
@@ -44,7 +44,10 @@
 relocation_packer_input := $(linked_module)
 relocation_packer_output := $(intermediates)/PACKED/$(my_built_module_stem)
 
-my_pack_module_relocations := $(LOCAL_PACK_MODULE_RELOCATIONS)
+my_pack_module_relocations := false
+ifneq ($(DISABLE_RELOCATION_PACKER),true)
+    my_pack_module_relocations := $(LOCAL_PACK_MODULE_RELOCATIONS)
+endif
 
 ifeq ($(my_pack_module_relocations),)
   my_pack_module_relocations := $($(LOCAL_2ND_ARCH_VAR_PREFIX)TARGET_PACK_MODULE_RELOCATIONS)
diff --git a/core/envsetup.mk b/core/envsetup.mk
index 199c6b7..050d445 100644
--- a/core/envsetup.mk
+++ b/core/envsetup.mk
@@ -141,8 +141,8 @@
 board_config_mk := \
 	$(strip $(sort $(wildcard \
 		$(SRC_TARGET_DIR)/board/$(TARGET_DEVICE)/BoardConfig.mk \
-		$(shell test -d device && find device -maxdepth 4 -path '*/$(TARGET_DEVICE)/BoardConfig.mk') \
-		$(shell test -d vendor && find vendor -maxdepth 4 -path '*/$(TARGET_DEVICE)/BoardConfig.mk') \
+		$(shell test -d device && find -L device -maxdepth 4 -path '*/$(TARGET_DEVICE)/BoardConfig.mk') \
+		$(shell test -d vendor && find -L vendor -maxdepth 4 -path '*/$(TARGET_DEVICE)/BoardConfig.mk') \
 	)))
 ifeq ($(board_config_mk),)
   $(error No config file found for TARGET_DEVICE $(TARGET_DEVICE))
diff --git a/core/goma.mk b/core/goma.mk
index ddd7d80..01a1d81 100644
--- a/core/goma.mk
+++ b/core/goma.mk
@@ -56,7 +56,11 @@
   # gomacc can start goma client's daemon process automatically, but
   # it is safer and faster to start up it beforehand. We run this as a
   # background process so this won't slow down the build.
-  $(shell $(goma_ctl) ensure_start &> /dev/null &)
+  # We use "ensure_start" command when the compiler_proxy is already
+  # running and uses GOMA_HERMETIC=error flag. The compiler_proxy will
+  # restart otherwise.
+  # TODO(hamaji): Remove this condition after http://b/25676777 is fixed.
+  $(shell ( if ( curl http://localhost:$$($(GOMA_CC) port)/flagz | grep GOMA_HERMETIC=error ); then cmd=ensure_start; else cmd=restart; fi; GOMA_HERMETIC=error $(goma_ctl) $${cmd} ) &> /dev/null &)
 
   goma_ctl :=
   goma_dir :=
diff --git a/core/host_dalvik_java_library.mk b/core/host_dalvik_java_library.mk
index 6db0cbf..6944bff 100644
--- a/core/host_dalvik_java_library.mk
+++ b/core/host_dalvik_java_library.mk
@@ -28,7 +28,7 @@
 #######################################
 
 ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-  LOCAL_JAVA_LIBRARIES +=  core-libart-hostdex
+  LOCAL_JAVA_LIBRARIES += core-oj-hostdex core-libart-hostdex
 endif
 
 full_classes_compiled_jar := $(intermediates.COMMON)/classes-full-debug.jar
diff --git a/core/host_dalvik_static_java_library.mk b/core/host_dalvik_static_java_library.mk
index 6248e1a..f992d3a 100644
--- a/core/host_dalvik_static_java_library.mk
+++ b/core/host_dalvik_static_java_library.mk
@@ -24,7 +24,7 @@
 LOCAL_UNINSTALLABLE_MODULE := true
 LOCAL_IS_STATIC_JAVA_LIBRARY := true
 USE_CORE_LIB_BOOTCLASSPATH := true
-LOCAL_JAVA_LIBRARIES += core-libart-hostdex
+LOCAL_JAVA_LIBRARIES += core-oj-hostdex core-libart-hostdex
 
 intermediates.COMMON := $(call intermediates-dir-for,JAVA_LIBRARIES,$(LOCAL_MODULE),true,COMMON,)
 full_classes_jack := $(intermediates.COMMON)/classes.jack
diff --git a/core/java.mk b/core/java.mk
index cfae48d..637dfe0 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -31,6 +31,8 @@
         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
@@ -157,7 +159,7 @@
 else
   ifneq (,$(LOCAL_SDK_VERSION))
     # Set target-api for LOCAL_SDK_VERSIONs other than current.
-    ifneq (,$(filter-out current system_current, $(LOCAL_SDK_VERSION)))
+    ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
       renderscript_target_api := $(LOCAL_SDK_VERSION)
     endif
   endif  # LOCAL_SDK_VERSION is set
@@ -182,7 +184,7 @@
 renderscript_flags += $(LOCAL_RENDERSCRIPT_FLAGS)
 
 # prepend the RenderScript system include path
-ifneq ($(filter-out current system_current,$(LOCAL_SDK_VERSION))$(if $(TARGET_BUILD_APPS),$(filter current system_current,$(LOCAL_SDK_VERSION))),)
+ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_VERSION))$(if $(TARGET_BUILD_APPS),$(filter current system_current test_current,$(LOCAL_SDK_VERSION))),)
 # if a numeric LOCAL_SDK_VERSION, or current LOCAL_SDK_VERSION with TARGET_BUILD_APPS
 LOCAL_RENDERSCRIPT_INCLUDES := \
     $(HISTORICAL_SDK_VERSIONS_ROOT)/renderscript/clang-include \
@@ -290,7 +292,7 @@
 
 aidl_preprocess_import :=
 ifdef LOCAL_SDK_VERSION
-ifneq ($(filter current system_current, $(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS)),)
+ifneq ($(filter current system_current test_current, $(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS)),)
   # LOCAL_SDK_VERSION is current and no TARGET_BUILD_APPS
   aidl_preprocess_import := $(TARGET_OUT_COMMON_INTERMEDIATES)/framework.aidl
 else
@@ -456,6 +458,8 @@
 	@echo Copying: $@
 	$(hide) $(ACP) -fp $< $@
 
+$(call define-jar-to-toc-rule, $(full_classes_jar))
+
 # Run proguard if necessary, otherwise just copy the file.
 ifdef LOCAL_PROGUARD_ENABLED
 ifneq ($(filter-out full custom nosystem obfuscation optimization shrinktests,$(LOCAL_PROGUARD_ENABLED)),)
@@ -473,7 +477,7 @@
 ifneq (,$(filter android-support-%,$(LOCAL_STATIC_JAVA_LIBRARIES)))
 ifdef LOCAL_SDK_VERSION
 ifdef TARGET_BUILD_APPS
-ifeq (,$(filter current system_current, $(LOCAL_SDK_VERSION)))
+ifeq (,$(filter current system_current test_current, $(LOCAL_SDK_VERSION)))
   my_support_library_sdk_raise := $(call java-lib-files, sdk_vcurrent)
 endif
 else
diff --git a/core/java_common.mk b/core/java_common.mk
index 9d81096..2494152 100644
--- a/core/java_common.mk
+++ b/core/java_common.mk
@@ -133,25 +133,39 @@
 #                 be up-to-date.
 ifndef LOCAL_IS_HOST_MODULE
 ifeq ($(LOCAL_SDK_VERSION),)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(call java-lib-files,core-libart)
+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.
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(call java-lib-files,android_stubs_current)
+my_bootclasspath := $(call java-lib-files,android_stubs_current)
 else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),system_current)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(call java-lib-files,android_system_stubs_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
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(call java-lib-files,sdk_v$(LOCAL_SDK_VERSION))
-endif # current or system_current
+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)
 
 full_shared_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
 full_java_lib_deps := $(call java-lib-deps,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
+full_java_lib_deps := $(addsuffix .toc, $(full_java_lib_deps))
 
 else # LOCAL_IS_HOST_MODULE
 
 ifeq ($(USE_CORE_LIB_BOOTCLASSPATH),true)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(call java-lib-files,core-libart-hostdex,$(LOCAL_IS_HOST_MODULE))
+ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
+my_bootclasspath := ""
+else
+my_bootclasspath := $(call java-lib-files,core-oj-hostdex,$(LOCAL_IS_HOST_MODULE)):$(call java-lib-files,core-libart-hostdex,$(LOCAL_IS_HOST_MODULE))
+endif
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(my_bootclasspath)
 
 full_shared_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
 full_java_lib_deps := $(call java-lib-deps,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE)) \
@@ -262,17 +276,24 @@
 
 ifndef LOCAL_IS_HOST_MODULE
 ifeq ($(LOCAL_SDK_VERSION),)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES := $(call jack-lib-files,core-libart)
+ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
+my_bootclasspath :=
 else
+my_bootclasspath := $(call jack-lib-files,core-oj core-libart)
+endif
+else  # LOCAL_SDK_VERSION
 ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),current)
 # LOCAL_SDK_VERSION is current and no TARGET_BUILD_APPS.
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES := $(call jack-lib-files,android_stubs_current)
+my_bootclasspath := $(call jack-lib-files,android_stubs_current)
 else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),system_current)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES := $(call jack-lib-files,android_system_stubs_current)
+my_bootclasspath := $(call jack-lib-files,android_system_stubs_current)
+else ifeq ($(LOCAL_SDK_VERSION)$(TARGET_BUILD_APPS),test_current)
+my_bootclasspath := $(call jack-lib-files,android_test_stubs_current)
 else
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES := $(call jack-lib-files,sdk_v$(LOCAL_SDK_VERSION))
-endif # current or system_current
+my_bootclasspath :=$(call jack-lib-files,sdk_v$(LOCAL_SDK_VERSION))
+endif # current, system_current, or test_current
 endif # LOCAL_SDK_VERSION
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES := $(my_bootclasspath)
 
 full_shared_jack_libs := $(call jack-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
 full_jack_lib_deps := $(call jack-lib-deps,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
@@ -280,7 +301,12 @@
 else # LOCAL_IS_HOST_MODULE
 
 ifeq ($(USE_CORE_LIB_BOOTCLASSPATH),true)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES := $(call jack-lib-files,core-libart-hostdex,$(LOCAL_IS_HOST_MODULE))
+ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
+my_bootclasspath :=
+else
+my_bootclasspath := $(call jack-lib-files,core-oj-hostdex core-libart-hostdex,$(LOCAL_IS_HOST_MODULE))
+endif
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES := $(my_bootclasspath)
 full_shared_jack_libs := $(call jack-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
 full_jack_lib_deps := $(call jack-lib-deps,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
 else
diff --git a/core/java_library.mk b/core/java_library.mk
index 5a2d19b..dc6186f 100644
--- a/core/java_library.mk
+++ b/core/java_library.mk
@@ -81,7 +81,7 @@
 $(common_javalib.jar): PRIVATE_DEX_FILE := $(built_dex)
 $(common_javalib.jar): PRIVATE_SOURCE_ARCHIVE := $(full_classes_jarjar_jar)
 $(common_javalib.jar): PRIVATE_DONT_DELETE_JAR_DIRS := $(LOCAL_DONT_DELETE_JAR_DIRS)
-$(common_javalib.jar) : $(built_dex) $(java_resource_sources)
+$(common_javalib.jar) : $(built_dex) $(java_resource_sources) | $(ZIPTIME)
 	@echo "target Jar: $(PRIVATE_MODULE) ($@)"
 ifdef LOCAL_JACK_ENABLED
 	$(create-empty-package)
@@ -92,6 +92,7 @@
 ifdef LOCAL_JACK_ENABLED
 	$(add-carried-jack-resources)
 endif
+	$(remove-timestamps-from-package)
 
 ifdef LOCAL_DEX_PREOPT
 ifneq ($(dexpreopt_boot_jar_module),) # boot jar
diff --git a/core/main.mk b/core/main.mk
index 15b3c27..aa6d15f 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -80,7 +80,8 @@
     vendorimage-nodeps \
     ramdisk-nodeps \
     bootimage-nodeps \
-    recoveryimage-nodeps
+    recoveryimage-nodeps \
+    product-graph dump-products
 
 ifneq ($(filter $(dont_bother_goals), $(MAKECMDGOALS)),)
 dont_bother := true
@@ -112,6 +113,11 @@
 $(shell rm -f $(OUT_DIR)/ninja_build)
 endif
 
+# With these files findleaves.py won't be unnecessarily slower even if
+# there is a user creates a copy of $(OUT_DIR).
+$(shell echo '# This file prevents findleaves.py from traversing this directory further' > $(OUT_DIR)/Android.mk)
+$(shell echo '# This file prevents findleaves.py from traversing this directory further' > $(OUT_DIR)/CleanSpec.mk)
+
 # Write the build number to a file so it can be read back in
 # without changing the command line every time.  Avoids rebuilds
 # when using ninja.
@@ -315,8 +321,8 @@
 #
 # -----------------------------------------------------------------
 # Jack version configuration
-include $(TOPDIR)prebuilts/sdk/tools/jack_versions.mk
-include $(TOPDIR)prebuilts/sdk/tools/jack_for_module.mk
+-include $(TOPDIR)prebuilts/sdk/tools/jack_versions.mk
+-include $(TOPDIR)prebuilts/sdk/tools/jack_for_module.mk
 
 # -----------------------------------------------------------------
 ###
@@ -332,7 +338,7 @@
 
 # Add build properties for ART. These define system properties used by installd
 # to pass flags to dex2oat.
-ADDITIONAL_BUILD_PROPERTIES += persist.sys.dalvik.vm.lib.2=libart
+ADDITIONAL_BUILD_PROPERTIES += persist.sys.dalvik.vm.lib.2=libart.so
 ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.isa.$(TARGET_ARCH).variant=$(DEX2OAT_TARGET_CPU_VARIANT)
 ifneq ($(DEX2OAT_TARGET_INSTRUCTION_SET_FEATURES),)
   ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.isa.$(TARGET_ARCH).features=$(DEX2OAT_TARGET_INSTRUCTION_SET_FEATURES)
@@ -414,7 +420,6 @@
   # Don't verify or compile apps on eng builds to speed startup.
   ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.dex2oat-filter=verify-at-runtime
 endif
-  ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.usejit=true
 endif
 
 ## sdk ##
@@ -425,7 +430,7 @@
 sdk_repo_goal := $(strip $(filter sdk_repo,$(MAKECMDGOALS)))
 MAKECMDGOALS := $(strip $(filter-out sdk_repo,$(MAKECMDGOALS)))
 
-ifneq ($(words $(filter-out $(INTERNAL_MODIFIER_TARGETS) checkbuild emulator_tests target-files-package,$(MAKECMDGOALS))),1)
+ifneq ($(words $(sort $(filter-out $(INTERNAL_MODIFIER_TARGETS) checkbuild emulator_tests target-files-package,$(MAKECMDGOALS)))),1)
 $(error The 'sdk' target may not be specified with any other targets)
 endif
 
diff --git a/core/ninja.mk b/core/ninja.mk
index def5823..862ed80 100644
--- a/core/ninja.mk
+++ b/core/ninja.mk
@@ -1,12 +1,18 @@
 KATI ?= $(HOST_OUT_EXECUTABLES)/ckati
 MAKEPARALLEL ?= $(HOST_OUT_EXECUTABLES)/makeparallel
+NINJA ?= prebuilts/ninja/$(HOST_PREBUILT_TAG)/ninja
 
 KATI_OUTPUT_PATTERNS := $(OUT_DIR)/build%.ninja $(OUT_DIR)/ninja%.sh
-NINJA_GOALS := droid showcommands
-# A list of goals which affect parsing of make.
+
+# Modifier goals we don't need to pass to Ninja.
+NINJA_EXCLUDE_GOALS := showcommands all dist
+.PHONY : $(NINJA_EXCLUDE_GOALS)
+
+# A list of goals which affect parsing of makefiles and we need to pass to Kati.
 PARSE_TIME_MAKE_GOALS := \
 	$(PARSE_TIME_MAKE_GOALS) \
 	$(dont_bother_goals) \
+	all \
 	APP-% \
 	DUMP_% \
 	ECLIPSE-% \
@@ -52,17 +58,21 @@
 
 -include vendor/google/build/ninja_config.mk
 
-ANDROID_TARGETS := $(filter-out $(KATI_OUTPUT_PATTERNS) $(NINJA_GOALS),$(ORIGINAL_MAKECMDGOALS))
-EXTRA_TARGETS := $(filter-out $(KATI_OUTPUT_PATTERNS) $(NINJA_GOALS),$(filter-out $(ORIGINAL_MAKECMDGOALS),$(MAKECMDGOALS)))
-KATI_TARGETS := $(filter $(PARSE_TIME_MAKE_GOALS),$(ANDROID_TARGETS))
+# Any Android goals that need to be built.
+ANDROID_GOALS := $(filter-out $(KATI_OUTPUT_PATTERNS) $(KATI) $(MAKEPARALLEL),\
+    $(sort $(ORIGINAL_MAKECMDGOALS) $(MAKECMDGOALS)))
+# Goals we need to pass to Ninja.
+NINJA_GOALS := $(filter-out $(NINJA_EXCLUDE_GOALS), $(ANDROID_GOALS))
+# Goals we need to pass to Kati.
+KATI_GOALS := $(filter $(PARSE_TIME_MAKE_GOALS),  $(ANDROID_GOALS))
 
 define replace_space_and_slash
 $(subst /,_,$(subst $(space),_,$(sort $1)))
 endef
 
 KATI_NINJA_SUFFIX := -$(TARGET_PRODUCT)
-ifneq ($(KATI_TARGETS),)
-KATI_NINJA_SUFFIX := $(KATI_NINJA_SUFFIX)-$(call replace_space_and_slash,$(KATI_TARGETS))
+ifneq ($(KATI_GOALS),)
+KATI_NINJA_SUFFIX := $(KATI_NINJA_SUFFIX)-$(call replace_space_and_slash,$(KATI_GOALS))
 endif
 ifneq ($(ONE_SHOT_MAKEFILE),)
 KATI_NINJA_SUFFIX := $(KATI_NINJA_SUFFIX)-mmm-$(call replace_space_and_slash,$(ONE_SHOT_MAKEFILE))
@@ -72,17 +82,15 @@
 endif
 
 my_checksum_suffix :=
-ifneq ($(KATI_NINJA_SUFFIX),)
 my_ninja_suffix_too_long := $(filter 1, $(shell v='$(KATI_NINJA_SUFFIX)' && echo $$(($${$(pound)v} > 64))))
 ifneq ($(my_ninja_suffix_too_long),)
 # Replace the suffix with a checksum if it gets too long.
 my_checksum_suffix := $(KATI_NINJA_SUFFIX)
 KATI_NINJA_SUFFIX := -$(word 1, $(shell echo $(my_checksum_suffix) | $(MD5SUM)))
 endif
-endif
 
 KATI_BUILD_NINJA := $(OUT_DIR)/build$(KATI_NINJA_SUFFIX).ninja
-KATI_NINJA_SH := $(OUT_DIR)/ninja$(KATI_NINJA_SUFFIX).sh
+KATI_ENV_SH := $(OUT_DIR)/env$(KATI_NINJA_SUFFIX).sh
 
 # Write out a file mapping checksum to the real suffix.
 ifneq ($(my_checksum_suffix),)
@@ -97,8 +105,6 @@
 
 ifneq (,$(filter showcommands,$(ORIGINAL_MAKECMDGOALS)))
 NINJA_ARGS += "-v"
-PHONY: showcommands
-showcommands: droid
 endif
 
 ifdef USE_GOMA
@@ -112,17 +118,21 @@
 NINJA_MAKEPARALLEL := $(MAKEPARALLEL) --ninja
 endif
 
-droid $(ANDROID_TARGETS) $(EXTRA_TARGETS): ninja_wrapper
+$(sort $(DEFAULT_GOAL) $(ANDROID_GOALS)) : ninja_wrapper
 	@#empty
 
 .PHONY: ninja_wrapper
 ninja_wrapper: $(KATI_BUILD_NINJA) $(MAKEPARALLEL)
 	@echo Starting build with ninja
-	+$(hide) PATH=prebuilts/ninja/$(HOST_PREBUILT_TAG)/:$$PATH NINJA_STATUS="$(NINJA_STATUS)" $(NINJA_MAKEPARALLEL) $(KATI_NINJA_SH) $(filter-out dist,$(ANDROID_TARGETS)) -C $(TOP) $(NINJA_ARGS)
+	+$(hide) export NINJA_STATUS="$(NINJA_STATUS)" && source $(KATI_ENV_SH) && $(NINJA_MAKEPARALLEL) $(NINJA) $(NINJA_GOALS) -C $(TOP) -f $(KATI_BUILD_NINJA) $(NINJA_ARGS)
 
+KATI_FIND_EMULATOR := --use_find_emulator
+ifeq ($(KATI_EMULATE_FIND),false)
+  KATI_FIND_EMULATOR :=
+endif
 $(KATI_BUILD_NINJA): $(KATI) $(MAKEPARALLEL) FORCE
 	@echo Running kati to generate build$(KATI_NINJA_SUFFIX).ninja...
-	+$(hide) $(KATI_MAKEPARALLEL) $(KATI) --ninja --ninja_dir=$(OUT_DIR) --ninja_suffix=$(KATI_NINJA_SUFFIX) --regen --ignore_dirty=$(OUT_DIR)/% --ignore_optional_include=$(OUT_DIR)/%.P --detect_android_echo --use_find_emulator -f build/core/main.mk $(KATI_TARGETS) --gen_all_targets BUILDING_WITH_NINJA=true
+	+$(hide) $(KATI_MAKEPARALLEL) $(KATI) --ninja --ninja_dir=$(OUT_DIR) --ninja_suffix=$(KATI_NINJA_SUFFIX) --regen --ignore_dirty=$(OUT_DIR)/% --ignore_optional_include=$(OUT_DIR)/%.P --detect_android_echo $(KATI_FIND_EMULATOR) -f build/core/main.mk $(KATI_GOALS) --gen_all_targets BUILDING_WITH_NINJA=true
 
 KATI_CXX := $(CLANG_CXX) $(CLANG_HOST_GLOBAL_CFLAGS) $(CLANG_HOST_GLOBAL_CPPFLAGS)
 KATI_LD := $(CLANG_CXX) $(CLANG_HOST_GLOBAL_LDFLAGS)
diff --git a/core/package_internal.mk b/core/package_internal.mk
index 5301139..ecb6913 100644
--- a/core/package_internal.mk
+++ b/core/package_internal.mk
@@ -176,7 +176,7 @@
 ifeq (true,$(EMMA_INSTRUMENT))
 ifndef LOCAL_EMMA_INSTRUMENT
 # No emma for test apks.
-ifeq (,$(filer tests,$(LOCAL_MODULE_TAGS))$(LOCAL_INSTRUMENTATION_FOR))
+ifeq (,$(LOCAL_INSTRUMENTATION_FOR))
 LOCAL_EMMA_INSTRUMENT := true
 endif # No test apk
 endif # LOCAL_EMMA_INSTRUMENT is not set
@@ -235,7 +235,7 @@
 
 $(LOCAL_INTERMEDIATE_TARGETS): \
     PRIVATE_ANDROID_MANIFEST := $(full_android_manifest)
-ifneq (,$(filter-out current system_current, $(LOCAL_SDK_VERSION)))
+ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
 $(LOCAL_INTERMEDIATE_TARGETS): \
     PRIVATE_DEFAULT_APP_TARGET_SDK := $(LOCAL_SDK_VERSION)
 else
@@ -361,7 +361,7 @@
 # Most packages should link against the resources defined by framework-res.
 # Even if they don't have their own resources, they may use framework
 # resources.
-ifneq ($(filter-out current system_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_APPS),$(filter current system_current,$(LOCAL_SDK_RES_VERSION))),)
+ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_APPS),$(filter current system_current test_current,$(LOCAL_SDK_RES_VERSION))),)
 # for released sdk versions, the platform resources were built into android.jar.
 framework_res_package_export := \
     $(HISTORICAL_SDK_VERSIONS_ROOT)/$(LOCAL_SDK_RES_VERSION)/android.jar
@@ -436,7 +436,7 @@
     $(LOCAL_ADDITIONAL_CERTIFICATES), $(c).x509.pem $(c).pk8)
 
 # Define the rule to build the actual package.
-$(LOCAL_BUILT_MODULE): $(AAPT) | $(ZIPALIGN)
+$(LOCAL_BUILT_MODULE): $(AAPT)
 # PRIVATE_JNI_SHARED_LIBRARIES is a list of <abi>:<path_of_built_lib>.
 $(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES := $(jni_shared_libraries_with_abis)
 # PRIVATE_JNI_SHARED_LIBRARIES_ABI is a list of ABI names.
@@ -485,8 +485,6 @@
 endif
 endif
 	$(sign-package)
-	@# Alignment must happen after all other zip operations.
-	$(align-package)
 
 ###############################
 ## Build dpi-specific apks, if it's apps_only build.
@@ -521,7 +519,7 @@
 installed_apk_splits := $(foreach s,$(my_split_suffixes),$(my_module_path)/$(LOCAL_MODULE)_$(s).apk)
 
 # The splits should have been built in the same command building the base apk.
-# This rule just runs signing and zipalign etc.
+# This rule just runs signing.
 # Note that we explicily check the existence of the split apk and remove the
 # built base apk if the split apk isn't there.
 # That way the build system will rerun the aapt after the user changes the splitting parameters.
@@ -533,7 +531,6 @@
 	  rm $<; exit 1; \
 	fi
 	$(sign-package)
-	$(align-package)
 
 # Rules to install the splits
 $(installed_apk_splits) : $(my_module_path)/$(LOCAL_MODULE)_%.apk : $(built_module_path)/package_%.apk | $(ACP)
diff --git a/core/pdk_config.mk b/core/pdk_config.mk
index 3397d9c..95eb0cc 100644
--- a/core/pdk_config.mk
+++ b/core/pdk_config.mk
@@ -58,6 +58,7 @@
   target/common/obj/JAVA_LIBRARIES/android_stubs_current_intermediates \
   target/common/obj/JAVA_LIBRARIES/bouncycastle_intermediates \
   target/common/obj/JAVA_LIBRARIES/conscrypt_intermediates \
+  target/common/obj/JAVA_LIBRARIES/core-oj_intermediates \
   target/common/obj/JAVA_LIBRARIES/core-libart_intermediates \
   target/common/obj/JAVA_LIBRARIES/core-junit_intermediates \
   target/common/obj/JAVA_LIBRARIES/ext_intermediates \
diff --git a/core/prebuilt_internal.mk b/core/prebuilt_internal.mk
index 505bd45..9ff348c 100644
--- a/core/prebuilt_internal.mk
+++ b/core/prebuilt_internal.mk
@@ -48,6 +48,10 @@
     # Do not pack relocations by default
     LOCAL_PACK_MODULE_RELOCATIONS := false
   endif
+
+  ifeq ($(DISABLE_RELOCATION_PACKER),true)
+    LOCAL_PACK_MODULE_RELOCATIONS := false
+  endif
 endif
 
 ifneq ($(filter STATIC_LIBRARIES SHARED_LIBRARIES,$(LOCAL_MODULE_CLASS)),)
@@ -220,8 +224,10 @@
 endif
 endif
 	$(sign-package)
-endif
+	# No need for align-package because sign-package takes care of alignment
+else
 	$(align-package)
+endif
 
 ###############################
 ## Rule to build the odex file
@@ -237,7 +243,7 @@
 built_apk_splits := $(addprefix $(built_module_path)/,$(notdir $(LOCAL_PACKAGE_SPLITS)))
 installed_apk_splits := $(addprefix $(my_module_path)/,$(notdir $(LOCAL_PACKAGE_SPLITS)))
 
-# Rules to sign and zipalign the split apks.
+# Rules to sign the split apks.
 my_src_dir := $(sort $(dir $(LOCAL_PACKAGE_SPLITS)))
 ifneq (1,$(words $(my_src_dir)))
 $(error You must put all the split source apks in the same folder: $(LOCAL_PACKAGE_SPLITS))
@@ -249,7 +255,6 @@
 $(built_apk_splits) : $(built_module_path)/%.apk : $(my_src_dir)/%.apk | $(ACP)
 	$(copy-file-to-new-target)
 	$(sign-package)
-	$(align-package)
 
 # Rules to install the split apks.
 $(installed_apk_splits) : $(my_module_path)/%.apk : $(built_module_path)/%.apk | $(ACP)
@@ -271,8 +276,13 @@
 $(built_module) : $(my_prebuilt_src_file)
 	$(transform-prebuilt-to-target-strip-comments)
 else
+ifneq ($(LOCAL_ACP_UNAVAILABLE),true)
 $(built_module) : $(my_prebuilt_src_file) | $(ACP)
 	$(transform-prebuilt-to-target)
+else
+$(built_module) : $(my_prebuilt_src_file)
+	$(copy-file-to-target-with-cp)
+endif
 endif
 endif # LOCAL_MODULE_CLASS != APPS
 
@@ -306,6 +316,8 @@
 $(common_javalib_jar) : $(common_classes_jar) | $(ACP)
 	$(transform-prebuilt-to-target)
 
+$(call define-jar-to-toc-rule, $(common_classes_jar))
+
 # make sure the classes.jar and javalib.jar are built before $(LOCAL_BUILT_MODULE)
 $(built_module) : $(common_javalib_jar)
 endif # TARGET JAVA_LIBRARIES
diff --git a/core/tasks/product-graph.mk b/core/product-graph.mk
similarity index 98%
rename from core/tasks/product-graph.mk
rename to core/product-graph.mk
index db2cf71..36e9037 100644
--- a/core/tasks/product-graph.mk
+++ b/core/product-graph.mk
@@ -34,7 +34,7 @@
 endef
 
 
-this_makefile := build/core/tasks/product-graph.mk
+this_makefile := build/core/product-graph.mk
 
 products_svg := $(OUT_DIR)/products.svg
 products_pdf := $(OUT_DIR)/products.pdf
diff --git a/core/product.mk b/core/product.mk
index c8e0e94..4d35704 100644
--- a/core/product.mk
+++ b/core/product.mk
@@ -28,8 +28,9 @@
 # $(call ) isn't necessary.
 #
 define _find-android-products-files
-$(sort $(shell test -d device && find device -maxdepth 6 -name AndroidProducts.mk)) \
-  $(sort $(shell test -d vendor && find vendor -maxdepth 6 -name AndroidProducts.mk)) \
+$(sort $(shell test -d device && find -L device -maxdepth 6 -name AndroidProducts.mk)) \
+  $(sort $(shell test -d vendor && find -L vendor -maxdepth 6 -name AndroidProducts.mk)) \
+  $(sort $(shell test -d product && find -L product -maxdepth 6 -name AndroidProducts.mk)) \
   $(SRC_TARGET_DIR)/product/AndroidProducts.mk
 endef
 
diff --git a/core/product_config.mk b/core/product_config.mk
index 64ac03a..fd61723 100644
--- a/core/product_config.mk
+++ b/core/product_config.mk
@@ -117,7 +117,7 @@
   # which really means TARGET_PRODUCT=dream make installclean.
   ifneq ($(filter-out $(INTERNAL_VALID_VARIANTS),$(TARGET_BUILD_VARIANT)),)
     MAKECMDGOALS := $(MAKECMDGOALS) $(TARGET_BUILD_VARIANT)
-    TARGET_BUILD_VARIANT := eng
+    TARGET_BUILD_VARIANT := userdebug
     default_goal_substitution :=
   else
     default_goal_substitution := $(DEFAULT_GOAL)
@@ -213,7 +213,19 @@
 current_product_makefile := $(strip $(current_product_makefile))
 all_product_makefiles := $(strip $(all_product_makefiles))
 
-ifneq (,$(filter product-graph dump-products, $(MAKECMDGOALS)))
+load_all_product_makefiles :=
+ifneq (,$(filter product-graph, $(MAKECMDGOALS)))
+ifeq ($(ANDROID_PRODUCT_GRAPH),--all)
+load_all_product_makefiles := true
+endif
+endif
+ifneq (,$(filter dump-products,$(MAKECMDGOALS)))
+ifeq ($(ANDROID_DUMP_PRODUCTS),all)
+load_all_product_makefiles := true
+endif
+endif
+
+ifeq ($(load_all_product_makefiles),true)
 # Import all product makefiles.
 $(call import-products, $(all_product_makefiles))
 else
diff --git a/core/static_java_library.mk b/core/static_java_library.mk
index 9b7b46a..fa65d21 100644
--- a/core/static_java_library.mk
+++ b/core/static_java_library.mk
@@ -90,7 +90,7 @@
 framework_res_package_export_deps :=
 # Please refer to package.mk
 ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-ifneq ($(filter-out current system_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_APPS),$(filter current system_current,$(LOCAL_SDK_RES_VERSION))),)
+ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_APPS),$(filter current system_current test_current,$(LOCAL_SDK_RES_VERSION))),)
 framework_res_package_export := \
     $(HISTORICAL_SDK_VERSIONS_ROOT)/$(LOCAL_SDK_RES_VERSION)/android.jar
 framework_res_package_export_deps := $(framework_res_package_export)
@@ -111,7 +111,7 @@
 $(R_file_stamp): PRIVATE_RESOURCE_PUBLICS_OUTPUT := $(intermediates.COMMON)/public_resources.xml
 $(R_file_stamp): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
 $(R_file_stamp): PRIVATE_AAPT_INCLUDES := $(framework_res_package_export)
-ifneq (,$(filter-out current system_current, $(LOCAL_SDK_VERSION)))
+ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
 $(R_file_stamp): PRIVATE_DEFAULT_APP_TARGET_SDK := $(LOCAL_SDK_VERSION)
 else
 $(R_file_stamp): PRIVATE_DEFAULT_APP_TARGET_SDK := $(DEFAULT_APP_TARGET_SDK)
diff --git a/core/tasks/apicheck.mk b/core/tasks/apicheck.mk
index 683a075..3975d20 100644
--- a/core/tasks/apicheck.mk
+++ b/core/tasks/apicheck.mk
@@ -129,4 +129,37 @@
 	@echo Copying system-removed.txt
 	$(hide) $(ACP) $(INTERNAL_PLATFORM_SYSTEM_REMOVED_API_FILE) frameworks/base/api/system-removed.txt
 
+#####################Check Test API#####################
+.PHONY: check-test-api
+checkapi : check-test-api
+
+# Check that the Test API we're building hasn't changed from the not-yet-released
+# SDK version. Note that we don't check that we haven't broken the previous
+# SDK's API because the test API is meant only for CTS which is always
+# associated with the current release.
+$(eval $(call check-api, \
+    checktestapi-current, \
+    frameworks/base/api/test-current.txt, \
+    $(INTERNAL_PLATFORM_TEST_API_FILE), \
+    frameworks/base/api/test-removed.txt, \
+    $(INTERNAL_PLATFORM_TEST_REMOVED_API_FILE), \
+    -error 2 -error 3 -error 4 -error 5 -error 6 \
+    -error 7 -error 8 -error 9 -error 10 -error 11 -error 12 -error 13 -error 14 -error 15 \
+    -error 16 -error 17 -error 18 -error 19 -error 20 -error 21 -error 23 -error 24 \
+    -error 25 -error 26 -error 27, \
+    cat $(BUILD_SYSTEM)/apicheck_msg_current.txt, \
+    check-test-api, \
+    $(call doc-timestamp-for,test-api-stubs) \
+    ))
+
+.PHONY: update-test-api
+update-api : update-test-api
+
+update-test-api: $(INTERNAL_PLATFORM_TEST_API_FILE) | $(ACP)
+	@echo Copying test-current.txt
+	$(hide) $(ACP) $(INTERNAL_PLATFORM_TEST_API_FILE) frameworks/base/api/test-current.txt
+	@echo Copying test-removed.txt
+	$(hide) $(ACP) $(INTERNAL_PLATFORM_TEST_REMOVED_API_FILE) frameworks/base/api/test-removed.txt
+
+
 endif
diff --git a/core/tasks/check_boot_jars/package_whitelist.txt b/core/tasks/check_boot_jars/package_whitelist.txt
index 4d62615..48fa4a1 100644
--- a/core/tasks/check_boot_jars/package_whitelist.txt
+++ b/core/tasks/check_boot_jars/package_whitelist.txt
@@ -2,7 +2,7 @@
 # Each line is interpreted as a regular expression.
 
 ###################################################
-# core-libart.jar
+# core-libart.jar & core-oj.jar
 java\.awt\.font
 java\.beans
 java\.io
@@ -13,6 +13,9 @@
 java\.math
 java\.net
 java\.nio
+java\.nio\.file
+java\.nio\.file\.spi
+java\.nio\.file\.attribute
 java\.nio\.channels
 java\.nio\.channels\.spi
 java\.nio\.charset
@@ -24,6 +27,7 @@
 java\.security\.spec
 java\.sql
 java\.text
+java\.text\.spi
 java\.util
 java\.util\.concurrent
 java\.util\.concurrent\.atomic
@@ -32,6 +36,7 @@
 java\.util\.logging
 java\.util\.prefs
 java\.util\.regex
+java\.util\.spi
 java\.util\.zip
 javax\.crypto
 javax\.crypto\.interfaces
@@ -54,10 +59,18 @@
 javax\.xml\.transform\.stream
 javax\.xml\.validation
 javax\.xml\.xpath
-sun\.misc
 org\.w3c\.dom
 org\.w3c\.dom\.ls
 org\.w3c\.dom\.traversal
+# OpenJdk internal implementation.
+sun\.misc
+sun\.util.*
+sun\.text.*
+sun\.security.*
+sun\.reflect.*
+sun\.nio.*
+sun\.net.*
+com\.sun\..*
 
 # TODO: Move these internal org.apache.harmony classes to libcore.*
 org\.apache\.harmony\.crypto\.internal
diff --git a/core/tasks/cts.mk b/core/tasks/cts.mk
index 56a7f6f..d923c26 100644
--- a/core/tasks/cts.mk
+++ b/core/tasks/cts.mk
@@ -126,6 +126,7 @@
 	$(PRIVATE_PARAMS) CollectAllTests $(1) $(2) $(3) "$(4)" $(5) $(6) $(7)
 endef
 
+OJ_INTERMEDIATES :=$(call intermediates-dir-for,JAVA_LIBRARIES,core-oj,,COMMON)
 CORE_INTERMEDIATES :=$(call intermediates-dir-for,JAVA_LIBRARIES,core-libart,,COMMON)
 CONSCRYPT_INTERMEDIATES :=$(call intermediates-dir-for,JAVA_LIBRARIES,conscrypt,,COMMON)
 BOUNCYCASTLE_INTERMEDIATES :=$(call intermediates-dir-for,JAVA_LIBRARIES,bouncycastle,,COMMON)
@@ -142,7 +143,7 @@
 TZDATAUPDATETESTS_INTERMEDIATES :=$(call intermediates-dir-for,JAVA_LIBRARIES,tzdata_update-tests,,COMMON)
 
 GEN_CLASSPATH := \
-    $(CORE_INTERMEDIATES)/classes.jar:$(CONSCRYPT_INTERMEDIATES)/classes.jar:$(BOUNCYCASTLE_INTERMEDIATES)/classes.jar:$(APACHEXML_INTERMEDIATES)/classes.jar:$(APACHEHARMONYTESTS_INTERMEDIATES)/classes.jar:$(OKHTTP_INTERMEDIATES)/classes.jar:$(OKHTTPTESTS_INTERMEDIATES)/classes.jar:$(OKHTTP_REPACKAGED_INTERMEDIATES)/classes.jar:$(JUNIT_INTERMEDIATES)/classes.jar:$(SQLITEJDBC_INTERMEDIATES)/javalib.jar:$(CORETESTS_INTERMEDIATES)/javalib.jar:$(JSR166TESTS_INTERMEDIATES)/javalib.jar:$(CONSCRYPTTESTS_INTERMEDIATES)/javalib.jar:$(TZDATAUPDATETESTS_INTERMEDIATES)/javalib.jar
+    $(OJ_INTERMEDIATES)/classes.jar:$(CORE_INTERMEDIATES)/classes.jar:$(CONSCRYPT_INTERMEDIATES)/classes.jar:$(BOUNCYCASTLE_INTERMEDIATES)/classes.jar:$(APACHEXML_INTERMEDIATES)/classes.jar:$(APACHEHARMONYTESTS_INTERMEDIATES)/classes.jar:$(OKHTTP_INTERMEDIATES)/classes.jar:$(OKHTTPTESTS_INTERMEDIATES)/classes.jar:$(OKHTTP_REPACKAGED_INTERMEDIATES)/classes.jar:$(JUNIT_INTERMEDIATES)/classes.jar:$(SQLITEJDBC_INTERMEDIATES)/javalib.jar:$(CORETESTS_INTERMEDIATES)/javalib.jar:$(JSR166TESTS_INTERMEDIATES)/javalib.jar:$(CONSCRYPTTESTS_INTERMEDIATES)/javalib.jar:$(TZDATAUPDATETESTS_INTERMEDIATES)/javalib.jar
 
 CTS_CORE_XMLS := \
 	$(CTS_TESTCASES_OUT)/android.core.tests.libcore.package.dalvik.xml \
@@ -352,10 +353,11 @@
 CORE_VM_TEST_TF_DESC := $(CTS_TESTCASES_OUT)/android.core.vm-tests-tf.xml
 
 # core tests only needed to get hold of junit-framework-classes
+OJ_INTERMEDIATES :=$(call intermediates-dir-for,JAVA_LIBRARIES,core-oj,,COMMON)
 CORE_INTERMEDIATES :=$(call intermediates-dir-for,JAVA_LIBRARIES,core-libart,,COMMON)
 JUNIT_INTERMEDIATES :=$(call intermediates-dir-for,JAVA_LIBRARIES,core-junit,,COMMON)
 
-GEN_CLASSPATH := $(CORE_INTERMEDIATES)/classes.jar:$(JUNIT_INTERMEDIATES)/classes.jar:$(VMTESTSTF_JAR):$(TF_JAR)
+GEN_CLASSPATH := $(OJ_INTERMEDIATES)/classes.jar:$(CORE_INTERMEDIATES)/classes.jar:$(JUNIT_INTERMEDIATES)/classes.jar:$(VMTESTSTF_JAR):$(TF_JAR)
 
 $(CORE_VM_TEST_TF_DESC): PRIVATE_CLASSPATH:=$(GEN_CLASSPATH)
 # Please see big comment above on why this line depends on javalib.jar instead of classes.jar
@@ -387,9 +389,8 @@
 $(INTERNAL_CTS_TARGET): TMP_DIR := $(cts_dir)/temp
 $(INTERNAL_CTS_TARGET): $(cts_dir)/all_cts_files_stamp $(DEFAULT_TEST_PLAN)
 	$(hide) echo "Package CTS: $@"
-	$(hide) cd $(dir $@) && zip -rq $(notdir $@) $(PRIVATE_NAME)
+	$(hide) cd $(dir $@) && zip -rqX $(notdir $@) $(PRIVATE_NAME)
 
 .PHONY: cts
 cts: $(INTERNAL_CTS_TARGET) adb
 $(call dist-for-goals,cts,$(INTERNAL_CTS_TARGET))
-
diff --git a/core/tasks/sdk-addon.mk b/core/tasks/sdk-addon.mk
index 5ac9b7d..362b229 100644
--- a/core/tasks/sdk-addon.mk
+++ b/core/tasks/sdk-addon.mk
@@ -111,13 +111,13 @@
 	    $(ACP) -r $$d $(PRIVATE_STAGING_DIR)/docs ;\
 	  done
 	$(hide) mkdir -p $(dir $@)
-	$(hide) ( F=$$(pwd)/$@ ; cd $(PRIVATE_STAGING_DIR)/.. && zip -rq $$F $(notdir $(PRIVATE_STAGING_DIR)) )
+	$(hide) ( F=$$(pwd)/$@ ; cd $(PRIVATE_STAGING_DIR)/.. && zip -rqX $$F $(notdir $(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)
 	@echo Packaging SDK Addon System-Image: $@
 	$(hide) mkdir -p $(dir $@)
-	$(hide) ( F=$$(pwd)/$@ ; cd $(PRIVATE_STAGING_DIR)/.. && zip -rq $$F $(notdir $(PRIVATE_STAGING_DIR)) )
+	$(hide) ( F=$$(pwd)/$@ ; cd $(PRIVATE_STAGING_DIR)/.. && zip -rqX $$F $(notdir $(PRIVATE_STAGING_DIR)) )
 
 
 .PHONY: sdk_addon
diff --git a/core/tasks/tools/package-modules.mk b/core/tasks/tools/package-modules.mk
index a70e644..24a7608 100644
--- a/core/tasks/tools/package-modules.mk
+++ b/core/tasks/tools/package-modules.mk
@@ -59,4 +59,4 @@
 	$(call copy-tests-in-batch,$(wordlist 1201,9999,$(PRIVATE_COPY_PAIRS)))
 	$(hide) $(foreach f, $(PRIVATE_PICKUP_FILES),\
 	  cp -RfL $(f) $(dir $@);)
-	$(hide) cd $(dir $@) && zip -rq $(notdir $@) *
+	$(hide) cd $(dir $@) && zip -rqX $(notdir $@) *
diff --git a/core/version_defaults.mk b/core/version_defaults.mk
index c56b3ff..6533bb4 100644
--- a/core/version_defaults.mk
+++ b/core/version_defaults.mk
@@ -43,7 +43,7 @@
   # which is the version that we reveal to the end user.
   # Update this value when the platform version changes (rather
   # than overriding it somewhere else).  Can be an arbitrary string.
-  PLATFORM_VERSION := NYC
+  PLATFORM_VERSION := N
 endif
 
 ifeq "" "$(PLATFORM_SDK_VERSION)"
@@ -61,12 +61,12 @@
 ifeq "" "$(PLATFORM_VERSION_CODENAME)"
   # This is the current development code-name, if the build is not a final
   # release build.  If this is a final release build, it is simply "REL".
-  PLATFORM_VERSION_CODENAME := NYC
+  PLATFORM_VERSION_CODENAME := N
 
   # 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.
-  PLATFORM_VERSION_ALL_CODENAMES := $(PLATFORM_VERSION_CODENAME),MNC
+  PLATFORM_VERSION_ALL_CODENAMES := $(PLATFORM_VERSION_CODENAME)
 endif
 
 ifeq "REL" "$(PLATFORM_VERSION_CODENAME)"
@@ -104,7 +104,7 @@
   # Can be an arbitrary string, but must be a single word.
   #
   # If there is no $PLATFORM_SECURITY_PATCH set, keep it empty.
-  PLATFORM_SECURITY_PATCH := 2015-12-01
+  PLATFORM_SECURITY_PATCH := 2016-02-01
 endif
 
 ifeq "" "$(PLATFORM_BASE_OS)"
diff --git a/envsetup.sh b/envsetup.sh
index 9328527..f266f1a 100644
--- a/envsetup.sh
+++ b/envsetup.sh
@@ -1413,11 +1413,7 @@
     \cd $T/$pathname
 }
 
-# Force JAVA_HOME to point to java 1.7 if it isn't already set.
-#
-# Note that the MacOS path for java 1.7 includes a minor revision number (sigh).
-# For some reason, installing the JDK doesn't make it show up in the
-# JavaVM.framework/Versions/1.7/ folder.
+# 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
@@ -1430,14 +1426,25 @@
     fi
 
     if [ ! "$JAVA_HOME" ]; then
-      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
+      if [ ! "$EXPERIMENTAL_USE_JAVA8" ]; then
+        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.
@@ -1520,7 +1527,8 @@
         echo ""
         echo "ALL DATA ON THE DEVICE WILL BE IRREVOCABLY ERASED."
         echo ""
-        read -p "Are you sure you want to do this (yes/no)? "
+        echo -n "Are you sure you want to do this (yes/no)? "
+        read
         if [[ "${REPLY}" != "yes" ]] ; then
             echo "Not taking any action. Exiting." >&2
             return 1
@@ -1541,7 +1549,8 @@
 
 # Execute the contents of any vendorsetup.sh files we can find.
 for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
-         `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort`
+         `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort` \
+         `test -d product && find -L product -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/null | sort`
 do
     echo "including $f"
     . $f
diff --git a/target/board/generic/sepolicy/file_contexts b/target/board/generic/sepolicy/file_contexts
index e626d9d..e704a8e 100644
--- a/target/board/generic/sepolicy/file_contexts
+++ b/target/board/generic/sepolicy/file_contexts
@@ -7,6 +7,6 @@
 /dev/ttyGF[0-9]*             u:object_r:serial_device:s0
 /dev/ttyS2                   u:object_r:console_device:s0
 /system/bin/qemud            u:object_r:qemud_exec:s0
-/sys/qemu_trace(/.*)?   --   u:object_r:sysfs_writable:s0
+/sys/qemu_trace(/.*)?        u:object_r:sysfs_writable:s0
 /system/etc/init.goldfish.sh u:object_r:goldfish_setup_exec:s0
 /system/bin/qemu-props       u:object_r:qemu_props_exec:s0
diff --git a/target/board/generic/sepolicy/goldfish_setup.te b/target/board/generic/sepolicy/goldfish_setup.te
index 85d5c8c..b472d2a 100644
--- a/target/board/generic/sepolicy/goldfish_setup.te
+++ b/target/board/generic/sepolicy/goldfish_setup.te
@@ -1,5 +1,5 @@
 # goldfish-setup service: runs init.goldfish.sh script
-type goldfish_setup, domain;
+type goldfish_setup, domain, domain_deprecated;
 type goldfish_setup_exec, exec_type, file_type;
 
 init_daemon_domain(goldfish_setup)
diff --git a/target/board/generic/sepolicy/property_contexts b/target/board/generic/sepolicy/property_contexts
index f4653c0..152f0c8 100644
--- a/target/board/generic/sepolicy/property_contexts
+++ b/target/board/generic/sepolicy/property_contexts
@@ -1,3 +1,5 @@
 qemu.                   u:object_r:qemu_prop:s0
+emu.                    u:object_r:qemu_prop:s0
+emulator.               u:object_r:qemu_prop:s0
 radio.noril             u:object_r:radio_noril_prop:s0
 opengles.               u:object_r:opengles_prop:s0
diff --git a/target/board/generic/sepolicy/qemu_props.te b/target/board/generic/sepolicy/qemu_props.te
index 60a09a5..6768ce7 100644
--- a/target/board/generic/sepolicy/qemu_props.te
+++ b/target/board/generic/sepolicy/qemu_props.te
@@ -1,5 +1,5 @@
 # qemu-props service:  Sets system properties on boot.
-type qemu_props, domain;
+type qemu_props, domain, domain_deprecated;
 type qemu_props_exec, exec_type, file_type;
 
 init_daemon_domain(qemu_props)
diff --git a/target/board/generic/sepolicy/qemud.te b/target/board/generic/sepolicy/qemud.te
index eee21c4..797cf5c 100644
--- a/target/board/generic/sepolicy/qemud.te
+++ b/target/board/generic/sepolicy/qemud.te
@@ -1,5 +1,5 @@
 # qemu support daemon
-type qemud, domain;
+type qemud, domain, domain_deprecated;
 type qemud_exec, exec_type, file_type;
 
 init_daemon_domain(qemud)
diff --git a/target/board/generic_mips/BoardConfig.mk b/target/board/generic_mips/BoardConfig.mk
index 76a2ef4..910107a 100644
--- a/target/board/generic_mips/BoardConfig.mk
+++ b/target/board/generic_mips/BoardConfig.mk
@@ -53,7 +53,7 @@
 USE_OPENGL_RENDERER := true
 
 TARGET_USERIMAGES_USE_EXT4 := true
-BOARD_SYSTEMIMAGE_PARTITION_SIZE := 1342177280
+BOARD_SYSTEMIMAGE_PARTITION_SIZE := 1610612736
 BOARD_USERDATAIMAGE_PARTITION_SIZE := 734003200
 BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
 BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
@@ -61,3 +61,5 @@
 TARGET_USERIMAGES_SPARSE_EXT_DISABLED := true
 
 BOARD_SEPOLICY_DIRS += build/target/board/generic/sepolicy
+
+USE_CLANG_PLATFORM_BUILD := true
diff --git a/target/board/generic_mips64/BoardConfig.mk b/target/board/generic_mips64/BoardConfig.mk
index 8e8a68b..97fd525 100644
--- a/target/board/generic_mips64/BoardConfig.mk
+++ b/target/board/generic_mips64/BoardConfig.mk
@@ -65,7 +65,7 @@
 USE_OPENGL_RENDERER := true
 
 TARGET_USERIMAGES_USE_EXT4 := true
-BOARD_SYSTEMIMAGE_PARTITION_SIZE := 1342177280  # 1.25 GB swag, 20% more than before
+BOARD_SYSTEMIMAGE_PARTITION_SIZE := 1610612736
 BOARD_USERDATAIMAGE_PARTITION_SIZE := 1610612736  # 1.5 GB, lots of space for running tests
 BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
 BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
@@ -75,3 +75,5 @@
 BOARD_SEPOLICY_DIRS += build/target/board/generic/sepolicy
 
 DEX_PREOPT_DEFAULT := nostripping
+
+USE_CLANG_PLATFORM_BUILD := true
diff --git a/target/board/generic_x86/BoardConfig.mk b/target/board/generic_x86/BoardConfig.mk
index a34e4f2..419c3f6 100644
--- a/target/board/generic_x86/BoardConfig.mk
+++ b/target/board/generic_x86/BoardConfig.mk
@@ -35,7 +35,7 @@
 USE_OPENGL_RENDERER := true
 
 TARGET_USERIMAGES_USE_EXT4 := true
-BOARD_SYSTEMIMAGE_PARTITION_SIZE := 1342177280  # 1.25 GB
+BOARD_SYSTEMIMAGE_PARTITION_SIZE := 1610612736
 BOARD_USERDATAIMAGE_PARTITION_SIZE := 576716800
 BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
 BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
diff --git a/target/board/generic_x86_64/BoardConfig.mk b/target/board/generic_x86_64/BoardConfig.mk
index fda9e45..de2f33d 100755
--- a/target/board/generic_x86_64/BoardConfig.mk
+++ b/target/board/generic_x86_64/BoardConfig.mk
@@ -41,7 +41,7 @@
 USE_OPENGL_RENDERER := true
 
 TARGET_USERIMAGES_USE_EXT4 := true
-BOARD_SYSTEMIMAGE_PARTITION_SIZE := 1342177280  # 1.25 GB
+BOARD_SYSTEMIMAGE_PARTITION_SIZE := 1610612736
 BOARD_USERDATAIMAGE_PARTITION_SIZE := 576716800
 BOARD_CACHEIMAGE_PARTITION_SIZE := 69206016
 BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
diff --git a/target/product/aosp_arm.mk b/target/product/aosp_arm.mk
index 86b715c..781cae6 100644
--- a/target/product/aosp_arm.mk
+++ b/target/product/aosp_arm.mk
@@ -13,6 +13,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-$(call inherit-product, $(SRC_TARGET_DIR)/product/full.mk)
+include $(SRC_TARGET_DIR)/product/full.mk
 
 PRODUCT_NAME := aosp_arm
diff --git a/target/product/aosp_mips.mk b/target/product/aosp_mips.mk
index ceeb433..a76b93a 100644
--- a/target/product/aosp_mips.mk
+++ b/target/product/aosp_mips.mk
@@ -13,6 +13,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-$(call inherit-product, $(SRC_TARGET_DIR)/product/full_mips.mk)
+include $(SRC_TARGET_DIR)/product/full_mips.mk
 
 PRODUCT_NAME := aosp_mips
diff --git a/target/product/aosp_x86.mk b/target/product/aosp_x86.mk
index 3e9b018..cba43c4 100644
--- a/target/product/aosp_x86.mk
+++ b/target/product/aosp_x86.mk
@@ -13,6 +13,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 #
-$(call inherit-product, $(SRC_TARGET_DIR)/product/full_x86.mk)
+include $(SRC_TARGET_DIR)/product/full_x86.mk
 
 PRODUCT_NAME := aosp_x86
diff --git a/target/product/core.mk b/target/product/core.mk
index 2992dbd..61b0b4a 100644
--- a/target/product/core.mk
+++ b/target/product/core.mk
@@ -43,6 +43,7 @@
     LatinIME \
     Launcher2 \
     ManagedProvisioning \
+    MtpDocumentsProvider \
     PicoTts \
     PacProcessor \
     libpac \
diff --git a/target/product/core_minimal.mk b/target/product/core_minimal.mk
index 27c10af..e890fd2 100644
--- a/target/product/core_minimal.mk
+++ b/target/product/core_minimal.mk
@@ -83,6 +83,7 @@
 
 # The order of PRODUCT_BOOT_JARS matters.
 PRODUCT_BOOT_JARS := \
+    core-oj \
     core-libart \
     conscrypt \
     okhttp \
diff --git a/target/product/core_tiny.mk b/target/product/core_tiny.mk
index a12d2d1..685ad31 100644
--- a/target/product/core_tiny.mk
+++ b/target/product/core_tiny.mk
@@ -80,6 +80,7 @@
 
 # The order matters
 PRODUCT_BOOT_JARS := \
+    core-oj \
     core-libart \
     conscrypt \
     okhttp \
diff --git a/target/product/embedded.mk b/target/product/embedded.mk
index 4162385..c0811ea 100644
--- a/target/product/embedded.mk
+++ b/target/product/embedded.mk
@@ -39,6 +39,7 @@
     libFFTEm \
     libGLESv1_CM \
     libGLESv2 \
+    libGLESv3 \
     libbinder \
     libc \
     libcutils \
diff --git a/target/product/runtime_libart.mk b/target/product/runtime_libart.mk
index c177981..6eba70a 100644
--- a/target/product/runtime_libart.mk
+++ b/target/product/runtime_libart.mk
@@ -22,6 +22,7 @@
     bouncycastle \
     cacerts \
     conscrypt \
+    core-oj \
     core-junit \
     core-libart \
     dalvikvm \
@@ -39,6 +40,7 @@
     libicui18n \
     libicuuc \
     libjavacore \
+    libopenjdk \
     libnativehelper \
     libssl \
     libz \
@@ -52,3 +54,4 @@
     dalvik.vm.dex2oat-Xms=64m \
     dalvik.vm.dex2oat-Xmx=512m \
     ro.dalvik.vm.native.bridge=0 \
+    dalvik.vm.usejit=true \
diff --git a/target/product/sdk_base.mk b/target/product/sdk_base.mk
index 73c2524..12a8da5 100644
--- a/target/product/sdk_base.mk
+++ b/target/product/sdk_base.mk
@@ -63,6 +63,7 @@
 	device/generic/goldfish/data/etc/apns-conf.xml:system/etc/apns-conf.xml \
 	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 \
diff --git a/target/product/sdk_phone_arm64.mk b/target/product/sdk_phone_arm64.mk
index a0cf6c1..1d13b9e 100644
--- a/target/product/sdk_phone_arm64.mk
+++ b/target/product/sdk_phone_arm64.mk
@@ -24,7 +24,7 @@
 $(call inherit-product, $(SRC_TARGET_DIR)/board/generic_arm64/device.mk)
 
 # Overrides
-PRODUCT_BRAND := generic_arm64
+PRODUCT_BRAND := Android
 PRODUCT_NAME := sdk_phone_arm64
 PRODUCT_DEVICE := generic_arm64
 PRODUCT_MODEL := Android SDK built for arm64
diff --git a/target/product/sdk_phone_armv7.mk b/target/product/sdk_phone_armv7.mk
index aeb4940..a0fa049 100644
--- a/target/product/sdk_phone_armv7.mk
+++ b/target/product/sdk_phone_armv7.mk
@@ -17,6 +17,6 @@
 $(call inherit-product, $(SRC_TARGET_DIR)/product/sdk_base.mk)
 
 # Overrides
-PRODUCT_BRAND := generic
+PRODUCT_BRAND := Android
 PRODUCT_NAME := sdk_phone_armv7
 PRODUCT_DEVICE := generic
diff --git a/target/product/sdk_phone_mips.mk b/target/product/sdk_phone_mips.mk
index 818491f..d7217a0 100644
--- a/target/product/sdk_phone_mips.mk
+++ b/target/product/sdk_phone_mips.mk
@@ -22,7 +22,7 @@
 $(call inherit-product, $(SRC_TARGET_DIR)/product/sdk_base.mk)
 
 # Overrides
-PRODUCT_BRAND := generic_mips
+PRODUCT_BRAND := Android
 PRODUCT_NAME := sdk_phone_mips
 PRODUCT_DEVICE := generic_mips
 PRODUCT_MODEL := Android SDK for Mips
diff --git a/target/product/sdk_phone_mips64.mk b/target/product/sdk_phone_mips64.mk
index afdb2a8..8ddcb58 100644
--- a/target/product/sdk_phone_mips64.mk
+++ b/target/product/sdk_phone_mips64.mk
@@ -23,7 +23,7 @@
 $(call inherit-product, $(SRC_TARGET_DIR)/product/sdk_base.mk)
 
 # Overrides
-PRODUCT_BRAND := generic_mips64
+PRODUCT_BRAND := Android
 PRODUCT_NAME := sdk_phone_mips64
 PRODUCT_DEVICE := generic_mips64
 PRODUCT_MODEL := Android SDK built for mips64
diff --git a/target/product/sdk_phone_x86.mk b/target/product/sdk_phone_x86.mk
index 95c49ab..a58d26f 100644
--- a/target/product/sdk_phone_x86.mk
+++ b/target/product/sdk_phone_x86.mk
@@ -22,7 +22,7 @@
 $(call inherit-product, $(SRC_TARGET_DIR)/product/sdk_base.mk)
 
 # Overrides
-PRODUCT_BRAND := generic_x86
+PRODUCT_BRAND := Android
 PRODUCT_NAME := sdk_phone_x86
 PRODUCT_DEVICE := generic_x86
 PRODUCT_MODEL := Android SDK built for x86
diff --git a/target/product/sdk_phone_x86_64.mk b/target/product/sdk_phone_x86_64.mk
index 69e37af..c39b274 100644
--- a/target/product/sdk_phone_x86_64.mk
+++ b/target/product/sdk_phone_x86_64.mk
@@ -23,7 +23,7 @@
 $(call inherit-product, $(SRC_TARGET_DIR)/product/sdk_base.mk)
 
 # Overrides
-PRODUCT_BRAND := generic_x86_64
+PRODUCT_BRAND := Android
 PRODUCT_NAME := sdk_phone_x86_64
 PRODUCT_DEVICE := generic_x86_64
 PRODUCT_MODEL := Android SDK built for x86_64
diff --git a/tools/Android.mk b/tools/Android.mk
index 30febd6..30a064f 100644
--- a/tools/Android.mk
+++ b/tools/Android.mk
@@ -27,6 +27,6 @@
 
 else # TARGET_BUILD_APPS
 
-include $(LOCAL_PATH)/apicheck/Android.mk
+include $(LOCAL_PATH)/apicheck/Android.mk $(LOCAL_PATH)/ijar/Android.mk
 
 endif
diff --git a/tools/buildinfo.sh b/tools/buildinfo.sh
index ece5103..dcb66bf 100755
--- a/tools/buildinfo.sh
+++ b/tools/buildinfo.sh
@@ -23,6 +23,9 @@
 if [ -n "$BOARD_BUILD_SYSTEM_ROOT_IMAGE" ] ; then
   echo "ro.build.system_root_image=$BOARD_BUILD_SYSTEM_ROOT_IMAGE"
 fi
+if [ -n "$AB_OTA_UPDATER" ] ; then
+  echo "ro.build.ab_update=$AB_OTA_UPDATER"
+fi
 echo "ro.product.model=$PRODUCT_MODEL"
 echo "ro.product.brand=$PRODUCT_BRAND"
 echo "ro.product.name=$PRODUCT_NAME"
diff --git a/tools/check_prereq/check_prereq.c b/tools/check_prereq/check_prereq.c
deleted file mode 100644
index d7b8918..0000000
--- a/tools/check_prereq/check_prereq.c
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * Copyright (C) 2009 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.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <sys/system_properties.h>
-#include <cutils/properties.h>
-
-// Compare the timestamp of the new build (passed on the command line)
-// against the current value of ro.build.date.utc.  Exit successfully
-// if the new build is newer than the current build (or if the
-// timestamps are the same).
-int main(int argc, char** argv) {
-  if (argc != 2) {
- usage:
-    fprintf(stderr, "usage: %s <timestamp>\n", argv[0]);
-    return 2;
-  }
-
-  char value[PROPERTY_VALUE_MAX];
-  char* default_value = "0";
-
-  property_get("ro.build.date.utc", value, default_value);
-
-  long current = strtol(value, NULL, 10);
-  char* end;
-  long install = strtol(argv[1], &end, 10);
-
-  printf("current build time: [%ld]  new build time: [%ld]\n",
-         current, install);
-
-  return (*end == 0 && current > 0 && install >= current) ? 0 : 1;
-}
diff --git a/tools/droiddoc/templates-ds/class.cs b/tools/droiddoc/templates-ds/class.cs
index 23e57ab..d82f1c1 100644
--- a/tools/droiddoc/templates-ds/class.cs
+++ b/tools/droiddoc/templates-ds/class.cs
@@ -184,7 +184,6 @@
     <tr class="<?cs if:count % #2 ?>alt-color<?cs /if ?> api apilevel-<?cs var:method.since ?>" >
         <td class="jd-typecol"><nobr>
             <?cs var:method.abstract ?>
-            <?cs var:method.synchronized ?>
             <?cs var:method.final ?>
             <?cs var:method.static ?>
             <?cs call:type_link(method.generic) ?>
@@ -535,7 +534,6 @@
         <?cs var:method.static ?> 
         <?cs var:method.final ?> 
         <?cs var:method.abstract ?> 
-        <?cs var:method.synchronized ?> 
         <?cs call:type_link(method.returnType) ?>
       </span>
       <span class="sympad"><?cs var:method.name ?></span>
diff --git a/tools/droiddoc/templates-ndk/class.cs b/tools/droiddoc/templates-ndk/class.cs
index 7aa99f9..e033fa2 100644
--- a/tools/droiddoc/templates-ndk/class.cs
+++ b/tools/droiddoc/templates-ndk/class.cs
@@ -188,7 +188,6 @@
     <tr class="<?cs if:count % #2 ?>alt-color<?cs /if ?> api apilevel-<?cs var:method.since ?>" >
         <td class="jd-typecol"><nobr>
             <?cs var:method.abstract ?>
-            <?cs var:method.synchronized ?>
             <?cs var:method.final ?>
             <?cs var:method.static ?>
             <?cs call:type_link(method.generic) ?>
@@ -558,7 +557,6 @@
         <?cs var:method.static ?> 
         <?cs var:method.final ?> 
         <?cs var:method.abstract ?> 
-        <?cs var:method.synchronized ?> 
         <?cs call:type_link(method.returnType) ?>
       </span>
       <span class="sympad"><?cs var:method.name ?></span>
diff --git a/tools/droiddoc/templates-sac/assets/css/default.css b/tools/droiddoc/templates-sac/assets/css/default.css
index 0ee8bc2..c349c89 100644
--- a/tools/droiddoc/templates-sac/assets/css/default.css
+++ b/tools/droiddoc/templates-sac/assets/css/default.css
@@ -1511,7 +1511,7 @@
 .devices a.selected {
     color: #F80;
 }
-.accessories a.selected {
+.security a.selected {
     color: #9C0;
 }
 .compatibility a.selected {
diff --git a/tools/droiddoc/templates-sac/assets/js/docs.js b/tools/droiddoc/templates-sac/assets/js/docs.js
index d3a9223..0c93780 100644
--- a/tools/droiddoc/templates-sac/assets/js/docs.js
+++ b/tools/droiddoc/templates-sac/assets/js/docs.js
@@ -1871,9 +1871,9 @@
   devicesSearcher.setUserDefinedLabel("Devices");
   devicesSearcher.setSiteRestriction("http://source.android.com/devices/");
 
-  accessoriesSearcher = new google.search.WebSearch();
-  accessoriesSearcher.setUserDefinedLabel("Accessories");
-  accessoriesSearcher.setSiteRestriction("http://source.android.com/accessories/");
+  securitySearcher = new google.search.WebSearch();
+  securitySearcher.setUserDefinedLabel("Security");
+  securitySearcher.setSiteRestriction("http://source.android.com/security/");
 
   compatibilitySearcher = new google.search.WebSearch();
   compatibilitySearcher.setUserDefinedLabel("Compatibility");
@@ -1883,7 +1883,7 @@
   searchControl.addSearcher(sacSiteSearcher, searchOptions);
   searchControl.addSearcher(sourceSearcher, searchOptions);
   searchControl.addSearcher(devicesSearcher, searchOptions);
-  searchControl.addSearcher(accessoriesSearcher, searchOptions);
+  searchControl.addSearcher(securitySearcher, searchOptions);
   searchControl.addSearcher(compatibilitySearcher, searchOptions);
 
 
diff --git a/tools/droiddoc/templates-sac/class.cs b/tools/droiddoc/templates-sac/class.cs
index 4003ff5..440e705 100644
--- a/tools/droiddoc/templates-sac/class.cs
+++ b/tools/droiddoc/templates-sac/class.cs
@@ -185,7 +185,6 @@
     <tr class="<?cs if:count % #2 ?>alt-color<?cs /if ?> api apilevel-<?cs var:method.since ?>" >
         <td class="jd-typecol"><nobr>
             <?cs var:method.abstract ?>
-            <?cs var:method.synchronized ?>
             <?cs var:method.final ?>
             <?cs var:method.static ?>
             <?cs call:type_link(method.generic) ?>
@@ -539,7 +538,6 @@
         <?cs var:method.static ?> 
         <?cs var:method.final ?> 
         <?cs var:method.abstract ?> 
-        <?cs var:method.synchronized ?> 
         <?cs call:type_link(method.returnType) ?>
       </span>
       <span class="sympad"><?cs var:method.name ?></span>
diff --git a/tools/droiddoc/templates-sac/components/masthead.cs b/tools/droiddoc/templates-sac/components/masthead.cs
index a8618c0..ee2582f 100644
--- a/tools/droiddoc/templates-sac/components/masthead.cs
+++ b/tools/droiddoc/templates-sac/components/masthead.cs
@@ -255,7 +255,7 @@
           <a href="<?cs var:toroot ?>index.html">
             <img src="<?cs var:toroot ?>assets/images/sac_logo.png"
                 srcset="<?cs var:toroot ?>assets/images/sac_logo@2x.png 2x"
-                width="123" height="25" alt="Android Developers" />
+                width="123" height="25" alt="Android Open Source Project" />
           </a>
           </div>
             <ul class="nav-x col-9">
@@ -266,9 +266,9 @@
                 <li class="devices"><a href="<?cs var:toroot ?>devices/index.html" <?cs
                   if:devices ?>class="selected"<?cs /if ?>
                   >Devices</a></li>
-                <li class="accessories"><a href="<?cs var:toroot ?>accessories/index.html" <?cs
-                  if:accessories ?>class="selected"<?cs /if ?>
-                  >Accessories</a></li>
+                <li class="security"><a href="<?cs var:toroot ?>security/index.html" <?cs
+                  if:security ?>class="selected"<?cs /if ?>
+                  >Security</a></li>
                 <li class="compatibility last"><a href="<?cs var:toroot ?>compatibility/index.html" <?cs
                   if:compatibility ?>class="selected"<?cs /if ?>
                   >Compatibility</a></li>
diff --git a/tools/droiddoc/templates-sac/customizations.cs b/tools/droiddoc/templates-sac/customizations.cs
index e489b63..1120e70 100644
--- a/tools/droiddoc/templates-sac/customizations.cs
+++ b/tools/droiddoc/templates-sac/customizations.cs
@@ -382,8 +382,8 @@
     <?cs call:compatibility_nav() ?>
   <?cs elif:source ?>
     <?cs call:source_nav() ?>
-  <?cs elif:accessories ?>
-    <?cs call:accessories_nav() ?>
+  <?cs elif:security ?>
+    <?cs call:security_nav() ?>
   <?cs elif:reference ?>
     <?cs call:default_left_nav() ?>
   <?cs /if ?>
@@ -450,14 +450,14 @@
 <?cs /def ?>
 
 <?cs
-def:accessories_nav() ?>
+def:security_nav() ?>
   <div class="wrap clearfix" id="body-content">
     <div class="col-4" id="side-nav" itemscope itemtype="http://schema.org/SiteNavigationElement">
       <div id="devdoc-nav" class="scroll-pane">
 <a class="totop" href="#top" data-g-event="left-nav-top">to top</a>
 
 <?cs 
-        include:"../../../../docs/source.android.com/src/accessories/accessories_toc.cs" ?>
+        include:"../../../../docs/source.android.com/src/security/security_toc.cs" ?>
 
       </div>
     </div> <!-- end side-nav -->
diff --git a/tools/droiddoc/templates-sdk-dev/class.cs b/tools/droiddoc/templates-sdk-dev/class.cs
index 693eaed..649aaff 100644
--- a/tools/droiddoc/templates-sdk-dev/class.cs
+++ b/tools/droiddoc/templates-sdk-dev/class.cs
@@ -190,7 +190,6 @@
     <tr class="<?cs if:count % #2 ?>alt-color<?cs /if ?> api apilevel-<?cs var:method.since ?>" >
         <td class="jd-typecol"><nobr>
             <?cs var:method.abstract ?>
-            <?cs var:method.synchronized ?>
             <?cs var:method.final ?>
             <?cs var:method.static ?>
             <?cs call:type_link(method.generic) ?>
@@ -560,7 +559,6 @@
         <?cs var:method.static ?> 
         <?cs var:method.final ?> 
         <?cs var:method.abstract ?> 
-        <?cs var:method.synchronized ?> 
         <?cs call:type_link(method.returnType) ?>
       </span>
       <span class="sympad"><?cs var:method.name ?></span>
diff --git a/tools/droiddoc/templates-sdk/class.cs b/tools/droiddoc/templates-sdk/class.cs
index 44eae97..93fcf88 100644
--- a/tools/droiddoc/templates-sdk/class.cs
+++ b/tools/droiddoc/templates-sdk/class.cs
@@ -188,7 +188,6 @@
     <tr class="<?cs if:count % #2 ?>alt-color<?cs /if ?> api apilevel-<?cs var:method.since ?>" >
         <td class="jd-typecol"><nobr>
             <?cs var:method.abstract ?>
-            <?cs var:method.synchronized ?>
             <?cs var:method.final ?>
             <?cs var:method.static ?>
             <?cs call:type_link(method.generic) ?>
@@ -558,7 +557,6 @@
         <?cs var:method.static ?> 
         <?cs var:method.final ?> 
         <?cs var:method.abstract ?> 
-        <?cs var:method.synchronized ?> 
         <?cs call:type_link(method.returnType) ?>
       </span>
       <span class="sympad"><?cs var:method.name ?></span>
diff --git a/tools/droiddoc/templates-sdk/components/masthead.cs b/tools/droiddoc/templates-sdk/components/masthead.cs
index 838ddbe..9db0466 100644
--- a/tools/droiddoc/templates-sdk/components/masthead.cs
+++ b/tools/droiddoc/templates-sdk/components/masthead.cs
@@ -80,7 +80,7 @@
         <?cs call:header_search_widget() ?>
         <?cs /if ?>
 
-        <?cs if:ndk ?><a class="dac-header-console-btn" href="http://developer.android.com">
+        <?cs if:ndk ?><a class="dac-header-console-btn" href="//developer.android.com">
           <span class="dac-visible-desktop-inline">Back to Android Developers</span>
         </a><?cs else ?><a class="dac-header-console-btn" href="https://play.google.com/apps/publish/">
           <span class="dac-sprite dac-google-play"></span>
@@ -281,7 +281,7 @@
     <div style="height:20px"><!-- spacer to bump header down --></div>
     <div id="butterbar-wrapper">
       <div id="butterbar">
-        <a href="http://googleblog.blogspot.com/" id="butterbar-message">
+        <a href="//googleblog.blogspot.com/" id="butterbar-message">
           The Android 5.0 SDK will be available on October 17th!
         </a>
       </div>
diff --git a/tools/droiddoc/templates-sdk/customizations.cs b/tools/droiddoc/templates-sdk/customizations.cs
index aaef8ed..d52f0e4 100644
--- a/tools/droiddoc/templates-sdk/customizations.cs
+++ b/tools/droiddoc/templates-sdk/customizations.cs
@@ -585,7 +585,7 @@
 <?cs # appears at the bottom of every page ?><?cs
 def:custom_cc_copyright() ?>
   Except as noted, this content is
-  licensed under <a href="http://creativecommons.org/licenses/by/2.5/">
+  licensed under <a href="//creativecommons.org/licenses/by/2.5/">
   Creative Commons Attribution 2.5</a>. For details and
   restrictions, see the <a href="<?cs var:toroot ?>license.html">Content
   License</a>.<?cs
@@ -594,7 +594,7 @@
 <?cs
 def:custom_copyright() ?>
   Except as noted, this content is licensed under <a
-  href="http://www.apache.org/licenses/LICENSE-2.0">Apache 2.0</a>.
+  href="//www.apache.org/licenses/LICENSE-2.0">Apache 2.0</a>.
   For details and restrictions, see the <a href="<?cs var:toroot ?>license.html">
   Content License</a>.<?cs
 /def ?>
diff --git a/tools/droiddoc/templates-sdk/designpage.cs b/tools/droiddoc/templates-sdk/designpage.cs
index b945a1c..4a523ce 100644
--- a/tools/droiddoc/templates-sdk/designpage.cs
+++ b/tools/droiddoc/templates-sdk/designpage.cs
@@ -7,7 +7,7 @@
       Android Design<?cs if:page.title ?> - <?cs var:page.title ?><?cs /if ?>
     </title>
     <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">
-    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic">
+    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:regular,medium,thin,italic,mediumitalic">
     <link rel="stylesheet" href="<?cs var:toroot ?>assets/yui-3.3.0-reset-min.css">
     <link rel="stylesheet" href="<?cs var:toroot ?>assets/design/default.css">
     <script src="<?cs var:toroot ?>assets/jquery-1.6.2.min.js"></script>
@@ -70,16 +70,16 @@
 
         <p id="copyright">
           Except as noted, this content is licensed under
-          <a href="http://creativecommons.org/licenses/by/2.5/">
+          <a href="//creativecommons.org/licenses/by/2.5/">
           Creative Commons Attribution 2.5</a>.<br>
           For details and restrictions, see the
-          <a href="http://developer.android.com/license.html">Content License</a>.
+          <a href="//developer.android.com/license.html">Content License</a>.
         </p>
 
         <p>
-          <a href="http://www.android.com/terms.html">Site Terms of Service</a> &ndash;
-          <a href="http://www.android.com/privacy.html">Privacy Policy</a> &ndash;
-          <a href="http://www.android.com/branding.html">Brand Guidelines</a>
+          <a href="//www.android.com/terms.html">Site Terms of Service</a> &ndash;
+          <a href="//www.android.com/privacy.html">Privacy Policy</a> &ndash;
+          <a href="//www.android.com/branding.html">Brand Guidelines</a>
         </p>
 
       </div>
diff --git a/tools/droiddoc/templates-sdk/footer.cs b/tools/droiddoc/templates-sdk/footer.cs
index 1ffee63..a0900f2 100644
--- a/tools/droiddoc/templates-sdk/footer.cs
+++ b/tools/droiddoc/templates-sdk/footer.cs
@@ -7,7 +7,7 @@
       </div>
       <div class="col-1of2 dac-footer-reachout">
         <div class="dac-footer-contact">
-          <a class="dac-footer-contact-link" href="http://android-developers.blogspot.com/">Blog</a>
+          <a class="dac-footer-contact-link" href="//android-developers.blogspot.com/">Blog</a>
           <a class="dac-footer-contact-link" href="/support.html">Support</a>
         </div>
         <div class="dac-footer-social">
diff --git a/tools/droiddoc/templates-sdk/head_tag.cs b/tools/droiddoc/templates-sdk/head_tag.cs
index babb3c7..c668203 100644
--- a/tools/droiddoc/templates-sdk/head_tag.cs
+++ b/tools/droiddoc/templates-sdk/head_tag.cs
@@ -30,15 +30,15 @@
 <!-- STYLESHEETS -->
 <link rel="stylesheet"
 href="<?cs
-if:android.whichdoc != 'online' ?>http:<?cs
+if:android.whichdoc != 'online' ?>https:<?cs
 /if ?>//fonts.googleapis.com/css?family=Roboto+Condensed">
 <link rel="stylesheet" href="<?cs
-if:android.whichdoc != 'online' ?>http:<?cs
+if:android.whichdoc != 'online' ?>https:<?cs
 /if ?>//fonts.googleapis.com/css?family=Roboto:light,regular,medium,thin,italic,mediumitalic,bold"
   title="roboto">
 <?cs 
   if:ndk ?><link rel="stylesheet" href="<?cs
-  if:android.whichdoc != 'online' ?>http:<?cs
+  if:android.whichdoc != 'online' ?>https:<?cs
   /if ?>//fonts.googleapis.com/css?family=Roboto+Mono:400,500,700" title="roboto-mono" type="text/css"><?cs
 /if ?>
 <link href="<?cs var:toroot ?>assets/css/default.css?v=7" rel="stylesheet" type="text/css">
@@ -50,7 +50,7 @@
 <?cs /if ?>
 
 <!-- JAVASCRIPT -->
-<script src="<?cs if:android.whichdoc != 'online' ?>http:<?cs /if ?>//www.google.com/jsapi" type="text/javascript"></script>
+<script src="<?cs if:android.whichdoc != 'online' ?>https:<?cs /if ?>//www.google.com/jsapi" type="text/javascript"></script>
 <?cs
 if:devsite
   ?><script src="<?cs var:toroot ?>_static/js/android_3p-bundle.js" type="text/javascript"></script><?cs
diff --git a/tools/droiddoc/templates-sdk/sdkpage.cs b/tools/droiddoc/templates-sdk/sdkpage.cs
index 47c2992..401f183 100644
--- a/tools/droiddoc/templates-sdk/sdkpage.cs
+++ b/tools/droiddoc/templates-sdk/sdkpage.cs
@@ -86,7 +86,7 @@
     <td>Windows 32-bit</td>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.win32_download ?>"><?cs var:ndk.win32_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.win32_download ?>"><?cs var:ndk.win32_download ?></a>
     </td>
     <td><?cs var:ndk.win32_bytes ?></td>
     <td><?cs var:ndk.win32_checksum ?></td>
@@ -94,7 +94,7 @@
  <!-- <tr>
    <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.win32.legacy_download ?>"><?cs var:ndk.win32.legacy_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.win32.legacy_download ?>"><?cs var:ndk.win32.legacy_download ?></a>
     </td>
     <td><?cs var:ndk.win32.legacy_bytes ?></td>
     <td><?cs var:ndk.win32.legacy_checksum ?></td>
@@ -103,7 +103,7 @@
     <td>Windows 64-bit</td>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.win64_download ?>"><?cs var:ndk.win64_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.win64_download ?>"><?cs var:ndk.win64_download ?></a>
     </td>
     <td><?cs var:ndk.win64_bytes ?></td>
     <td><?cs var:ndk.win64_checksum ?></td>
@@ -111,7 +111,7 @@
  <!--  <tr>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.win64.legacy_download ?>"><?cs var:ndk.win64.legacy_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.win64.legacy_download ?>"><?cs var:ndk.win64.legacy_download ?></a>
     </td>
     <td><?cs var:ndk.win64.legacy_bytes ?></td>
     <td><?cs var:ndk.win64.legacy_checksum ?></td>
@@ -121,7 +121,7 @@
     <td>Mac OS X 32-bit</td>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.mac32_download ?>"><?cs var:ndk.mac32_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.mac32_download ?>"><?cs var:ndk.mac32_download ?></a>
     </td>
     <td><?cs var:ndk.mac32_bytes ?></td>
     <td><?cs var:ndk.mac32_checksum ?></td>
@@ -130,7 +130,7 @@
   <tr>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.mac32.legacy_download ?>"><?cs var:ndk.mac32.legacy_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.mac32.legacy_download ?>"><?cs var:ndk.mac32.legacy_download ?></a>
     </td>
     <td><?cs var:ndk.mac32.legacy_bytes ?></td>
     <td><?cs var:ndk.mac32.legacy_checksum ?></td>
@@ -138,7 +138,7 @@
     <td>Mac OS X 64-bit</td>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.mac64_download ?>"><?cs var:ndk.mac64_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.mac64_download ?>"><?cs var:ndk.mac64_download ?></a>
     </td>
     <td><?cs var:ndk.mac64_bytes ?></td>
     <td><?cs var:ndk.mac64_checksum ?></td>
@@ -146,7 +146,7 @@
  <!--  <tr>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.mac64.legacy_download ?>"><?cs var:ndk.mac64.legacy_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.mac64.legacy_download ?>"><?cs var:ndk.mac64.legacy_download ?></a>
     </td>
     <td><?cs var:ndk.mac64.legacy_bytes ?></td>
     <td><?cs var:ndk.mac64.legacy_checksum ?></td>
@@ -155,7 +155,7 @@
     <td>Linux 32-bit (x86)</td>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.linux32_download ?>"><?cs var:ndk.linux32_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.linux32_download ?>"><?cs var:ndk.linux32_download ?></a>
     </td>
     <td><?cs var:ndk.linux32_bytes ?></td>
     <td><?cs var:ndk.linux32_checksum ?></td>
@@ -163,7 +163,7 @@
  <!--  <tr>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.linux32.legacy_download ?>"><?cs var:ndk.linux32.legacy_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.linux32.legacy_download ?>"><?cs var:ndk.linux32.legacy_download ?></a>
     </td>
     <td><?cs var:ndk.linux32.legacy_bytes ?></td>
     <td><?cs var:ndk.linux32.legacy_checksum ?></td>
@@ -172,7 +172,7 @@
     <td>Linux 64-bit (x86)</td>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.linux64_download ?>"><?cs var:ndk.linux64_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.linux64_download ?>"><?cs var:ndk.linux64_download ?></a>
     </td>
     <td><?cs var:ndk.linux64_bytes ?></td>
     <td><?cs var:ndk.linux64_checksum ?></td>
@@ -180,7 +180,7 @@
   <!--  <tr>
     <td>
   <a onClick="return onDownload(this)"
-     href="http://dl.google.com/android/ndk/<?cs var:ndk.linux64.legacy_download ?>"><?cs var:ndk.linux64.legacy_download ?></a>
+     href="//dl.google.com/android/ndk/<?cs var:ndk.linux64.legacy_download ?>"><?cs var:ndk.linux64.legacy_download ?></a>
     </td>
     <td><?cs var:ndk.linux64.legacy_bytes ?></td>
     <td><?cs var:ndk.linux64.legacy_checksum ?></td>
@@ -286,7 +286,7 @@
   <tr>
     <td rowspan="2">Windows</td>
     <td>
-  <a onclick="return onDownload(this)" id="win-tools" href="http://dl.google.com/android/<?cs
+  <a onclick="return onDownload(this)" id="win-tools" href="//dl.google.com/android/<?cs
 var:sdk.win_installer
 ?>"><?cs var:sdk.win_installer ?></a> (Recommended)
     </td>
@@ -296,7 +296,7 @@
   <tr>
     <!-- blank TD from Windows rowspan -->
     <td>
-  <a onclick="return onDownload(this)" href="http://dl.google.com/android/<?cs var:sdk.win_download
+  <a onclick="return onDownload(this)" href="//dl.google.com/android/<?cs var:sdk.win_download
 ?>"><?cs var:sdk.win_download ?></a>
     </td>
     <td><?cs var:sdk.win_bytes ?> bytes</td>
@@ -305,7 +305,7 @@
   <tr>
     <td><nobr>Mac OS X</nobr></td>
     <td>
-  <a onclick="return onDownload(this)" id="mac-tools" href="http://dl.google.com/android/<?cs
+  <a onclick="return onDownload(this)" id="mac-tools" href="//dl.google.com/android/<?cs
 var:sdk.mac_download
 ?>"><?cs var:sdk.mac_download ?></a>
     </td>
@@ -315,7 +315,7 @@
   <tr>
     <td>Linux</td>
     <td>
-  <a onclick="return onDownload(this)" id="linux-tools" href="http://dl.google.com/android/<?cs
+  <a onclick="return onDownload(this)" id="linux-tools" href="//dl.google.com/android/<?cs
 var:sdk.linux_download
 ?>"><?cs var:sdk.linux_download ?></a>
     </td>
diff --git a/tools/generate-notice-files.py b/tools/generate-notice-files.py
index 4571b70..5b13bf5 100755
--- a/tools/generate-notice-files.py
+++ b/tools/generate-notice-files.py
@@ -99,7 +99,7 @@
     # most browsers, but href's to table row ids do)
     id_table = {}
     id_count = 0
-    for value in file_hash.values():
+    for value in file_hash:
         for filename in value:
              id_table[filename] = id_count
         id_count += 1
@@ -116,7 +116,7 @@
     print >> output_file, "<ul>"
 
     # Flatten the list of lists into a single list of filenames
-    sorted_filenames = sorted(itertools.chain.from_iterable(file_hash.values()))
+    sorted_filenames = sorted(itertools.chain.from_iterable(file_hash))
 
     # Print out a nice table of contents
     for filename in sorted_filenames:
@@ -127,11 +127,11 @@
     print >> output_file, "</div><!-- table of contents -->"
     # Output the individual notice file lists
     print >>output_file, '<table cellpadding="0" cellspacing="0" border="0">'
-    for value in file_hash.values():
+    for value in file_hash:
         print >> output_file, '<tr id="id%d"><td class="same-license">' % id_table.get(value[0])
         print >> output_file, '<div class="label">Notices for file(s):</div>'
         print >> output_file, '<div class="file-list">'
-        for filename in sorted(value):
+        for filename in value:
             print >> output_file, "%s <br/>" % (SRC_DIR_STRIP_RE.sub(r"\1", filename))
         print >> output_file, "</div><!-- file-list -->"
         print >> output_file
@@ -154,10 +154,10 @@
     SRC_DIR_STRIP_RE = re.compile(input_dir + "(/.*).txt")
     output_file = open(output_filename, "wb")
     print >> output_file, file_title
-    for value in file_hash.values():
+    for value in file_hash:
       print >> output_file, "============================================================"
       print >> output_file, "Notices for file(s):"
-      for filename in sorted(value):
+      for filename in value:
         print >> output_file, SRC_DIR_STRIP_RE.sub(r"\1", filename)
       print >> output_file, "------------------------------------------------------------"
       print >> output_file, open(value[0]).read()
@@ -178,11 +178,12 @@
                 file_md5sum = md5sum(filename)
                 files_with_same_hash[file_md5sum].append(filename)
 
+    filesets = [sorted(files_with_same_hash[md5]) for md5 in sorted(files_with_same_hash.keys())]
 
     print "Combining NOTICE files into HTML"
-    combine_notice_files_html(files_with_same_hash, input_dir, html_output_file)
+    combine_notice_files_html(filesets, input_dir, html_output_file)
     print "Combining NOTICE files into text"
-    combine_notice_files_text(files_with_same_hash, input_dir, txt_output_file, file_title)
+    combine_notice_files_text(filesets, input_dir, txt_output_file, file_title)
 
 if __name__ == "__main__":
     main(args)
diff --git a/tools/ijar/Android.mk b/tools/ijar/Android.mk
new file mode 100644
index 0000000..8b2a02c
--- /dev/null
+++ b/tools/ijar/Android.mk
@@ -0,0 +1,16 @@
+# 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
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_CPP_EXTENSION := .cc
+LOCAL_SRC_FILES := classfile.cc ijar.cc zip.cc
+LOCAL_CFLAGS += -Wall -Werror
+LOCAL_SHARED_LIBRARIES := libz-host
+LOCAL_MODULE := ijar
+# libc++ is not supported for TARGET_BUILD_APPS builds
+LOCAL_CXX_STL := libstdc++
+include $(BUILD_HOST_EXECUTABLE)
diff --git a/tools/ijar/LICENSE b/tools/ijar/LICENSE
new file mode 100644
index 0000000..6b0b127
--- /dev/null
+++ b/tools/ijar/LICENSE
@@ -0,0 +1,203 @@
+
+                                 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
new file mode 100644
index 0000000..d5a6a0f
--- /dev/null
+++ b/tools/ijar/README.txt
@@ -0,0 +1,120 @@
+
+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
new file mode 100644
index 0000000..e0cf42e
--- /dev/null
+++ b/tools/ijar/classfile.cc
@@ -0,0 +1,1788 @@
+// 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 {
+
+  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
+{
+  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
+{
+  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
+{
+  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
new file mode 100644
index 0000000..118041b
--- /dev/null
+++ b/tools/ijar/common.h
@@ -0,0 +1,102 @@
+// 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
new file mode 100644
index 0000000..1925b48
--- /dev/null
+++ b/tools/ijar/ijar.cc
@@ -0,0 +1,182 @@
+// 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
new file mode 100644
index 0000000..ca5f396
--- /dev/null
+++ b/tools/ijar/zip.cc
@@ -0,0 +1,1031 @@
+// 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 *)"";
+
+  // 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
new file mode 100644
index 0000000..dda2c6e
--- /dev/null
+++ b/tools/ijar/zip.h
@@ -0,0 +1,173 @@
+// 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
new file mode 100644
index 0000000..3f4a50c
--- /dev/null
+++ b/tools/ijar/zip_main.cc
@@ -0,0 +1,312 @@
+// 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/releasetools/add_img_to_target_files.py b/tools/releasetools/add_img_to_target_files.py
index b6cee63..7cb9072 100755
--- a/tools/releasetools/add_img_to_target_files.py
+++ b/tools/releasetools/add_img_to_target_files.py
@@ -319,6 +319,23 @@
   banner("cache")
   AddCache(output_zip)
 
+  # For devices using A/B update, copy over images from RADIO/ to IMAGES/ and
+  # make sure we have all the needed images ready under IMAGES/.
+  ab_partitions = os.path.join(OPTIONS.input_tmp, "META", "ab_partitions.txt")
+  if os.path.exists(ab_partitions):
+    with open(ab_partitions, 'r') as f:
+      lines = f.readlines()
+    for line in lines:
+      img_name = line.strip() + ".img"
+      img_radio_path = os.path.join(OPTIONS.input_tmp, "RADIO", img_name)
+      if os.path.exists(img_radio_path):
+        common.ZipWrite(output_zip, img_radio_path,
+                        os.path.join("IMAGES", img_name))
+
+      # Zip spec says: All slashes MUST be forward slashes.
+      img_path = 'IMAGES/' + img_name
+      assert img_path in output_zip.namelist(), "cannot find " + img_name
+
   common.ZipClose(output_zip)
 
 def main(argv):
diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py
index 1295230..ee0cdf9 100644
--- a/tools/releasetools/common.py
+++ b/tools/releasetools/common.py
@@ -44,6 +44,7 @@
 
     self.search_path = platform_search_path.get(sys.platform, None)
     self.signapk_path = "framework/signapk.jar"  # Relative to search_path
+    self.signapk_shared_library_path = "lib64"   # Relative to search_path
     self.extra_signapk_args = []
     self.java_path = "java"  # Use the one on the path by default.
     self.java_args = "-Xmx2048m" # JVM Args
@@ -211,8 +212,11 @@
   makeint("boot_size")
   makeint("fstab_version")
 
-  d["fstab"] = LoadRecoveryFSTab(read_helper, d["fstab_version"],
-                                 d.get("system_root_image", False))
+  if d.get("no_recovery", False) == "true":
+    d["fstab"] = None
+  else:
+    d["fstab"] = LoadRecoveryFSTab(read_helper, d["fstab_version"],
+                                   d.get("system_root_image", False))
   d["build.prop"] = LoadBuildProp(read_helper)
   return d
 
@@ -585,37 +589,29 @@
   return key_passwords
 
 
-def SignFile(input_name, output_name, key, password, align=None,
-             whole_file=False):
+def SignFile(input_name, output_name, key, password, whole_file=False):
   """Sign the input_name zip/jar/apk, producing output_name.  Use the
   given key and password (the latter may be None if the key does not
   have a password.
 
-  If align is an integer > 1, zipalign is run to align stored files in
-  the output zip on 'align'-byte boundaries.
-
   If whole_file is true, use the "-w" option to SignApk to embed a
   signature that covers the whole file in the archive comment of the
   zip file.
   """
 
-  if align == 0 or align == 1:
-    align = None
+  java_library_path = os.path.join(
+      OPTIONS.search_path, OPTIONS.signapk_shared_library_path)
 
-  if align:
-    temp = tempfile.NamedTemporaryFile()
-    sign_name = temp.name
-  else:
-    sign_name = output_name
-
-  cmd = [OPTIONS.java_path, OPTIONS.java_args, "-jar",
+  cmd = [OPTIONS.java_path, OPTIONS.java_args,
+         "-Djava.library.path=" + java_library_path,
+         "-jar",
          os.path.join(OPTIONS.search_path, OPTIONS.signapk_path)]
   cmd.extend(OPTIONS.extra_signapk_args)
   if whole_file:
     cmd.append("-w")
   cmd.extend([key + OPTIONS.public_key_suffix,
               key + OPTIONS.private_key_suffix,
-              input_name, sign_name])
+              input_name, output_name])
 
   p = Run(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
   if password is not None:
@@ -624,13 +620,6 @@
   if p.returncode != 0:
     raise ExternalError("signapk.jar failed: return code %s" % (p.returncode,))
 
-  if align:
-    p = Run(["zipalign", "-f", "-p", str(align), sign_name, output_name])
-    p.communicate()
-    if p.returncode != 0:
-      raise ExternalError("zipalign failed: return code %s" % (p.returncode,))
-    temp.close()
-
 
 def CheckSize(data, target, info_dict):
   """Check the data string passed against the max size limit, if
@@ -735,7 +724,8 @@
   try:
     opts, args = getopt.getopt(
         argv, "hvp:s:x:" + extra_opts,
-        ["help", "verbose", "path=", "signapk_path=", "extra_signapk_args=",
+        ["help", "verbose", "path=", "signapk_path=",
+         "signapk_shared_library_path=", "extra_signapk_args=",
          "java_path=", "java_args=", "public_key_suffix=",
          "private_key_suffix=", "boot_signer_path=", "boot_signer_args=",
          "verity_signer_path=", "verity_signer_args=", "device_specific=",
@@ -756,6 +746,8 @@
       OPTIONS.search_path = a
     elif o in ("--signapk_path",):
       OPTIONS.signapk_path = a
+    elif o in ("--signapk_shared_library_path",):
+      OPTIONS.signapk_shared_library_path = a
     elif o in ("--extra_signapk_args",):
       OPTIONS.extra_signapk_args = shlex.split(a)
     elif o in ("--java_path",):
@@ -1085,6 +1077,9 @@
     processor."""
     return self._DoCall("IncrementalOTA_InstallEnd")
 
+  def VerifyOTA_Assertions(self):
+    return self._DoCall("VerifyOTA_Assertions")
+
 class File(object):
   def __init__(self, name, data):
     self.name = name
@@ -1279,6 +1274,25 @@
     self._WriteUpdate(script, output_zip)
     self._WritePostInstallVerifyScript(script)
 
+  def WriteStrictVerifyScript(self, script):
+    """Verify all the blocks in the care_map, including clobbered blocks.
+
+    This differs from the WriteVerifyScript() function: a) it prints different
+    error messages; b) it doesn't allow half-way updated images to pass the
+    verification."""
+
+    partition = self.partition
+    script.Print("Verifying %s..." % (partition,))
+    ranges = self.tgt.care_map
+    ranges_str = ranges.to_string_raw()
+    script.AppendExtra('range_sha1("%s", "%s") == "%s" && '
+                       'ui_print("    Verified.") || '
+                       'ui_print("\\"%s\\" has unexpected contents.");' % (
+                       self.device, ranges_str,
+                       self.tgt.TotalSha1(include_clobbered_blocks=True),
+                       self.device))
+    script.AppendExtra("")
+
   def WriteVerifyScript(self, script):
     partition = self.partition
     if not self.src:
diff --git a/tools/releasetools/edify_generator.py b/tools/releasetools/edify_generator.py
index a4fc0b3..d923cc8 100644
--- a/tools/releasetools/edify_generator.py
+++ b/tools/releasetools/edify_generator.py
@@ -152,6 +152,15 @@
         "".join([', "%s"' % (i,) for i in sha1]) +
         ') || abort("\\"%s\\" has unexpected contents.");' % (filename,))
 
+  def Verify(self, filename):
+    """Check that the given file (or MTD reference) has one of the
+    given hashes (encoded in the filename)."""
+    self.script.append(
+        'apply_patch_check("{filename}") && '
+        'ui_print("    Verified.") || '
+        'ui_print("\\"{filename}\\" has unexpected contents.");'.format(
+            filename=filename))
+
   def FileCheck(self, filename, *sha1):
     """Check that the given file (or MTD reference) has one of the
     given *sha1 hashes."""
@@ -275,8 +284,8 @@
     cmd = ['apply_patch("%s",\0"%s",\0%s,\0%d'
            % (srcfile, tgtfile, tgtsha1, tgtsize)]
     for i in range(0, len(patchpairs), 2):
-      cmd.append(',\0%s, package_extract_file("%s")' % patchpairs[i:i+2])
-    cmd.append(');')
+      cmd.append(',\0%s,\0package_extract_file("%s")' % patchpairs[i:i+2])
+    cmd.append(') ||\n    abort("Failed to apply patch to %s");' % (srcfile,))
     cmd = "".join(cmd)
     self.script.append(self.WordWrap(cmd))
 
diff --git a/tools/releasetools/ota_from_target_files.py b/tools/releasetools/ota_from_target_files.py
index e019b1e..5259ede 100755
--- a/tools/releasetools/ota_from_target_files.py
+++ b/tools/releasetools/ota_from_target_files.py
@@ -91,6 +91,14 @@
   --stash_threshold <float>
       Specifies the threshold that will be used to compute the maximum
       allowed stash size (defaults to 0.8).
+
+  --gen_verify
+      Generate an OTA package that verifies the partitions.
+
+  --log_diff <file>
+      Generate a log file that shows the differences in the source and target
+      builds for an incremental package. This option is only meaningful when
+      -i is specified.
 """
 
 import sys
@@ -133,6 +141,8 @@
 # Stash size cannot exceed cache_size * threshold.
 OPTIONS.cache_size = None
 OPTIONS.stash_threshold = 0.8
+OPTIONS.gen_verify = False
+OPTIONS.log_diff = None
 
 def MostPopularKey(d, default):
   """Given a dict, return the key corresponding to the largest
@@ -984,6 +994,79 @@
   WriteMetadata(metadata, output_zip)
 
 
+def WriteVerifyPackage(input_zip, output_zip):
+  script = edify_generator.EdifyGenerator(3, OPTIONS.info_dict)
+
+  oem_props = OPTIONS.info_dict.get("oem_fingerprint_properties")
+  recovery_mount_options = OPTIONS.info_dict.get(
+      "recovery_mount_options")
+  oem_dict = None
+  if oem_props is not None and len(oem_props) > 0:
+    if OPTIONS.oem_source is None:
+      raise common.ExternalError("OEM source required for this build")
+    script.Mount("/oem", recovery_mount_options)
+    oem_dict = common.LoadDictionaryFromLines(
+        open(OPTIONS.oem_source).readlines())
+
+  target_fp = CalculateFingerprint(oem_props, oem_dict, OPTIONS.info_dict)
+  metadata = {
+      "post-build": target_fp,
+      "pre-device": GetOemProperty("ro.product.device", oem_props, oem_dict,
+                                   OPTIONS.info_dict),
+      "post-timestamp": GetBuildProp("ro.build.date.utc", OPTIONS.info_dict),
+  }
+
+  device_specific = common.DeviceSpecificParams(
+      input_zip=input_zip,
+      input_version=OPTIONS.info_dict["recovery_api_version"],
+      output_zip=output_zip,
+      script=script,
+      input_tmp=OPTIONS.input_tmp,
+      metadata=metadata,
+      info_dict=OPTIONS.info_dict)
+
+  AppendAssertions(script, OPTIONS.info_dict, oem_dict)
+
+  script.Print("Verifying device images against %s..." % target_fp)
+  script.AppendExtra("")
+
+  script.Print("Verifying boot...")
+  boot_img = common.GetBootableImage(
+      "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
+  boot_type, boot_device = common.GetTypeAndDevice(
+      "/boot", OPTIONS.info_dict)
+  script.Verify("%s:%s:%d:%s" % (
+      boot_type, boot_device, boot_img.size, boot_img.sha1))
+  script.AppendExtra("")
+
+  script.Print("Verifying recovery...")
+  recovery_img = common.GetBootableImage(
+      "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
+  recovery_type, recovery_device = common.GetTypeAndDevice(
+      "/recovery", OPTIONS.info_dict)
+  script.Verify("%s:%s:%d:%s" % (
+      recovery_type, recovery_device, recovery_img.size, recovery_img.sha1))
+  script.AppendExtra("")
+
+  system_tgt = GetImage("system", OPTIONS.input_tmp, OPTIONS.info_dict)
+  system_tgt.ResetFileMap()
+  system_diff = common.BlockDifference("system", system_tgt, src=None)
+  system_diff.WriteStrictVerifyScript(script)
+
+  if HasVendorPartition(input_zip):
+    vendor_tgt = GetImage("vendor", OPTIONS.input_tmp, OPTIONS.info_dict)
+    vendor_tgt.ResetFileMap()
+    vendor_diff = common.BlockDifference("vendor", vendor_tgt, src=None)
+    vendor_diff.WriteStrictVerifyScript(script)
+
+  # Device specific partitions, such as radio, bootloader and etc.
+  device_specific.VerifyOTA_Assertions()
+
+  script.SetProgress(1.0)
+  script.AddToZip(input_zip, output_zip, input_path=OPTIONS.updater_binary)
+  WriteMetadata(metadata, output_zip)
+
+
 class FileDifference(object):
   def __init__(self, partition, source_zip, target_zip, output_zip):
     self.deferred_patch_list = None
@@ -1489,13 +1572,13 @@
   if OPTIONS.verify and system_diff:
     script.Print("Remounting and verifying system partition files...")
     script.Unmount("/system")
-    script.Mount("/system")
+    script.Mount("/system", recovery_mount_options)
     system_diff.EmitExplicitTargetVerification(script)
 
   if OPTIONS.verify and vendor_diff:
     script.Print("Remounting and verifying vendor partition files...")
     script.Unmount("/vendor")
-    script.Mount("/vendor")
+    script.Mount("/vendor", recovery_mount_options)
     vendor_diff.EmitExplicitTargetVerification(script)
   script.AddToZip(target_zip, output_zip, input_path=OPTIONS.updater_binary)
 
@@ -1552,6 +1635,10 @@
       except ValueError:
         raise ValueError("Cannot parse value %r for option %r - expecting "
                          "a float" % (a, o))
+    elif o == "--gen_verify":
+      OPTIONS.gen_verify = True
+    elif o == "--log_diff":
+      OPTIONS.log_diff = a
     else:
       return False
     return True
@@ -1577,6 +1664,8 @@
                                  "verify",
                                  "no_fallback_to_full",
                                  "stash_threshold=",
+                                 "gen_verify",
+                                 "log_diff=",
                              ], extra_option_handler=option_handler)
 
   if len(args) != 2:
@@ -1618,57 +1707,69 @@
     raise common.ExternalError(
         "--- target build has specified no recovery ---")
 
-  while True:
+  # Use the default key to sign the package if not specified with package_key.
+  if not OPTIONS.no_signing:
+    if OPTIONS.package_key is None:
+      OPTIONS.package_key = OPTIONS.info_dict.get(
+          "default_system_dev_certificate",
+          "build/target/product/security/testkey")
 
-    if OPTIONS.no_signing:
-      if os.path.exists(args[1]):
-        os.unlink(args[1])
-      output_zip = zipfile.ZipFile(args[1], "w",
-                                   compression=zipfile.ZIP_DEFLATED)
-    else:
-      temp_zip_file = tempfile.NamedTemporaryFile()
-      output_zip = zipfile.ZipFile(temp_zip_file, "w",
-                                   compression=zipfile.ZIP_DEFLATED)
+  # Set up the output zip. Create a temporary zip file if signing is needed.
+  if OPTIONS.no_signing:
+    if os.path.exists(args[1]):
+      os.unlink(args[1])
+    output_zip = zipfile.ZipFile(args[1], "w",
+                                 compression=zipfile.ZIP_DEFLATED)
+  else:
+    temp_zip_file = tempfile.NamedTemporaryFile()
+    output_zip = zipfile.ZipFile(temp_zip_file, "w",
+                                 compression=zipfile.ZIP_DEFLATED)
 
-    cache_size = OPTIONS.info_dict.get("cache_size", None)
-    if cache_size is None:
-      print "--- can't determine the cache partition size ---"
-    OPTIONS.cache_size = cache_size
+  cache_size = OPTIONS.info_dict.get("cache_size", None)
+  if cache_size is None:
+    print "--- can't determine the cache partition size ---"
+  OPTIONS.cache_size = cache_size
 
-    if OPTIONS.incremental_source is None:
+  # Generate a verify package.
+  if OPTIONS.gen_verify:
+    WriteVerifyPackage(input_zip, output_zip)
+
+  # Generate a full OTA.
+  elif OPTIONS.incremental_source is None:
+    WriteFullOTAPackage(input_zip, output_zip)
+
+  # Generate an incremental OTA. It will fall back to generate a full OTA on
+  # failure unless no_fallback_to_full is specified.
+  else:
+    print "unzipping source target-files..."
+    OPTIONS.source_tmp, source_zip = common.UnzipTemp(
+        OPTIONS.incremental_source)
+    OPTIONS.target_info_dict = OPTIONS.info_dict
+    OPTIONS.source_info_dict = common.LoadInfoDict(source_zip,
+                                                   OPTIONS.source_tmp)
+    if OPTIONS.verbose:
+      print "--- source info ---"
+      common.DumpInfoDict(OPTIONS.source_info_dict)
+    try:
+      WriteIncrementalOTAPackage(input_zip, source_zip, output_zip)
+      if OPTIONS.log_diff:
+        out_file = open(OPTIONS.log_diff, 'w')
+        import target_files_diff
+        target_files_diff.recursiveDiff('',
+                                        OPTIONS.source_tmp,
+                                        OPTIONS.input_tmp,
+                                        out_file)
+        out_file.close()
+    except ValueError:
+      if not OPTIONS.fallback_to_full:
+        raise
+      print "--- failed to build incremental; falling back to full ---"
+      OPTIONS.incremental_source = None
       WriteFullOTAPackage(input_zip, output_zip)
-      if OPTIONS.package_key is None:
-        OPTIONS.package_key = OPTIONS.info_dict.get(
-            "default_system_dev_certificate",
-            "build/target/product/security/testkey")
-      common.ZipClose(output_zip)
-      break
 
-    else:
-      print "unzipping source target-files..."
-      OPTIONS.source_tmp, source_zip = common.UnzipTemp(
-          OPTIONS.incremental_source)
-      OPTIONS.target_info_dict = OPTIONS.info_dict
-      OPTIONS.source_info_dict = common.LoadInfoDict(source_zip,
-                                                     OPTIONS.source_tmp)
-      if OPTIONS.package_key is None:
-        OPTIONS.package_key = OPTIONS.source_info_dict.get(
-            "default_system_dev_certificate",
-            "build/target/product/security/testkey")
-      if OPTIONS.verbose:
-        print "--- source info ---"
-        common.DumpInfoDict(OPTIONS.source_info_dict)
-      try:
-        WriteIncrementalOTAPackage(input_zip, source_zip, output_zip)
-        common.ZipClose(output_zip)
-        break
-      except ValueError:
-        if not OPTIONS.fallback_to_full:
-          raise
-        print "--- failed to build incremental; falling back to full ---"
-        OPTIONS.incremental_source = None
-        common.ZipClose(output_zip)
+  common.ZipClose(output_zip)
 
+  # Sign the generated zip package unless no_signing is specified.
   if not OPTIONS.no_signing:
     SignOutput(temp_zip_file.name, args[1])
     temp_zip_file.close()
diff --git a/tools/releasetools/sign_target_files_apks.py b/tools/releasetools/sign_target_files_apks.py
index cbf78a1..baf60f5 100755
--- a/tools/releasetools/sign_target_files_apks.py
+++ b/tools/releasetools/sign_target_files_apks.py
@@ -134,7 +134,7 @@
 
   signed = tempfile.NamedTemporaryFile()
 
-  common.SignFile(unsigned.name, signed.name, keyname, pw, align=4)
+  common.SignFile(unsigned.name, signed.name, keyname, pw)
 
   data = signed.read()
   unsigned.close()
diff --git a/tools/releasetools/target_files_diff.py b/tools/releasetools/target_files_diff.py
new file mode 100755
index 0000000..405c42b
--- /dev/null
+++ b/tools/releasetools/target_files_diff.py
@@ -0,0 +1,242 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2009 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.
+#
+
+#
+# Finds differences between two target files packages
+#
+
+from __future__ import print_function
+
+import argparse
+import contextlib
+import os
+import re
+import subprocess
+import sys
+import tempfile
+
+def ignore(name):
+  """
+  Files to ignore when diffing
+
+  These are packages that we're already diffing elsewhere,
+  or files that we expect to be different for every build,
+  or known problems.
+  """
+
+  # We're looking at the files that make the images, so no need to search them
+  if name in ['IMAGES']:
+    return True
+  # These are packages of the recovery partition, which we're already diffing
+  if name in ['SYSTEM/etc/recovery-resource.dat',
+              'SYSTEM/recovery-from-boot.p']:
+    return True
+
+  # These files are just the BUILD_NUMBER, and will always be different
+  if name in ['BOOT/RAMDISK/selinux_version',
+              'RECOVERY/RAMDISK/selinux_version']:
+    return True
+
+  # b/24201956 .art/.oat/.odex files are different with every build
+  if name.endswith('.art') or name.endswith('.oat') or name.endswith('.odex'):
+    return True
+  # b/25348136 libpac.so changes with every build
+  if name in ['SYSTEM/lib/libpac.so',
+              'SYSTEM/lib64/libpac.so']:
+    return True
+
+  return False
+
+
+def rewrite_build_property(original, new):
+  """
+  Rewrite property files to remove values known to change for every build
+  """
+
+  skipped = ['ro.bootimage.build.date=',
+             'ro.bootimage.build.date.utc=',
+             'ro.bootimage.build.fingerprint=',
+             'ro.build.id=',
+             'ro.build.display.id=',
+             'ro.build.version.incremental=',
+             'ro.build.date=',
+             'ro.build.date.utc=',
+             'ro.build.host=',
+             'ro.build.description=',
+             'ro.build.fingerprint=',
+             'ro.expect.recovery_id=',
+             'ro.vendor.build.date=',
+             'ro.vendor.build.date.utc=',
+             'ro.vendor.build.fingerprint=']
+
+  for line in original:
+    skip = False
+    for s in skipped:
+      if line.startswith(s):
+        skip = True
+        break
+    if not skip:
+      new.write(line)
+
+
+def trim_install_recovery(original, new):
+  """
+  Rewrite the install-recovery script to remove the hash of the recovery
+  partition.
+  """
+  for line in original:
+    new.write(re.sub(r'[0-9a-f]{40}', '0'*40, line))
+
+def sort_file(original, new):
+  """
+  Sort the file. Some OTA metadata files are not in a deterministic order
+  currently.
+  """
+  lines = original.readlines()
+  lines.sort()
+  for line in lines:
+    new.write(line)
+
+# Map files to the functions that will modify them for diffing
+REWRITE_RULES = {
+    'BOOT/RAMDISK/default.prop': rewrite_build_property,
+    'RECOVERY/RAMDISK/default.prop': rewrite_build_property,
+    'SYSTEM/build.prop': rewrite_build_property,
+    'VENDOR/build.prop': rewrite_build_property,
+
+    'SYSTEM/bin/install-recovery.sh': trim_install_recovery,
+
+    'META/boot_filesystem_config.txt': sort_file,
+    'META/filesystem_config.txt': sort_file,
+    'META/recovery_filesystem_config.txt': sort_file,
+    'META/vendor_filesystem_config.txt': sort_file,
+}
+
+@contextlib.contextmanager
+def preprocess(name, filename):
+  """
+  Optionally rewrite files before diffing them, to remove known-variable
+  information.
+  """
+  if name in REWRITE_RULES:
+    with tempfile.NamedTemporaryFile() as newfp:
+      with open(filename, 'r') as oldfp:
+        REWRITE_RULES[name](oldfp, newfp)
+      newfp.flush()
+      yield newfp.name
+  else:
+    yield filename
+
+def diff(name, file1, file2, out_file):
+  """
+  Diff a file pair with diff, running preprocess() on the arguments first.
+  """
+  with preprocess(name, file1) as f1:
+    with preprocess(name, file2) as f2:
+      proc = subprocess.Popen(['diff', f1, f2], stdout=subprocess.PIPE,
+                              stderr=subprocess.STDOUT)
+      (stdout, _) = proc.communicate()
+      if proc.returncode == 0:
+        return
+      stdout = stdout.strip()
+      if stdout == 'Binary files %s and %s differ' % (f1, f2):
+        print("%s: Binary files differ" % name, file=out_file)
+      else:
+        for line in stdout.strip().split('\n'):
+          print("%s: %s" % (name, line), file=out_file)
+
+def recursiveDiff(prefix, dir1, dir2, out_file):
+  """
+  Recursively diff two directories, checking metadata then calling diff()
+  """
+  list1 = sorted(os.listdir(dir1))
+  list2 = sorted(os.listdir(dir2))
+
+  for entry in list1:
+    name = os.path.join(prefix, entry)
+    name1 = os.path.join(dir1, entry)
+    name2 = os.path.join(dir2, entry)
+
+    if ignore(name):
+      continue
+
+    if entry in list2:
+      if os.path.islink(name1):
+        if os.path.islink(name2):
+          link1 = os.readlink(name1)
+          link2 = os.readlink(name2)
+          if link1 != link2:
+            print("%s: Symlinks differ: %s vs %s" % (name, link1, link2),
+                  file=out_file)
+        else:
+          print("%s: File types differ, skipping compare" % name,
+                file=out_file)
+        continue
+
+      stat1 = os.stat(name1)
+      stat2 = os.stat(name2)
+      type1 = stat1.st_mode & ~0o777
+      type2 = stat2.st_mode & ~0o777
+
+      if type1 != type2:
+        print("%s: File types differ, skipping compare" % name, file=out_file)
+        continue
+
+      if stat1.st_mode != stat2.st_mode:
+        print("%s: Modes differ: %o vs %o" %
+            (name, stat1.st_mode, stat2.st_mode), file=out_file)
+
+      if os.path.isdir(name1):
+        recursiveDiff(name, name1, name2, out_file)
+      elif os.path.isfile(name1):
+        diff(name, name1, name2, out_file)
+      else:
+        print("%s: Unknown file type, skipping compare" % name, file=out_file)
+    else:
+      print("%s: Only in base package" % name, file=out_file)
+
+  for entry in list2:
+    name = os.path.join(prefix, entry)
+    name1 = os.path.join(dir1, entry)
+    name2 = os.path.join(dir2, entry)
+
+    if ignore(name):
+      continue
+
+    if entry not in list1:
+      print("%s: Only in new package" % name, file=out_file)
+
+def main():
+  parser = argparse.ArgumentParser()
+  parser.add_argument('dir1', help='The base target files package (extracted)')
+  parser.add_argument('dir2', help='The new target files package (extracted)')
+  parser.add_argument('--output',
+      help='The output file, otherwise it prints to stdout')
+  args = parser.parse_args()
+
+  if args.output:
+    out_file = open(args.output, 'w')
+  else:
+    out_file = sys.stdout
+
+  recursiveDiff('', args.dir1, args.dir2, out_file)
+
+  if args.output:
+    out_file.close()
+
+if __name__ == '__main__':
+  main()
diff --git a/tools/signapk/Android.mk b/tools/signapk/Android.mk
index 620ccb1..da1501b 100644
--- a/tools/signapk/Android.mk
+++ b/tools/signapk/Android.mk
@@ -21,11 +21,16 @@
 LOCAL_MODULE := signapk
 LOCAL_SRC_FILES := SignApk.java
 LOCAL_JAR_MANIFEST := SignApk.mf
-LOCAL_STATIC_JAVA_LIBRARIES := bouncycastle-host bouncycastle-bcpkix-host
+LOCAL_STATIC_JAVA_LIBRARIES := 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, but we don't
-# need this if we're just doing unbundled apps.
-$(call dist-for-goals,droidcore,$(LOCAL_INSTALLED_MODULE))
+# 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) \
+    $(HOST_OUT_SHARED_LIBRARIES)/libconscrypt_openjdk_jni$(HOST_SHLIB_SUFFIX)
+
+$(call dist-for-goals,droidcore,$(my_dist_files))
+my_dist_files :=
 endif
diff --git a/tools/signapk/SignApk.java b/tools/signapk/SignApk.java
index 3ddab11..5afb8d1 100644
--- a/tools/signapk/SignApk.java
+++ b/tools/signapk/SignApk.java
@@ -34,6 +34,7 @@
 import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
 import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
 import org.bouncycastle.util.encoders.Base64;
+import org.conscrypt.OpenSSLProvider;
 
 import java.io.Console;
 import java.io.BufferedReader;
@@ -110,8 +111,6 @@
 
     private static final String OTACERT_NAME = "META-INF/com/android/otacert";
 
-    private static Provider sBouncyCastleProvider;
-
     // bitmasks for which hash algorithms we need the manifest to include.
     private static final int USE_SHA1 = 1;
     private static final int USE_SHA256 = 2;
@@ -135,7 +134,6 @@
 
     /** Returns the expected signature algorithm for this key type. */
     private static String getSignatureAlgorithm(X509Certificate cert) {
-        String sigAlg = cert.getSigAlgName().toUpperCase(Locale.US);
         String keyType = cert.getPublicKey().getAlgorithm().toUpperCase(Locale.US);
         if ("RSA".equalsIgnoreCase(keyType)) {
             if (getDigestAlgorithm(cert) == USE_SHA256) {
@@ -246,8 +244,11 @@
              * Now it's in a PKCS#8 PrivateKeyInfo structure. Read its Algorithm
              * OID and use that to construct a KeyFactory.
              */
-            ASN1InputStream bIn = new ASN1InputStream(new ByteArrayInputStream(spec.getEncoded()));
-            PrivateKeyInfo pki = PrivateKeyInfo.getInstance(bIn.readObject());
+            PrivateKeyInfo pki;
+            try (ASN1InputStream bIn =
+                    new ASN1InputStream(new ByteArrayInputStream(spec.getEncoded()))) {
+                pki = PrivateKeyInfo.getInstance(bIn.readObject());
+            }
             String algOid = pki.getPrivateKeyAlgorithm().getAlgorithm().getId();
 
             return KeyFactory.getInstance(algOid).generatePrivate(spec);
@@ -449,21 +450,20 @@
 
         CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
         ContentSigner signer = new JcaContentSignerBuilder(getSignatureAlgorithm(publicKey))
-            .setProvider(sBouncyCastleProvider)
             .build(privateKey);
         gen.addSignerInfoGenerator(
             new JcaSignerInfoGeneratorBuilder(
                 new JcaDigestCalculatorProviderBuilder()
-                .setProvider(sBouncyCastleProvider)
                 .build())
             .setDirectSignature(true)
             .build(signer, publicKey));
         gen.addCertificates(certs);
         CMSSignedData sigData = gen.generate(data, false);
 
-        ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded());
-        DEROutputStream dos = new DEROutputStream(out);
-        dos.writeObject(asn1.readObject());
+        try (ASN1InputStream asn1 = new ASN1InputStream(sigData.getEncoded())) {
+            DEROutputStream dos = new DEROutputStream(out);
+            dos.writeObject(asn1.readObject());
+        }
     }
 
     /**
@@ -473,7 +473,7 @@
      * more efficient.
      */
     private static void copyFiles(Manifest manifest, JarFile in, JarOutputStream out,
-                                  long timestamp, int alignment) throws IOException {
+                                  long timestamp, int defaultAlignment) throws IOException {
         byte[] buffer = new byte[4096];
         int num;
 
@@ -512,6 +512,7 @@
                 offset += 4;
                 firstEntry = false;
             }
+            int alignment = getStoredEntryDataAlignment(name, defaultAlignment);
             if (alignment > 0 && (offset % alignment != 0)) {
                 // Set the "extra data" of the entry to between 1 and
                 // alignment-1 bytes, to make the file data begin at
@@ -552,6 +553,24 @@
         }
     }
 
+    /**
+     * Returns the multiple (in bytes) at which the provided {@code STORED} entry's data must start
+     * relative to start of file or {@code 0} if alignment of this entry's data is not important.
+     */
+    private static int getStoredEntryDataAlignment(String entryName, int defaultAlignment) {
+        if (defaultAlignment <= 0) {
+            return 0;
+        }
+
+        if (entryName.endsWith(".so")) {
+            // Align .so contents to memory page boundary to enable memory-mapped
+            // execution.
+            return 4096;
+        } else {
+            return defaultAlignment;
+        }
+    }
+
     private static class WholeFileSignerOutputStream extends FilterOutputStream {
         private boolean closing = false;
         private ByteArrayOutputStream footer = new ByteArrayOutputStream();
@@ -616,7 +635,6 @@
         private File publicKeyFile;
         private X509Certificate publicKey;
         private PrivateKey privateKey;
-        private String outputFile;
         private OutputStream outputStream;
         private final ASN1ObjectIdentifier type;
         private WholeFileSignerOutputStream signer;
@@ -636,14 +654,17 @@
          * This should actually return byte[] or something similar, but nothing
          * actually checks it currently.
          */
+        @Override
         public Object getContent() {
             return this;
         }
 
+        @Override
         public ASN1ObjectIdentifier getContentType() {
             return type;
         }
 
+        @Override
         public void write(OutputStream out) throws IOException {
             try {
                 signer = new WholeFileSignerOutputStream(out, outputStream);
@@ -658,7 +679,7 @@
                 copyFiles(manifest, inputJar, outputJar, timestamp, 0);
                 addOtacert(outputJar, publicKeyFile, timestamp, manifest, hash);
 
-                signFile(manifest, inputJar,
+                signFile(manifest,
                          new X509Certificate[]{ publicKey },
                          new PrivateKey[]{ privateKey },
                          outputJar);
@@ -753,7 +774,7 @@
         temp.writeTo(outputStream);
     }
 
-    private static void signFile(Manifest manifest, JarFile inputJar,
+    private static void signFile(Manifest manifest,
                                  X509Certificate[] publicKey, PrivateKey[] privateKey,
                                  JarOutputStream outputJar)
         throws Exception {
@@ -855,12 +876,16 @@
     public static void main(String[] args) {
         if (args.length < 4) usage();
 
-        sBouncyCastleProvider = new BouncyCastleProvider();
-        Security.addProvider(sBouncyCastleProvider);
+        // Install Conscrypt as the highest-priority provider. Its crypto primitives are faster than
+        // the standard or Bouncy Castle ones.
+        Security.insertProviderAt(new OpenSSLProvider(), 1);
+        // Install Bouncy Castle (as the lowest-priority provider) because Conscrypt does not offer
+        // DSA which may still be needed.
+        // TODO: Stop installing Bouncy Castle provider once DSA is no longer needed.
+        Security.addProvider(new BouncyCastleProvider());
 
         boolean signWholeFile = false;
         String providerClass = null;
-        String providerArg = null;
         int alignment = 4;
 
         int argstart = 0;
@@ -944,7 +969,7 @@
 
                 Manifest manifest = addDigestsToManifest(inputJar, hashes);
                 copyFiles(manifest, inputJar, outputJar, timestamp, alignment);
-                signFile(manifest, inputJar, publicKey, privateKey, outputJar);
+                signFile(manifest, publicKey, privateKey, outputJar);
                 outputJar.close();
             }
         } catch (Exception e) {
diff --git a/tools/zipalign/ZipEntry.cpp b/tools/zipalign/ZipEntry.cpp
index b2270cb..a13b8d1 100644
--- a/tools/zipalign/ZipEntry.cpp
+++ b/tools/zipalign/ZipEntry.cpp
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
+#include <inttypes.h>
 
 using namespace android;
 
@@ -56,7 +57,7 @@
     /* using the info in the CDE, go load up the LFH */
     posn = ftell(fp);
     if (fseek(fp, mCDE.mLocalHeaderRelOffset, SEEK_SET) != 0) {
-        ALOGD("local header seek failed (%ld)\n",
+        ALOGD("local header seek failed (%" PRIu32 ")\n",
             mCDE.mLocalHeaderRelOffset);
         return UNKNOWN_ERROR;
     }
@@ -123,12 +124,12 @@
     mCDE.mExternalAttrs = 0x81b60020;   // matches what WinZip does
 
     if (mCDE.mFileNameLength > 0) {
-        mCDE.mFileName = new unsigned char[mCDE.mFileNameLength+1];
+        mCDE.mFileName = new uint8_t[mCDE.mFileNameLength+1];
         strcpy((char*) mCDE.mFileName, fileName);
     }
     if (mCDE.mFileCommentLength > 0) {
         /* TODO: stop assuming null-terminated ASCII here? */
-        mCDE.mFileComment = new unsigned char[mCDE.mFileCommentLength+1];
+        mCDE.mFileComment = new uint8_t[mCDE.mFileCommentLength+1];
         strcpy((char*) mCDE.mFileComment, comment);
     }
 
@@ -150,20 +151,20 @@
     memcpy(&mCDE, &pEntry->mCDE, sizeof(mCDE));
 
     if (mCDE.mFileNameLength > 0) {
-        mCDE.mFileName = new unsigned char[mCDE.mFileNameLength+1];
+        mCDE.mFileName = new uint8_t[mCDE.mFileNameLength+1];
         if (mCDE.mFileName == NULL)
             return NO_MEMORY;
         strcpy((char*) mCDE.mFileName, (char*)pEntry->mCDE.mFileName);
     }
     if (mCDE.mFileCommentLength > 0) {
-        mCDE.mFileComment = new unsigned char[mCDE.mFileCommentLength+1];
+        mCDE.mFileComment = new uint8_t[mCDE.mFileCommentLength+1];
         if (mCDE.mFileComment == NULL)
             return NO_MEMORY;
         strcpy((char*) mCDE.mFileComment, (char*)pEntry->mCDE.mFileComment);
     }
     if (mCDE.mExtraFieldLength > 0) {
         /* we null-terminate this, though it may not be a string */
-        mCDE.mExtraField = new unsigned char[mCDE.mExtraFieldLength+1];
+        mCDE.mExtraField = new uint8_t[mCDE.mExtraFieldLength+1];
         if (mCDE.mExtraField == NULL)
             return NO_MEMORY;
         memcpy(mCDE.mExtraField, pEntry->mCDE.mExtraField,
@@ -180,7 +181,7 @@
     assert(mLFH.mExtraField == NULL);
     mLFH.mExtraFieldLength = pEntry->mLFH.mExtraFieldLength;
     if (mLFH.mExtraFieldLength > 0) {
-        mLFH.mExtraField = new unsigned char[mLFH.mExtraFieldLength+1];
+        mLFH.mExtraField = new uint8_t[mLFH.mExtraFieldLength+1];
         if (mLFH.mExtraField == NULL)
             return NO_MEMORY;
         memcpy(mLFH.mExtraField, pEntry->mLFH.mExtraField,
@@ -205,9 +206,9 @@
 
     if (mLFH.mExtraFieldLength > 0) {
         /* extend existing field */
-        unsigned char* newExtra;
+        uint8_t* newExtra;
 
-        newExtra = new unsigned char[mLFH.mExtraFieldLength + padding];
+        newExtra = new uint8_t[mLFH.mExtraFieldLength + padding];
         if (newExtra == NULL)
             return NO_MEMORY;
         memset(newExtra + mLFH.mExtraFieldLength, 0, padding);
@@ -218,7 +219,7 @@
         mLFH.mExtraFieldLength += padding;
     } else {
         /* create new field */
-        mLFH.mExtraField = new unsigned char[padding];
+        mLFH.mExtraField = new uint8_t[padding];
         memset(mLFH.mExtraField, 0, padding);
         mLFH.mExtraFieldLength = padding;
     }
@@ -246,7 +247,7 @@
 
     delete[] mLFH.mFileName;
     if (mLFH.mFileNameLength > 0) {
-        mLFH.mFileName = new unsigned char[mLFH.mFileNameLength+1];
+        mLFH.mFileName = new uint8_t[mLFH.mFileNameLength+1];
         strcpy((char*) mLFH.mFileName, (const char*) mCDE.mFileName);
     } else {
         mLFH.mFileName = NULL;
@@ -256,7 +257,7 @@
 /*
  * Set some information about a file after we add it.
  */
-void ZipEntry::setDataInfo(long uncompLen, long compLen, unsigned long crc32,
+void ZipEntry::setDataInfo(long uncompLen, long compLen, uint32_t crc32,
     int compressionMethod)
 {
     mCDE.mCompressionMethod = compressionMethod;
@@ -360,7 +361,7 @@
     struct tm tmResult;
 #endif
     time_t even;
-    unsigned short zdate, ztime;
+    uint16_t zdate, ztime;
 
     struct tm* ptm;
 
@@ -402,7 +403,7 @@
 status_t ZipEntry::LocalFileHeader::read(FILE* fp)
 {
     status_t result = NO_ERROR;
-    unsigned char buf[kLFHLen];
+    uint8_t buf[kLFHLen];
 
     assert(mFileName == NULL);
     assert(mExtraField == NULL);
@@ -433,7 +434,7 @@
 
     /* grab filename */
     if (mFileNameLength != 0) {
-        mFileName = new unsigned char[mFileNameLength+1];
+        mFileName = new uint8_t[mFileNameLength+1];
         if (mFileName == NULL) {
             result = NO_MEMORY;
             goto bail;
@@ -447,7 +448,7 @@
 
     /* grab extra field */
     if (mExtraFieldLength != 0) {
-        mExtraField = new unsigned char[mExtraFieldLength+1];
+        mExtraField = new uint8_t[mExtraFieldLength+1];
         if (mExtraField == NULL) {
             result = NO_MEMORY;
             goto bail;
@@ -468,7 +469,7 @@
  */
 status_t ZipEntry::LocalFileHeader::write(FILE* fp)
 {
-    unsigned char buf[kLFHLen];
+    uint8_t buf[kLFHLen];
 
     ZipEntry::putLongLE(&buf[0x00], kSignature);
     ZipEntry::putShortLE(&buf[0x04], mVersionToExtract);
@@ -507,13 +508,13 @@
 void ZipEntry::LocalFileHeader::dump(void) const
 {
     ALOGD(" LocalFileHeader contents:\n");
-    ALOGD("  versToExt=%u gpBits=0x%04x compression=%u\n",
+    ALOGD("  versToExt=%" PRIu16 " gpBits=0x%04" PRIx16 " compression=%" PRIu16 "\n",
         mVersionToExtract, mGPBitFlag, mCompressionMethod);
-    ALOGD("  modTime=0x%04x modDate=0x%04x crc32=0x%08lx\n",
+    ALOGD("  modTime=0x%04" PRIx16 " modDate=0x%04" PRIx16 " crc32=0x%08" PRIx32 "\n",
         mLastModFileTime, mLastModFileDate, mCRC32);
-    ALOGD("  compressedSize=%lu uncompressedSize=%lu\n",
+    ALOGD("  compressedSize=%" PRIu32 " uncompressedSize=%" PRIu32 "\n",
         mCompressedSize, mUncompressedSize);
-    ALOGD("  filenameLen=%u extraLen=%u\n",
+    ALOGD("  filenameLen=%" PRIu16 " extraLen=%" PRIu16 "\n",
         mFileNameLength, mExtraFieldLength);
     if (mFileName != NULL)
         ALOGD("  filename: '%s'\n", mFileName);
@@ -536,7 +537,7 @@
 status_t ZipEntry::CentralDirEntry::read(FILE* fp)
 {
     status_t result = NO_ERROR;
-    unsigned char buf[kCDELen];
+    uint8_t buf[kCDELen];
 
     /* no re-use */
     assert(mFileName == NULL);
@@ -575,7 +576,7 @@
 
     /* grab filename */
     if (mFileNameLength != 0) {
-        mFileName = new unsigned char[mFileNameLength+1];
+        mFileName = new uint8_t[mFileNameLength+1];
         if (mFileName == NULL) {
             result = NO_MEMORY;
             goto bail;
@@ -589,7 +590,7 @@
 
     /* read "extra field" */
     if (mExtraFieldLength != 0) {
-        mExtraField = new unsigned char[mExtraFieldLength+1];
+        mExtraField = new uint8_t[mExtraFieldLength+1];
         if (mExtraField == NULL) {
             result = NO_MEMORY;
             goto bail;
@@ -604,7 +605,7 @@
 
     /* grab comment, if any */
     if (mFileCommentLength != 0) {
-        mFileComment = new unsigned char[mFileCommentLength+1];
+        mFileComment = new uint8_t[mFileCommentLength+1];
         if (mFileComment == NULL) {
             result = NO_MEMORY;
             goto bail;
@@ -626,7 +627,7 @@
  */
 status_t ZipEntry::CentralDirEntry::write(FILE* fp)
 {
-    unsigned char buf[kCDELen];
+    uint8_t buf[kCDELen];
 
     ZipEntry::putLongLE(&buf[0x00], kSignature);
     ZipEntry::putShortLE(&buf[0x04], mVersionMadeBy);
@@ -676,15 +677,15 @@
 void ZipEntry::CentralDirEntry::dump(void) const
 {
     ALOGD(" CentralDirEntry contents:\n");
-    ALOGD("  versMadeBy=%u versToExt=%u gpBits=0x%04x compression=%u\n",
+    ALOGD("  versMadeBy=%" PRIu16 " versToExt=%" PRIu16 " gpBits=0x%04" PRIx16 " compression=%" PRIu16 "\n",
         mVersionMadeBy, mVersionToExtract, mGPBitFlag, mCompressionMethod);
-    ALOGD("  modTime=0x%04x modDate=0x%04x crc32=0x%08lx\n",
+    ALOGD("  modTime=0x%04" PRIx16 " modDate=0x%04" PRIx16 " crc32=0x%08" PRIx32 "\n",
         mLastModFileTime, mLastModFileDate, mCRC32);
-    ALOGD("  compressedSize=%lu uncompressedSize=%lu\n",
+    ALOGD("  compressedSize=%" PRIu32 " uncompressedSize=%" PRIu32 "\n",
         mCompressedSize, mUncompressedSize);
-    ALOGD("  filenameLen=%u extraLen=%u commentLen=%u\n",
+    ALOGD("  filenameLen=%" PRIu16 " extraLen=%" PRIu16 " commentLen=%" PRIu16 "\n",
         mFileNameLength, mExtraFieldLength, mFileCommentLength);
-    ALOGD("  diskNumStart=%u intAttr=0x%04x extAttr=0x%08lx relOffset=%lu\n",
+    ALOGD("  diskNumStart=%" PRIu16 " intAttr=0x%04" PRIx16 " extAttr=0x%08" PRIx32 " relOffset=%" PRIu32 "\n",
         mDiskNumberStart, mInternalAttrs, mExternalAttrs,
         mLocalHeaderRelOffset);
 
diff --git a/tools/zipalign/ZipEntry.h b/tools/zipalign/ZipEntry.h
index 7f721b4..129a759 100644
--- a/tools/zipalign/ZipEntry.h
+++ b/tools/zipalign/ZipEntry.h
@@ -25,6 +25,7 @@
 #include <utils/Errors.h>
 
 #include <stdlib.h>
+#include <stdint.h>
 #include <stdio.h>
 
 namespace android {
@@ -85,7 +86,7 @@
     /*
      * Return the data CRC.
      */
-    unsigned long getCRC32(void) const { return mCDE.mCRC32; }
+    uint32_t getCRC32(void) const { return mCDE.mCRC32; }
 
     /*
      * Return file modification time in UNIX seconds-since-epoch.
@@ -108,21 +109,21 @@
      * Some basic functions for raw data manipulation.  "LE" means
      * Little Endian.
      */
-    static inline unsigned short getShortLE(const unsigned char* buf) {
+    static inline uint16_t getShortLE(const uint8_t* buf) {
         return buf[0] | (buf[1] << 8);
     }
-    static inline unsigned long getLongLE(const unsigned char* buf) {
+    static inline uint32_t getLongLE(const uint8_t* buf) {
         return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
     }
-    static inline void putShortLE(unsigned char* buf, short val) {
-        buf[0] = (unsigned char) val;
-        buf[1] = (unsigned char) (val >> 8);
+    static inline void putShortLE(uint8_t* buf, uint16_t val) {
+        buf[0] = (uint8_t) val;
+        buf[1] = (uint8_t) (val >> 8);
     }
-    static inline void putLongLE(unsigned char* buf, long val) {
-        buf[0] = (unsigned char) val;
-        buf[1] = (unsigned char) (val >> 8);
-        buf[2] = (unsigned char) (val >> 16);
-        buf[3] = (unsigned char) (val >> 24);
+    static inline void putLongLE(uint8_t* buf, uint32_t val) {
+        buf[0] = (uint8_t) val;
+        buf[1] = (uint8_t) (val >> 8);
+        buf[2] = (uint8_t) (val >> 16);
+        buf[3] = (uint8_t) (val >> 24);
     }
 
     /* defined for Zip archives */
@@ -177,7 +178,7 @@
     /*
      * Set information about the data for this entry.
      */
-    void setDataInfo(long uncompLen, long compLen, unsigned long crc32,
+    void setDataInfo(long uncompLen, long compLen, uint32_t crc32,
         int compressionMethod);
 
     /*
@@ -195,7 +196,7 @@
      * the current file.
      */
     void setLFHOffset(off_t offset) {
-        mCDE.mLocalHeaderRelOffset = (long) offset;
+        mCDE.mLocalHeaderRelOffset = (uint32_t) offset;
     }
 
     /* mark for deletion; used by ZipFile::remove() */
@@ -240,19 +241,19 @@
         status_t read(FILE* fp);
         status_t write(FILE* fp);
 
-        // unsigned long mSignature;
-        unsigned short  mVersionToExtract;
-        unsigned short  mGPBitFlag;
-        unsigned short  mCompressionMethod;
-        unsigned short  mLastModFileTime;
-        unsigned short  mLastModFileDate;
-        unsigned long   mCRC32;
-        unsigned long   mCompressedSize;
-        unsigned long   mUncompressedSize;
-        unsigned short  mFileNameLength;
-        unsigned short  mExtraFieldLength;
-        unsigned char*  mFileName;
-        unsigned char*  mExtraField;
+        // uint32_t mSignature;
+        uint16_t mVersionToExtract;
+        uint16_t mGPBitFlag;
+        uint16_t mCompressionMethod;
+        uint16_t mLastModFileTime;
+        uint16_t mLastModFileDate;
+        uint32_t mCRC32;
+        uint32_t mCompressedSize;
+        uint32_t mUncompressedSize;
+        uint16_t mFileNameLength;
+        uint16_t mExtraFieldLength;
+        uint8_t* mFileName;
+        uint8_t* mExtraField;
 
         enum {
             kSignature      = 0x04034b50,
@@ -298,26 +299,26 @@
         status_t read(FILE* fp);
         status_t write(FILE* fp);
 
-        // unsigned long mSignature;
-        unsigned short  mVersionMadeBy;
-        unsigned short  mVersionToExtract;
-        unsigned short  mGPBitFlag;
-        unsigned short  mCompressionMethod;
-        unsigned short  mLastModFileTime;
-        unsigned short  mLastModFileDate;
-        unsigned long   mCRC32;
-        unsigned long   mCompressedSize;
-        unsigned long   mUncompressedSize;
-        unsigned short  mFileNameLength;
-        unsigned short  mExtraFieldLength;
-        unsigned short  mFileCommentLength;
-        unsigned short  mDiskNumberStart;
-        unsigned short  mInternalAttrs;
-        unsigned long   mExternalAttrs;
-        unsigned long   mLocalHeaderRelOffset;
-        unsigned char*  mFileName;
-        unsigned char*  mExtraField;
-        unsigned char*  mFileComment;
+        // uint32_t mSignature;
+        uint16_t mVersionMadeBy;
+        uint16_t mVersionToExtract;
+        uint16_t mGPBitFlag;
+        uint16_t mCompressionMethod;
+        uint16_t mLastModFileTime;
+        uint16_t mLastModFileDate;
+        uint32_t mCRC32;
+        uint32_t mCompressedSize;
+        uint32_t mUncompressedSize;
+        uint16_t mFileNameLength;
+        uint16_t mExtraFieldLength;
+        uint16_t mFileCommentLength;
+        uint16_t mDiskNumberStart;
+        uint16_t mInternalAttrs;
+        uint32_t mExternalAttrs;
+        uint32_t mLocalHeaderRelOffset;
+        uint8_t* mFileName;
+        uint8_t* mExtraField;
+        uint8_t* mFileComment;
 
         void dump(void) const;
 
diff --git a/tools/zipalign/ZipFile.cpp b/tools/zipalign/ZipFile.cpp
index 3c5ec15..7c3ff37 100644
--- a/tools/zipalign/ZipFile.cpp
+++ b/tools/zipalign/ZipFile.cpp
@@ -34,6 +34,7 @@
 #include <sys/stat.h>
 #include <errno.h>
 #include <assert.h>
+#include <inttypes.h>
 
 using namespace android;
 
@@ -206,7 +207,7 @@
 status_t ZipFile::readCentralDir(void)
 {
     status_t result = NO_ERROR;
-    unsigned char* buf = NULL;
+    uint8_t* buf = NULL;
     off_t fileLength, seekStart;
     long readAmount;
     int i;
@@ -222,7 +223,7 @@
         goto bail;
     }
 
-    buf = new unsigned char[EndOfCentralDir::kMaxEOCDSearch];
+    buf = new uint8_t[EndOfCentralDir::kMaxEOCDSearch];
     if (buf == NULL) {
         ALOGD("Failure allocating %d bytes for EOCD search",
              EndOfCentralDir::kMaxEOCDSearch);
@@ -296,7 +297,7 @@
      * we're hoping to preserve.
      */
     if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
-        ALOGD("Failure seeking to central dir offset %ld\n",
+        ALOGD("Failure seeking to central dir offset %" PRIu32 "\n",
              mEOCD.mCentralDirOffset);
         result = UNKNOWN_ERROR;
         goto bail;
@@ -305,7 +306,7 @@
     /*
      * Loop through and read the central dir entries.
      */
-    ALOGV("Scanning %d entries...\n", mEOCD.mTotalNumEntries);
+    ALOGV("Scanning %" PRIu16 " entries...\n", mEOCD.mTotalNumEntries);
     int entry;
     for (entry = 0; entry < mEOCD.mTotalNumEntries; entry++) {
         ZipEntry* pEntry = new ZipEntry;
@@ -325,7 +326,7 @@
      * If all went well, we should now be back at the EOCD.
      */
     {
-        unsigned char checkBuf[4];
+        uint8_t checkBuf[4];
         if (fread(checkBuf, 1, 4, mZipFp) != 4) {
             ALOGD("EOCD check read failed\n");
             result = INVALID_OPERATION;
@@ -365,7 +366,7 @@
     status_t result = NO_ERROR;
     long lfhPosn, startPosn, endPosn, uncompressedLen;
     FILE* inputFp = NULL;
-    unsigned long crc;
+    uint32_t crc;
     time_t modWhen;
 
     if (mReadOnly)
@@ -466,14 +467,16 @@
         bool scanResult;
         int method;
         long compressedLen;
+        unsigned long longcrc;
 
         scanResult = ZipUtils::examineGzip(inputFp, &method, &uncompressedLen,
-                        &compressedLen, &crc);
+                        &compressedLen, &longcrc);
         if (!scanResult || method != ZipEntry::kCompressDeflated) {
             ALOGD("this isn't a deflated gzip file?");
             result = UNKNOWN_ERROR;
             goto bail;
         }
+        crc = longcrc;
 
         result = copyPartialFpToFp(mZipFp, inputFp, compressedLen, NULL);
         if (result != NO_ERROR) {
@@ -710,7 +713,7 @@
             goto bail;
         }
         long startPosn = ftell(mZipFp);
-        unsigned long crc;
+        uint32_t crc;
         if (compressFpToFp(mZipFp, NULL, buf, uncompressedLen, &crc) != NO_ERROR) {
             ALOGW("recompress of '%s' failed\n", pEntry->mCDE.mFileName);
             result = UNKNOWN_ERROR;
@@ -780,9 +783,9 @@
  * On exit, "srcFp" will be seeked to the end of the file, and "dstFp"
  * will be seeked immediately past the data.
  */
-status_t ZipFile::copyFpToFp(FILE* dstFp, FILE* srcFp, unsigned long* pCRC32)
+status_t ZipFile::copyFpToFp(FILE* dstFp, FILE* srcFp, uint32_t* pCRC32)
 {
-    unsigned char tmpBuf[32768];
+    uint8_t tmpBuf[32768];
     size_t count;
 
     *pCRC32 = crc32(0L, Z_NULL, 0);
@@ -811,7 +814,7 @@
  * On exit, "dstFp" will be seeked immediately past the data.
  */
 status_t ZipFile::copyDataToFp(FILE* dstFp,
-    const void* data, size_t size, unsigned long* pCRC32)
+    const void* data, size_t size, uint32_t* pCRC32)
 {
     size_t count;
 
@@ -836,9 +839,9 @@
  * will be seeked immediately past the data just written.
  */
 status_t ZipFile::copyPartialFpToFp(FILE* dstFp, FILE* srcFp, long length,
-    unsigned long* pCRC32)
+    uint32_t* pCRC32)
 {
-    unsigned char tmpBuf[32768];
+    uint8_t tmpBuf[32768];
     size_t count;
 
     if (pCRC32 != NULL)
@@ -846,7 +849,7 @@
 
     while (length) {
         long readSize;
-        
+
         readSize = sizeof(tmpBuf);
         if (readSize > length)
             readSize = length;
@@ -878,15 +881,15 @@
  * will be seeked immediately past the compressed data.
  */
 status_t ZipFile::compressFpToFp(FILE* dstFp, FILE* srcFp,
-    const void* data, size_t size, unsigned long* pCRC32)
+    const void* data, size_t size, uint32_t* pCRC32)
 {
     status_t result = NO_ERROR;
     const size_t kBufSize = 1024 * 1024;
-    unsigned char* inBuf = NULL;
-    unsigned char* outBuf = NULL;
+    uint8_t* inBuf = NULL;
+    uint8_t* outBuf = NULL;
     size_t outSize = 0;
     bool atEof = false;     // no feof() aviailable yet
-    unsigned long crc;
+    uint32_t crc;
     ZopfliOptions options;
     unsigned char bp = 0;
 
@@ -902,7 +905,7 @@
         /*
          * Create an input buffer and an output buffer.
          */
-        inBuf = new unsigned char[kBufSize];
+        inBuf = new uint8_t[kBufSize];
         if (inBuf == NULL) {
             result = NO_MEMORY;
             goto bail;
@@ -1128,7 +1131,7 @@
     if (dst == src || n <= 0)
         return NO_ERROR;
 
-    unsigned char readBuf[32768];
+    uint8_t readBuf[32768];
 
     if (dst < src) {
         /* shift stuff toward start of file; must read from start */
@@ -1294,7 +1297,7 @@
  * "buf" should be positioned at the EOCD signature, and should contain
  * the entire EOCD area including the comment.
  */
-status_t ZipFile::EndOfCentralDir::readBuf(const unsigned char* buf, int len)
+status_t ZipFile::EndOfCentralDir::readBuf(const uint8_t* buf, int len)
 {
     /* don't allow re-use */
     assert(mComment == NULL);
@@ -1322,11 +1325,11 @@
 
     if (mCommentLen > 0) {
         if (kEOCDLen + mCommentLen > len) {
-            ALOGD("EOCD(%d) + comment(%d) exceeds len (%d)\n",
+            ALOGD("EOCD(%d) + comment(%" PRIu16 ") exceeds len (%d)\n",
                 kEOCDLen, mCommentLen, len);
             return UNKNOWN_ERROR;
         }
-        mComment = new unsigned char[mCommentLen];
+        mComment = new uint8_t[mCommentLen];
         memcpy(mComment, buf + kEOCDLen, mCommentLen);
     }
 
@@ -1338,7 +1341,7 @@
  */
 status_t ZipFile::EndOfCentralDir::write(FILE* fp)
 {
-    unsigned char buf[kEOCDLen];
+    uint8_t buf[kEOCDLen];
 
     ZipEntry::putLongLE(&buf[0x00], kSignature);
     ZipEntry::putShortLE(&buf[0x04], mDiskNumber);
@@ -1366,9 +1369,9 @@
 void ZipFile::EndOfCentralDir::dump(void) const
 {
     ALOGD(" EndOfCentralDir contents:\n");
-    ALOGD("  diskNum=%u diskWCD=%u numEnt=%u totalNumEnt=%u\n",
+    ALOGD("  diskNum=%" PRIu16 " diskWCD=%" PRIu16 " numEnt=%" PRIu16 " totalNumEnt=%" PRIu16 "\n",
         mDiskNumber, mDiskWithCentralDir, mNumEntries, mTotalNumEntries);
-    ALOGD("  centDirSize=%lu centDirOff=%lu commentLen=%u\n",
+    ALOGD("  centDirSize=%" PRIu32 " centDirOff=%" PRIu32 " commentLen=%" PRIu32 "\n",
         mCentralDirSize, mCentralDirOffset, mCommentLen);
 }
 
diff --git a/tools/zipalign/ZipFile.h b/tools/zipalign/ZipFile.h
index b99cda5..b0bafe9 100644
--- a/tools/zipalign/ZipFile.h
+++ b/tools/zipalign/ZipFile.h
@@ -194,18 +194,18 @@
             delete[] mComment;
         }
 
-        status_t readBuf(const unsigned char* buf, int len);
+        status_t readBuf(const uint8_t* buf, int len);
         status_t write(FILE* fp);
 
-        //unsigned long   mSignature;
-        unsigned short  mDiskNumber;
-        unsigned short  mDiskWithCentralDir;
-        unsigned short  mNumEntries;
-        unsigned short  mTotalNumEntries;
-        unsigned long   mCentralDirSize;
-        unsigned long   mCentralDirOffset;      // offset from first disk
-        unsigned short  mCommentLen;
-        unsigned char*  mComment;
+        //uint32_t mSignature;
+        uint16_t mDiskNumber;
+        uint16_t mDiskWithCentralDir;
+        uint16_t mNumEntries;
+        uint16_t mTotalNumEntries;
+        uint32_t mCentralDirSize;
+        uint32_t mCentralDirOffset;      // offset from first disk
+        uint16_t mCommentLen;
+        uint8_t* mComment;
 
         enum {
             kSignature      = 0x06054b50,
@@ -235,18 +235,18 @@
         ZipEntry** ppEntry);
 
     /* copy all of "srcFp" into "dstFp" */
-    status_t copyFpToFp(FILE* dstFp, FILE* srcFp, unsigned long* pCRC32);
+    status_t copyFpToFp(FILE* dstFp, FILE* srcFp, uint32_t* pCRC32);
     /* copy all of "data" into "dstFp" */
     status_t copyDataToFp(FILE* dstFp,
-        const void* data, size_t size, unsigned long* pCRC32);
+        const void* data, size_t size, uint32_t* pCRC32);
     /* copy some of "srcFp" into "dstFp" */
     status_t copyPartialFpToFp(FILE* dstFp, FILE* srcFp, long length,
-        unsigned long* pCRC32);
+        uint32_t* pCRC32);
     /* like memmove(), but on parts of a single file */
     status_t filemove(FILE* fp, off_t dest, off_t src, size_t n);
     /* compress all of "srcFp" into "dstFp", using Deflate */
     status_t compressFpToFp(FILE* dstFp, FILE* srcFp,
-        const void* data, size_t size, unsigned long* pCRC32);
+        const void* data, size_t size, uint32_t* pCRC32);
 
     /* get modification date from a file descriptor */
     time_t getModTime(int fd);
diff --git a/tools/check_prereq/Android.mk b/tools/ziptime/Android.mk
similarity index 65%
rename from tools/check_prereq/Android.mk
rename to tools/ziptime/Android.mk
index 4329aff..3575229 100644
--- a/tools/check_prereq/Android.mk
+++ b/tools/ziptime/Android.mk
@@ -1,4 +1,5 @@
-# Copyright (C) 2009 The Android Open Source Project
+#
+# Copyright 2015 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.
@@ -11,15 +12,21 @@
 # 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_PATH := $(call my-dir)
+#
+# Zip timestamp removal tool
+#
+
+LOCAL_PATH:= $(call my-dir)
 include $(CLEAR_VARS)
 
-LOCAL_SRC_FILES := check_prereq.c
-LOCAL_MODULE := check_prereq
-LOCAL_FORCE_STATIC_EXECUTABLE := true
-LOCAL_MODULE_TAGS := eng
-LOCAL_C_INCLUDES +=
-LOCAL_STATIC_LIBRARIES += libcutils libc
+LOCAL_SRC_FILES := \
+	ZipTime.cpp \
+	ZipEntry.cpp \
+	ZipFile.cpp
 
-include $(BUILD_EXECUTABLE)
+LOCAL_MODULE := ziptime
+LOCAL_MODULE_HOST_OS := darwin linux windows
+
+include $(BUILD_HOST_EXECUTABLE)
diff --git a/tools/ziptime/README.txt b/tools/ziptime/README.txt
new file mode 100644
index 0000000..8a101e9
--- /dev/null
+++ b/tools/ziptime/README.txt
@@ -0,0 +1,10 @@
+ziptime -- zip timestamp tool
+
+usage: ziptime file.zip
+
+  file.zip is an existing Zip archive to rewrite
+
+
+This tools replaces the timestamps in the zip headers with a static time
+(Jan 1 2008). The extra fields are not changed, so you'll need to use the
+-X option to zip so that it doesn't create the 'universal time' extra.
diff --git a/tools/ziptime/ZipEntry.cpp b/tools/ziptime/ZipEntry.cpp
new file mode 100644
index 0000000..51ce09f
--- /dev/null
+++ b/tools/ziptime/ZipEntry.cpp
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2006 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.
+ */
+
+//
+// Access to entries in a Zip archive.
+//
+
+#include "ZipEntry.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <inttypes.h>
+
+using namespace android;
+
+#define LOG(...) fprintf(stderr, __VA_ARGS__)
+
+/* Jan 01 2008 */
+#define STATIC_DATE (28 << 9 | 1 << 5 | 1)
+#define STATIC_TIME 0
+
+/*
+ * Initialize a new ZipEntry structure from a FILE* positioned at a
+ * CentralDirectoryEntry. Rewrites the headers to remove the dynamic
+ * timestamps.
+ *
+ * On exit, the file pointer will be at the start of the next CDE or
+ * at the EOCD.
+ */
+status_t ZipEntry::initAndRewriteFromCDE(FILE* fp)
+{
+    status_t result;
+    long posn;
+
+    /* read the CDE */
+    result = mCDE.rewrite(fp);
+    if (result != 0) {
+        LOG("mCDE.rewrite failed\n");
+        return result;
+    }
+
+    /* using the info in the CDE, go load up the LFH */
+    posn = ftell(fp);
+    if (fseek(fp, mCDE.mLocalHeaderRelOffset, SEEK_SET) != 0) {
+        LOG("local header seek failed (%" PRIu32 ")\n",
+            mCDE.mLocalHeaderRelOffset);
+        return -1;
+    }
+
+    result = mLFH.rewrite(fp);
+    if (result != 0) {
+        LOG("mLFH.rewrite failed\n");
+        return result;
+    }
+
+    if (fseek(fp, posn, SEEK_SET) != 0)
+        return -1;
+
+    return 0;
+}
+
+/*
+ * ===========================================================================
+ *      ZipEntry::LocalFileHeader
+ * ===========================================================================
+ */
+
+/*
+ * Rewrite a local file header.
+ *
+ * On entry, "fp" points to the signature at the start of the header.
+ */
+status_t ZipEntry::LocalFileHeader::rewrite(FILE* fp)
+{
+    status_t result = 0;
+    uint8_t buf[kLFHLen];
+
+    if (fread(buf, 1, kLFHLen, fp) != kLFHLen)
+        return -1;
+
+    if (ZipEntry::getLongLE(&buf[0x00]) != kSignature) {
+        LOG("whoops: didn't find expected signature\n");
+        return -1;
+    }
+
+    ZipEntry::putShortLE(&buf[0x0a], STATIC_TIME);
+    ZipEntry::putShortLE(&buf[0x0c], STATIC_DATE);
+
+    if (fseek(fp, -kLFHLen, SEEK_CUR) != 0)
+        return -1;
+
+    if (fwrite(buf, 1, kLFHLen, fp) != kLFHLen)
+        return -1;
+
+    return 0;
+}
+
+/*
+ * ===========================================================================
+ *      ZipEntry::CentralDirEntry
+ * ===========================================================================
+ */
+
+/*
+ * Read and rewrite the central dir entry that appears next in the file.
+ *
+ * On entry, "fp" should be positioned on the signature bytes for the
+ * entry.  On exit, "fp" will point at the signature word for the next
+ * entry or for the EOCD.
+ */
+status_t ZipEntry::CentralDirEntry::rewrite(FILE* fp)
+{
+    status_t result = 0;
+    uint8_t buf[kCDELen];
+    uint16_t fileNameLength, extraFieldLength, fileCommentLength;
+
+    if (fread(buf, 1, kCDELen, fp) != kCDELen)
+        return -1;
+
+    if (ZipEntry::getLongLE(&buf[0x00]) != kSignature) {
+        LOG("Whoops: didn't find expected signature\n");
+        return -1;
+    }
+
+    ZipEntry::putShortLE(&buf[0x0c], STATIC_TIME);
+    ZipEntry::putShortLE(&buf[0x0e], STATIC_DATE);
+
+    fileNameLength = ZipEntry::getShortLE(&buf[0x1c]);
+    extraFieldLength = ZipEntry::getShortLE(&buf[0x1e]);
+    fileCommentLength = ZipEntry::getShortLE(&buf[0x20]);
+    mLocalHeaderRelOffset = ZipEntry::getLongLE(&buf[0x2a]);
+
+    if (fseek(fp, -kCDELen, SEEK_CUR) != 0)
+        return -1;
+
+    if (fwrite(buf, 1, kCDELen, fp) != kCDELen)
+        return -1;
+
+    if (fseek(fp, fileNameLength + extraFieldLength + fileCommentLength, SEEK_CUR) != 0)
+        return -1;
+
+    return 0;
+}
diff --git a/tools/ziptime/ZipEntry.h b/tools/ziptime/ZipEntry.h
new file mode 100644
index 0000000..26bf596
--- /dev/null
+++ b/tools/ziptime/ZipEntry.h
@@ -0,0 +1,117 @@
+/*
+ * Copyright (C) 2006 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.
+ */
+
+//
+// Zip archive entries.
+//
+// The ZipEntry class is tightly meshed with the ZipFile class.
+//
+#ifndef __LIBS_ZIPENTRY_H
+#define __LIBS_ZIPENTRY_H
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+
+typedef int status_t;
+
+namespace android {
+
+class ZipFile;
+
+/*
+ * ZipEntry objects represent a single entry in a Zip archive.
+ *
+ * File information is stored in two places: next to the file data (the Local
+ * File Header, and possibly a Data Descriptor), and at the end of the file
+ * (the Central Directory Entry).  The two must be kept in sync.
+ */
+class ZipEntry {
+public:
+    friend class ZipFile;
+
+    ZipEntry(void) {}
+    ~ZipEntry(void) {}
+
+    /*
+     * Some basic functions for raw data manipulation.  "LE" means
+     * Little Endian.
+     */
+    static inline uint16_t getShortLE(const uint8_t* buf) {
+        return buf[0] | (buf[1] << 8);
+    }
+    static inline uint32_t getLongLE(const uint8_t* buf) {
+        return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
+    }
+    static inline void putShortLE(uint8_t* buf, uint16_t val) {
+        buf[0] = (uint8_t) val;
+        buf[1] = (uint8_t) (val >> 8);
+    }
+
+protected:
+    /*
+     * Initialize the structure from the file, which is pointing at
+     * our Central Directory entry. And rewrite it.
+     */
+    status_t initAndRewriteFromCDE(FILE* fp);
+
+private:
+    /* these are private and not defined */
+    ZipEntry(const ZipEntry& src);
+    ZipEntry& operator=(const ZipEntry& src);
+
+    /*
+     * Every entry in the Zip archive starts off with one of these.
+     */
+    class LocalFileHeader {
+    public:
+        LocalFileHeader(void) {}
+
+        status_t rewrite(FILE* fp);
+
+        enum {
+            kSignature      = 0x04034b50,
+            kLFHLen         = 30,       // LocalFileHdr len, excl. var fields
+        };
+    };
+
+    /*
+     * Every entry in the Zip archive has one of these in the "central
+     * directory" at the end of the file.
+     */
+    class CentralDirEntry {
+    public:
+        CentralDirEntry(void) :
+            mLocalHeaderRelOffset(0)
+        {}
+
+        status_t rewrite(FILE* fp);
+
+        uint32_t mLocalHeaderRelOffset;
+
+        enum {
+            kSignature      = 0x02014b50,
+            kCDELen         = 46,       // CentralDirEnt len, excl. var fields
+        };
+    };
+
+    LocalFileHeader     mLFH;
+    CentralDirEntry     mCDE;
+};
+
+}; // namespace android
+
+#endif // __LIBS_ZIPENTRY_H
diff --git a/tools/ziptime/ZipFile.cpp b/tools/ziptime/ZipFile.cpp
new file mode 100644
index 0000000..1d111af
--- /dev/null
+++ b/tools/ziptime/ZipFile.cpp
@@ -0,0 +1,241 @@
+/*
+ * Copyright (C) 2006 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.
+ */
+
+//
+// Access to Zip archives.
+//
+
+#include "ZipFile.h"
+
+#include <memory.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <assert.h>
+#include <inttypes.h>
+
+using namespace android;
+
+#define LOG(...) fprintf(stderr, __VA_ARGS__)
+
+/*
+ * Open a file and rewrite the headers
+ */
+status_t ZipFile::rewrite(const char* zipFileName)
+{
+    assert(mZipFp == NULL);     // no reopen
+
+    /* open the file */
+    mZipFp = fopen(zipFileName, "r+b");
+    if (mZipFp == NULL) {
+        int err = errno;
+        LOG("fopen failed: %d\n", err);
+        return -1;
+    }
+
+    /*
+     * Load the central directory.  If that fails, then this probably
+     * isn't a Zip archive.
+     */
+    return rewriteCentralDir();
+}
+
+/*
+ * Find the central directory, read and rewrite the contents.
+ *
+ * The fun thing about ZIP archives is that they may or may not be
+ * readable from start to end.  In some cases, notably for archives
+ * that were written to stdout, the only length information is in the
+ * central directory at the end of the file.
+ *
+ * Of course, the central directory can be followed by a variable-length
+ * comment field, so we have to scan through it backwards.  The comment
+ * is at most 64K, plus we have 18 bytes for the end-of-central-dir stuff
+ * itself, plus apparently sometimes people throw random junk on the end
+ * just for the fun of it.
+ *
+ * This is all a little wobbly.  If the wrong value ends up in the EOCD
+ * area, we're hosed.  This appears to be the way that everbody handles
+ * it though, so we're in pretty good company if this fails.
+ */
+status_t ZipFile::rewriteCentralDir(void)
+{
+    status_t result = 0;
+    uint8_t* buf = NULL;
+    off_t fileLength, seekStart;
+    long readAmount;
+    int i;
+
+    fseek(mZipFp, 0, SEEK_END);
+    fileLength = ftell(mZipFp);
+    rewind(mZipFp);
+
+    /* too small to be a ZIP archive? */
+    if (fileLength < EndOfCentralDir::kEOCDLen) {
+        LOG("Length is %ld -- too small\n", (long)fileLength);
+        result = -1;
+        goto bail;
+    }
+
+    buf = new uint8_t[EndOfCentralDir::kMaxEOCDSearch];
+    if (buf == NULL) {
+        LOG("Failure allocating %d bytes for EOCD search",
+             EndOfCentralDir::kMaxEOCDSearch);
+        result = -1;
+        goto bail;
+    }
+
+    if (fileLength > EndOfCentralDir::kMaxEOCDSearch) {
+        seekStart = fileLength - EndOfCentralDir::kMaxEOCDSearch;
+        readAmount = EndOfCentralDir::kMaxEOCDSearch;
+    } else {
+        seekStart = 0;
+        readAmount = (long) fileLength;
+    }
+    if (fseek(mZipFp, seekStart, SEEK_SET) != 0) {
+        LOG("Failure seeking to end of zip at %ld", (long) seekStart);
+        result = -1;
+        goto bail;
+    }
+
+    /* read the last part of the file into the buffer */
+    if (fread(buf, 1, readAmount, mZipFp) != (size_t) readAmount) {
+        LOG("short file? wanted %ld\n", readAmount);
+        result = -1;
+        goto bail;
+    }
+
+    /* find the end-of-central-dir magic */
+    for (i = readAmount - 4; i >= 0; i--) {
+        if (buf[i] == 0x50 &&
+            ZipEntry::getLongLE(&buf[i]) == EndOfCentralDir::kSignature)
+        {
+            break;
+        }
+    }
+    if (i < 0) {
+        LOG("EOCD not found, not Zip\n");
+        result = -1;
+        goto bail;
+    }
+
+    /* extract eocd values */
+    result = mEOCD.readBuf(buf + i, readAmount - i);
+    if (result != 0) {
+        LOG("Failure reading %ld bytes of EOCD values", readAmount - i);
+        goto bail;
+    }
+
+    /*
+     * So far so good.  "mCentralDirSize" is the size in bytes of the
+     * central directory, so we can just seek back that far to find it.
+     * We can also seek forward mCentralDirOffset bytes from the
+     * start of the file.
+     *
+     * We're not guaranteed to have the rest of the central dir in the
+     * buffer, nor are we guaranteed that the central dir will have any
+     * sort of convenient size.  We need to skip to the start of it and
+     * read the header, then the other goodies.
+     *
+     * The only thing we really need right now is the file comment, which
+     * we're hoping to preserve.
+     */
+    if (fseek(mZipFp, mEOCD.mCentralDirOffset, SEEK_SET) != 0) {
+        LOG("Failure seeking to central dir offset %" PRIu32 "\n",
+             mEOCD.mCentralDirOffset);
+        result = -1;
+        goto bail;
+    }
+
+    /*
+     * Loop through and read the central dir entries.
+     */
+    int entry;
+    for (entry = 0; entry < mEOCD.mTotalNumEntries; entry++) {
+        ZipEntry* pEntry = new ZipEntry;
+
+        result = pEntry->initAndRewriteFromCDE(mZipFp);
+        if (result != 0) {
+            LOG("initFromCDE failed\n");
+            delete pEntry;
+            goto bail;
+        }
+
+        delete pEntry;
+    }
+
+
+    /*
+     * If all went well, we should now be back at the EOCD.
+     */
+    uint8_t checkBuf[4];
+    if (fread(checkBuf, 1, 4, mZipFp) != 4) {
+        LOG("EOCD check read failed\n");
+        result = -1;
+        goto bail;
+    }
+    if (ZipEntry::getLongLE(checkBuf) != EndOfCentralDir::kSignature) {
+        LOG("EOCD read check failed\n");
+        result = -1;
+        goto bail;
+    }
+
+bail:
+    delete[] buf;
+    return result;
+}
+
+/*
+ * ===========================================================================
+ *      ZipFile::EndOfCentralDir
+ * ===========================================================================
+ */
+
+/*
+ * Read the end-of-central-dir fields.
+ *
+ * "buf" should be positioned at the EOCD signature, and should contain
+ * the entire EOCD area including the comment.
+ */
+status_t ZipFile::EndOfCentralDir::readBuf(const uint8_t* buf, int len)
+{
+    uint16_t diskNumber, diskWithCentralDir, numEntries;
+
+    if (len < kEOCDLen) {
+        /* looks like ZIP file got truncated */
+        LOG(" Zip EOCD: expected >= %d bytes, found %d\n",
+            kEOCDLen, len);
+        return -1;
+    }
+
+    /* this should probably be an assert() */
+    if (ZipEntry::getLongLE(&buf[0x00]) != kSignature)
+        return -1;
+
+    diskNumber = ZipEntry::getShortLE(&buf[0x04]);
+    diskWithCentralDir = ZipEntry::getShortLE(&buf[0x06]);
+    numEntries = ZipEntry::getShortLE(&buf[0x08]);
+    mTotalNumEntries = ZipEntry::getShortLE(&buf[0x0a]);
+    mCentralDirOffset = ZipEntry::getLongLE(&buf[0x10]);
+
+    if (diskNumber != 0 || diskWithCentralDir != 0 ||
+        numEntries != mTotalNumEntries)
+    {
+        LOG("Archive spanning not supported\n");
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/tools/ziptime/ZipFile.h b/tools/ziptime/ZipFile.h
new file mode 100644
index 0000000..b049e05
--- /dev/null
+++ b/tools/ziptime/ZipFile.h
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2006 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.
+ */
+
+//
+// Class to rewrite zip file headers to remove dynamic timestamps.
+//
+#ifndef __LIBS_ZIPFILE_H
+#define __LIBS_ZIPFILE_H
+
+#include <stdio.h>
+
+#include "ZipEntry.h"
+
+namespace android {
+
+/*
+ * Manipulate a Zip archive.
+ */
+class ZipFile {
+public:
+    ZipFile(void) : mZipFp(NULL) {}
+    ~ZipFile(void) {
+        if (mZipFp != NULL)
+            fclose(mZipFp);
+    }
+
+    /*
+     * Rewrite an archive's headers to remove dynamic timestamps.
+     */
+    status_t rewrite(const char* zipFileName);
+
+private:
+    /* these are private and not defined */
+    ZipFile(const ZipFile& src);
+    ZipFile& operator=(const ZipFile& src);
+
+    class EndOfCentralDir {
+    public:
+        EndOfCentralDir(void) : mTotalNumEntries(0), mCentralDirOffset(0) {}
+
+        status_t readBuf(const uint8_t* buf, int len);
+
+        uint16_t mTotalNumEntries;
+        uint32_t mCentralDirOffset;      // offset from first disk
+
+        enum {
+            kSignature      = 0x06054b50,
+            kEOCDLen        = 22,       // EndOfCentralDir len, excl. comment
+
+            kMaxCommentLen  = 65535,    // longest possible in ushort
+            kMaxEOCDSearch  = kMaxCommentLen + EndOfCentralDir::kEOCDLen,
+
+        };
+    };
+
+    /* read all entries in the central dir */
+    status_t rewriteCentralDir(void);
+
+    /*
+     * We use stdio FILE*, which gives us buffering but makes dealing
+     * with files >2GB awkward.  Until we support Zip64, we're fine.
+     */
+    FILE*           mZipFp;             // Zip file pointer
+
+    /* one of these per file */
+    EndOfCentralDir mEOCD;
+};
+
+}; // namespace android
+
+#endif // __LIBS_ZIPFILE_H
diff --git a/tools/ziptime/ZipTime.cpp b/tools/ziptime/ZipTime.cpp
new file mode 100644
index 0000000..99d3231
--- /dev/null
+++ b/tools/ziptime/ZipTime.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+/*
+ * Zip tool to remove dynamic timestamps
+ */
+#include "ZipFile.h"
+
+#include <stdlib.h>
+#include <stdio.h>
+
+using namespace android;
+
+static void usage(void)
+{
+    fprintf(stderr, "Zip timestamp utility\n");
+    fprintf(stderr, "Copyright (C) 2015 The Android Open Source Project\n\n");
+    fprintf(stderr, "Usage: ziptime file.zip\n");
+}
+
+int main(int argc, char* const argv[])
+{
+    if (argc != 2) {
+        usage();
+        return 2;
+    }
+
+    ZipFile zip;
+    if (zip.rewrite(argv[1]) != 0) {
+        fprintf(stderr, "Unable to rewrite '%s' as zip archive\n", argv[1]);
+        return 1;
+    }
+
+    return 0;
+}