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_APPS_USE_PREBUILT_SDK
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      endif
55    endif
56  endif
57endif
58
59$(fixed_android_manifest): PRIVATE_MIN_SDK_VERSION := $(my_min_sdk_version)
60$(fixed_android_manifest): PRIVATE_TARGET_SDK_VERSION := $(my_target_sdk_version)
61
62my_exported_sdk_libs_file := $(call local-intermediates-dir,COMMON)/exported-sdk-libs
63$(fixed_android_manifest): PRIVATE_EXPORTED_SDK_LIBS_FILE := $(my_exported_sdk_libs_file)
64$(fixed_android_manifest): $(my_exported_sdk_libs_file)
65
66my_manifest_fixer_flags :=
67ifneq ($(LOCAL_MODULE_CLASS),APPS)
68    my_manifest_fixer_flags += --library
69endif
70ifeq ($(LOCAL_PRIVATE_PLATFORM_APIS),true)
71    my_manifest_fixer_flags += --uses-non-sdk-api
72endif
73
74ifeq (true,$(LOCAL_USE_EMBEDDED_DEX))
75    my_manifest_fixer_flags += --use-embedded-dex
76endif
77
78ifeq ($(LOCAL_MODULE_CLASS),APPS)
79  ifeq (true,$(call math_gt_or_eq,$(patsubst $(PLATFORM_VERSION_CODENAME),100,$(call module-min-sdk-version)),23))
80    ifeq (true,$(LOCAL_USE_EMBEDDED_NATIVE_LIBS))
81      my_manifest_fixer_flags += --extract-native-libs=false
82    else
83      my_manifest_fixer_flags += --extract-native-libs=true
84    endif
85  else ifeq (true,$(LOCAL_USE_EMBEDDED_NATIVE_LIBS))
86    $(call pretty-error,LOCAL_USE_EMBEDDED_NATIVE_LIBS is set but minSdkVersion $(call module-min-sdk-version) does not support it)
87  endif
88endif
89
90$(fixed_android_manifest): PRIVATE_MANIFEST_FIXER_FLAGS := $(my_manifest_fixer_flags)
91# These two libs are added as optional dependencies (<uses-library> with
92# android:required set to false). This is because they haven't existed in pre-P
93# devices, but classes in them were in bootclasspath jars, etc. So making them
94# hard dependencies (andriod:required=true) would prevent apps from being
95# installed to such legacy devices.
96$(fixed_android_manifest): PRIVATE_OPTIONAL_SDK_LIB_NAMES := android.test.base android.test.mock
97$(fixed_android_manifest): $(MANIFEST_FIXER)
98$(fixed_android_manifest): $(main_android_manifest)
99	echo $(PRIVATE_OPTIONAL_SDK_LIB_NAMES) | tr ' ' '\n' > $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional
100	@echo "Fix manifest: $@"
101	$(MANIFEST_FIXER) \
102	  --minSdkVersion $(PRIVATE_MIN_SDK_VERSION) \
103          --targetSdkVersion $(PRIVATE_TARGET_SDK_VERSION) \
104          --raise-min-sdk-version \
105	  $(PRIVATE_MANIFEST_FIXER_FLAGS) \
106	  $(if (PRIVATE_EXPORTED_SDK_LIBS_FILE),\
107	    $$(cat $(PRIVATE_EXPORTED_SDK_LIBS_FILE) | grep -v -f $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional | sort -u | sed -e 's/^/\ --uses-library\ /' | tr '\n' ' ') \
108	    $$(cat $(PRIVATE_EXPORTED_SDK_LIBS_FILE) | grep -f $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional | sort -u | sed -e 's/^/\ --optional-uses-library\ /' | tr '\n' ' ') \
109	   ) \
110	  $< $@
111	rm $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional
112