Allow forcing AAPT2 on

Building with FORCE_AAPT2=true will turn on AAPT2 for all modules
unless they set LOCAL_USE_AAPT2 := false.  The build system will
attempt to rewrite common AAPT patterns into AAPT2 patterns,
including removing --extra-packages for support library packages,
removing LOCAL_RESOURCE_DIR point to support library resources,
adding a default empty manifest file if it doesn't exist, and
converting LOCAL_STATIC_JAVA_AAR_LIBRARIES to
LOCAL_STATIC_ANDROID_LIBRARIES.

Bug: 79481102
Test: m checkbuild
Change-Id: I8d9d55fe4d5d5c965c64b0407efe74e0afc35c3a
This commit is contained in:
Colin Cross 2018-05-09 13:29:51 -07:00
parent c27df2b71b
commit 2029903b64
8 changed files with 95 additions and 15 deletions

View File

@ -23,7 +23,7 @@ my_full_libs_manifest_files += $(foreach lib, $(LOCAL_STATIC_JAVA_AAR_LIBRARIES)
$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/aar/AndroidManifest.xml)
# With aapt2, we'll link in the built resource from the AAR.
ifndef LOCAL_USE_AAPT2
ifneq ($(LOCAL_USE_AAPT2),true)
LOCAL_RESOURCE_DIR += $(foreach lib, $(LOCAL_STATIC_JAVA_AAR_LIBRARIES),\
$(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/aar/res)
endif # LOCAL_USE_AAPT2

View File

@ -2147,6 +2147,17 @@ $(SOONG_ZIP) -o $(PRIVATE_SRCJAR) -C $(PRIVATE_JAVA_GEN_DIR) -D $(PRIVATE_JAVA_G
$(EXTRACT_JAR_PACKAGES) -i $(PRIVATE_SRCJAR) -o $(PRIVATE_AAPT_EXTRA_PACKAGES) --prefix '--extra-packages '
endef
define _create-default-manifest-file
$(1):
rm -f $1
echo '<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="missing.manifest"></manifest>' > $1
endef
define create-default-manifest-file
$(eval $(call _create-default-manifest-file,$(1)))
endef
###########################################################
xlint_unchecked := -Xlint:unchecked

63
core/force_aapt2.mk Normal file
View File

@ -0,0 +1,63 @@
#
# Copyright (C) 2018 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.
#
# Including this makefile will force AAPT2 on if FORCE_AAPT2==true,
# rewriting some properties to convert standard AAPT usage to AAPT2.
ifeq ($(FORCE_AAPT2),true)
ifneq ($(LOCAL_USE_AAPT2),true)
# Force AAPT2 on
LOCAL_USE_AAPT2 := true
# Move LOCAL_STATIC_JAVA_AAR_LIBRARIES to LOCAL_STATIC_ANDROID_LIBRARIES
LOCAL_STATIC_ANDROID_LIBRARIES := $(strip $(LOCAL_STATIC_ANDROID_LIBRARIES) $(LOCAL_STATIC_JAVA_AAR_LIBRARIES))
LOCAL_STATIC_JAVA_AAR_LIBRARIES :=
# Filter out support library resources
LOCAL_RESOURCE_DIR := $(filter-out \
prebuilts/sdk/current/% \
frameworks/support/%,\
$(LOCAL_RESOURCE_DIR))
# Filter out unnecessary aapt flags
LOCAL_AAPT_FLAGS := $(subst --extra-packages=,--extra-packages$(space), \
$(filter-out \
--extra-packages=android.support.% \
--extra-packages=androidx.% \
--auto-add-overlay,\
$(subst --extra-packages$(space),--extra-packages=,$(LOCAL_AAPT_FLAGS))))
# AAPT2 is pickier about missing resources. Support library may have references to resources
# added in current, so always treat LOCAL_SDK_VERSION as LOCAL_SDK_RES_VERSION := current.
ifdef LOCAL_SDK_VERSION
LOCAL_SDK_RES_VERSION := current
endif
ifeq (,$(strip $(LOCAL_MANIFEST_FILE)$(LOCAL_FULL_MANIFEST_FILE)))
ifeq (,$(wildcard $(LOCAL_PATH)/AndroidManifest.xml))
# work around missing manifests by creating a default one
$(call pretty-warning, Missing manifest file)
LOCAL_FULL_MANIFEST_FILE := $(call local-intermediates-dir,COMMON)/DefaultManifest.xml
$(call create-default-manifest-file,$(LOCAL_FULL_MANIFEST_FILE))
endif
endif
endif
endif
ifneq ($(LOCAL_USE_AAPT2),true)
ifneq ($(LOCAL_USE_AAPT2),false)
ifneq ($(LOCAL_USE_AAPT2),)
$(call pretty-error,Invalid value for LOCAL_USE_AAPT2: "$(LOCAL_USE_AAPT2)")
endif
endif
endif

View File

@ -444,7 +444,7 @@ ifneq ($(LOCAL_INSTRUMENTATION_FOR)$(filter tests,$(LOCAL_MODULE_TAGS)),)
common_proguard_flags += -dontshrink # don't shrink tests by default
endif # test package
ifneq ($(LOCAL_PROGUARD_ENABLED),custom)
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
common_proguard_flag_files += $(foreach l,$(LOCAL_STATIC_ANDROID_LIBRARIES),\
$(call intermediates-dir-for,JAVA_LIBRARIES,$(l),,COMMON)/export_proguard_flags)
endif

View File

@ -477,7 +477,7 @@ endif
ifdef LOCAL_AAPT2_ONLY
my_link_type += aapt2_only
endif
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
my_allowed_types += aapt2_only
endif

View File

@ -87,6 +87,8 @@ else
LOCAL_RESOURCE_DIR := $(foreach d,$(LOCAL_RESOURCE_DIR),$(call clean-path,$(d)))
endif
include $(BUILD_SYSTEM)/force_aapt2.mk
package_resource_overlays := $(strip \
$(wildcard $(foreach dir, $(PRODUCT_PACKAGE_OVERLAYS), \
$(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))) \
@ -153,7 +155,7 @@ LOCAL_USE_AAPT2 := true
endif
my_res_package :=
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
# In aapt2 the last takes precedence.
my_resource_dirs := $(call reverse-list,$(LOCAL_RESOURCE_DIR))
my_res_dir :=
@ -357,7 +359,7 @@ $(R_file_stamp) $(my_res_package): PRIVATE_MANIFEST_INSTRUMENTATION_FOR := $(LOC
###############################
## AAPT/AAPT2
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
my_compiled_res_base_dir := $(intermediates.COMMON)/flat-res
ifneq (,$(renderscript_target_api))
ifneq ($(call math_gt_or_eq,$(renderscript_target_api),21),true)
@ -509,7 +511,7 @@ $(resource_export_package) $(R_file_stamp) $(LOCAL_BUILT_MODULE): $(all_library_
$(LOCAL_INTERMEDIATE_TARGETS): \
PRIVATE_AAPT_INCLUDES := $(all_library_res_package_exports)
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
$(my_res_package) : $(all_library_res_package_export_deps)
endif
@ -593,7 +595,7 @@ $(LOCAL_BUILT_MODULE): PRIVATE_RESOURCE_INTERMEDIATES_DIR := $(intermediates.COM
$(LOCAL_BUILT_MODULE): PRIVATE_FULL_CLASSES_JAR := $(full_classes_jar)
$(LOCAL_BUILT_MODULE) : $(jni_shared_libraries)
$(LOCAL_BUILT_MODULE) : $(JAR_ARGS)
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
$(LOCAL_BUILT_MODULE): PRIVATE_RES_PACKAGE := $(my_res_package)
$(LOCAL_BUILT_MODULE) : $(my_res_package) $(AAPT2) | $(ACP)
else
@ -604,7 +606,7 @@ ifdef LOCAL_COMPRESSED_MODULE
$(LOCAL_BUILT_MODULE) : $(MINIGZIP)
endif
@echo "target Package: $(PRIVATE_MODULE) ($@)"
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
$(call copy-file-to-new-target)
else # ! LOCAL_USE_AAPT2
$(if $(PRIVATE_SOURCE_ARCHIVE),\
@ -620,7 +622,7 @@ ifeq ($(full_classes_jar),)
$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
else # full_classes_jar
$(add-dex-to-package)
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
$(call add-jar-resources-to-package,$@,$(PRIVATE_FULL_CLASSES_JAR),$(PRIVATE_RESOURCE_INTERMEDIATES_DIR))
endif
endif # full_classes_jar

View File

@ -607,11 +607,13 @@ $(common_classes_pre_proguard_jar) : $(my_src_jar)
$(common_javalib_jar) : $(common_classes_jar)
$(transform-prebuilt-to-target)
include $(BUILD_SYSTEM)/force_aapt2.mk
ifdef LOCAL_AAPT2_ONLY
LOCAL_USE_AAPT2 := true
endif
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
ifneq ($(my_src_aar),)
$(intermediates.COMMON)/export_proguard_flags : $(my_src_proguard_options)

View File

@ -28,6 +28,8 @@ intermediates.COMMON := $(call local-intermediates-dir,COMMON)
my_res_package :=
include $(BUILD_SYSTEM)/force_aapt2.mk
ifdef LOCAL_AAPT2_ONLY
LOCAL_USE_AAPT2 := true
endif
@ -44,7 +46,7 @@ ifdef LOCAL_RESOURCE_DIR
need_compile_res := true
LOCAL_RESOURCE_DIR := $(foreach d,$(LOCAL_RESOURCE_DIR),$(call clean-path,$(d)))
endif
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
ifneq ($(LOCAL_STATIC_ANDROID_LIBRARIES),)
need_compile_res := true
endif
@ -82,7 +84,7 @@ LOCAL_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_
R_file_stamp := $(intermediates.COMMON)/src/R.stamp
LOCAL_INTERMEDIATE_TARGETS += $(R_file_stamp)
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
# For library we treat all the resource equal with no overlay.
my_res_resources := $(all_resources)
my_overlay_resources :=
@ -117,7 +119,7 @@ framework_res_package_export := \
endif
endif
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
import_proguard_flag_files := $(strip $(foreach l,$(LOCAL_STATIC_ANDROID_LIBRARIES),\
$(call intermediates-dir-for,JAVA_LIBRARIES,$(l),,COMMON)/export_proguard_flags))
$(intermediates.COMMON)/export_proguard_flags: $(import_proguard_flag_files) $(addprefix $(LOCAL_PATH)/,$(LOCAL_EXPORT_PROGUARD_FLAG_FILES))
@ -140,7 +142,7 @@ $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_INSTRUMENTATION_FOR := $(LOCAL_M
# add --non-constant-id to prevent inlining constants.
# AAR needs text symbol file R.txt.
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --static-lib --output-text-symbols $(intermediates.COMMON)/R.txt
ifndef LOCAL_AAPT_NAMESPACES
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS += --no-static-lib-packages
@ -168,7 +170,7 @@ $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PROGUARD_OPTIONS_FILE := $(proguard_optio
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_PACKAGE_NAME :=
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_INSTRUMENTATION_FOR :=
ifdef LOCAL_USE_AAPT2
ifeq ($(LOCAL_USE_AAPT2),true)
# 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
ifneq (,$(renderscript_target_api))