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
26# With aapt2, we'll link in the built resource from the AAR.
27ifneq ($(LOCAL_USE_AAPT2),true)
28  LOCAL_RESOURCE_DIR += $(foreach lib, $(LOCAL_STATIC_JAVA_AAR_LIBRARIES),\
29    $(call intermediates-dir-for,JAVA_LIBRARIES,$(lib),,COMMON)/aar/res)
30endif
31
32full_android_manifest := $(intermediates.COMMON)/manifest/AndroidManifest.xml
33
34ifneq (,$(strip $(my_full_libs_manifest_files)))
35  # Set up rules to merge library manifest files
36  fixed_android_manifest := $(intermediates.COMMON)/manifest/AndroidManifest.xml.fixed
37
38  $(full_android_manifest): PRIVATE_LIBS_MANIFESTS := $(my_full_libs_manifest_files)
39  $(full_android_manifest): $(ANDROID_MANIFEST_MERGER)
40  $(full_android_manifest) : $(fixed_android_manifest) $(my_full_libs_manifest_files)
41	@echo "Merge android manifest files: $@ <-- $< $(PRIVATE_LIBS_MANIFESTS)"
42	@mkdir -p $(dir $@)
43	$(hide) $(ANDROID_MANIFEST_MERGER) --main $< \
44	    --libs $(call normalize-path-list,$(PRIVATE_LIBS_MANIFESTS)) \
45	    --out $@
46else
47  fixed_android_manifest := $(full_android_manifest)
48endif
49
50my_target_sdk_version := $(call module-target-sdk-version)
51
52ifdef TARGET_BUILD_APPS
53  ifndef TARGET_BUILD_APPS_USE_PREBUILT_SDK
54    ifeq ($(my_target_sdk_version),$(PLATFORM_VERSION_CODENAME))
55      ifdef UNBUNDLED_BUILD_TARGET_SDK_WITH_API_FINGERPRINT
56        my_target_sdk_version := $(my_target_sdk_version).$$(cat $(API_FINGERPRINT))
57        $(fixed_android_manifest): $(API_FINGERPRINT)
58      endif
59    endif
60  endif
61endif
62
63$(fixed_android_manifest): PRIVATE_MIN_SDK_VERSION := $(call module-min-sdk-version)
64$(fixed_android_manifest): PRIVATE_TARGET_SDK_VERSION := $(my_target_sdk_version)
65
66my_exported_sdk_libs_file := $(call local-intermediates-dir,COMMON)/exported-sdk-libs
67$(fixed_android_manifest): PRIVATE_EXPORTED_SDK_LIBS_FILE := $(my_exported_sdk_libs_file)
68$(fixed_android_manifest): $(my_exported_sdk_libs_file)
69
70my_manifest_fixer_flags :=
71ifneq ($(LOCAL_MODULE_CLASS),APPS)
72    my_manifest_fixer_flags += --library
73endif
74ifeq ($(LOCAL_PRIVATE_PLATFORM_APIS),true)
75    my_manifest_fixer_flags += --uses-non-sdk-api
76endif
77
78ifeq (true,$(LOCAL_USE_EMBEDDED_DEX))
79    my_manifest_fixer_flags += --use-embedded-dex
80endif
81
82ifeq ($(LOCAL_MODULE_CLASS),APPS)
83  ifeq (true,$(call math_gt_or_eq,$(patsubst $(PLATFORM_VERSION_CODENAME),100,$(call module-min-sdk-version)),23))
84    ifeq (true,$(LOCAL_USE_EMBEDDED_NATIVE_LIBS))
85      my_manifest_fixer_flags += --extract-native-libs=false
86    else
87      my_manifest_fixer_flags += --extract-native-libs=true
88    endif
89  else ifeq (true,$(LOCAL_USE_EMBEDDED_NATIVE_LIBS))
90    $(call pretty-error,LOCAL_USE_EMBEDDED_NATIVE_LIBS is set but minSdkVersion $(call module-min-sdk-version) does not support it)
91  endif
92endif
93
94$(fixed_android_manifest): PRIVATE_MANIFEST_FIXER_FLAGS := $(my_manifest_fixer_flags)
95# These two libs are added as optional dependencies (<uses-library> with
96# android:required set to false). This is because they haven't existed in pre-P
97# devices, but classes in them were in bootclasspath jars, etc. So making them
98# hard dependencies (andriod:required=true) would prevent apps from being
99# installed to such legacy devices.
100$(fixed_android_manifest): PRIVATE_OPTIONAL_SDK_LIB_NAMES := android.test.base android.test.mock
101$(fixed_android_manifest): $(MANIFEST_FIXER)
102$(fixed_android_manifest): $(main_android_manifest)
103	echo $(PRIVATE_OPTIONAL_SDK_LIB_NAMES) | tr ' ' '\n' > $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional
104	@echo "Fix manifest: $@"
105	$(MANIFEST_FIXER) \
106	  --minSdkVersion $(PRIVATE_MIN_SDK_VERSION) \
107          --targetSdkVersion $(PRIVATE_TARGET_SDK_VERSION) \
108          --raise-min-sdk-version \
109	  $(PRIVATE_MANIFEST_FIXER_FLAGS) \
110	  $(if (PRIVATE_EXPORTED_SDK_LIBS_FILE),\
111	    $$(cat $(PRIVATE_EXPORTED_SDK_LIBS_FILE) | grep -v -f $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional | sort -u | sed -e 's/^/\ --uses-library\ /' | tr '\n' ' ') \
112	    $$(cat $(PRIVATE_EXPORTED_SDK_LIBS_FILE) | grep -f $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional | sort -u | sed -e 's/^/\ --optional-uses-library\ /' | tr '\n' ' ') \
113	   ) \
114	  $< $@
115	rm $(PRIVATE_EXPORTED_SDK_LIBS_FILE).optional
116