• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# Copyright (C) 2008 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17###########################################################
18## Standard rules for building an application package.
19##
20## Additional inputs from base_rules.make:
21## LOCAL_PACKAGE_NAME: The name of the package; the directory
22## will be called this.
23##
24## MODULE, MODULE_PATH, and MODULE_SUFFIX will
25## be set for you.
26###########################################################
27
28LOCAL_PACKAGE_NAME := $(strip $(LOCAL_PACKAGE_NAME))
29ifeq ($(LOCAL_PACKAGE_NAME),)
30$(error $(LOCAL_PATH): Package modules must define LOCAL_PACKAGE_NAME)
31endif
32
33ifneq ($(strip $(LOCAL_MODULE_SUFFIX)),)
34$(error $(LOCAL_PATH): Package modules may not define LOCAL_MODULE_SUFFIX)
35endif
36LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
37
38ifneq ($(strip $(LOCAL_MODULE)),)
39$(error $(LOCAL_PATH): Package modules may not define LOCAL_MODULE)
40endif
41LOCAL_MODULE := $(LOCAL_PACKAGE_NAME)
42
43ifneq ($(strip $(LOCAL_MODULE_CLASS)),)
44$(error $(LOCAL_PATH): Package modules may not set LOCAL_MODULE_CLASS)
45endif
46LOCAL_MODULE_CLASS := APPS
47
48intermediates := $(call local-intermediates-dir)
49intermediates.COMMON := $(call local-intermediates-dir,COMMON)
50
51# Package LOCAL_MODULE_TAGS default to optional
52LOCAL_MODULE_TAGS := $(strip $(LOCAL_MODULE_TAGS))
53ifeq ($(LOCAL_MODULE_TAGS),)
54LOCAL_MODULE_TAGS := optional
55endif
56
57ifeq ($(filter tests, $(LOCAL_MODULE_TAGS)),)
58# Force localization check if it's not tagged as tests.
59LOCAL_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) -z
60endif
61
62need_compile_asset :=
63ifeq (,$(LOCAL_ASSET_DIR))
64LOCAL_ASSET_DIR := $(LOCAL_PATH)/assets
65else
66need_compile_asset := true
67endif
68
69# LOCAL_RESOURCE_DIR may point to resource generated during the build
70need_compile_res :=
71ifeq (,$(LOCAL_RESOURCE_DIR))
72  LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
73else
74  need_compile_res := true
75  LOCAL_RESOURCE_DIR := $(foreach d,$(LOCAL_RESOURCE_DIR),$(call clean-path,$(d)))
76endif
77
78# If LOCAL_MODULE matches a rule in PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES,
79# override the manifest package name by the (first) rule matched
80override_manifest_name := $(strip $(word 1,\
81  $(foreach rule,$(PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES),\
82    $(eval _pkg_name_pat := $(call word-colon,1,$(rule)))\
83    $(eval _manifest_name_pat := $(call word-colon,2,$(rule)))\
84    $(if $(filter $(_pkg_name_pat),$(LOCAL_MODULE)),\
85      $(patsubst $(_pkg_name_pat),$(_manifest_name_pat),$(LOCAL_MODULE))\
86     )\
87   )\
88))
89
90ifneq (,$(override_manifest_name))
91# Note: this can override LOCAL_MANIFEST_PACKAGE_NAME value set in Android.mk
92LOCAL_MANIFEST_PACKAGE_NAME := $(override_manifest_name)
93endif
94
95include $(BUILD_SYSTEM)/force_aapt2.mk
96
97# Process Support Library dependencies.
98include $(BUILD_SYSTEM)/support_libraries.mk
99
100# Determine whether auto-RRO is enabled for this package.
101enforce_rro_enabled :=
102ifneq (,$(filter *, $(PRODUCT_ENFORCE_RRO_TARGETS)))
103  # * means all system and system_ext APKs, so enable conditionally based on module path.
104  # Note that modules in PRODUCT_ENFORCE_RRO_EXEMPTED_TARGETS are excluded even if it is '*'
105
106  # Note that base_rules.mk has not yet been included, so it's likely that only
107  # one of LOCAL_MODULE_PATH and the LOCAL_X_MODULE flags has been set.
108  ifeq (,$(LOCAL_MODULE_PATH))
109    non_rro_target_module := $(filter true,\
110        $(LOCAL_ODM_MODULE) \
111        $(LOCAL_OEM_MODULE) \
112        $(LOCAL_PRODUCT_MODULE) \
113        $(LOCAL_PROPRIETARY_MODULE) \
114        $(LOCAL_VENDOR_MODULE))
115    enforce_rro_enabled := $(if $(non_rro_target_module),,true)
116  else ifneq ($(filter $(TARGET_OUT)/%,$(LOCAL_MODULE_PATH)),)
117    enforce_rro_enabled := true
118  endif
119else ifneq (,$(filter $(LOCAL_PACKAGE_NAME), $(PRODUCT_ENFORCE_RRO_TARGETS)))
120  enforce_rro_enabled := true
121endif
122
123# TODO(b/150820813) Some modules depend on static overlay, remove this after eliminating the dependency.
124ifneq (,$(filter $(LOCAL_PACKAGE_NAME), $(PRODUCT_ENFORCE_RRO_EXEMPTED_TARGETS)))
125  enforce_rro_enabled :=
126endif
127
128
129product_package_overlays := $(strip \
130    $(wildcard $(foreach dir, $(PRODUCT_PACKAGE_OVERLAYS), \
131      $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))))
132device_package_overlays := $(strip \
133    $(wildcard $(foreach dir, $(DEVICE_PACKAGE_OVERLAYS), \
134      $(addprefix $(dir)/, $(LOCAL_RESOURCE_DIR)))))
135
136static_resource_overlays :=
137runtime_resource_overlays_product :=
138runtime_resource_overlays_vendor :=
139ifdef enforce_rro_enabled
140  ifneq ($(PRODUCT_ENFORCE_RRO_EXCLUDED_OVERLAYS),)
141    # The PRODUCT_ exclusion variable applies to both inclusion variables..
142    static_resource_overlays += $(filter $(addsuffix %,$(PRODUCT_ENFORCE_RRO_EXCLUDED_OVERLAYS)),$(product_package_overlays))
143    static_resource_overlays += $(filter $(addsuffix %,$(PRODUCT_ENFORCE_RRO_EXCLUDED_OVERLAYS)),$(device_package_overlays))
144  endif
145  runtime_resource_overlays_product := $(filter-out $(static_resource_overlays),$(product_package_overlays))
146  runtime_resource_overlays_vendor := $(filter-out $(static_resource_overlays),$(device_package_overlays))
147else
148  static_resource_overlays := $(product_package_overlays) $(device_package_overlays)
149endif
150
151# Add the static overlays. Auto-RRO is created later, as it depends on
152# other logic in this file.
153LOCAL_RESOURCE_DIR := $(static_resource_overlays) $(LOCAL_RESOURCE_DIR)
154
155all_assets := $(strip \
156    $(foreach dir, $(LOCAL_ASSET_DIR), \
157      $(addprefix $(dir)/, \
158        $(patsubst assets/%,%, \
159          $(call find-subdir-assets, $(dir)) \
160         ) \
161       ) \
162     ))
163
164ifneq ($(all_assets),)
165need_compile_asset := true
166endif
167
168my_res_package :=
169# In aapt2 the last takes precedence.
170my_resource_dirs := $(call reverse-list,$(LOCAL_RESOURCE_DIR))
171my_res_dir :=
172my_overlay_res_dirs :=
173
174ifneq ($(strip $(LOCAL_STATIC_ANDROID_LIBRARIES) $(LOCAL_STATIC_JAVA_AAR_LIBRARIES)),)
175# If we are using static android libraries, every source file becomes an overlay.
176# This is to emulate old AAPT behavior which simulated library support.
177my_res_dir :=
178my_overlay_res_dirs := $(my_resource_dirs)
179else
180# Without static libraries, the first directory is our directory, which can then be
181# overlaid by the rest. (First directory in my_resource_dirs is last directory in
182# $(LOCAL_RESOURCE_DIR) due to it being reversed.
183my_res_dir := $(firstword $(my_resource_dirs))
184my_overlay_res_dirs := $(wordlist 2,999,$(my_resource_dirs))
185endif
186
187my_overlay_resources := $(strip \
188  $(foreach d,$(my_overlay_res_dirs),\
189    $(addprefix $(d)/, \
190        $(call find-subdir-assets,$(d)))))
191
192my_res_resources := $(if $(my_res_dir),$(strip \
193    $(addprefix $(my_res_dir)/, \
194        $(call find-subdir-assets,$(my_res_dir)))))
195
196all_resources := $(strip $(my_res_resources) $(my_overlay_resources))
197
198# The linked resource package.
199my_res_package := $(intermediates)/package-res.apk
200LOCAL_INTERMEDIATE_TARGETS += $(my_res_package)
201
202my_bundle_module := $(intermediates)/base.zip
203LOCAL_INTERMEDIATE_TARGETS += $(my_bundle_module)
204
205# Always run aapt2, because we need to at least compile the AndroidManifest.xml.
206need_compile_res := true
207
208ifneq ($(all_resources),)
209  need_compile_res := true
210endif
211
212all_res_assets := $(strip $(all_assets) $(all_resources))
213
214# If no assets or resources were found, clear the directory variables so
215# we don't try to build them.
216ifneq (true,$(need_compile_asset))
217LOCAL_ASSET_DIR:=
218endif
219ifneq (true,$(need_compile_res))
220LOCAL_RESOURCE_DIR:=
221R_file_stamp :=
222else
223# Make sure that R_file_stamp inherits the proper PRIVATE vars.
224# If R.stamp moves, be sure to update the framework makefile,
225# which has intimate knowledge of its location.
226R_file_stamp := $(intermediates.COMMON)/src/R.stamp
227LOCAL_INTERMEDIATE_TARGETS += $(R_file_stamp)
228endif
229
230ifdef LOCAL_COMPRESSED_MODULE
231ifneq (true,$(LOCAL_COMPRESSED_MODULE))
232$(call pretty-error, Unknown value for LOCAL_COMPRESSED_MODULE $(LOCAL_COMPRESSED_MODULE))
233endif
234endif
235
236ifdef LOCAL_COMPRESSED_MODULE
237PACKAGES.$(LOCAL_PACKAGE_NAME).COMPRESSED := gz
238LOCAL_BUILT_MODULE_STEM := package.apk.gz
239LOCAL_INSTALLED_MODULE_STEM := $(LOCAL_MODULE).apk.gz
240else  # !LOCAL_COMPRESSED_MODULE
241LOCAL_BUILT_MODULE_STEM := package.apk
242LOCAL_INSTALLED_MODULE_STEM := $(LOCAL_MODULE).apk
243endif
244
245LOCAL_PROGUARD_ENABLED:=$(strip $(LOCAL_PROGUARD_ENABLED))
246ifndef LOCAL_PROGUARD_ENABLED
247ifneq ($(DISABLE_PROGUARD),true)
248    LOCAL_PROGUARD_ENABLED :=full
249endif
250endif
251ifeq ($(LOCAL_PROGUARD_ENABLED),disabled)
252    # the package explicitly request to disable proguard.
253    LOCAL_PROGUARD_ENABLED :=
254endif
255proguard_options_file :=
256ifneq ($(LOCAL_PROGUARD_ENABLED),custom)
257ifeq ($(need_compile_res),true)
258    proguard_options_file := $(intermediates.COMMON)/proguard_options
259endif # need_compile_res
260endif # !custom
261LOCAL_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_PROGUARD_FLAGS)
262LOCAL_PROGUARD_FLAGS_DEPS += $(proguard_options_file)
263
264ifeq (true,$(EMMA_INSTRUMENT))
265ifndef LOCAL_EMMA_INSTRUMENT
266# No jacoco for test apks.
267ifeq (,$(LOCAL_INSTRUMENTATION_FOR))
268LOCAL_EMMA_INSTRUMENT := true
269endif # No test apk
270endif # LOCAL_EMMA_INSTRUMENT is not set
271else
272LOCAL_EMMA_INSTRUMENT := false
273endif # EMMA_INSTRUMENT is true
274
275ifeq (true,$(LOCAL_EMMA_INSTRUMENT))
276ifeq (true,$(EMMA_INSTRUMENT_STATIC))
277ifneq ($(LOCAL_SRC_FILES)$(LOCAL_SRCJARS)$(LOCAL_STATIC_JAVA_LIBRARIES)$(LOCAL_SOURCE_FILES_ALL_GENERATED),)
278# Only add jacocoagent if the package contains some java code
279LOCAL_STATIC_JAVA_LIBRARIES += jacocoagent
280# Exclude jacoco classes from proguard
281LOCAL_PROGUARD_FLAGS += -include $(BUILD_SYSTEM)/proguard.jacoco.flags
282LOCAL_PROGUARD_FLAGS_DEPS += $(BUILD_SYSTEM)/proguard.jacoco.flags
283endif # Contains java code
284else
285ifdef LOCAL_SDK_VERSION
286ifdef TARGET_BUILD_APPS
287# In unbundled build, merge the coverage library into the apk.
288ifneq ($(LOCAL_SRC_FILES)$(LOCAL_STATIC_JAVA_LIBRARIES)$(LOCAL_SOURCE_FILES_ALL_GENERATED),)
289# Only add jacocoagent if the package contains some java code
290LOCAL_STATIC_JAVA_LIBRARIES += jacocoagent
291# Exclude jacoco classes from proguard
292LOCAL_PROGUARD_FLAGS += -include $(BUILD_SYSTEM)/proguard.jacoco.flags
293LOCAL_PROGUARD_FLAGS_DEPS += $(BUILD_SYSTEM)/proguard.jacoco.flags
294endif # Contains java code
295endif # TARGET_BUILD_APPS
296endif # LOCAL_SDK_VERSION
297endif # EMMA_INSTRUMENT_STATIC
298endif # LOCAL_EMMA_INSTRUMENT
299
300rs_compatibility_jni_libs :=
301
302# If the module is a compressed module, we don't pre-opt it because its final
303# installation location will be the data partition.
304ifdef LOCAL_COMPRESSED_MODULE
305LOCAL_DEX_PREOPT := false
306endif
307
308# Default to use uncompressed native libraries in APKs if minSdkVersion >= marshmallow
309ifndef LOCAL_USE_EMBEDDED_NATIVE_LIBS
310  LOCAL_USE_EMBEDDED_NATIVE_LIBS := $(call math_gt_or_eq, \
311    $(patsubst $(PLATFORM_VERSION_CODENAME),100,$(call module-min-sdk-version)),23)
312endif
313
314include $(BUILD_SYSTEM)/android_manifest.mk
315
316resource_export_package :=
317
318include $(BUILD_SYSTEM)/java_renderscript.mk
319
320include $(BUILD_SYSTEM)/aapt_flags.mk
321
322ifeq ($(need_compile_res),true)
323
324###############################
325## APK splits
326built_apk_splits :=
327installed_apk_splits :=
328my_apk_split_configs :=
329
330ifdef LOCAL_PACKAGE_SPLITS
331ifdef LOCAL_COMPRESSED_MODULE
332$(error $(LOCAL_MODULE): LOCAL_COMPRESSED_MODULE is not currently supported for split installs)
333endif  # LOCAL_COMPRESSED_MODULE
334
335my_apk_split_configs := $(LOCAL_PACKAGE_SPLITS)
336my_split_suffixes := $(subst $(comma),_,$(my_apk_split_configs))
337built_apk_splits := $(foreach s,$(my_split_suffixes),$(intermediates)/package_$(s).apk)
338endif
339
340$(R_file_stamp) $(my_res_package): PRIVATE_AAPT_FLAGS := $(filter-out --legacy,$(LOCAL_AAPT_FLAGS))
341$(R_file_stamp) $(my_res_package): PRIVATE_TARGET_AAPT_CHARACTERISTICS := $(TARGET_AAPT_CHARACTERISTICS)
342$(R_file_stamp) $(my_res_package): PRIVATE_MANIFEST_PACKAGE_NAME := $(LOCAL_MANIFEST_PACKAGE_NAME)
343$(R_file_stamp) $(my_res_package): PRIVATE_MANIFEST_INSTRUMENTATION_FOR := $(LOCAL_MANIFEST_INSTRUMENTATION_FOR)
344
345###############################
346## AAPT2
347
348my_compiled_res_base_dir := $(intermediates.COMMON)/flat-res
349ifneq (,$(filter-out current,$(renderscript_target_api)))
350  ifneq ($(call math_gt_or_eq,$(renderscript_target_api),21),true)
351    my_generated_res_zips := $(rs_generated_res_zip)
352  endif  # renderscript_target_api < 21
353endif  # renderscript_target_api is set
354my_asset_dirs := $(LOCAL_ASSET_DIR)
355my_full_asset_paths := $(all_assets)
356
357# Add AAPT2 link specific flags.
358ifndef LOCAL_AAPT_NAMESPACES
359  $(my_res_package): PRIVATE_AAPT_FLAGS += --no-static-lib-packages
360endif
361
362include $(BUILD_SYSTEM)/aapt2.mk
363
364endif  # need_compile_res
365
366my_dex_jar := $(intermediates.COMMON)/dex.jar
367
368called_from_package_internal := true
369#################################
370include $(BUILD_SYSTEM)/java.mk
371#################################
372called_from_package_internal :=
373
374ifeq ($(need_compile_res),true)
375
376# Other modules should depend on the BUILT module if
377# they want to use this module's R.java file.
378$(LOCAL_BUILT_MODULE): $(R_file_stamp)
379
380# The R.java file must exist by the time the java source
381# list is generated
382$(java_source_list_file): $(R_file_stamp)
383
384endif # need_compile_res
385
386LOCAL_SDK_RES_VERSION:=$(strip $(LOCAL_SDK_RES_VERSION))
387ifeq ($(LOCAL_SDK_RES_VERSION),)
388  LOCAL_SDK_RES_VERSION:=$(LOCAL_SDK_VERSION)
389endif
390
391$(LOCAL_INTERMEDIATE_TARGETS): \
392    PRIVATE_ANDROID_MANIFEST := $(full_android_manifest)
393
394framework_res_package_export :=
395
396ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
397# Most packages should link against the resources defined by framework-res.
398# Even if they don't have their own resources, they may use framework
399# resources.
400ifeq ($(LOCAL_SDK_RES_VERSION),core_current)
401# core_current doesn't contain any framework resources.
402else ifneq ($(filter-out current system_current test_current,$(LOCAL_SDK_RES_VERSION))$(if $(TARGET_BUILD_APPS_USE_PREBUILT_SDK),$(filter current system_current test_current,$(LOCAL_SDK_RES_VERSION))),)
403# for released sdk versions, the platform resources were built into android.jar.
404framework_res_package_export := \
405    $(call resolve-prebuilt-sdk-jar-path,$(LOCAL_SDK_RES_VERSION))
406else # LOCAL_SDK_RES_VERSION
407framework_res_package_export := \
408    $(call intermediates-dir-for,APPS,framework-res,,COMMON)/package-export.apk
409endif # LOCAL_SDK_RES_VERSION
410endif # LOCAL_NO_STANDARD_LIBRARIES
411
412all_library_res_package_exports := \
413    $(framework_res_package_export) \
414    $(foreach lib,$(LOCAL_RES_LIBRARIES),\
415        $(call intermediates-dir-for,APPS,$(lib),,COMMON)/package-export.apk)
416
417all_library_res_package_export_deps := \
418    $(framework_res_package_export) \
419    $(foreach lib,$(LOCAL_RES_LIBRARIES),\
420        $(call intermediates-dir-for,APPS,$(lib),,COMMON)/src/R.stamp)
421$(resource_export_package) $(R_file_stamp) $(LOCAL_BUILT_MODULE): $(all_library_res_package_export_deps)
422$(LOCAL_INTERMEDIATE_TARGETS): \
423    PRIVATE_AAPT_INCLUDES := $(all_library_res_package_exports)
424
425$(my_res_package) : $(all_library_res_package_export_deps)
426
427ifneq ($(full_classes_jar),)
428$(LOCAL_BUILT_MODULE): PRIVATE_DEX_FILE := $(built_dex)
429# Use the jarjar processed arhive as the initial package file.
430$(LOCAL_BUILT_MODULE): PRIVATE_SOURCE_ARCHIVE := $(full_classes_pre_proguard_jar)
431$(LOCAL_BUILT_MODULE): $(built_dex) $(full_classes_pre_proguard_jar)
432else
433$(LOCAL_BUILT_MODULE): PRIVATE_DEX_FILE :=
434$(LOCAL_BUILT_MODULE): PRIVATE_SOURCE_ARCHIVE :=
435endif # full_classes_jar
436
437include $(BUILD_SYSTEM)/install_jni_libs.mk
438
439# Pick a key to sign the package with.  If this package hasn't specified
440# an explicit certificate, use the default.
441# Secure release builds will have their packages signed after the fact,
442# so it's ok for these private keys to be in the clear.
443ifeq ($(LOCAL_CERTIFICATE),)
444    LOCAL_CERTIFICATE := $(DEFAULT_SYSTEM_DEV_CERTIFICATE)
445endif
446
447ifeq ($(LOCAL_CERTIFICATE),EXTERNAL)
448  # The special value "EXTERNAL" means that we will sign it with the
449  # default devkey, apply predexopt, but then expect the final .apk
450  # (after dexopting) to be signed by an outside tool.
451  LOCAL_CERTIFICATE := $(DEFAULT_SYSTEM_DEV_CERTIFICATE)
452  PACKAGES.$(LOCAL_PACKAGE_NAME).EXTERNAL_KEY := 1
453endif
454
455# If this is not an absolute certificate, assign it to a generic one.
456ifeq ($(dir $(strip $(LOCAL_CERTIFICATE))),./)
457    LOCAL_CERTIFICATE := $(dir $(DEFAULT_SYSTEM_DEV_CERTIFICATE))$(LOCAL_CERTIFICATE)
458endif
459include $(BUILD_SYSTEM)/app_certificate_validate.mk
460private_key := $(LOCAL_CERTIFICATE).pk8
461certificate := $(LOCAL_CERTIFICATE).x509.pem
462additional_certificates := $(foreach c,$(LOCAL_ADDITIONAL_CERTIFICATES), $(c).x509.pem $(c).pk8)
463
464$(LOCAL_BUILT_MODULE): $(private_key) $(certificate) $(SIGNAPK_JAR) $(SIGNAPK_JNI_LIBRARY_PATH)
465$(LOCAL_BUILT_MODULE): PRIVATE_PRIVATE_KEY := $(private_key)
466$(LOCAL_BUILT_MODULE): PRIVATE_CERTIFICATE := $(certificate)
467
468PACKAGES.$(LOCAL_PACKAGE_NAME).PRIVATE_KEY := $(private_key)
469PACKAGES.$(LOCAL_PACKAGE_NAME).CERTIFICATE := $(certificate)
470
471$(LOCAL_BUILT_MODULE): $(additional_certificates)
472$(LOCAL_BUILT_MODULE): PRIVATE_ADDITIONAL_CERTIFICATES := $(additional_certificates)
473
474$(LOCAL_BUILT_MODULE): $(LOCAL_CERTIFICATE_LINEAGE)
475$(LOCAL_BUILT_MODULE): PRIVATE_CERTIFICATE_LINEAGE := $(LOCAL_CERTIFICATE_LINEAGE)
476
477# Set a actual_partition_tag (calculated in base_rules.mk) for the package.
478PACKAGES.$(LOCAL_PACKAGE_NAME).PARTITION := $(actual_partition_tag)
479
480# Verify LOCAL_USES_LIBRARIES/LOCAL_OPTIONAL_USES_LIBRARIES
481# If LOCAL_ENFORCE_USES_LIBRARIES is not set, default to true if either of LOCAL_USES_LIBRARIES or
482# LOCAL_OPTIONAL_USES_LIBRARIES are specified.
483# Will change the default to true unconditionally in the future.
484ifndef LOCAL_ENFORCE_USES_LIBRARIES
485  ifneq (,$(strip $(LOCAL_USES_LIBRARIES)$(LOCAL_OPTIONAL_USES_LIBRARIES)))
486    LOCAL_ENFORCE_USES_LIBRARIES := true
487  endif
488endif
489
490my_enforced_uses_libraries :=
491ifdef LOCAL_ENFORCE_USES_LIBRARIES
492  my_manifest_check := $(intermediates.COMMON)/manifest/AndroidManifest.xml.check
493  $(my_manifest_check): $(MANIFEST_CHECK)
494  $(my_manifest_check): PRIVATE_USES_LIBRARIES := $(LOCAL_USES_LIBRARIES)
495  $(my_manifest_check): PRIVATE_OPTIONAL_USES_LIBRARIES := $(LOCAL_OPTIONAL_USES_LIBRARIES)
496  $(my_manifest_check): $(full_android_manifest)
497	@echo Checking manifest: $<
498	$(MANIFEST_CHECK) --enforce-uses-libraries \
499	  $(addprefix --uses-library ,$(PRIVATE_USES_LIBRARIES)) \
500	  $(addprefix --optional-uses-library ,$(PRIVATE_OPTIONAL_USES_LIBRARIES)) \
501	  $< -o $@
502  $(LOCAL_BUILT_MODULE): $(my_manifest_check)
503endif
504
505# Define the rule to build the actual package.
506# PRIVATE_JNI_SHARED_LIBRARIES is a list of <abi>:<path_of_built_lib>.
507$(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES := $(jni_shared_libraries_with_abis)
508# PRIVATE_JNI_SHARED_LIBRARIES_ABI is a list of ABI names.
509$(LOCAL_BUILT_MODULE): PRIVATE_JNI_SHARED_LIBRARIES_ABI := $(jni_shared_libraries_abis)
510ifneq ($(TARGET_BUILD_APPS),)
511    # Include all resources for unbundled apps.
512    LOCAL_AAPT_INCLUDE_ALL_RESOURCES := true
513endif
514ifeq ($(LOCAL_AAPT_INCLUDE_ALL_RESOURCES),true)
515    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_CONFIG :=
516    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
517else
518    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_CONFIG := $(PRODUCT_AAPT_CONFIG)
519ifdef LOCAL_PACKAGE_SPLITS
520    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
521else
522    $(my_res_package) $(LOCAL_BUILT_MODULE): PRIVATE_PRODUCT_AAPT_PREF_CONFIG := $(PRODUCT_AAPT_PREF_CONFIG)
523endif
524endif
525
526# Run veridex on product, system_ext and vendor modules.
527# We skip it for unbundled app builds where we cannot build veridex.
528module_run_appcompat :=
529ifeq (true,$(non_system_module))
530ifeq (,$(TARGET_BUILD_APPS)$(filter true,$(TARGET_BUILD_PDK)))  # ! unbundled app build
531ifneq ($(UNSAFE_DISABLE_HIDDENAPI_FLAGS),true)
532  module_run_appcompat := true
533endif
534endif
535endif
536
537ifeq ($(module_run_appcompat),true)
538$(LOCAL_BUILT_MODULE) : $(appcompat-files)
539$(LOCAL_BUILT_MODULE): PRIVATE_INSTALLED_MODULE := $(LOCAL_INSTALLED_MODULE)
540endif
541
542$(LOCAL_BUILT_MODULE): PRIVATE_RESOURCE_INTERMEDIATES_DIR := $(intermediates.COMMON)/resources
543$(LOCAL_BUILT_MODULE) : $(jni_shared_libraries)
544$(LOCAL_BUILT_MODULE) : $(JAR_ARGS) $(SOONG_ZIP) $(MERGE_ZIPS) $(ZIP2ZIP)
545$(LOCAL_BUILT_MODULE): PRIVATE_RES_PACKAGE := $(my_res_package)
546$(LOCAL_BUILT_MODULE) : $(my_res_package) $(AAPT2)
547ifdef LOCAL_COMPRESSED_MODULE
548$(LOCAL_BUILT_MODULE) : $(MINIGZIP)
549endif
550ifeq (true, $(LOCAL_UNCOMPRESS_DEX))
551$(LOCAL_BUILT_MODULE) : $(ZIP2ZIP)
552endif
553$(LOCAL_BUILT_MODULE): PRIVATE_USE_EMBEDDED_NATIVE_LIBS := $(LOCAL_USE_EMBEDDED_NATIVE_LIBS)
554$(LOCAL_BUILT_MODULE):
555	@echo "target Package: $(PRIVATE_MODULE) ($@)"
556	rm -rf $@.parts
557	mkdir -p $@.parts
558	cp -f $(PRIVATE_RES_PACKAGE) $@.parts/apk.zip
559ifneq ($(jni_shared_libraries),)
560	$(call create-jni-shared-libs-package,$@.parts/jni.zip,$(PRIVATE_USE_EMBEDDED_NATIVE_LIBS))
561endif
562ifeq ($(full_classes_jar),)
563# We don't build jar, need to add the Java resources here.
564	$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call create-java-resources-jar,$@.parts/res.zip))
565else  # full_classes_jar
566	$(call create-dex-jar,$@.parts/dex.zip,$(PRIVATE_DEX_FILE))
567	$(call extract-resources-jar,$@.parts/res.zip,$(PRIVATE_SOURCE_ARCHIVE))
568endif  # full_classes_jar
569	$(MERGE_ZIPS) $@ $@.parts/*.zip
570	rm -rf $@.parts
571ifeq (true, $(LOCAL_UNCOMPRESS_DEX))
572	@# No need to align, sign-package below will do it.
573	$(uncompress-dexs)
574endif
575# Run appcompat before signing.
576ifeq ($(module_run_appcompat),true)
577	$(appcompat-header)
578	$(run-appcompat)
579endif  # module_run_appcompat
580	$(sign-package)
581ifdef LOCAL_COMPRESSED_MODULE
582	$(compress-package)
583endif  # LOCAL_COMPRESSED_MODULE
584
585my_package_res_pb := $(intermediates)/package-res.pb.apk
586$(my_package_res_pb): $(my_res_package) $(AAPT2)
587	$(AAPT2) convert --output-format proto $< -o $@
588
589$(my_bundle_module): $(my_package_res_pb)
590$(my_bundle_module): PRIVATE_RES_PACKAGE := $(my_package_res_pb)
591
592$(my_bundle_module): $(jni_shared_libraries)
593$(my_bundle_module): PRIVATE_JNI_SHARED_LIBRARIES := $(jni_shared_libraries_with_abis)
594$(my_bundle_module): PRIVATE_JNI_SHARED_LIBRARIES_ABI := $(jni_shared_libraries_abis)
595
596ifneq ($(full_classes_jar),)
597  $(my_bundle_module): PRIVATE_DEX_FILE := $(built_dex)
598  # Use the jarjar processed archive as the initial package file.
599  $(my_bundle_module): PRIVATE_SOURCE_ARCHIVE := $(full_classes_pre_proguard_jar)
600  $(my_bundle_module): $(built_dex)
601else
602  $(my_bundle_module): PRIVATE_DEX_FILE :=
603  $(my_bundle_module): PRIVATE_SOURCE_ARCHIVE :=
604endif # full_classes_jar
605
606$(my_bundle_module): $(MERGE_ZIPS) $(SOONG_ZIP) $(ZIP2ZIP)
607	@echo "target Bundle: $(PRIVATE_MODULE) ($@)"
608	rm -rf $@.parts
609	mkdir -p $@.parts
610	$(ZIP2ZIP) -i $(PRIVATE_RES_PACKAGE) -o $@.parts/apk.zip AndroidManifest.xml:manifest/AndroidManifest.xml resources.pb "res/**/*" "assets/**/*"
611      ifneq ($(jni_shared_libraries),)
612	  $(call create-jni-shared-libs-package,$@.parts/jni.zip)
613      endif
614      ifeq ($(full_classes_jar),)
615      # We don't build jar, need to add the Java resources here.
616	  $(if $(PRIVATE_EXTRA_JAR_ARGS),\
617	    $(call create-java-resources-jar,$@.parts/res.zip) && \
618	    $(ZIP2ZIP) -i $@.parts/res.zip -o $@.parts/res.zip.tmp "**/*:root/" && \
619	    mv -f $@.parts/res.zip.tmp $@.parts/res.zip)
620      else  # full_classes_jar
621	  $(call create-dex-jar,$@.parts/dex.zip,$(PRIVATE_DEX_FILE))
622	  $(ZIP2ZIP) -i $@.parts/dex.zip -o $@.parts/dex.zip.tmp "classes*.dex:dex/"
623	  mv -f $@.parts/dex.zip.tmp $@.parts/dex.zip
624	  $(call extract-resources-jar,$@.parts/res.zip,$(PRIVATE_SOURCE_ARCHIVE))
625	  $(ZIP2ZIP) -i $@.parts/res.zip -o $@.parts/res.zip.tmp "**/*:root/"
626	  mv -f $@.parts/res.zip.tmp $@.parts/res.zip
627      endif  # full_classes_jar
628	$(MERGE_ZIPS) $@ $@.parts/*.zip
629	rm -rf $@.parts
630ALL_MODULES.$(LOCAL_MODULE).BUNDLE := $(my_bundle_module)
631
632ifdef TARGET_BUILD_APPS
633  ifdef LOCAL_DPI_VARIANTS
634    $(call pretty-error,Building DPI-specific APKs is no longer supported)
635  endif
636endif
637
638###############################
639## Rule to build a jar containing dex files to dexpreopt without waiting for
640## the APK
641ifdef LOCAL_DEX_PREOPT
642  $(my_dex_jar): PRIVATE_DEX_FILE := $(built_dex)
643  $(my_dex_jar): $(built_dex) $(SOONG_ZIP)
644	$(hide) mkdir -p $(dir $@) && rm -f $@
645	$(call create-dex-jar,$@,$(PRIVATE_DEX_FILE))
646endif
647
648###############################
649## APK splits
650ifdef LOCAL_PACKAGE_SPLITS
651# The splits should have been built in the same command building the base apk.
652# This rule just runs signing.
653# Note that we explicily check the existence of the split apk and remove the
654# built base apk if the split apk isn't there.
655# That way the build system will rerun the aapt after the user changes the splitting parameters.
656$(built_apk_splits): PRIVATE_PRIVATE_KEY := $(private_key)
657$(built_apk_splits): PRIVATE_CERTIFICATE := $(certificate)
658$(built_apk_splits) : $(intermediates)/%.apk : $(LOCAL_BUILT_MODULE)
659	$(hide) if [ ! -f $@ ]; then \
660	  echo 'No $@ generated, check your apk splitting parameters.' 1>&2; \
661	  rm $<; exit 1; \
662	fi
663	$(sign-package)
664
665# Rules to install the splits
666installed_apk_splits := $(foreach s,$(my_split_suffixes),$(my_module_path)/$(LOCAL_MODULE)_$(s).apk)
667$(installed_apk_splits) : $(my_module_path)/$(LOCAL_MODULE)_%.apk : $(intermediates)/package_%.apk
668	@echo "Install: $@"
669	$(copy-file-to-new-target)
670
671# Register the additional built and installed files.
672ALL_MODULES.$(my_register_name).INSTALLED += $(installed_apk_splits)
673ALL_MODULES.$(my_register_name).BUILT_INSTALLED += \
674  $(foreach s,$(my_split_suffixes),$(intermediates)/package_$(s).apk:$(my_module_path)/$(LOCAL_MODULE)_$(s).apk)
675
676# Make sure to install the splits when you run "make <module_name>".
677$(my_all_targets): $(installed_apk_splits)
678
679ifdef LOCAL_COMPATIBILITY_SUITE
680
681$(foreach suite, $(LOCAL_COMPATIBILITY_SUITE), \
682  $(eval my_compat_dist_$(suite) := $(foreach dir, $(call compatibility_suite_dirs,$(suite)), \
683    $(foreach s,$(my_split_suffixes),\
684      $(call compat-copy-pair,$(intermediates)/package_$(s).apk,$(dir)/$(LOCAL_MODULE)_$(s).apk)))))
685
686$(call create-suite-dependencies)
687
688endif # LOCAL_COMPATIBILITY_SUITE
689endif # LOCAL_PACKAGE_SPLITS
690
691# Save information about this package
692PACKAGES.$(LOCAL_PACKAGE_NAME).OVERRIDES := $(strip $(LOCAL_OVERRIDES_PACKAGES))
693PACKAGES.$(LOCAL_PACKAGE_NAME).RESOURCE_FILES := $(all_resources)
694
695ifneq ($(LOCAL_MODULE_STEM),)
696  PACKAGES.$(LOCAL_MODULE).STEM := $(LOCAL_MODULE_STEM)
697else
698  PACKAGES.$(LOCAL_MODULE).STEM := $(LOCAL_MODULE)
699endif
700
701PACKAGES := $(PACKAGES) $(LOCAL_PACKAGE_NAME)
702
703# Reset internal variables.
704all_res_assets :=
705
706ifneq (,$(runtime_resource_overlays_product)$(runtime_resource_overlays_vendor))
707  ifdef LOCAL_EXPORT_PACKAGE_RESOURCES
708    enforce_rro_use_res_lib := true
709  else
710    enforce_rro_use_res_lib := false
711  endif
712
713  ifdef LOCAL_MANIFEST_PACKAGE_NAME
714    enforce_rro_is_manifest_package_name := true
715    enforce_rro_manifest_package_info := $(LOCAL_MANIFEST_PACKAGE_NAME)
716  else
717    enforce_rro_is_manifest_package_name := false
718    enforce_rro_manifest_package_info := $(full_android_manifest)
719  endif
720
721  ifdef runtime_resource_overlays_product
722    $(call append_enforce_rro_sources, \
723        $(my_register_name), \
724        $(enforce_rro_is_manifest_package_name), \
725        $(enforce_rro_manifest_package_info), \
726        $(enforce_rro_use_res_lib), \
727        $(runtime_resource_overlays_product), \
728        product \
729    )
730  endif
731  ifdef runtime_resource_overlays_vendor
732    $(call append_enforce_rro_sources, \
733        $(my_register_name), \
734        $(enforce_rro_is_manifest_package_name), \
735        $(enforce_rro_manifest_package_info), \
736        $(enforce_rro_use_res_lib), \
737        $(runtime_resource_overlays_vendor), \
738        vendor \
739    )
740  endif
741endif
742