1# Handle AndroidManifest.xmls
2# Input: LOCAL_MANIFEST_FILE, LOCAL_FULL_MANIFEST_FILE, LOCAL_FULL_LIBS_MANIFEST_FILES,
3#        LOCAL_USE_EMBEDDED_NATIVE_LIBS
4# Output: full_android_manifest
5
6ifeq ($(strip $(LOCAL_MANIFEST_FILE)),)
7  LOCAL_MANIFEST_FILE := AndroidManifest.xml
8endif
9ifdef LOCAL_FULL_MANIFEST_FILE
10  main_android_manifest := $(LOCAL_FULL_MANIFEST_FILE)
11else
12  main_android_manifest := $(LOCAL_PATH)/$(LOCAL_MANIFEST_FILE)
13endif
14
15LOCAL_STATIC_JAVA_AAR_LIBRARIES := $(strip $(LOCAL_STATIC_JAVA_AAR_LIBRARIES))
16
17my_full_libs_manifest_files :=
18
19ifndef LOCAL_DONT_MERGE_MANIFESTS
20  my_full_libs_manifest_files += $(LOCAL_FULL_LIBS_MANIFEST_FILES)
21
22  my_full_libs_manifest_files += $(foreach lib, $(LOCAL_STATIC_JAVA_AAR_LIBRARIES) $(LOCAL_STATIC_ANDROID_LIBRARIES),\
23    $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/manifest/AndroidManifest.xml)
24endif
25
26full_android_manifest := $(intermediates.COMMON)/manifest/AndroidManifest.xml
27
28ifneq (,$(strip $(my_full_libs_manifest_files)))
29  # Set up rules to merge library manifest files
30  fixed_android_manifest := $(intermediates.COMMON)/manifest/AndroidManifest.xml.fixed
31
32  $(full_android_manifest): PRIVATE_LIBS_MANIFESTS := $(my_full_libs_manifest_files)
33  $(full_android_manifest): $(ANDROID_MANIFEST_MERGER)
34  $(full_android_manifest) : $(fixed_android_manifest) $(my_full_libs_manifest_files)
35	@echo "Merge android manifest files: $@ <-- $< $(PRIVATE_LIBS_MANIFESTS)"
36	@mkdir -p $(dir $@)
37	$(hide) $(ANDROID_MANIFEST_MERGER) --main $< \
38	    --libs $(call normalize-path-list,$(PRIVATE_LIBS_MANIFESTS)) \
39	    --out $@
40else
41  fixed_android_manifest := $(full_android_manifest)
42endif
43
44my_target_sdk_version := $(call module-target-sdk-version)
45my_min_sdk_version := $(call module-min-sdk-version)
46
47ifdef TARGET_BUILD_APPS
48  ifndef TARGET_BUILD_USE_PREBUILT_SDKS
49    ifeq ($(my_target_sdk_version),$(PLATFORM_VERSION_CODENAME))
50      ifdef UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT
51        my_target_sdk_version := $(my_target_sdk_version).$$(cat $(API_FINGERPRINT))
52        my_min_sdk_version := $(my_min_sdk_version).$$(cat $(API_FINGERPRINT))
53        $(fixed_android_manifest): $(API_FINGERPRINT)
54      else ifdef UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA
55        my_target_sdk_version := $(UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA)
56        my_min_sdk_version := $(UNBUNDLED_BUILD_TARGET_SDK_WITH_DESSERT_SHA)
57      endif
58    endif
59  endif
60endif
61
62$(fixed_android_manifest): PRIVATE_MIN_SDK_VERSION := $(my_min_sdk_version)
63$(fixed_android_manifest): PRIVATE_TARGET_SDK_VERSION := $(my_target_sdk_version)
64
65my_exported_sdk_libs_file := $(call local-intermediates-dir,COMMON)/exported-sdk-libs
66$(fixed_android_manifest): PRIVATE_EXPORTED_SDK_LIBS_FILE := $(my_exported_sdk_libs_file)
67$(fixed_android_manifest): $(my_exported_sdk_libs_file)
68
69my_manifest_fixer_flags :=
70ifneq ($(LOCAL_MODULE_CLASS),APPS)
71    my_manifest_fixer_flags += --library
72endif
73ifeq ($(LOCAL_PRIVATE_PLATFORM_APIS),true)
74    my_manifest_fixer_flags += --uses-non-sdk-api
75endif
76
77ifeq (true,$(LOCAL_USE_EMBEDDED_DEX))
78    my_manifest_fixer_flags += --use-embedded-dex
79endif
80
81ifeq ($(LOCAL_MODULE_CLASS),APPS)
82  ifeq (true,$(call math_gt_or_eq,$(patsubst $(PLATFORM_VERSION_CODENAME),100,$(call module-min-sdk-version)),23))
83    ifeq (true,$(LOCAL_USE_EMBEDDED_NATIVE_LIBS))
84      my_manifest_fixer_flags += --extract-native-libs=false
85    else
86      my_manifest_fixer_flags += --extract-native-libs=true
87    endif
88  else ifeq (true,$(LOCAL_USE_EMBEDDED_NATIVE_LIBS))
89    $(call pretty-error,LOCAL_USE_EMBEDDED_NATIVE_LIBS is set but minSdkVersion $(call module-min-sdk-version) does not support it)
90  endif
91endif
92
93# TODO: Replace this hardcoded list of optional uses-libraries with build logic
94# that propagates optionality via the generated exported-sdk-libs files.
95# Hardcodng doesn't scale and enforces a single choice on each library, while in
96# reality this is a choice of the library users (which may differ).
97my_optional_sdk_lib_names := \
98    android.test.base \
99    android.test.mock \
100    androidx.window.extensions \
101    androidx.window.sidecar
102
103$(fixed_android_manifest): PRIVATE_MANIFEST_FIXER_FLAGS := $(my_manifest_fixer_flags)
104# These two libs are added as optional dependencies (<uses-library> with
105# android:required set to false). This is because they haven't existed in pre-P
106# devices, but classes in them were in bootclasspath jars, etc. So making them
107# hard dependencies (andriod:required=true) would prevent apps from being
108# installed to such legacy devices.
109$(fixed_android_manifest): PRIVATE_OPTIONAL_SDK_LIB_NAMES := $(my_optional_sdk_lib_names)
110$(fixed_android_manifest): $(MANIFEST_FIXER)
111$(fixed_android_manifest): $(main_android_manifest)
112	echo $(PRIVATE_OPTIONAL_SDK_LIB_NAMES) | tr ' ' '\n' > $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional
113	@echo "Fix manifest: $@"
114	$(MANIFEST_FIXER) \
115	  --minSdkVersion $(PRIVATE_MIN_SDK_VERSION) \
116          --targetSdkVersion $(PRIVATE_TARGET_SDK_VERSION) \
117          --raise-min-sdk-version \
118	  $(PRIVATE_MANIFEST_FIXER_FLAGS) \
119	  $(if (PRIVATE_EXPORTED_SDK_LIBS_FILE),\
120	    $$(cat $(PRIVATE_EXPORTED_SDK_LIBS_FILE) | grep -v -f $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional | sort -u | sed -e 's/^/\ --uses-library\ /' | tr '\n' ' ') \
121	    $$(cat $(PRIVATE_EXPORTED_SDK_LIBS_FILE) | grep -f $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional | sort -u | sed -e 's/^/\ --optional-uses-library\ /' | tr '\n' ' ') \
122	   ) \
123	  $< $@
124	rm $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional
125
126my_optional_sdk_lib_names :=
127