AAPT2: library resource compilation

- Switch to static library's package-res.apk as the link artifact.
- For prebuilt AAR file, build package-res.apk using aapt2's "--dir"
  option.
- New LOCAL variables:
  LOCAL_USE_AAPT2: a module can opt-in with "LOCAL_USE_AAPT2 := true".
  LOCAL_STATIC_ANDROID_LIBRARIES: static Java libraries with compiled
                   resources.
  LOCAL_SHARED_ANDROID_LIBRARIES: shared Java libraries with compiled
                   resources.

Bug: 25958912
Change-Id: I4be2d1e8137555986338c2583cb39b0c97391751
diff --git a/core/aapt2.mk b/core/aapt2.mk
index 5c4cf73..1d47c54 100644
--- a/core/aapt2.mk
+++ b/core/aapt2.mk
@@ -3,8 +3,11 @@
 # Input variables:
 # full_android_manifest,
 # my_res_resources, my_overlay_resources, my_aapt_characteristics,
-# my_compiled_res_base_dir, rs_generated_res_dir, my_res_package,
+# 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.
+# my_generated_res_dirs_deps: the dependency to use for my_generated_res_dirs.
+#
 # Output variables:
 # my_res_resources_flat, my_overlay_resources_flat,
 # my_generated_resources_flata
@@ -27,39 +30,45 @@
 
 my_generated_resources_flata :=
 # Compile generated resources
-ifneq ($(rs_generated_res_dir),)
-rs_gen_resource_flata := $(my_compiled_res_base_dir)/renderscript_gen_res.flata
-$(rs_gen_resource_flata): PRIVATE_SOURCE_RES_DIR := $(rs_generated_res_dir)
-$(rs_gen_resource_flata) : $(RenderScript_file_stamp)
-	@echo "AAPT2 compile $@ <- $(PRIVATE_SOURCE_RES_DIR)"
-	$(call aapt2-compile-one-resource-dir)
+ifneq ($(my_generated_res_dirs),)
+my_generated_resources_flata := $(my_compiled_res_base_dir)/gen_res.flata
+$(my_generated_resources_flata): PRIVATE_SOURCE_RES_DIRS := $(my_generated_res_dirs)
+$(my_generated_resources_flata) : $(my_generated_res_dirs_deps)
+	@echo "AAPT2 compile $@ <- $(PRIVATE_SOURCE_RES_DIRS)"
+	$(call aapt2-compile-resource-dirs)
 
-my_generated_resources_flata += $(rs_gen_resource_flata)
+my_generated_resources_flata += $(my_generated_resources_flata)
 endif
 
 $(my_res_resources_flat) $(my_overlay_resources_flat) $(my_generated_resources_flata): \
   PRIVATE_AAPT2_CFLAGS := $(addprefix --product ,$(my_aapt_characteristics)) $(PRODUCT_AAPT2_CFLAGS)
 
-# Link the static library resource packages.
-my_static_library_resources := $(foreach l, $(LOCAL_STATIC_JAVA_LIBRARIES),\
-  $(call intermediates-dir-for,JAVA_LIBRARIES,$(l),,COMMON)/library-res.flata)
+my_static_library_resources := $(foreach l, $(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_SHARED_ANDROID_LIBRARIES := $(my_shared_library_resources)
 $(my_res_package): PRIVATE_PROGUARD_OPTIONS_FILE := $(proguard_options_file)
-$(my_res_package) : $(full_android_manifest)
+$(my_res_package) : $(full_android_manifest) $(my_static_library_resources) $(my_shared_library_resources)
 $(my_res_package) : $(my_res_resources_flat) $(my_overlay_resources_flat) \
   $(my_generated_resources_flata) $(my_static_library_resources) \
   $(AAPT2)
 	@echo "AAPT2 link $@"
 	$(call aapt2-link)
 
+ifdef R_file_stamp
 $(R_file_stamp) : $(my_res_package) | $(ACP)
 	@echo "target R.java/Manifest.java: $(PRIVATE_MODULE) ($@)"
 	@rm -rf $@ && mkdir -p $(dir $@)
 	$(call find-generated-R.java)
+endif
 
+ifdef proguard_options_file
 $(proguard_options_file) : $(my_res_package)
+endif
 
 resource_export_package :=
 ifdef LOCAL_EXPORT_PACKAGE_RESOURCES
diff --git a/core/android_manifest.mk b/core/android_manifest.mk
index b945ea9..0093e02 100644
--- a/core/android_manifest.mk
+++ b/core/android_manifest.mk
@@ -22,11 +22,11 @@
 my_full_libs_manifest_files += $(foreach lib, $(LOCAL_STATIC_JAVA_AAR_LIBRARIES),\
   $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/aar/AndroidManifest.xml)
 
-# With USE_AAPT2, we'll link in the built resource from the AAR.
-ifndef USE_AAPT2
+# With aapt2, we'll link in the built resource from the AAR.
+ifndef LOCAL_USE_AAPT2
 LOCAL_RESOURCE_DIR += $(foreach lib, $(LOCAL_STATIC_JAVA_AAR_LIBRARIES),\
   $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/aar/res)
-endif  # USE_AAPT2
+endif  # LOCAL_USE_AAPT2
 endif  # LOCAL_STATIC_JAVA_AAR_LIBRARIES
 
 # Set up rules to merge library manifest files
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index 069fbf9..bb6ce93 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -193,6 +193,9 @@
 LOCAL_MODULE_HOST_OS:=
 LOCAL_FINDBUGS_FLAGS:=
 LOCAL_NOTICE_FILE:=
+LOCAL_USE_AAPT2:=$(USE_AAPT2)
+LOCAL_STATIC_ANDROID_LIBRARIES:=
+LOCAL_SHARED_ANDROID_LIBRARIES:=
 
 # arch specific variables
 LOCAL_SRC_FILES_$(TARGET_ARCH):=
diff --git a/core/definitions.mk b/core/definitions.mk
index 2586398..0e9f454 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -1825,9 +1825,9 @@
 $(hide) $(AAPT2) compile -o $(dir $@) $(PRIVATE_AAPT2_CFLAGS) --legacy $<
 endef
 
-define aapt2-compile-one-resource-dir
+define aapt2-compile-resource-dirs
 @mkdir -p $(dir $@)
-$(hide) $(AAPT2) compile -o $@ --dir $(PRIVATE_SOURCE_RES_DIR) \
+$(hide) $(AAPT2) compile -o $@ $(addprefix --dir ,$(PRIVATE_SOURCE_RES_DIRS)) \
   $(PRIVATE_AAPT2_CFLAGS) --legacy
 endef
 
@@ -1856,8 +1856,10 @@
 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)) \
   $(addprefix --java ,$(PRIVATE_SOURCE_INTERMEDIATES_DIR)) \
   $(addprefix --proguard ,$(PRIVATE_PROGUARD_OPTIONS_FILE)) \
   $(addprefix --min-sdk-version ,$(PRIVATE_DEFAULT_APP_TARGET_SDK)) \
diff --git a/core/java.mk b/core/java.mk
index d8bbe37..5848fca 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -57,10 +57,12 @@
 endif
 endif
 
-# LOCAL_STATIC_JAVA_AAR_LIBRARIES are special LOCAL_STATIC_JAVA_LIBRARIES
-LOCAL_STATIC_JAVA_LIBRARIES := $(strip $(LOCAL_STATIC_JAVA_LIBRARIES) $(LOCAL_STATIC_JAVA_AAR_LIBRARIES))
-
-LOCAL_JAVA_LIBRARIES := $(sort $(LOCAL_JAVA_LIBRARIES))
+# LOCAL_STATIC_JAVA_AAR_LIBRARIES and LOCAL_STATIC_ANDROID_LIBRARIES are also LOCAL_STATIC_JAVA_LIBRARIES.
+LOCAL_STATIC_JAVA_LIBRARIES := $(strip $(LOCAL_STATIC_JAVA_LIBRARIES) \
+    $(LOCAL_STATIC_JAVA_AAR_LIBRARIES) \
+    $(LOCAL_STATIC_ANDROID_LIBRARIES))
+# LOCAL_SHARED_ANDROID_LIBRARIES are also LOCAL_JAVA_LIBRARIES.
+LOCAL_JAVA_LIBRARIES := $(sort $(LOCAL_JAVA_LIBRARIES) $(LOCAL_SHARED_ANDROID_LIBRARIES))
 
 LOCAL_BUILT_MODULE_STEM := $(strip $(LOCAL_BUILT_MODULE_STEM))
 ifeq ($(LOCAL_BUILT_MODULE_STEM),)
diff --git a/core/package_internal.mk b/core/package_internal.mk
index c1da5ed..3e1c152 100644
--- a/core/package_internal.mk
+++ b/core/package_internal.mk
@@ -115,7 +115,7 @@
 need_compile_asset := true
 endif
 
-ifdef USE_AAPT2
+ifdef LOCAL_USE_AAPT2
 # In aapt2 the last takes precedence.
 my_resource_dirs := $(call reverse-list,$(LOCAL_RESOURCE_DIR))
 my_res_resources :=
@@ -140,7 +140,7 @@
 # Always run aapt2
 need_compile_res := true
 
-else  # USE_AAPT2
+else  # LOCAL_USE_AAPT2
 all_resources := $(strip \
     $(foreach dir, $(LOCAL_RESOURCE_DIR), \
       $(addprefix $(dir)/, \
@@ -150,7 +150,7 @@
        ) \
      ))
 
-endif  # USE_AAPT2
+endif  # LOCAL_USE_AAPT2
 
 ifneq ($(all_resources),)
   need_compile_res := true
@@ -312,11 +312,16 @@
 endif  # LOCAL_DATA_BINDING
 
 ifeq ($(need_compile_res),true)
-ifdef USE_AAPT2
-my_aapt_characteristics := $(TARGET_AAPT_CHARACTERISTICS)
+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
 include $(BUILD_SYSTEM)/aapt2.mk
-else  # USE_AAPT2
+else  # LOCAL_USE_AAPT2
 
 # Since we don't know where the real R.java file is going to end up,
 # we need to use another file to stand in its place.  We'll just
@@ -356,7 +361,7 @@
 	$(add-assets-to-package)
 endif
 
-endif  # USE_AAPT2
+endif  # LOCAL_USE_AAPT2
 
 # Other modules should depend on the BUILT module if
 # they want to use this module's R.java file.
@@ -419,7 +424,7 @@
 $(LOCAL_INTERMEDIATE_TARGETS): \
     PRIVATE_AAPT_INCLUDES := $(all_library_res_package_exports)
 
-ifdef USE_AAPT2
+ifdef LOCAL_USE_AAPT2
 $(my_res_package) : $(all_library_res_package_export_deps)
 endif
 endif # LOCAL_NO_STANDARD_LIBRARIES
@@ -491,14 +496,14 @@
 endif
 $(LOCAL_BUILT_MODULE): PRIVATE_DONT_DELETE_JAR_DIRS := $(LOCAL_DONT_DELETE_JAR_DIRS)
 $(LOCAL_BUILT_MODULE) : $(jni_shared_libraries)
-ifdef USE_AAPT2
+ifdef LOCAL_USE_AAPT2
 $(LOCAL_BUILT_MODULE): PRIVATE_RES_PACKAGE := $(my_res_package)
 $(LOCAL_BUILT_MODULE) : $(my_res_package) $(AAPT2) | $(ACP)
 else
 $(LOCAL_BUILT_MODULE) : $(all_res_assets) $(full_android_manifest) $(AAPT)
 endif
 	@echo "target Package: $(PRIVATE_MODULE) ($@)"
-ifdef USE_AAPT2
+ifdef LOCAL_USE_AAPT2
 ifdef LOCAL_JACK_ENABLED
 	$(call copy-file-to-new-target)
 else
@@ -507,7 +512,7 @@
 	  $(call merge-two-packages,$(PRIVATE_RES_PACKAGE) $(PRIVATE_SOURCE_ARCHIVE),$@),
 	  $(call copy-file-to-new-target))
 endif
-else  # USE_AAPT2
+else  # LOCAL_USE_AAPT2
 ifdef LOCAL_JACK_ENABLED
 	$(create-empty-package)
 else
@@ -516,7 +521,7 @@
 	  $(create-empty-package))
 endif
 	$(add-assets-to-package)
-endif  # USE_AAPT2
+endif  # LOCAL_USE_AAPT2
 ifneq ($(jni_shared_libraries),)
 	$(add-jni-shared-libs-to-package)
 endif
diff --git a/core/prebuilt_internal.mk b/core/prebuilt_internal.mk
index ca70473..36d2df5 100644
--- a/core/prebuilt_internal.mk
+++ b/core/prebuilt_internal.mk
@@ -322,26 +322,34 @@
 
 $(call define-jar-to-toc-rule, $(common_classes_jar))
 
-ifdef USE_AAPT2
-my_library_resources := $(intermediates.COMMON)/library-res.flata
+ifdef LOCAL_USE_AAPT2
 ifneq ($(my_src_aar),)
-# Compile the AAR resources to a .flata.
-$(my_library_resources): PRIVATE_SOURCE_RES_DIR := $(intermediates.COMMON)/aar/res
-$(my_library_resources) : $(my_src_jar)
-	@echo "AAPT2 compile AAR $@ <- $(PRIVATE_SOURCE_RES_DIR)"
-	$(call aapt2-compile-one-resource-dir)
-else  # $(my_src_aar)
-# Create an empty packag for prebuilt static Java library.
-# TODO: support compiled resources inside a prebuilt static Java library.
-$(my_library_resources): PRIVATE_RES_DIR := $(intermediates.COMMON)/flat-res/res
-$(my_library_resources) :
-	@echo "Create empty library resources $@"
-	@rm -rf $@ && mkdir -p $(dir $@) $(PRIVATE_RES_DIR)
-	$(hide) cd $(dir $(PRIVATE_RES_DIR)) && zip -qrX $(abspath $@) $(notdir $(PRIVATE_RES_DIR))
+my_res_package := $(intermediates.COMMON)/package-res.apk
+
+# We needed only very few PRIVATE variables and aapt2.mk input variables. Reset the unnecessary ones.
+$(my_res_package): PRIVATE_AAPT2_CFLAGS :=
+$(my_res_package): PRIVATE_ANDROID_MANIFEST := $(intermediates.COMMON)/aar/AndroidManifest.xml
+$(my_res_package): PRIVATE_AAPT_INCLUDES :=
+$(my_res_package): PRIVATE_SOURCE_INTERMEDIATES_DIR :=
+$(my_res_package): PRIVATE_PROGUARD_OPTIONS_FILE :=
+$(my_res_package): PRIVATE_DEFAULT_APP_TARGET_SDK :=
+$(my_res_package): PRIVATE_DEFAULT_APP_TARGET_SDK :=
+
+full_android_manifest :=
+my_res_resources :=
+my_overlay_resources :=
+my_aapt_characteristics :=
+my_compiled_res_base_dir :=
+R_file_stamp :=
+proguard_options_file :=
+my_generated_res_dirs := $(intermediates.COMMON)/aar/res
+my_generated_res_dirs_deps := $(my_src_jar)
+include $(BUILD_SYSTEM)/aapt2.mk
+
+# Make sure my_res_package is created when you run mm/mmm.
+$(built_module) : $(my_res_package)
 endif  # $(my_src_aar)
-# Make sure my_library_resources is created when you run mm/mmm.
-$(built_module) : $(my_library_resources)
-endif  # USE_AAPT2
+endif  # LOCAL_USE_AAPT2
 # 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/product_config.mk b/core/product_config.mk
index 151dac0..c0a1a9d 100644
--- a/core/product_config.mk
+++ b/core/product_config.mk
@@ -302,11 +302,8 @@
 PRODUCT_AAPT2_CFLAGS :=
 ifneq ($(filter en_XA ar_XB,$(PRODUCT_LOCALES)),)
   # Force generating resources for pseudo-locales.
-  ifdef USE_AAPT2
-    PRODUCT_AAPT2_CFLAGS += --pseudo-localize
-  else
-    PRODUCT_AAPT_FLAGS += --pseudo-localize
-  endif
+  PRODUCT_AAPT2_CFLAGS += --pseudo-localize
+  PRODUCT_AAPT_FLAGS += --pseudo-localize
 endif
 
 PRODUCT_BRAND := $(strip $(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_BRAND))
diff --git a/core/static_java_library.mk b/core/static_java_library.mk
index 6efeb35..e70e783 100644
--- a/core/static_java_library.mk
+++ b/core/static_java_library.mk
@@ -30,9 +30,6 @@
 intermediates.COMMON := $(call local-intermediates-dir,COMMON)
 
 my_res_package :=
-# A zip file that apps can link with aapt2
-# We need my_library_resources as dependency even if a library doesn't have resource.
-my_library_resources := $(intermediates.COMMON)/library-res.flata
 
 # Hack to build static Java library with Android resource
 # See bug 5714516
@@ -80,7 +77,7 @@
 R_file_stamp := $(intermediates.COMMON)/src/R.stamp
 LOCAL_INTERMEDIATE_TARGETS += $(R_file_stamp)
 
-ifdef USE_AAPT2
+ifdef LOCAL_USE_AAPT2
 # For library we treat all the resource equal with no overlay.
 my_res_resources := $(all_resources)
 my_overlay_resources :=
@@ -88,7 +85,7 @@
 my_res_package := $(intermediates.COMMON)/package-res.apk
 
 LOCAL_INTERMEDIATE_TARGETS += $(my_res_package)
-endif  # USE_AAPT2
+endif  # LOCAL_USE_AAPT2
 endif  # LOCAL_RESOURCE_DIR
 
 all_res_assets := $(all_resources)
@@ -121,8 +118,8 @@
 
 # add --non-constant-id to prevent inlining constants.
 # AAR needs text symbol file R.txt.
-ifdef USE_AAPT2
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --non-final-ids
+ifdef LOCAL_USE_AAPT2
+$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --static-lib --no-static-lib-packages
 else
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --non-constant-id --output-text-symbols $(LOCAL_INTERMEDIATE_SOURCE_DIR)
 endif
@@ -141,11 +138,13 @@
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_PACKAGE_NAME :=
 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_INSTRUMENTATION_FOR :=
 
-ifdef USE_AAPT2
+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)
+my_generated_res_dirs_deps := $(RenderScript_file_stamp)
 include $(BUILD_SYSTEM)/aapt2.mk
 $(my_res_package) : $(framework_res_package_export_deps)
 else
@@ -153,7 +152,7 @@
 	@echo "target R.java/Manifest.java: $(PRIVATE_MODULE) ($@)"
 	$(create-resource-java-files)
 	$(hide) find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name R.java | xargs cat > $@
-endif  # USE_AAPT2
+endif  # LOCAL_USE_AAPT2
 
 $(LOCAL_BUILT_MODULE): $(R_file_stamp)
 ifdef LOCAL_JACK_ENABLED
@@ -184,18 +183,6 @@
 ALL_MODULES.$(LOCAL_MODULE).AAR := $(built_aar)
 endif  # need_compile_res
 
-ifdef USE_AAPT2
-# If a static Java library has no resources, create empty package apps can depend on.
-$(my_library_resources): PRIVATE_RES_DIR := $(intermediates.COMMON)/flat-res/res
-$(my_library_resources) : $(my_res_package)
-	@echo "Package static library resources $@"
-	@rm -rf $@ && mkdir -p $(dir $@) $(PRIVATE_RES_DIR)
-	$(hide) cd $(dir $(PRIVATE_RES_DIR)) && zip -qrX $(abspath $@) $(notdir $(PRIVATE_RES_DIR))
-
-# Make sure my_library_resources is created when you run mm/mmm.
-$(LOCAL_BUILT_MODULE) : $(my_library_resources)
-endif  # USE_AAPT2
-
 # Reset internal variables.
 all_res_assets :=
 LOCAL_IS_STATIC_JAVA_LIBRARY :=