Merge "Discard hidden files when rounding up resource."
diff --git a/core/Makefile b/core/Makefile
index 14de987..debe691 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -590,6 +590,14 @@
 INTERNAL_USERIMAGES_DEPS := $(MKEXTUSERIMG) $(MAKE_EXT4FS)
 INTERNAL_USERIMAGES_BINARY_PATHS := $(sort $(dir $(INTERNAL_USERIMAGES_DEPS)))
 
+# Calculate size of all files for device image and report on filesizes
+# $(1): src directory
+define calculate-userimage-ext-size
+  @echo Calculating image size...
+  @find $(1) -type f | xargs du -k | sort -gr > $(PRODUCT_OUT)/imagesize-report.txt
+  @du -s -h -k $(1)
+endef
+
 # $(1): src directory
 # $(2): output file
 # $(3): mount point
@@ -738,6 +746,7 @@
 # $(1): output file
 define build-systemimage-target
     @echo "Target system fs image: $(1)"
+    $(call calculate-userimage-ext-size,$(TARGET_OUT))
     $(call build-userimage-ext-target,$(TARGET_OUT),$(1),system,$(INTERNAL_USERIMAGES_EXT_VARIANT),$(BOARD_SYSTEMIMAGE_PARTITION_SIZE))
 endef
 
@@ -1078,9 +1087,11 @@
 endif
 	@# Zip everything up, preserving symlinks
 	$(hide) (cd $(zip_root) && zip -qry ../$(notdir $@) .)
-	@# Run fs_config on all the system files in the zip, and save the output
-	$(hide) zipinfo -1 $@ | awk -F/ 'BEGIN { OFS="/" } /^SYSTEM\// {$$1 = "system"; print}' | $(HOST_OUT_EXECUTABLES)/fs_config > $(zip_root)/META/filesystem_config.txt
-	$(hide) (cd $(zip_root) && zip -q ../$(notdir $@) META/filesystem_config.txt)
+	@# Run fs_config on all the system, 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 > $(zip_root)/META/filesystem_config.txt
+	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="BOOT/RAMDISK/" } /^BOOT\/RAMDISK\// {print $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config > $(zip_root)/META/boot_filesystem_config.txt
+	$(hide) zipinfo -1 $@ | awk 'BEGIN { FS="RECOVERY/RAMDISK/" } /^RECOVERY\/RAMDISK\// {print $$2}' | $(HOST_OUT_EXECUTABLES)/fs_config > $(zip_root)/META/recovery_filesystem_config.txt
+	$(hide) (cd $(zip_root) && zip -q ../$(notdir $@) META/*filesystem_config.txt)
 
 
 target-files-package: $(BUILT_TARGET_FILES_PACKAGE)
@@ -1416,6 +1427,7 @@
 			-v "OUT_DIR=$(OUT_DIR)" \
 			-v "TARGET_ARCH=$(TARGET_ARCH)" \
 			-v "TARGET_CPU_ABI=$(TARGET_CPU_ABI)" \
+			-v "DLL_EXTENSION=$(HOST_SHLIB_SUFFIX)" \
 			-o $(PRIVATE_DIR) && \
 		cp -f $(target_notice_file_txt) \
 				$(PRIVATE_DIR)/platforms/android-$(PLATFORM_VERSION)/images/$(TARGET_CPU_ABI)/NOTICE.txt && \
diff --git a/core/binary.mk b/core/binary.mk
index dadfd5e..1a5af5f 100644
--- a/core/binary.mk
+++ b/core/binary.mk
@@ -56,7 +56,8 @@
     endif
   else
     # LOCAL_NDK_STL_VARIANT is gnustl_static
-    my_ndk_stl_include_path := $(my_ndk_source_root)/cxx-stl/gnu-libstdc++/include
+    my_ndk_stl_include_path := $(my_ndk_source_root)/cxx-stl/gnu-libstdc++/libs/$(TARGET_CPU_ABI)/include \
+                               $(my_ndk_source_root)/cxx-stl/gnu-libstdc++/include
     my_ndk_stl_static_lib := $(my_ndk_source_root)/cxx-stl/gnu-libstdc++/libs/$(TARGET_CPU_ABI)/libstdc++.a
   endif
   endif
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index bc085f5..1b9bb8a 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -104,6 +104,7 @@
 LOCAL_WARNINGS_ENABLE:=
 LOCAL_MANIFEST_FILE:=
 LOCAL_RENDERSCRIPT_INCLUDES:=
+LOCAL_RENDERSCRIPT_INCLUDES_OVERRIDE:=
 LOCAL_RENDERSCRIPT_CC:=
 LOCAL_BUILD_HOST_DEX:=
 LOCAL_DEX_PREOPT:=
diff --git a/core/combo/TARGET_linux-x86.mk b/core/combo/TARGET_linux-x86.mk
index 0d05dd2..0fb6edc 100644
--- a/core/combo/TARGET_linux-x86.mk
+++ b/core/combo/TARGET_linux-x86.mk
@@ -92,10 +92,6 @@
 #TARGET_GLOBAL_CFLAGS += \
 #			-DNDEBUG
 
-
-# Fix this after ssp.c is fixed for x86
-# TARGET_GLOBAL_CFLAGS += -fstack-protector
-
 TARGET_GLOBAL_CPPFLAGS += \
 			-fno-use-cxa-atexit
 
diff --git a/core/definitions.mk b/core/definitions.mk
index 5867d09..1afc0f4 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -801,6 +801,7 @@
   -p $(PRIVATE_RS_OUTPUT_DIR)/src \
   -d $(PRIVATE_RS_OUTPUT_DIR) \
   -a $@ -MD \
+  $(addprefix -target-api , $(PRIVATE_RS_TARGET_API)) \
   $(foreach inc,$(PRIVATE_RS_INCLUDES),$(addprefix -I , $(inc))) \
   $(PRIVATE_RS_SOURCE_FILES)
 #$(hide) $(LLVM_RS_LINK) \
diff --git a/core/dex_preopt.mk b/core/dex_preopt.mk
index 9f44c32..b801ac2 100644
--- a/core/dex_preopt.mk
+++ b/core/dex_preopt.mk
@@ -4,7 +4,7 @@
 ####################################
 
 # TODO: replace it with device's BOOTCLASSPATH
-DEXPREOPT_BOOT_JARS := core:apache-xml:bouncycastle:ext:framework:android.policy:services:core-junit
+DEXPREOPT_BOOT_JARS := core:core-junit:bouncycastle:ext:framework:android.policy:services:apache-xml:filterfw
 DEXPREOPT_BOOT_JARS_MODULES := $(subst :, ,$(DEXPREOPT_BOOT_JARS))
 
 DEXPREOPT_BUILD_DIR := $(OUT_DIR)
diff --git a/core/java.mk b/core/java.mk
index 5324e7d..16cbb9c 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -29,7 +29,7 @@
   endif
 else
   ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
-    LOCAL_JAVA_LIBRARIES := core core-junit ext framework $(LOCAL_JAVA_LIBRARIES)
+    LOCAL_JAVA_LIBRARIES := core core-junit ext framework filterfw $(LOCAL_JAVA_LIBRARIES)
   endif
 endif
 
@@ -135,6 +135,24 @@
 RenderScript_file_stamp := $(LOCAL_INTERMEDIATE_SOURCE_DIR)/RenderScript.stamp
 renderscript_intermediate := $(LOCAL_INTERMEDIATE_SOURCE_DIR)/renderscript
 
+renderscript_target_api :=
+ifneq (,$(LOCAL_SDK_VERSION))
+# Only HC (version 11) should be using the prebuilt RS tools.
+ifeq (11,$(LOCAL_SDK_VERSION))
+ifeq ($(LOCAL_RENDERSCRIPT_CC),)
+LOCAL_RENDERSCRIPT_CC := prebuilt/$(HOST_PREBUILT_TAG)/llvm-rs-cc/llvm-rs-cc
+endif
+endif
+# Set target-api for LOCAL_SDK_VERSIONs other than 11 and current.
+ifneq (,$(filter-out 11 current, $(LOCAL_SDK_VERSION)))
+renderscript_target_api := $(LOCAL_SDK_VERSION)
+endif
+endif  # LOCAL_SDK_VERSION is set
+
+ifeq ($(LOCAL_RENDERSCRIPT_CC),)
+LOCAL_RENDERSCRIPT_CC := $(LLVM_RS_CC)
+endif
+
 # prepend the RenderScript system include path
 ifneq ($(filter-out current,$(LOCAL_SDK_VERSION)),)
 LOCAL_RENDERSCRIPT_INCLUDES := \
@@ -143,13 +161,13 @@
     $(LOCAL_RENDERSCRIPT_INCLUDES)
 else
 LOCAL_RENDERSCRIPT_INCLUDES := \
-     $(TOPDIR)external/clang/lib/Headers \
-     $(TOPDIR)frameworks/base/libs/rs/scriptc \
-     $(LOCAL_RENDERSCRIPT_INCLUDES)
+    $(TOPDIR)external/clang/lib/Headers \
+    $(TOPDIR)frameworks/base/libs/rs/scriptc \
+    $(LOCAL_RENDERSCRIPT_INCLUDES)
 endif
 
-ifeq ($(LOCAL_RENDERSCRIPT_CC),)
-LOCAL_RENDERSCRIPT_CC := $(LLVM_RS_CC)
+ifneq ($(LOCAL_RENDERSCRIPT_INCLUDES_OVERRIDE),)
+LOCAL_RENDERSCRIPT_INCLUDES := $(LOCAL_RENDERSCRIPT_INCLUDES_OVERRIDE)
 endif
 
 $(RenderScript_file_stamp): PRIVATE_RS_INCLUDES := $(LOCAL_RENDERSCRIPT_INCLUDES)
@@ -158,6 +176,7 @@
 # By putting the generated java files into $(LOCAL_INTERMEDIATE_SOURCE_DIR), they will be
 # automatically found by the java compiling function transform-java-to-classes.jar.
 $(RenderScript_file_stamp): PRIVATE_RS_OUTPUT_DIR := $(renderscript_intermediate)
+$(RenderScript_file_stamp): PRIVATE_RS_TARGET_API := $(renderscript_target_api)
 $(RenderScript_file_stamp): $(renderscript_sources_fullpath) $(LOCAL_RENDERSCRIPT_CC)
 	$(transform-renderscripts-to-java-and-bc)
 
@@ -334,23 +353,6 @@
 # Override PRIVATE_INTERMEDIATES_DIR so that install-dex-debug
 # will work even when intermediates != intermediates.COMMON.
 $(built_dex_intermediate): PRIVATE_INTERMEDIATES_DIR := $(intermediates.COMMON)
-
-ifeq (,$(filter --target-api=%, $(LOCAL_DX_FLAGS)))
-possible_dx_target_api :=
-ifneq (,$(LOCAL_MANIFEST_FILE))
-  manifest_min_sdk_version := \
-      $(shell grep -o 'android:minSdkVersion="[0-9]\{1,\}"' $(LOCAL_PATH)/$(LOCAL_MANIFEST_FILE) 2>/dev/null)
-  possible_dx_target_api := $(patsubst android:minSdkVersion="%",%,$(firstword $(manifest_min_sdk_version)))
-endif
-ifeq (,$(possible_dx_target_api))
-# In case minSdkVersion is not available, eg shared library built against SDK,
-# LOCAL_SDK_VERSION may be a good guess
-ifneq (,$(filter-out current, $(LOCAL_SDK_VERSION)))
-  possible_dx_target_api := $(LOCAL_SDK_VERSION)
-endif
-endif
-LOCAL_DX_FLAGS += $(addprefix --target-api=, $(possible_dx_target_api))
-endif # --target-api not defined in LOCAL_DX_FLAGS
 $(built_dex_intermediate): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS)
 # If you instrument class files that have local variable debug information in
 # them emma does not correctly maintain the local variable table.
diff --git a/core/main.mk b/core/main.mk
index 571e779..1f03355 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -113,14 +113,17 @@
 
 
 # Check for the correct version of java
-java_version := $(shell java -version 2>&1 | head -n 1 | grep '[ "]1\.6[\. "$$]')
+java_version := $(shell java -version 2>&1 | head -n 1 | grep '^java .*[ "]1\.6[\. "$$]')
+ifneq ($(shell java -version 2>&1 | grep -i openjdk),)
+java_version :=
+endif
 ifeq ($(strip $(java_version)),)
 $(info ************************************************************)
 $(info You are attempting to build with the incorrect version)
 $(info of java.)
 $(info $(space))
 $(info Your version is: $(shell java -version 2>&1 | head -n 1).)
-$(info The correct version is: 1.6.)
+$(info The correct version is: Java SE 1.6.)
 $(info $(space))
 $(info Please follow the machine setup instructions at)
 $(info $(space)$(space)$(space)$(space)http://source.android.com/source/download.html)
@@ -401,6 +404,7 @@
 	development/host \
 	development/tools/etc1tool \
 	development/tools/line_endings \
+	development/tools/emulator/opengl \
 	external/clang \
 	external/easymock \
 	external/expat \
@@ -438,9 +442,11 @@
 	sdk/layoutlib_api \
 	sdk/layoutopt \
 	sdk/ninepatch \
+	sdk/rule_api \
 	sdk/sdkstats \
 	sdk/sdkmanager \
 	sdk/swtmenubar \
+	sdk/traceview \
 	development/apps \
 	development/tools/mkstubs \
 	packages
@@ -467,6 +473,7 @@
 	build/libs \
 	build/target \
 	build/tools/acp \
+	external/mksh \
 	external/yaffs2 \
 	external/zlib
 else	# !BUILD_TINY_ANDROID
diff --git a/core/multi_prebuilt.mk b/core/multi_prebuilt.mk
index 7602673..50a4a38 100644
--- a/core/multi_prebuilt.mk
+++ b/core/multi_prebuilt.mk
@@ -22,6 +22,7 @@
 prebuilt_static_java_libraries := $(LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES)
 prebuilt_is_host := $(LOCAL_IS_HOST_MODULE)
 prebuilt_module_tags := $(LOCAL_MODULE_TAGS)
+prebuilt_strip_module := $(LOCAL_STRIP_MODULE)
 
 
 ifndef multi_prebuilt_once
@@ -34,6 +35,7 @@
 # $(5): OVERRIDE_BUILT_MODULE_PATH
 # $(6): UNINSTALLABLE_MODULE
 # $(7): BUILT_MODULE_STEM
+# $(8): LOCAL_STRIP_MODULE
 #
 # Elements in the file list may be bare filenames,
 # or of the form "<modulename>:<filename>".
@@ -65,6 +67,8 @@
     $(eval LOCAL_BUILT_MODULE_STEM := $(notdir $(LOCAL_SRC_FILES))) \
    ) \
   $(eval LOCAL_MODULE_SUFFIX := $(suffix $(LOCAL_SRC_FILES))) \
+  $(if $(filter user,$(TARGET_BUILD_VARIANT)), \
+    $(eval LOCAL_STRIP_MODULE := $(8))) \
   $(eval include $(BUILD_PREBUILT)) \
  )
 endef
@@ -85,7 +89,10 @@
     $(prebuilt_is_host), \
     SHARED_LIBRARIES, \
     $(prebuilt_module_tags), \
-    $($(if $(prebuilt_is_host),HOST,TARGET)_OUT_INTERMEDIATE_LIBRARIES))
+    $($(if $(prebuilt_is_host),HOST,TARGET)_OUT_INTERMEDIATE_LIBRARIES), \
+    , \
+    , \
+    $(prebuilt_strip_module))
 
 $(call auto-prebuilt-boilerplate, \
     $(prebuilt_executables), \
diff --git a/core/prebuilt.mk b/core/prebuilt.mk
index 07ecf64..bd4d91f 100644
--- a/core/prebuilt.mk
+++ b/core/prebuilt.mk
@@ -16,16 +16,6 @@
 $(error dont use LOCAL_PREBUILT_JAVA_LIBRARIES anymore LOCAL_PATH=$(LOCAL_PATH))
 endif
 
-ifneq ($(filter APPS,$(LOCAL_MODULE_CLASS)),)
-ifeq (true,$(WITH_DEXPREOPT))
-ifeq (,$(TARGET_BUILD_APPS))
-ifndef LOCAL_DEX_PREOPT
-LOCAL_DEX_PREOPT := true
-endif
-endif
-endif
-endif
-
 ifeq ($(LOCAL_STRIP_MODULE),true)
   ifdef LOCAL_IS_HOST_MODULE
     $(error Cannot strip host module LOCAL_PATH=$(LOCAL_PATH))
@@ -55,20 +45,8 @@
 
 # Ensure that prebuilt .apks have been aligned.
 ifneq ($(filter APPS,$(LOCAL_MODULE_CLASS)),)
-ifeq ($(LOCAL_DEX_PREOPT),true)
-# Make sure the boot jars get dexpreopt-ed first
-$(built_module): $(DEXPREOPT_BOOT_ODEXS) | $(DEXPREOPT) $(DEXOPT) $(AAPT)
-endif
 $(built_module) : $(LOCAL_PATH)/$(LOCAL_SRC_FILES) | $(ZIPALIGN)
 	$(transform-prebuilt-to-target-with-zipalign)
-ifeq ($(LOCAL_DEX_PREOPT),true)
-	$(hide) rm -f $(patsubst %.apk,%.odex,$@)
-	$(call dexpreopt-one-file,$@,$(patsubst %.apk,%.odex,$@))
-	$(call dexpreopt-remove-classes.dex,$@)
-
-built_odex := $(basename $(built_module)).odex
-$(built_odex): $(built_module)
-endif
 else
 ifneq ($(LOCAL_PREBUILT_STRIP_COMMENTS),)
 $(built_module) : $(LOCAL_PATH)/$(LOCAL_SRC_FILES)
diff --git a/core/tasks/cts.mk b/core/tasks/cts.mk
index af20da8..975c4f4 100644
--- a/core/tasks/cts.mk
+++ b/core/tasks/cts.mk
@@ -201,7 +201,7 @@
 
 define copy-testcase-apk
 
-$(hide) $(ACP) -fp $(call intermediates-dir-for,APPS,$(1))/package.apk \
+$(hide) $(ACP) -fp $(call intermediates-dir-for,APPS,$(1))/package.apk.unaligned \
 	$(PRIVATE_DIR)/repository/testcases/$(1).apk
 
 endef
diff --git a/core/user_tags.mk b/core/user_tags.mk
index c1c8ccf..d09a2cd 100644
--- a/core/user_tags.mk
+++ b/core/user_tags.mk
@@ -69,7 +69,6 @@
 	check_stack \
 	check_trace \
 	cmu2nuance \
-	com.android.inputmethod.pinyin.lib \
 	com.android.phone.common \
 	com.android.vcard \
 	commons-compress-1.0 \
@@ -254,7 +253,6 @@
 	libiptc \
 	libjnigraphics \
 	libjni_latinime \
-	libjni_pinyinime \
 	libjpeg \
 	libjs \
 	liblinenoise \
diff --git a/target/board/generic/BoardConfig.mk b/target/board/generic/BoardConfig.mk
index d02205b..f247bca 100644
--- a/target/board/generic/BoardConfig.mk
+++ b/target/board/generic/BoardConfig.mk
@@ -30,3 +30,6 @@
 
 # Set /system/bin/sh to ash, not mksh, to make sure we can switch back.
 TARGET_SHELL := ash
+
+# Build OpenGLES emulation guest and host libraries
+BUILD_EMULATOR_OPENGL := true
diff --git a/target/board/generic_x86/BoardConfig.mk b/target/board/generic_x86/BoardConfig.mk
index 7277908..84b28ca 100644
--- a/target/board/generic_x86/BoardConfig.mk
+++ b/target/board/generic_x86/BoardConfig.mk
@@ -20,3 +20,6 @@
 
 # Set /system/bin/sh to ash, not mksh, to make sure we can switch back.
 TARGET_SHELL := ash
+
+# Build OpenGLES emulation host and guest libraries
+BUILD_EMULATOR_OPENGL := true
diff --git a/target/product/generic_no_telephony.mk b/target/product/generic_no_telephony.mk
index c31e5f7..6e052fd 100644
--- a/target/product/generic_no_telephony.mk
+++ b/target/product/generic_no_telephony.mk
@@ -30,7 +30,7 @@
     DrmProvider \
     Email \
     Exchange \
-    GalleryNew3D \
+    Gallery2 \
     LatinIME \
     Launcher2 \
     Music \
diff --git a/target/product/large_emu_hw.mk b/target/product/large_emu_hw.mk
index f38ef61..9224130 100644
--- a/target/product/large_emu_hw.mk
+++ b/target/product/large_emu_hw.mk
@@ -32,7 +32,7 @@
     DrmProvider \
     Email \
     Exchange \
-    GalleryNew3D \
+    Gallery2 \
     LatinIME \
     Launcher2 \
     Music \
diff --git a/target/product/sdk.mk b/target/product/sdk.mk
index d942cde..aaef0b9 100644
--- a/target/product/sdk.mk
+++ b/target/product/sdk.mk
@@ -60,7 +60,8 @@
 	jython \
 	jsilver \
 	librs_jni \
-	ConnectivityTest
+	ConnectivityTest \
+	GpsLocationTest
 
 
 # Host tools that are parts of the SDK.
@@ -125,6 +126,11 @@
 	sdkuilib-tests \
 	layoutlib-tests
 
+# audio libraries.
+PRODUCT_PACKAGES += \
+	audio.primary.goldfish \
+	audio_policy.default
+
 PRODUCT_PACKAGE_OVERLAYS := development/sdk_overlay
 
 PRODUCT_COPY_FILES := \
diff --git a/tools/findleaves.py b/tools/findleaves.py
index 0adf188..52c4d9f 100755
--- a/tools/findleaves.py
+++ b/tools/findleaves.py
@@ -89,9 +89,9 @@
     usage()
   dirlist = argv[i:-1]
   filename = argv[-1]
-  results = perform_find(mindepth, prune, dirlist, filename)
+  results = list(set(perform_find(mindepth, prune, dirlist, filename)))
   results.sort()
-  for r in set(results):
+  for r in results:
     print r
 
 if __name__ == "__main__":
diff --git a/tools/releasetools/check_target_files_signatures b/tools/releasetools/check_target_files_signatures
index 2c97e2e..1325ef4 100755
--- a/tools/releasetools/check_target_files_signatures
+++ b/tools/releasetools/check_target_files_signatures
@@ -52,6 +52,11 @@
 import tempfile
 import zipfile
 
+try:
+  from hashlib import sha1 as sha1
+except ImportError:
+  from sha import sha as sha1
+
 import common
 
 # Work around a bug in python's zipfile module that prevents opening
diff --git a/tools/releasetools/common.py b/tools/releasetools/common.py
index 40da73a..2cb5680 100644
--- a/tools/releasetools/common.py
+++ b/tools/releasetools/common.py
@@ -29,11 +29,9 @@
 import zipfile
 
 try:
-  import hashlib
-  sha1 = hashlib.sha1
+  from hashlib import sha1 as sha1
 except ImportError:
-  import sha
-  sha1 = sha.sha
+  from sha import sha as sha1
 
 # missing in Python 2.4 and before
 if not hasattr(os, "SEEK_SET"):
diff --git a/tools/releasetools/ota_from_target_files b/tools/releasetools/ota_from_target_files
index 8f5c3fe..4264efa 100755
--- a/tools/releasetools/ota_from_target_files
+++ b/tools/releasetools/ota_from_target_files
@@ -63,6 +63,11 @@
 import time
 import zipfile
 
+try:
+  from hashlib import sha1 as sha1
+except ImportError:
+  from sha import sha as sha1
+
 import common
 import edify_generator
 
@@ -608,7 +613,12 @@
   so_far = 0
 
   script.Print("Patching system files...")
-  for fn, tf, sf, size, _ in patch_list:
+  deferred_patch_list = []
+  for item in patch_list:
+    fn, tf, sf, size, _ = item
+    if tf.name == "system/build.prop":
+      deferred_patch_list.append(item)
+      continue
     script.ApplyPatch("/"+fn, "-", tf.size, tf.sha1, sf.sha1, "patch/"+fn+".p")
     so_far += tf.size
     script.SetProgress(so_far / total_patch_size)
@@ -710,6 +720,15 @@
   if OPTIONS.extra_script is not None:
     script.AppendExtra(OPTIONS.extra_script)
 
+  # Patch the build.prop file last, so if something fails but the
+  # device can still come up, it appears to be the old build and will
+  # get set the OTA package again to retry.
+  script.Print("Patching remaining system files...")
+  for item in deferred_patch_list:
+    fn, tf, sf, size, _ = item
+    script.ApplyPatch("/"+fn, "-", tf.size, tf.sha1, sf.sha1, "patch/"+fn+".p")
+  script.SetPermissions("/system/build.prop", 0, 0, 0644)
+
   script.AddToZip(target_zip, output_zip)
   WriteMetadata(metadata, output_zip)