Signature test needs api resources to be generated

Change-Id: I837d1323df9f0e859ad3a983f66e082eb4d1a4f7
diff --git a/tests/SignatureTest/Android.mk b/tests/SignatureTest/Android.mk
index 3656afb..209e78a 100644
--- a/tests/SignatureTest/Android.mk
+++ b/tests/SignatureTest/Android.mk
@@ -18,9 +18,6 @@
 # don't include this package in any target
 LOCAL_MODULE_TAGS := optional
 
-# and because it is in data, do not strip classes.dex
-LOCAL_DEX_PREOPT := false
-
 LOCAL_SRC_FILES := $(call all-java-files-under, src)
 
 LOCAL_JAVA_LIBRARIES := android.test.runner
@@ -29,7 +26,65 @@
 
 LOCAL_SDK_VERSION := current
 
+# To be passed in on command line
+CTS_API_VERSION ?= current
+ifeq (current,$(CTS_API_VERSION))
+android_api_description := frameworks/base/api/$(CTS_API_VERSION).txt
+else
+android_api_description := $(SRC_API_DIR)/$(CTS_API_VERSION).txt
+endif
+
+# Can't call local-intermediates-dir directly here because we have to
+# include BUILD_CTS_PACKAGE first.  Can't include BUILD_CTS_PACKAGE first
+# because we have to override LOCAL_RESOURCE_DIR first.  Hence this
+# hack.
+intermediates.COMMON := $(call intermediates-dir-for,APPS,$(LOCAL_PACKAGE_NAME),,COMMON)
+signature_res_dir := $(intermediates.COMMON)/genres
+LOCAL_RESOURCE_DIR := $(signature_res_dir) $(LOCAL_PATH)/res
+
 include $(BUILD_CTS_PACKAGE)
 
+$(info $(call local-intermediates-dir))
+
+generated_res_stamp := $(intermediates.COMMON)/genres.stamp
+api_ver_file := $(intermediates.COMMON)/api_ver_is_$(CTS_API_VERSION)
+
+# The api_ver_file keeps track of which api version was last built.
+# By only ever having one of these magic files in existence and making
+# sure the generated resources rule depend on it, we can ensure that
+# the proper version of the api resource gets generated.
+$(api_ver_file):
+	$(hide) rm -f $(dir $@)/api_ver_is_* \
+		&& mkdir -p $(dir $@) && touch $@
+
+android_api_xml_description := $(intermediates.COMMON)/api.xml
+$(android_api_xml_description): $(android_api_description) | $(APICHECK)
+	@ echo "Convert api file to xml: $@"
+	@ mkdir -p $(dir $@)
+	$(hide) $(APICHECK_COMMAND) -convert2xml $< $@
+
+# Split up config/api/1.xml by "package" and save the files as the
+# resource files of SignatureTest.
+$(generated_res_stamp): PRIVATE_PATH := $(LOCAL_PATH)
+$(generated_res_stamp): PRIVATE_MODULE := $(LOCAL_MODULE)
+$(generated_res_stamp): PRIVATE_RES_DIR := $(signature_res_dir)
+$(generated_res_stamp): PRIVATE_API_XML_DESC := $(android_api_xml_description)
+$(generated_res_stamp): $(api_ver_file)
+$(generated_res_stamp): $(android_api_xml_description)
+	@ echo "Copy generated resources: $(PRIVATE_MODULE)"
+	$(hide) python cts/tools/utils/android_api_description_splitter.py \
+		$(PRIVATE_API_XML_DESC) $(PRIVATE_RES_DIR) package
+	$(hide) touch $@
+
+$(R_file_stamp): $(generated_res_stamp)
+
+# clean up temp vars
+android_api_xml_description :=
+api_ver_file :=
+generated_res_stamp :=
+signature_res_dir :=
+android_api_description :=
+CTS_API_VERSION :=
+
 # Use the following include to make our test apk.
 include $(call all-makefiles-under,$(LOCAL_PATH))