AAPT2: Enable product/config filtering and improve source ordering

- Restore old ordering of resource files to match AAPT behavior
- Change the order in which overlays are processed.

Change-Id: I9da88f6b4124e7f89a70eb485b4591d5c4c87d38
diff --git a/core/aapt2.mk b/core/aapt2.mk
index 1d47c54..bfc01da 100644
--- a/core/aapt2.mk
+++ b/core/aapt2.mk
@@ -2,7 +2,7 @@
 # Compile resource with AAPT2
 # Input variables:
 # full_android_manifest,
-# my_res_resources, my_overlay_resources, my_aapt_characteristics,
+# my_res_resources, my_overlay_resources,
 # my_compiled_res_base_dir, my_res_package,
 # R_file_stamp, proguard_options_file
 # my_generated_res_dirs: Resources generated during the build process and we have to compile them in a single run of aapt2.
@@ -41,15 +41,15 @@
 endif
 
 $(my_res_resources_flat) $(my_overlay_resources_flat) $(my_generated_resources_flata): \
-  PRIVATE_AAPT2_CFLAGS := $(addprefix --product ,$(my_aapt_characteristics)) $(PRODUCT_AAPT2_CFLAGS)
+  PRIVATE_AAPT2_CFLAGS := $(PRODUCT_AAPT2_CFLAGS)
 
-my_static_library_resources := $(foreach l, $(LOCAL_STATIC_ANDROID_LIBRARIES),\
+my_static_library_resources := $(foreach l, $(call reverse-list,$(LOCAL_STATIC_ANDROID_LIBRARIES)),\
   $(call intermediates-dir-for,JAVA_LIBRARIES,$(l),,COMMON)/package-res.apk)
 my_shared_library_resources := $(foreach l, $(LOCAL_SHARED_ANDROID_LIBRARIES),\
   $(call intermediates-dir-for,JAVA_LIBRARIES,$(l),,COMMON)/package-res.apk)
 
 $(my_res_package): PRIVATE_RES_FLAT := $(my_res_resources_flat)
-$(my_res_package): PRIVATE_OVERLAY_FLAT := $(my_overlay_resources_flat) $(my_generated_resources_flata) $(my_static_library_resources)
+$(my_res_package): PRIVATE_OVERLAY_FLAT := $(my_static_library_resources) $(my_generated_resources_flata) $(my_overlay_resources_flat)
 $(my_res_package): PRIVATE_SHARED_ANDROID_LIBRARIES := $(my_shared_library_resources)
 $(my_res_package): PRIVATE_PROGUARD_OPTIONS_FILE := $(proguard_options_file)
 $(my_res_package) : $(full_android_manifest) $(my_static_library_resources) $(my_shared_library_resources)
diff --git a/core/definitions.mk b/core/definitions.mk
index 0e9f454..c29d921 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -1856,7 +1856,6 @@
 define aapt2-link
 $(hide) $(AAPT2) link -o $@ \
   $(PRIVATE_AAPT_FLAGS) \
-  --auto-add-overlay \
   $(addprefix --manifest ,$(PRIVATE_ANDROID_MANIFEST)) \
   $(addprefix -I ,$(PRIVATE_AAPT_INCLUDES)) \
   $(addprefix -I ,$(PRIVATE_SHARED_ANDROID_LIBRARIES)) \
@@ -1864,6 +1863,13 @@
   $(addprefix --proguard ,$(PRIVATE_PROGUARD_OPTIONS_FILE)) \
   $(addprefix --min-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
   $(addprefix --target-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
+  $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product ,$(PRIVATE_TARGET_AAPT_CHARACTERISTICS))) \
+  $(addprefix -c ,$(PRIVATE_PRODUCT_AAPT_CONFIG)) \
+  $(addprefix --preferred-density ,$(PRIVATE_PRODUCT_AAPT_PREF_CONFIG)) \
+  $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
+  $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
+  $(addprefix --rename-manifest-package ,$(PRIVATE_MANIFEST_PACKAGE_NAME)) \
+  $(addprefix --rename-instrumentation-target-package ,$(PRIVATE_MANIFEST_INSTRUMENTATION_FOR)) \
   $(addprefix -R , $(PRIVATE_OVERLAY_FLAT)) \
   $(PRIVATE_RES_FLAT)
 endef
@@ -2274,7 +2280,7 @@
     $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
     $(addprefix --min-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
     $(addprefix --target-sdk-version , $(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
-    $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product , $(TARGET_AAPT_CHARACTERISTICS))) \
+    $(if $(filter --product,$(PRIVATE_AAPT_FLAGS)),,$(addprefix --product , $(PRIVATE_TARGET_AAPT_CHARACTERISTICS))) \
     $(if $(filter --version-code,$(PRIVATE_AAPT_FLAGS)),,--version-code $(PLATFORM_SDK_VERSION)) \
     $(if $(filter --version-name,$(PRIVATE_AAPT_FLAGS)),,--version-name $(APPS_DEFAULT_VERSION_NAME)) \
     $(addprefix --rename-manifest-package , $(PRIVATE_MANIFEST_PACKAGE_NAME)) \
diff --git a/core/package_internal.mk b/core/package_internal.mk
index 3e1c152..2ffbcd4 100644
--- a/core/package_internal.mk
+++ b/core/package_internal.mk
@@ -115,18 +115,31 @@
 need_compile_asset := true
 endif
 
+my_res_package :=
 ifdef LOCAL_USE_AAPT2
 # In aapt2 the last takes precedence.
 my_resource_dirs := $(call reverse-list,$(LOCAL_RESOURCE_DIR))
-my_res_resources :=
-my_overlay_resources :=
-# Treat all but the first directory as overlays.
+my_res_dir :=
+my_overlay_res_dirs :=
+
+ifneq ($(LOCAL_STATIC_ANDROID_LIBRARIES),)
+# If we are using static android libraries, every source file becomes an overlay.
+# This is to emulate old AAPT behavior which simulated library support.
+my_res_dir :=
+my_overlay_res_dirs := $(my_resource_dirs)
+else
+# Without static libraries, the first directory is our directory, which can then be
+# overlaid by the rest. (First directory in my_resource_dirs is last directory in
+# $(LOCAL_RESOURCE_DIR) due to it being reversed.
+my_res_dir := $(firstword $(my_resource_dirs))
+my_overlay_res_dirs := $(wordlist 2,999,$(my_resource_dirs))
+endif
+
 my_overlay_resources := $(strip \
-  $(foreach d,$(wordlist 2,999,$(my_resource_dirs)),\
+  $(foreach d,$(my_overlay_res_dirs),\
     $(addprefix $(d)/, \
         $(call find-subdir-assets,$(d)))))
 
-my_res_dir := $(firstword $(my_resource_dirs))
 my_res_resources := $(strip \
     $(addprefix $(my_res_dir)/, \
         $(call find-subdir-assets,$(my_res_dir))))
@@ -313,13 +326,12 @@
 
 ifeq ($(need_compile_res),true)
 ifdef LOCAL_USE_AAPT2
-# my_aapt_characteristics := $(TARGET_AAPT_CHARACTERISTICS)
-my_aapt_characteristics :=
 my_compiled_res_base_dir := $(intermediates)/flat-res
 my_generated_res_dirs := $(rs_generated_res_dir)
 my_generated_res_dirs_deps := $(RenderScript_file_stamp)
-# Add AAPT2 specific flags.
-$(LOCAL_INTERMEDIATE_TARGETS) : PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --no-static-lib-packages
+# Add AAPT2 link specific flags.
+$(my_res_package): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --no-static-lib-packages
+$(my_res_package): PRIVATE_TARGET_AAPT_CHARACTERISTICS := $(TARGET_AAPT_CHARACTERISTICS)
 include $(BUILD_SYSTEM)/aapt2.mk
 else  # LOCAL_USE_AAPT2
 
@@ -484,14 +496,14 @@
     LOCAL_AAPT_INCLUDE_ALL_RESOURCES := true
 endif
 ifeq ($(LOCAL_AAPT_INCLUDE_ALL_RESOURCES),true)
-    $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_CONFIG :=
-    $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
+    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_CONFIG :=
+    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
 else
-    $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_CONFIG := $(PRODUCT_AAPT_CONFIG)
+    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_CONFIG := $(PRODUCT_AAPT_CONFIG)
 ifdef LOCAL_PACKAGE_SPLITS
-    $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
+    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
 else
-    $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG := $(PRODUCT_AAPT_PREF_CONFIG)
+    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG := $(PRODUCT_AAPT_PREF_CONFIG)
 endif
 endif
 $(LOCAL_BUILT_MODULE): PRIVATE_DONT_DELETE_JAR_DIRS := $(LOCAL_DONT_DELETE_JAR_DIRS)
diff --git a/core/prebuilt_internal.mk b/core/prebuilt_internal.mk
index 7c08af7..958525c 100644
--- a/core/prebuilt_internal.mk
+++ b/core/prebuilt_internal.mk
@@ -333,11 +333,13 @@
 $(my_res_package): PRIVATE_PROGUARD_OPTIONS_FILE :=
 $(my_res_package): PRIVATE_DEFAULT_APP_TARGET_SDK :=
 $(my_res_package): PRIVATE_DEFAULT_APP_TARGET_SDK :=
+$(my_res_package): PRIVATE_PRODUCT_AAPT_CONFIG :=
+$(my_res_package): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
+$(my_res_package): PRIVATE_TARGET_AAPT_CHARACTERISTICS :=
 
 full_android_manifest :=
 my_res_resources :=
 my_overlay_resources :=
-my_aapt_characteristics :=
 my_compiled_res_base_dir :=
 R_file_stamp :=
 proguard_options_file :=
diff --git a/core/static_java_library.mk b/core/static_java_library.mk
index e70e783..0b521d5 100644
--- a/core/static_java_library.mk
+++ b/core/static_java_library.mk
@@ -120,27 +120,31 @@
 # AAR needs text symbol file R.txt.
 ifdef LOCAL_USE_AAPT2
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --static-lib --no-static-lib-packages
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PRODUCT_AAPT_CONFIG :=
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_TARGET_AAPT_CHARACTERISTICS :=
 else
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --non-constant-id --output-text-symbols $(LOCAL_INTERMEDIATE_SOURCE_DIR)
 endif
+
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_SOURCE_INTERMEDIATES_DIR := $(LOCAL_INTERMEDIATE_SOURCE_DIR)
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ANDROID_MANIFEST := $(full_android_manifest)
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_PUBLICS_OUTPUT := $(intermediates.COMMON)/public_resources.xml
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_INCLUDES := $(framework_res_package_export)
+
 ifneq (,$(filter-out current system_current test_current, $(LOCAL_SDK_VERSION)))
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEFAULT_APP_TARGET_SDK := $(LOCAL_SDK_VERSION)
 else
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEFAULT_APP_TARGET_SDK := $(DEFAULT_APP_TARGET_SDK)
 endif
+
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASSET_DIR :=
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PROGUARD_OPTIONS_FILE := $(proguard_options_file)
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_PACKAGE_NAME :=
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_INSTRUMENTATION_FOR :=
 
 ifdef LOCAL_USE_AAPT2
-# For libraries don't use any product specific flags.
-my_aapt_characteristics :=
 # One more level with name res so we can zip up the flat resources that can be linked by apps.
 my_compiled_res_base_dir := $(intermediates.COMMON)/flat-res/res
 my_generated_res_dirs := $(rs_generated_res_dir)