1# 2# Copyright (C) 2019 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# Internal build rules for APPS prebuilt modules 19############################################################ 20 21ifneq (APPS,$(LOCAL_MODULE_CLASS)) 22$(call pretty-error,app_prebuilt_internal.mk is for APPS modules only) 23endif 24 25ifdef LOCAL_COMPRESSED_MODULE 26 ifneq (true,$(LOCAL_COMPRESSED_MODULE)) 27 $(call pretty-error, Unknown value for LOCAL_COMPRESSED_MODULE $(LOCAL_COMPRESSED_MODULE)) 28 endif 29 LOCAL_BUILT_MODULE_STEM := package.apk.gz 30 ifndef LOCAL_INSTALLED_MODULE_STEM 31 PACKAGES.$(LOCAL_MODULE).COMPRESSED := gz 32 LOCAL_INSTALLED_MODULE_STEM := $(LOCAL_MODULE).apk.gz 33 endif 34else # LOCAL_COMPRESSED_MODULE 35 LOCAL_BUILT_MODULE_STEM := package.apk 36 ifndef LOCAL_INSTALLED_MODULE_STEM 37 LOCAL_INSTALLED_MODULE_STEM := $(LOCAL_MODULE).apk 38 endif 39endif # LOCAL_COMPRESSED_MODULE 40 41include $(BUILD_SYSTEM)/base_rules.mk 42built_module := $(LOCAL_BUILT_MODULE) 43 44# Run veridex on product, product_services and vendor modules. 45# We skip it for unbundled app builds where we cannot build veridex. 46module_run_appcompat := 47ifeq (true,$(non_system_module)) 48ifeq (,$(TARGET_BUILD_APPS)$(filter true,$(TARGET_BUILD_PDK))) # ! unbundled app build 49ifneq ($(UNSAFE_DISABLE_HIDDENAPI_FLAGS),true) 50 module_run_appcompat := true 51endif 52endif 53endif 54 55PACKAGES.$(LOCAL_MODULE).OVERRIDES := $(strip $(LOCAL_OVERRIDES_PACKAGES)) 56 57my_extract_apk := $(strip $(LOCAL_EXTRACT_APK)) 58 59# Select dpi-specific source 60ifdef LOCAL_DPI_VARIANTS 61my_dpi := $(firstword $(filter $(LOCAL_DPI_VARIANTS),$(PRODUCT_AAPT_PREF_CONFIG) $(PRODUCT_AAPT_PREBUILT_DPI))) 62ifdef my_dpi 63ifdef LOCAL_DPI_FILE_STEM 64my_prebuilt_dpi_file_stem := $(LOCAL_DPI_FILE_STEM) 65else 66my_prebuilt_dpi_file_stem := $(LOCAL_MODULE)_%.apk 67endif 68my_prebuilt_src_file := $(dir $(my_prebuilt_src_file))$(subst %,$(my_dpi),$(my_prebuilt_dpi_file_stem)) 69 70ifneq ($(strip $(LOCAL_EXTRACT_DPI_APK)),) 71my_extract_apk := $(subst %,$(my_dpi),$(LOCAL_EXTRACT_DPI_APK)) 72endif # LOCAL_EXTRACT_DPI_APK 73endif # my_dpi 74endif # LOCAL_DPI_VARIANTS 75 76ifdef my_extract_apk 77my_extracted_apk := $(intermediates)/extracted.apk 78 79$(my_extracted_apk): PRIVATE_EXTRACT := $(my_extract_apk) 80$(my_extracted_apk): $(my_prebuilt_src_file) 81 @echo Extract APK: $@ 82 $(hide) mkdir -p $(dir $@) && rm -f $@ 83 $(hide) unzip -p $< $(PRIVATE_EXTRACT) >$@ 84 85my_prebuilt_src_file := $(my_extracted_apk) 86my_extracted_apk := 87my_extract_apk := 88ifeq ($(PRODUCT_ALWAYS_PREOPT_EXTRACTED_APK),true) 89# If the product property is set, always preopt for extracted modules to prevent executing out of 90# the APK. 91my_preopt_for_extracted_apk := true 92endif 93endif 94 95dex_preopt_profile_src_file := $(my_prebuilt_src_file) 96 97rs_compatibility_jni_libs := 98include $(BUILD_SYSTEM)/install_jni_libs.mk 99 100ifeq ($(LOCAL_CERTIFICATE),EXTERNAL) 101 # The magic string "EXTERNAL" means this package will be signed with 102 # the default dev key throughout the build process, but we expect 103 # the final package to be signed with a different key. 104 # 105 # This can be used for packages where we don't have access to the 106 # keys, but want the package to be predexopt'ed. 107 LOCAL_CERTIFICATE := $(DEFAULT_SYSTEM_DEV_CERTIFICATE) 108 PACKAGES.$(LOCAL_MODULE).EXTERNAL_KEY := 1 109 110 $(built_module) : $(LOCAL_CERTIFICATE).pk8 $(LOCAL_CERTIFICATE).x509.pem 111 $(built_module) : PRIVATE_PRIVATE_KEY := $(LOCAL_CERTIFICATE).pk8 112 $(built_module) : PRIVATE_CERTIFICATE := $(LOCAL_CERTIFICATE).x509.pem 113endif 114ifeq ($(LOCAL_CERTIFICATE),) 115 # It is now a build error to add a prebuilt .apk without 116 # specifying a key for it. 117 $(error No LOCAL_CERTIFICATE specified for prebuilt "$(my_prebuilt_src_file)") 118else ifeq ($(LOCAL_CERTIFICATE),PRESIGNED) 119 # The magic string "PRESIGNED" means this package is already checked 120 # signed with its release key. 121 # 122 # By setting .CERTIFICATE but not .PRIVATE_KEY, this package will be 123 # mentioned in apkcerts.txt (with certificate set to "PRESIGNED") 124 # but the dexpreopt process will not try to re-sign the app. 125 PACKAGES.$(LOCAL_MODULE).CERTIFICATE := PRESIGNED 126 PACKAGES := $(PACKAGES) $(LOCAL_MODULE) 127else 128 # If this is not an absolute certificate, assign it to a generic one. 129 ifeq ($(dir $(strip $(LOCAL_CERTIFICATE))),./) 130 LOCAL_CERTIFICATE := $(dir $(DEFAULT_SYSTEM_DEV_CERTIFICATE))$(LOCAL_CERTIFICATE) 131 endif 132 133 PACKAGES.$(LOCAL_MODULE).PRIVATE_KEY := $(LOCAL_CERTIFICATE).pk8 134 PACKAGES.$(LOCAL_MODULE).CERTIFICATE := $(LOCAL_CERTIFICATE).x509.pem 135 PACKAGES := $(PACKAGES) $(LOCAL_MODULE) 136 137 $(built_module) : $(LOCAL_CERTIFICATE).pk8 $(LOCAL_CERTIFICATE).x509.pem 138 $(built_module) : PRIVATE_PRIVATE_KEY := $(LOCAL_CERTIFICATE).pk8 139 $(built_module) : PRIVATE_CERTIFICATE := $(LOCAL_CERTIFICATE).x509.pem 140endif 141 142include $(BUILD_SYSTEM)/app_certificate_validate.mk 143 144# Disable dex-preopt of prebuilts to save space, if requested. 145ifndef LOCAL_DEX_PREOPT 146ifeq ($(DONT_DEXPREOPT_PREBUILTS),true) 147LOCAL_DEX_PREOPT := false 148endif 149endif 150 151# If the module is a compressed module, we don't pre-opt it because its final 152# installation location will be the data partition. 153ifdef LOCAL_COMPRESSED_MODULE 154LOCAL_DEX_PREOPT := false 155endif 156 157my_dex_jar := $(my_prebuilt_src_file) 158 159####################################### 160# defines built_odex along with rule to install odex 161include $(BUILD_SYSTEM)/dex_preopt_odex_install.mk 162####################################### 163ifneq ($(LOCAL_REPLACE_PREBUILT_APK_INSTALLED),) 164# There is a replacement for the prebuilt .apk we can install without any processing. 165$(built_module) : $(LOCAL_REPLACE_PREBUILT_APK_INSTALLED) 166 $(transform-prebuilt-to-target) 167 168else # ! LOCAL_REPLACE_PREBUILT_APK_INSTALLED 169# Sign and align non-presigned .apks. 170# The embedded prebuilt jni to uncompress. 171ifeq ($(LOCAL_CERTIFICATE),PRESIGNED) 172# For PRESIGNED apks we must uncompress every .so file: 173# even if the .so file isn't for the current TARGET_ARCH, 174# we can't strip the file. 175embedded_prebuilt_jni_libs := 176endif 177ifndef embedded_prebuilt_jni_libs 178# No LOCAL_PREBUILT_JNI_LIBS, uncompress all. 179embedded_prebuilt_jni_libs := 180endif 181$(built_module): PRIVATE_EMBEDDED_JNI_LIBS := $(embedded_prebuilt_jni_libs) 182 183ifdef LOCAL_COMPRESSED_MODULE 184$(built_module) : $(MINIGZIP) 185endif 186 187ifeq ($(module_run_appcompat),true) 188$(built_module) : $(appcompat-files) 189$(LOCAL_BUILT_MODULE): PRIVATE_INSTALLED_MODULE := $(LOCAL_INSTALLED_MODULE) 190endif 191 192ifneq ($(BUILD_PLATFORM_ZIP),) 193$(built_module) : .KATI_IMPLICIT_OUTPUTS := $(dir $(LOCAL_BUILT_MODULE))package.dex.apk 194endif 195ifneq ($(LOCAL_CERTIFICATE),PRESIGNED) 196ifdef LOCAL_DEX_PREOPT 197$(built_module) : PRIVATE_STRIP_SCRIPT := $(intermediates)/strip.sh 198$(built_module) : $(intermediates)/strip.sh 199$(built_module) : | $(DEXPREOPT_STRIP_DEPS) 200$(built_module) : .KATI_DEPFILE := $(built_module).d 201endif 202endif 203ifeq ($(module_run_appcompat),true) 204$(built_module) : $(AAPT2) 205endif 206$(built_module) : $(my_prebuilt_src_file) | $(ZIPALIGN) $(ZIP2ZIP) $(SIGNAPK_JAR) 207 $(transform-prebuilt-to-target) 208 $(uncompress-prebuilt-embedded-jni-libs) 209ifeq (true, $(LOCAL_UNCOMPRESS_DEX)) 210 $(uncompress-dexs) 211endif # LOCAL_UNCOMPRESS_DEX 212ifdef LOCAL_DEX_PREOPT 213ifneq ($(BUILD_PLATFORM_ZIP),) 214 @# Keep a copy of apk with classes.dex unstripped 215 $(hide) cp -f $@ $(dir $@)package.dex.apk 216endif # BUILD_PLATFORM_ZIP 217endif # LOCAL_DEX_PREOPT 218ifneq ($(LOCAL_CERTIFICATE),PRESIGNED) 219 @# Only strip out files if we can re-sign the package. 220# Run appcompat before stripping the classes.dex file. 221ifeq ($(module_run_appcompat),true) 222 $(call appcompat-header, aapt2) 223 $(run-appcompat) 224endif # module_run_appcompat 225ifdef LOCAL_DEX_PREOPT 226 mv -f $@ $@.tmp 227 $(PRIVATE_STRIP_SCRIPT) $@.tmp $@ 228endif # LOCAL_DEX_PREOPT 229 $(sign-package) 230 # No need for align-package because sign-package takes care of alignment 231else # LOCAL_CERTIFICATE == PRESIGNED 232 $(align-package) 233endif # LOCAL_CERTIFICATE 234ifdef LOCAL_COMPRESSED_MODULE 235 $(compress-package) 236endif # LOCAL_COMPRESSED_MODULE 237endif # ! LOCAL_REPLACE_PREBUILT_APK_INSTALLED 238 239 240############################### 241## Install split apks. 242ifdef LOCAL_PACKAGE_SPLITS 243ifdef LOCAL_COMPRESSED_MODULE 244$(error $(LOCAL_MODULE): LOCAL_COMPRESSED_MODULE is not currently supported for split installs) 245endif # LOCAL_COMPRESSED_MODULE 246 247# LOCAL_PACKAGE_SPLITS is a list of apks to be installed. 248built_apk_splits := $(addprefix $(intermediates)/,$(notdir $(LOCAL_PACKAGE_SPLITS))) 249installed_apk_splits := $(addprefix $(my_module_path)/,$(notdir $(LOCAL_PACKAGE_SPLITS))) 250 251# Rules to sign the split apks. 252my_src_dir := $(sort $(dir $(LOCAL_PACKAGE_SPLITS))) 253ifneq (1,$(words $(my_src_dir))) 254$(error You must put all the split source apks in the same folder: $(LOCAL_PACKAGE_SPLITS)) 255endif 256my_src_dir := $(LOCAL_PATH)/$(my_src_dir) 257 258$(built_apk_splits) : $(LOCAL_CERTIFICATE).pk8 $(LOCAL_CERTIFICATE).x509.pem 259$(built_apk_splits) : PRIVATE_PRIVATE_KEY := $(LOCAL_CERTIFICATE).pk8 260$(built_apk_splits) : PRIVATE_CERTIFICATE := $(LOCAL_CERTIFICATE).x509.pem 261$(built_apk_splits) : $(intermediates)/%.apk : $(my_src_dir)/%.apk 262 $(copy-file-to-new-target) 263 $(sign-package) 264 265# Rules to install the split apks. 266$(installed_apk_splits) : $(my_module_path)/%.apk : $(intermediates)/%.apk 267 @echo "Install: $@" 268 $(copy-file-to-new-target) 269 270# Register the additional built and installed files. 271ALL_MODULES.$(my_register_name).INSTALLED += $(installed_apk_splits) 272ALL_MODULES.$(my_register_name).BUILT_INSTALLED += \ 273 $(foreach s,$(LOCAL_PACKAGE_SPLITS),$(intermediates)/$(notdir $(s)):$(my_module_path)/$(notdir $(s))) 274 275# Make sure to install the splits when you run "make <module_name>". 276$(my_all_targets): $(installed_apk_splits) 277 278endif # LOCAL_PACKAGE_SPLITS 279 280