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
36# Hack to build static Java library with Android resource
37# See bug 5714516
38all_resources :=
39need_compile_res :=
40# A static Java library needs to explicily set LOCAL_RESOURCE_DIR.
41ifdef LOCAL_RESOURCE_DIR
42need_compile_res := true
43LOCAL_RESOURCE_DIR := $(foreach d,$(LOCAL_RESOURCE_DIR),$(call clean-path,$(d)))
44endif
45ifneq ($(strip $(LOCAL_STATIC_ANDROID_LIBRARIES) $(LOCAL_STATIC_JAVA_AAR_LIBRARIES)),)
46need_compile_res := true
47endif
48
49ifeq ($(need_compile_res),true)
50all_resources := $(strip \
51    $(foreach dir, $(LOCAL_RESOURCE_DIR), \
52      $(addprefix $(dir)/, \
53        $(patsubst res/%,%, \
54          $(call find-subdir-assets,$(dir)) \
55        ) \
56      ) \
57    ))
58
59# By default we should remove the R/Manifest classes from a static Java library,
60# because they will be regenerated in the app that uses it.
61# But if the static Java library will be used by a library, then we may need to
62# keep the generated classes with "LOCAL_JAR_EXCLUDE_FILES := none".
63ifndef LOCAL_JAR_EXCLUDE_FILES
64LOCAL_JAR_EXCLUDE_FILES := $(ANDROID_RESOURCE_GENERATED_CLASSES)
65endif
66ifeq (none,$(LOCAL_JAR_EXCLUDE_FILES))
67LOCAL_JAR_EXCLUDE_FILES :=
68endif
69
70proguard_options_file :=
71
72ifneq ($(filter custom,$(LOCAL_PROGUARD_ENABLED)),custom)
73  proguard_options_file := $(intermediates.COMMON)/proguard_options
74endif
75
76LOCAL_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_PROGUARD_FLAGS)
77LOCAL_PROGUARD_FLAGS_DEPS += $(proguard_options_file)
78
79R_file_stamp := $(intermediates.COMMON)/src/R.stamp
80LOCAL_INTERMEDIATE_TARGETS += $(R_file_stamp)
81
82ifneq ($(strip $(LOCAL_STATIC_ANDROID_LIBRARIES) $(LOCAL_STATIC_JAVA_AAR_LIBRARIES)),)
83  # If we are using static android libraries, every source file becomes an overlay.
84  # This is to emulate old AAPT behavior which simulated library support.
85  my_res_resources :=
86  my_overlay_resources := $(all_resources)
87else
88  # Otherwise, for a library we treat all the resource equal with no overlay.
89  my_res_resources := $(all_resources)
90  my_overlay_resources :=
91endif
92# For libraries put everything in the COMMON intermediate directory.
93my_res_package := $(intermediates.COMMON)/package-res.apk
94
95LOCAL_INTERMEDIATE_TARGETS += $(my_res_package)
96
97endif  # need_compile_res
98
99all_res_assets := $(all_resources)
100
101include $(BUILD_SYSTEM)/java_renderscript.mk
102
103ifeq (true,$(need_compile_res))
104# work around missing manifests by creating a default one
105ifeq (,$(strip $(LOCAL_MANIFEST_FILE)$(LOCAL_FULL_MANIFEST_FILE)))
106  ifeq (,$(wildcard $(LOCAL_PATH)/AndroidManifest.xml))
107    LOCAL_FULL_MANIFEST_FILE := $(call local-intermediates-dir,COMMON)/DefaultManifest.xml
108    $(call create-default-manifest-file,$(LOCAL_FULL_MANIFEST_FILE),$(call module-min-sdk-version))
109  endif
110endif
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_USE_PREBUILT_SDKS),$(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
130# transitive-res-packages is only populated for Soong modules for now, but needs
131# to exist so that other Make modules can depend on it.  Create an empty file.
132my_transitive_res_packages := $(intermediates.COMMON)/transitive-res-packages
133$(my_transitive_res_packages):
134	touch $@
135
136import_proguard_flag_files := $(strip $(foreach l,$(LOCAL_STATIC_ANDROID_LIBRARIES) $(LOCAL_STATIC_JAVA_AAR_LIBRARIES),\
137    $(call intermediates-dir-for,JAVA_LIBRARIES,$(l),,COMMON)/export_proguard_flags))
138$(intermediates.COMMON)/export_proguard_flags: $(import_proguard_flag_files) $(addprefix $(LOCAL_PATH)/,$(LOCAL_EXPORT_PROGUARD_FLAG_FILES))
139	@echo "Export proguard flags: $@"
140	rm -f $@
141	touch $@
142	for f in $+; do \
143		echo -e "\n# including $$f" >>$@; \
144		cat $$f >>$@; \
145	done
146import_proguard_flag_files :=
147
148include $(BUILD_SYSTEM)/aapt_flags.mk
149
150$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS)
151$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_TARGET_AAPT_CHARACTERISTICS := $(TARGET_AAPT_CHARACTERISTICS)
152$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_PACKAGE_NAME := $(LOCAL_MANIFEST_PACKAGE_NAME)
153$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_INSTRUMENTATION_FOR := $(LOCAL_MANIFEST_INSTRUMENTATION_FOR)
154
155# add --non-constant-id to prevent inlining constants.
156# AAR needs text symbol file R.txt.
157$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --static-lib --output-text-symbols $(intermediates.COMMON)/R.txt
158ifndef LOCAL_AAPT_NAMESPACES
159  $(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_FLAGS += --no-static-lib-packages
160endif
161$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PRODUCT_AAPT_CONFIG :=
162$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PRODUCT_AAPT_PREF_CONFIG :=
163$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_TARGET_AAPT_CHARACTERISTICS :=
164
165$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ANDROID_MANIFEST := $(full_android_manifest)
166$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_PUBLICS_OUTPUT := $(intermediates.COMMON)/public_resources.xml
167$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
168$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_AAPT_INCLUDES := $(framework_res_package_export)
169
170$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASSET_DIR :=
171$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_PROGUARD_OPTIONS_FILE := $(proguard_options_file)
172$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_PACKAGE_NAME :=
173$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_MANIFEST_INSTRUMENTATION_FOR :=
174
175# One more level with name res so we can zip up the flat resources that can be linked by apps.
176my_compiled_res_base_dir := $(intermediates.COMMON)/flat-res/res
177ifneq (,$(filter-out current,$(renderscript_target_api)))
178  ifneq ($(call math_gt_or_eq,$(renderscript_target_api),21),true)
179    my_generated_res_zips := $(rs_generated_res_zip)
180  endif  # renderscript_target_api < 21
181endif  # renderscript_target_api is set
182include $(BUILD_SYSTEM)/aapt2.mk
183$(my_res_package) : $(framework_res_package_export)
184$(my_res_package): .KATI_IMPLICIT_OUTPUTS += $(intermediates.COMMON)/R.txt
185
186endif # need_compile_res
187
188include $(BUILD_SYSTEM)/java_library.mk
189
190ifeq (true,$(need_compile_res))
191
192$(LOCAL_BUILT_MODULE): $(R_file_stamp)
193$(java_source_list_file): $(R_file_stamp)
194$(full_classes_compiled_jar): $(R_file_stamp)
195$(full_classes_turbine_jar): $(R_file_stamp)
196
197
198# if we have custom proguarding done use the proguarded classes jar instead of the normal classes jar
199ifeq ($(filter custom,$(LOCAL_PROGUARD_ENABLED)),custom)
200aar_classes_jar = $(full_classes_jar)
201else
202aar_classes_jar = $(full_classes_pre_proguard_jar)
203endif
204
205# Rule to build AAR, archive including classes.jar, resource, etc.
206built_aar := $(intermediates.COMMON)/javalib.aar
207$(built_aar): PRIVATE_MODULE := $(LOCAL_MODULE)
208$(built_aar): PRIVATE_ANDROID_MANIFEST := $(full_android_manifest)
209$(built_aar): PRIVATE_CLASSES_JAR := $(aar_classes_jar)
210$(built_aar): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
211$(built_aar): PRIVATE_R_TXT := $(intermediates.COMMON)/R.txt
212$(built_aar): $(JAR_ARGS)
213$(built_aar) : $(aar_classes_jar) $(full_android_manifest) $(intermediates.COMMON)/R.txt
214	@echo "target AAR:  $(PRIVATE_MODULE) ($@)"
215	$(hide) rm -rf $(dir $@)aar && mkdir -p $(dir $@)aar/res
216	$(hide) cp $(PRIVATE_ANDROID_MANIFEST) $(dir $@)aar/AndroidManifest.xml
217	$(hide) cp $(PRIVATE_CLASSES_JAR) $(dir $@)aar/classes.jar
218	# Note: Use "cp -n" to honor the resource overlay rules, if multiple res dirs exist.
219	$(hide) $(foreach res,$(PRIVATE_RESOURCE_DIR),cp -Rfn $(res)/* $(dir $@)aar/res;)
220	$(hide) cp $(PRIVATE_R_TXT) $(dir $@)aar/R.txt
221	$(hide) $(JAR) -cMf $@ \
222	  $(call jar-args-sorted-files-in-directory,$(dir $@)aar)
223
224# Register the aar file.
225ALL_MODULES.$(my_register_name).AAR := $(built_aar)
226endif  # need_compile_res
227
228# Reset internal variables.
229aar_classes_jar :=
230all_res_assets :=
231LOCAL_IS_STATIC_JAVA_LIBRARY :=
232