Clean common intermediate files if overlay changed

This change cleans the packages' intermediate files if their overlays
changed between incremental builds.
If two builds have different overlay, they will have different R
classes, and so the jar files in the common intermediate dirs can
not be shared. Therefore incremental build can't be applied.

This change detects the overlay changes on package's base.
If a package's overlay is different from the previous build, its common
intermediate dir is nuked.
This makes broader incremental builds possible.

Change-Id: I368610ebbbbc85a80f9aecd714ab22cd78da7f12
diff --git a/core/package.mk b/core/package.mk
index 195b346..c689031 100644
--- a/core/package.mk
+++ b/core/package.mk
@@ -94,12 +94,14 @@
 ifeq (,$(LOCAL_RESOURCE_DIR))
   LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
 endif
-LOCAL_RESOURCE_DIR := \
-  $(wildcard $(foreach dir, $(PRODUCT_PACKAGE_OVERLAYS), \
-    $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))) \
-  $(wildcard $(foreach dir, $(DEVICE_PACKAGE_OVERLAYS), \
-    $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))) \
-  $(LOCAL_RESOURCE_DIR)
+
+package_resource_overlays := $(strip \
+    $(wildcard $(foreach dir, $(PRODUCT_PACKAGE_OVERLAYS), \
+      $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))) \
+    $(wildcard $(foreach dir, $(DEVICE_PACKAGE_OVERLAYS), \
+      $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))))
+
+LOCAL_RESOURCE_DIR := $(package_resource_overlays) $(LOCAL_RESOURCE_DIR)
 
 all_assets := $(call find-subdir-assets,$(LOCAL_ASSET_DIR))
 all_assets := $(addprefix $(LOCAL_ASSET_DIR)/,$(patsubst assets/%,%,$(all_assets)))
@@ -399,6 +401,9 @@
 # Save information about this package
 PACKAGES.$(LOCAL_PACKAGE_NAME).OVERRIDES := $(strip $(LOCAL_OVERRIDES_PACKAGES))
 PACKAGES.$(LOCAL_PACKAGE_NAME).RESOURCE_FILES := $(all_resources)
+ifdef package_resource_overlays
+PACKAGES.$(LOCAL_PACKAGE_NAME).RESOURCE_OVERLAYS := $(package_resource_overlays)
+endif
 
 PACKAGES := $(PACKAGES) $(LOCAL_PACKAGE_NAME)