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# Standard rules for building a "static" java library. 18# Static java libraries are not installed, nor listed on any 19# classpaths. They can, however, be included wholesale in 20# other java modules. 21 22$(call record-module-type,STATIC_JAVA_LIBRARY) 23LOCAL_UNINSTALLABLE_MODULE := true 24LOCAL_IS_STATIC_JAVA_LIBRARY := true 25LOCAL_MODULE_CLASS := JAVA_LIBRARIES 26 27intermediates.COMMON := $(call local-intermediates-dir,COMMON) 28 29my_res_package := 30 31# Process Support Library dependencies. 32include $(BUILD_SYSTEM)/support_libraries.mk 33 34include $(BUILD_SYSTEM)/force_aapt2.mk 35 36ifdef LOCAL_AAPT2_ONLY 37LOCAL_USE_AAPT2 := true 38endif 39 40# Hack to build static Java library with Android resource 41# See bug 5714516 42all_resources := 43need_compile_res := 44# A static Java library needs to explicily set LOCAL_RESOURCE_DIR. 45ifdef LOCAL_RESOURCE_DIR 46need_compile_res := true 47LOCAL_RESOURCE_DIR := $(foreach d,$(LOCAL_RESOURCE_DIR),$(call clean-path,$(d))) 48endif 49ifeq ($(LOCAL_USE_AAPT2),true) 50ifneq ($(strip $(LOCAL_STATIC_ANDROID_LIBRARIES) $(LOCAL_STATIC_JAVA_AAR_LIBRARIES)),) 51need_compile_res := true 52endif 53endif 54 55ifeq ($(need_compile_res),true) 56all_resources := $(strip \ 57 $(foreach dir, $(LOCAL_RESOURCE_DIR), \ 58 $(addprefix $(dir)/, \ 59 $(patsubst res/%,%, \ 60 $(call find-subdir-assets,$(dir)) \ 61 ) \ 62 ) \ 63 )) 64 65# By default we should remove the R/Manifest classes from a static Java library, 66# because they will be regenerated in the app that uses it. 67# But if the static Java library will be used by a library, then we may need to 68# keep the generated classes with "LOCAL_JAR_EXCLUDE_FILES := none". 69ifndef LOCAL_JAR_EXCLUDE_FILES 70LOCAL_JAR_EXCLUDE_FILES := $(ANDROID_RESOURCE_GENERATED_CLASSES) 71endif 72ifeq (none,$(LOCAL_JAR_EXCLUDE_FILES)) 73LOCAL_JAR_EXCLUDE_FILES := 74endif 75 76proguard_options_file := 77 78ifneq ($(filter custom,$(LOCAL_PROGUARD_ENABLED)),custom) 79 proguard_options_file := $(intermediates.COMMON)/proguard_options 80endif 81 82LOCAL_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_PROGUARD_FLAGS) 83 84R_file_stamp := $(intermediates.COMMON)/src/R.stamp 85LOCAL_INTERMEDIATE_TARGETS += $(R_file_stamp) 86 87ifeq ($(LOCAL_USE_AAPT2),true) 88 ifneq ($(strip $(LOCAL_STATIC_ANDROID_LIBRARIES) $(LOCAL_STATIC_JAVA_AAR_LIBRARIES)),) 89 # If we are using static android libraries, every source file becomes an overlay. 90 # This is to emulate old AAPT behavior which simulated library support. 91 my_res_resources := 92 my_overlay_resources := $(all_resources) 93 else 94 # Otherwise, for a library we treat all the resource equal with no overlay. 95 my_res_resources := $(all_resources) 96 my_overlay_resources := 97 endif 98 # For libraries put everything in the COMMON intermediate directory. 99 my_res_package := $(intermediates.COMMON)/package-res.apk 100 101 LOCAL_INTERMEDIATE_TARGETS += $(my_res_package) 102endif # LOCAL_USE_AAPT2 103 104endif # need_compile_res 105 106all_res_assets := $(all_resources) 107 108include $(BUILD_SYSTEM)/java_renderscript.mk 109 110ifeq (true,$(need_compile_res)) 111include $(BUILD_SYSTEM)/android_manifest.mk 112 113LOCAL_SDK_RES_VERSION:=$(strip $(LOCAL_SDK_RES_VERSION)) 114ifeq ($(LOCAL_SDK_RES_VERSION),) 115 LOCAL_SDK_RES_VERSION:=$(LOCAL_SDK_VERSION) 116endif 117 118framework_res_package_export := 119# Please refer to package.mk 120ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true) 121ifneq ($(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))),) 122framework_res_package_export := \ 123 $(call resolve-prebuilt-sdk-jar-path,$(LOCAL_SDK_RES_VERSION)) 124else 125framework_res_package_export := \ 126 $(call intermediates-dir-for,APPS,framework-res,,COMMON)/package-export.apk 127endif 128endif 129 130ifeq ($(LOCAL_USE_AAPT2),true) 131import_proguard_flag_files := $(strip $(foreach l,$(LOCAL_STATIC_ANDROID_LIBRARIES) $(LOCAL_STATIC_JAVA_AAR_LIBRARIES),\ 132 $(call intermediates-dir-for,JAVA_LIBRARIES,$(l),,COMMON)/export_proguard_flags)) 133$(intermediates.COMMON)/export_proguard_flags: $(import_proguard_flag_files) $(addprefix $(LOCAL_PATH)/,$(LOCAL_EXPORT_PROGUARD_FLAG_FILES)) 134 @echo "Export proguard flags: $@" 135 rm -f $@ 136 touch $@ 137 for f in $+; do \ 138 echo -e "\n# including $$f" >>$@; \ 139 cat $$f >>$@; \ 140 done 141import_proguard_flag_files := 142endif 143 144include $(BUILD_SYSTEM)/aapt_flags.mk 145 146$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) 147$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_TARGET_AAPT_CHARACTERISTICS := $(TARGET_AAPT_CHARACTERISTICS) 148$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_PACKAGE_NAME := $(LOCAL_MANIFEST_PACKAGE_NAME) 149$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_INSTRUMENTATION_FOR := $(LOCAL_MANIFEST_INSTRUMENTATION_FOR) 150 151# add --non-constant-id to prevent inlining constants. 152# AAR needs text symbol file R.txt. 153ifeq ($(LOCAL_USE_AAPT2),true) 154$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --static-lib --output-text-symbols $(intermediates.COMMON)/R.txt 155ifndef LOCAL_AAPT_NAMESPACES 156 $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS += --no-static-lib-packages 157endif 158$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PRODUCT_AAPT_CONFIG := 159$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PRODUCT_AAPT_PREF_CONFIG := 160$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_TARGET_AAPT_CHARACTERISTICS := 161else 162$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --non-constant-id --output-text-symbols $(intermediates.COMMON) 163 164my_srcjar := $(intermediates.COMMON)/aapt.srcjar 165LOCAL_SRCJARS += $(my_srcjar) 166$(R_file_stamp): PRIVATE_SRCJAR := $(my_srcjar) 167$(R_file_stamp): PRIVATE_JAVA_GEN_DIR := $(intermediates.COMMON)/aapt 168$(R_file_stamp): .KATI_IMPLICIT_OUTPUTS := $(my_srcjar) 169endif 170 171$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ANDROID_MANIFEST := $(full_android_manifest) 172$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_PUBLICS_OUTPUT := $(intermediates.COMMON)/public_resources.xml 173$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR) 174$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_INCLUDES := $(framework_res_package_export) 175 176$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASSET_DIR := 177$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PROGUARD_OPTIONS_FILE := $(proguard_options_file) 178$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_PACKAGE_NAME := 179$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_INSTRUMENTATION_FOR := 180 181ifeq ($(LOCAL_USE_AAPT2),true) 182 # One more level with name res so we can zip up the flat resources that can be linked by apps. 183 my_compiled_res_base_dir := $(intermediates.COMMON)/flat-res/res 184 ifneq (,$(filter-out current,$(renderscript_target_api))) 185 ifneq ($(call math_gt_or_eq,$(renderscript_target_api),21),true) 186 my_generated_res_zips := $(rs_generated_res_zip) 187 endif # renderscript_target_api < 21 188 endif # renderscript_target_api is set 189 include $(BUILD_SYSTEM)/aapt2.mk 190 $(my_res_package) : $(framework_res_package_export) 191 $(my_res_package): .KATI_IMPLICIT_OUTPUTS += $(intermediates.COMMON)/R.txt 192else 193 $(R_file_stamp): .KATI_IMPLICIT_OUTPUTS += $(intermediates.COMMON)/R.txt 194 $(R_file_stamp): PRIVATE_RESOURCE_LIST := $(all_resources) 195 $(R_file_stamp) : $(all_resources) $(full_android_manifest) $(AAPT) $(SOONG_ZIP) \ 196 $(framework_res_package_export) $(rs_generated_res_zip) 197 @echo "target R.java/Manifest.java: $(PRIVATE_MODULE) ($@)" 198 $(create-resource-java-files) 199 $(hide) find $(PRIVATE_JAVA_GEN_DIR) -name R.java | xargs cat > $@ 200endif # LOCAL_USE_AAPT2 201 202endif # need_compile_res 203 204include $(BUILD_SYSTEM)/java_library.mk 205 206ifeq (true,$(need_compile_res)) 207 208$(LOCAL_BUILT_MODULE): $(R_file_stamp) 209$(java_source_list_file): $(R_file_stamp) 210$(full_classes_compiled_jar): $(R_file_stamp) 211$(full_classes_turbine_jar): $(R_file_stamp) 212 213 214# if we have custom proguarding done use the proguarded classes jar instead of the normal classes jar 215ifeq ($(filter custom,$(LOCAL_PROGUARD_ENABLED)),custom) 216aar_classes_jar = $(full_classes_jar) 217else 218aar_classes_jar = $(full_classes_pre_proguard_jar) 219endif 220 221# Rule to build AAR, archive including classes.jar, resource, etc. 222built_aar := $(intermediates.COMMON)/javalib.aar 223$(built_aar): PRIVATE_MODULE := $(LOCAL_MODULE) 224$(built_aar): PRIVATE_ANDROID_MANIFEST := $(full_android_manifest) 225$(built_aar): PRIVATE_CLASSES_JAR := $(aar_classes_jar) 226$(built_aar): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR) 227$(built_aar): PRIVATE_R_TXT := $(intermediates.COMMON)/R.txt 228$(built_aar): $(JAR_ARGS) 229$(built_aar) : $(aar_classes_jar) $(full_android_manifest) $(intermediates.COMMON)/R.txt 230 @echo "target AAR: $(PRIVATE_MODULE) ($@)" 231 $(hide) rm -rf $(dir $@)aar && mkdir -p $(dir $@)aar/res 232 $(hide) cp $(PRIVATE_ANDROID_MANIFEST) $(dir $@)aar/AndroidManifest.xml 233 $(hide) cp $(PRIVATE_CLASSES_JAR) $(dir $@)aar/classes.jar 234 # Note: Use "cp -n" to honor the resource overlay rules, if multiple res dirs exist. 235 $(hide) $(foreach res,$(PRIVATE_RESOURCE_DIR),cp -Rfn $(res)/* $(dir $@)aar/res;) 236 $(hide) cp $(PRIVATE_R_TXT) $(dir $@)aar/R.txt 237 $(hide) $(JAR) -cMf $@ \ 238 $(call jar-args-sorted-files-in-directory,$(dir $@)aar) 239 240# Register the aar file. 241ALL_MODULES.$(LOCAL_MODULE).AAR := $(built_aar) 242endif # need_compile_res 243 244# Reset internal variables. 245aar_classes_jar := 246all_res_assets := 247LOCAL_IS_STATIC_JAVA_LIBRARY := 248