java.mk revision 0b9382c792c303a93f5805adf4eba377dac01950
1# Requires:
2# LOCAL_MODULE_SUFFIX
3# LOCAL_MODULE_CLASS
4# all_res_assets
5
6# Make sure there's something to build.
7# It's possible to build a package that doesn't contain any classes.
8ifeq (,$(strip $(LOCAL_SRC_FILES)$(all_res_assets)))
9$(error $(LOCAL_PATH): Target java module does not define any source or resource files)
10endif
11
12LOCAL_NO_STANDARD_LIBRARIES:=$(strip $(LOCAL_NO_STANDARD_LIBRARIES))
13LOCAL_SDK_VERSION:=$(strip $(LOCAL_SDK_VERSION))
14
15ifneq ($(LOCAL_SDK_VERSION),)
16  ifeq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
17    $(error $(LOCAL_PATH): Must not define both LOCAL_NO_STANDARD_LIBRARIES and LOCAL_SDK_VERSION)
18  else
19    ifeq ($(strip $(filter $(LOCAL_SDK_VERSION),$(TARGET_AVAILABLE_SDK_VERSIONS))),)
20      $(error $(LOCAL_PATH): Invalid LOCAL_SDK_VERSION '$(LOCAL_SDK_VERSION)' \
21             Choices are: $(TARGET_AVAILABLE_SDK_VERSIONS))
22    else
23      ifeq ($(LOCAL_SDK_VERSION),current)
24        LOCAL_JAVA_LIBRARIES := android_stubs_$(LOCAL_SDK_VERSION) $(LOCAL_JAVA_LIBRARIES)
25      else
26        LOCAL_JAVA_LIBRARIES := sdk_v$(LOCAL_SDK_VERSION) $(LOCAL_JAVA_LIBRARIES)
27      endif
28    endif
29  endif
30else
31  ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true)
32    LOCAL_JAVA_LIBRARIES := core core-junit ext framework $(LOCAL_JAVA_LIBRARIES)
33  endif
34endif
35LOCAL_JAVA_LIBRARIES := $(sort $(LOCAL_JAVA_LIBRARIES))
36
37LOCAL_BUILT_MODULE_STEM := $(strip $(LOCAL_BUILT_MODULE_STEM))
38ifeq ($(LOCAL_BUILT_MODULE_STEM),)
39$(error $(LOCAL_PATH): Target java template must define LOCAL_BUILT_MODULE_STEM)
40endif
41ifneq ($(filter classes-compiled.jar classes.jar,$(LOCAL_BUILT_MODULE_STEM)),)
42$(error LOCAL_BUILT_MODULE_STEM may not be "$(LOCAL_BUILT_MODULE_STEM)")
43endif
44
45
46##############################################################################
47# Define the intermediate targets before including base_rules so they get
48# the correct environment.
49##############################################################################
50
51intermediates := $(call local-intermediates-dir)
52intermediates.COMMON := $(call local-intermediates-dir,COMMON)
53
54# Emma source code coverage
55ifneq ($(EMMA_INSTRUMENT),true)
56LOCAL_NO_EMMA_INSTRUMENT := true
57LOCAL_NO_EMMA_COMPILE := true
58endif
59
60# Choose leaf name for the compiled jar file.
61ifneq ($(LOCAL_NO_EMMA_COMPILE),true)
62full_classes_compiled_jar_leaf := classes-no-debug-var.jar
63built_dex_intermediate_leaf := classes-no-local.dex
64else
65full_classes_compiled_jar_leaf := classes-full-debug.jar
66built_dex_intermediate_leaf := classes-with-local.dex
67endif
68
69LOCAL_PROGUARD_ENABLED:=$(strip $(LOCAL_PROGUARD_ENABLED))
70ifeq ($(LOCAL_PROGUARD_ENABLED),disabled)
71LOCAL_PROGUARD_ENABLED :=
72endif
73
74# By giving different file name, files can be updated correctly when switching
75# between builds with and without Proguard enabled.
76ifdef LOCAL_PROGUARD_ENABLED
77proguard_jar_leaf := proguard.classes.jar
78built_dex_leaf := progaurd.classes.dex
79else
80proguard_jar_leaf := noproguard.classes.jar
81built_dex_leaf := noproguard.classes.dex
82endif
83
84full_classes_compiled_jar := $(intermediates.COMMON)/$(full_classes_compiled_jar_leaf)
85jarjar_leaf := classes-jarjar.jar
86full_classes_jarjar_jar := $(intermediates.COMMON)/$(jarjar_leaf)
87emma_intermediates_dir := $(intermediates.COMMON)/emma_out
88# emma is hardcoded to use the leaf name of its input for the output file --
89# only the output directory can be changed
90full_classes_emma_jar := $(emma_intermediates_dir)/lib/$(jarjar_leaf)
91full_classes_full_names_jar := $(intermediates.COMMON)/classes-full-names.jar
92full_classes_proguard_jar := $(intermediates.COMMON)/$(proguard_jar_leaf)
93built_dex_intermediate := $(intermediates.COMMON)/$(built_dex_intermediate_leaf)
94full_classes_stubs_jar := $(intermediates.COMMON)/stubs.jar
95
96# full_classes_jar and built_dex are cleared below, and re-set if we really need them.
97full_classes_jar := $(intermediates.COMMON)/classes.jar
98built_dex := $(intermediates.COMMON)/$(built_dex_leaf)
99
100LOCAL_INTERMEDIATE_TARGETS += \
101    $(full_classes_compiled_jar) \
102    $(full_classes_jarjar_jar) \
103    $(full_classes_emma_jar) \
104    $(full_classes_full_names_jar) \
105    $(full_classes_proguard_jar) \
106    $(full_classes_jar) \
107    $(built_dex_intermediate) \
108    $(built_dex) \
109    $(full_classes_stubs_jar)
110
111
112LOCAL_INTERMEDIATE_SOURCE_DIR := $(intermediates.COMMON)/src
113
114# TODO: It looks like the only thing we need from base_rules is
115# all_java_sources.  See if we can get that by adding a
116# common_java.mk, and moving the include of base_rules.mk to
117# after all the declarations.
118
119#######################################
120include $(BUILD_SYSTEM)/base_rules.mk
121#######################################
122
123# We use intermediates.COMMON because the classes.jar/.dex files will be
124# common even if LOCAL_BUILT_MODULE isn't.
125#
126# Override some target variables that base_rules set up for us.
127$(LOCAL_INTERMEDIATE_TARGETS): \
128	PRIVATE_CLASS_INTERMEDIATES_DIR := $(intermediates.COMMON)/classes
129$(LOCAL_INTERMEDIATE_TARGETS): \
130	PRIVATE_SOURCE_INTERMEDIATES_DIR := $(LOCAL_INTERMEDIATE_SOURCE_DIR)
131
132# Since we're using intermediates.COMMON, make sure that it gets cleaned
133# properly.
134$(cleantarget): PRIVATE_CLEAN_FILES += $(intermediates.COMMON)
135
136# If the module includes java code (i.e., it's not framework-res), compile it.
137full_classes_jar :=
138built_dex :=
139ifneq (,$(strip $(all_java_sources)))
140
141# If LOCAL_BUILT_MODULE_STEM wasn't overridden by our caller,
142# full_classes_jar will be the same module as LOCAL_BUILT_MODULE.
143# Otherwise, the caller will define it as a prerequisite of
144# LOCAL_BUILT_MODULE, so it will inherit the necessary PRIVATE_*
145# variable definitions.
146full_classes_jar := $(intermediates.COMMON)/classes.jar
147built_dex := $(intermediates.COMMON)/$(built_dex_leaf)
148
149# Droiddoc isn't currently able to generate stubs for modules, so we're just
150# allowing it to use the classes.jar as the "stubs" that would be use to link
151# against, for the cases where someone needs the jar to link against.
152# - Use the classes.jar instead of the handful of other intermediates that
153#   we have, because it's the most processed, but still hasn't had dex run on
154#   it, so it's closest to what's on the device.
155# - This extra copy, with the dependency on LOCAL_BUILT_MODULE allows the
156#   PRIVATE_ vars to be preserved.
157$(full_classes_stubs_jar): PRIVATE_SOURCE_FILE := $(full_classes_jar)
158$(full_classes_stubs_jar) : $(LOCAL_BUILT_MODULE) | $(ACP)
159	@echo Copying $(PRIVATE_SOURCE_FILE)
160	$(hide) $(ACP) -fp $(PRIVATE_SOURCE_FILE) $@
161ALL_MODULES.$(LOCAL_MODULE).STUBS := $(full_classes_stubs_jar)
162
163# Compile the java files to a .jar file.
164# This intentionally depends on java_sources, not all_java_sources.
165# Deps for generated source files must be handled separately,
166# via deps on the target that generates the sources.
167$(full_classes_compiled_jar): PRIVATE_JAVACFLAGS := $(LOCAL_JAVACFLAGS)
168$(full_classes_compiled_jar): $(java_sources) $(java_resource_sources) $(full_java_lib_deps) $(jar_manifest_file)
169	$(transform-java-to-classes.jar)
170
171# All of the rules after full_classes_compiled_jar are very unlikely
172# to fail except for bugs in their respective tools.  If you would
173# like to run these rules, add the "all" modifier goal to the make
174# command line.
175# This overwrites the value defined in base_rules.mk.  That's a little
176# dirty.  It's preferable to set LOCAL_CHECKED_MODULE, but this has to
177# be done after the inclusion of base_rules.mk.
178ALL_MODULES.$(LOCAL_MODULE).CHECKED := $(full_classes_compiled_jar)
179
180$(full_classes_compiled_jar): PRIVATE_JAVAC_DEBUG_FLAGS := -g
181
182# Run jarjar if necessary, otherwise just copy the file.
183ifneq ($(strip $(LOCAL_JARJAR_RULES)),)
184$(full_classes_jarjar_jar): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
185$(full_classes_jarjar_jar): $(full_classes_compiled_jar) | $(JARJAR)
186	@echo JarJar: $@
187	$(hide) java -jar $(JARJAR) process $(PRIVATE_JARJAR_RULES) $< $@
188else
189$(full_classes_jarjar_jar): $(full_classes_compiled_jar) | $(ACP)
190	@echo Copying: $@
191	$(hide) $(ACP) $< $@
192endif
193
194ifeq ($(LOCAL_IS_STATIC_JAVA_LIBRARY),true)
195# Skip adding emma instrumentation to class files if this is a static library,
196# since it will be instrumented by the package that includes it
197LOCAL_NO_EMMA_INSTRUMENT:= true
198endif
199
200ifneq ($(LOCAL_NO_EMMA_INSTRUMENT),true)
201$(full_classes_emma_jar): PRIVATE_EMMA_COVERAGE_FILE := $(intermediates.COMMON)/coverage.em
202$(full_classes_emma_jar): PRIVATE_EMMA_INTERMEDIATES_DIR := $(emma_intermediates_dir)
203# module level coverage filter can be defined using LOCAL_EMMA_COVERAGE_FILTER
204# in Android.mk
205ifdef LOCAL_EMMA_COVERAGE_FILTER
206$(full_classes_emma_jar): PRIVATE_EMMA_COVERAGE_FILTER := $(LOCAL_EMMA_COVERAGE_FILTER)
207else
208# by default, avoid applying emma instrumentation onto emma classes itself,
209# otherwise there will be exceptions thrown
210$(full_classes_emma_jar): PRIVATE_EMMA_COVERAGE_FILTER := *,-emma,-emmarun,-com.vladium.*
211endif
212# this rule will generate both $(PRIVATE_EMMA_COVERAGE_FILE) and
213# $(full_classes_emma_jar)
214$(full_classes_emma_jar): $(full_classes_jarjar_jar) | $(EMMA_JAR)
215	$(transform-classes.jar-to-emma)
216$(PRIVATE_EMMA_COVERAGE_FILE): $(full_classes_emma_jar)
217
218# tell proguard to load emma jar
219LOCAL_PROGUARD_FLAGS := $(LOCAL_PROGUARD_FLAGS) $(addprefix -libraryjars ,$(EMMA_JAR))
220else
221$(full_classes_emma_jar): $(full_classes_jarjar_jar) | $(ACP)
222	@echo Copying: $@
223	$(copy-file-to-target)
224endif
225
226# Keep a copy of the jar just before proguard processing.
227$(full_classes_full_names_jar): $(full_classes_emma_jar) | $(ACP)
228	@echo Copying: $@
229	$(hide) $(ACP) $< $@
230
231# Run proguard if necessary, otherwise just copy the file.  This is the last
232# part of this step, so the output of this command is full_classes_jar.
233proguard_dictionary := $(intermediates.COMMON)/proguard_dictionary
234# Proguard doesn't like a class in both library and the jar to be processed.
235proguard_full_java_libs := $(filter-out $(full_static_java_libs),$(full_java_libs))
236proguard_flags := $(addprefix -libraryjars ,$(proguard_full_java_libs)) \
237                  -include $(BUILD_SYSTEM)/proguard.flags \
238                  -forceprocessing \
239                  -printmapping $(proguard_dictionary)
240# If this is a test package, add proguard keep flags for tests.
241ifneq ($(strip $(LOCAL_INSTRUMENTATION_FOR)$(filter tests,$(LOCAL_MODULE_TAGS))$(filter android.test.runner,$(LOCAL_JAVA_LIBRARIES))),)
242proguard_flags := $(proguard_flags) -include $(BUILD_SYSTEM)/proguard_tests.flags
243endif # test package
244
245ifneq ($(LOCAL_PROGUARD_ENABLED),)
246ifeq ($(LOCAL_PROGUARD_ENABLED),full)
247    # full
248else
249ifeq ($(LOCAL_PROGUARD_ENABLED),optonly)
250    # optonly
251    proguard_flags += -dontobfuscate
252else
253ifeq ($(LOCAL_PROGUARD_ENABLED),custom)
254    # custom
255else
256    $(warning while processing: $(LOCAL_MODULE))
257    $(error invalid value for LOCAL_PROGUARD_ENABLED: $(LOCAL_PROGUARD_ENABLED))
258endif # custom
259endif # optonly
260endif # full
261endif # LOCAL_PROGUARD_ENABLED
262
263proguard_flag_files := $(addprefix $(LOCAL_PATH)/, $(LOCAL_PROGUARD_FLAG_FILES))
264LOCAL_PROGUARD_FLAGS += $(addprefix -include , $(proguard_flag_files))
265
266$(full_classes_proguard_jar): PRIVATE_PROGUARD_ENABLED:=$(LOCAL_PROGUARD_ENABLED)
267$(full_classes_proguard_jar): PRIVATE_PROGUARD_FLAGS := $(proguard_flags) $(LOCAL_PROGUARD_FLAGS)
268$(full_classes_proguard_jar): PRIVATE_INSTRUMENTATION_FOR:=$(strip $(LOCAL_INSTRUMENTATION_FOR))
269$(full_classes_proguard_jar) : $(full_classes_full_names_jar) $(proguard_flag_files) | $(ACP) $(PROGUARD)
270	$(call transform-jar-to-proguard)
271
272ALL_MODULES.$(LOCAL_MODULE).PROGUARD_ENABLED:=$(LOCAL_PROGUARD_ENABLED)
273
274$(full_classes_jar) : $(full_classes_proguard_jar)
275	@echo Copying: $@
276	$(hide) $(ACP) $< $@
277
278# If you instrument class files that have local variable debug information in
279# them emma does not correctly maintain the local variable table.
280# This will cause an error when you try to convert the class files for Android.
281# The workaround here is to build different dex file here based on emma switch
282# then later copy into classes.dex. When emma is on, dx is run with --no-locals
283# option to remove local variable information
284
285# Override PRIVATE_INTERMEDIATES_DIR so that install-dex-debug
286# will work even when intermediates != intermediates.COMMON.
287$(built_dex_intermediate): PRIVATE_INTERMEDIATES_DIR := $(intermediates.COMMON)
288$(built_dex_intermediate): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS)
289ifneq ($(LOCAL_NO_EMMA_COMPILE),true)
290$(built_dex_intermediate): PRIVATE_DX_FLAGS += --no-locals
291endif
292$(built_dex_intermediate): $(full_classes_jar) $(DX)
293	$(transform-classes.jar-to-dex)
294$(built_dex): $(built_dex_intermediate) | $(ACP)
295	@echo Copying: $@
296	$(hide) $(ACP) $< $@
297ifneq ($(GENERATE_DEX_DEBUG),)
298	$(install-dex-debug)
299endif
300
301findbugs_xml := $(intermediates.COMMON)/findbugs.xml
302$(findbugs_xml) : PRIVATE_JAR_FILE := $(full_classes_jar)
303$(findbugs_xml) : PRIVATE_AUXCLASSPATH := $(addprefix -auxclasspath ,$(strip \
304								$(call normalize-path-list,$(filter %.jar,\
305										$(full_java_libs)))))
306# We can't depend directly on full_classes_jar because the PRIVATE_
307# vars won't be set up correctly.
308$(findbugs_xml) : $(LOCAL_BUILT_MODULE)
309	@echo Findbugs: $@
310	$(hide) $(FINDBUGS) -textui -effort:min -xml:withMessages \
311		$(PRIVATE_AUXCLASSPATH) \
312		$(PRIVATE_JAR_FILE) \
313		> $@
314
315ALL_FINDBUGS_FILES += $(findbugs_xml)
316
317findbugs_html := $(PRODUCT_OUT)/findbugs/$(LOCAL_MODULE).html
318$(findbugs_html) : PRIVATE_XML_FILE := $(findbugs_xml)
319$(LOCAL_MODULE)-findbugs : $(findbugs_html)
320$(findbugs_html) : $(findbugs_xml)
321	@mkdir -p $(dir $@)
322	@echo ConvertXmlToText: $@
323	$(hide) prebuilt/common/findbugs/bin/convertXmlToText -html:fancy.xsl $(PRIVATE_XML_FILE) \
324	> $@
325
326$(LOCAL_MODULE)-findbugs : $(findbugs_html)
327
328endif
329