From 20e2083ea0f8bae57ace17f098e7fe71a9625f84 Mon Sep 17 00:00:00 2001 From: Ying Wang Date: Wed, 14 Dec 2011 14:29:28 -0800 Subject: [PATCH] Support to build static Java library with Android resource Bug: 5714516 The rationale behind this change: - the library is compiled into a jar file, but its R class is generated making the constant not constant (static, not final static) (aapt option --non-constant-id). Also the jar file does not contain the R class. - this allows the integer value to not be inlined in the compiled class files. Note that this prevents using switch statements. - the main project use this jar file as a normal static library: it will add all the class files except the R.class. - the main project uses the library res folder as a resource folder with lower priority than the main project (basically the main project is an overlay. This is accomplished using aapt's --auto-add-overlay to handle resources only in the main project (which the normal overlay mechanism doesn't allow). - the main project creates R classes in the main project's package but also in the library's package. This is done with aapt's --extra-packages which accept as many packages as needed, separated by a :. - manifest merging is not done yet, so activities/services/permissions/etc... have to be manually declared in the main app. To use a static library with Android resource in your app, 1. Add the library's resource dir to your app as an overlay: LOCAL_RESOURCE_DIR := 2. Set the proper aapt flags: LOCAL_AAPT_FLAGS := --auto-add-overlay \ --extra-packages ::... Change-Id: Ifb4d2300b952ea4aaee74da1bb0c6c72ea0698a3 --- core/definitions.mk | 4 +++ core/java_library.mk | 2 ++ core/static_java_library.mk | 70 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/core/definitions.mk b/core/definitions.mk index e950d38a6..f16b4c9c7 100644 --- a/core/definitions.mk +++ b/core/definitions.mk @@ -1408,6 +1408,9 @@ endef # For a list of jar files, unzip them to a specified directory, # but make sure that no META-INF files come along for the ride. +# We also remove any R.class/Manifest.class from the jar files; +# R/Manifest class for the static Java libraries should be +# re-generate in the app module instead. # # $(1): files to unzip # $(2): destination directory @@ -1421,6 +1424,7 @@ define unzip-jar-files unzip -qo $$f -d $(2); \ (cd $(2) && rm -rf META-INF); \ done + $(hide) find $(2) -name 'R.class' -o -name 'R$$*.class' -o -name 'Manifest.class' -o -name 'Manifest$$*.class' | xargs rm -rf endef # Common definition to invoke javac on the host and target. diff --git a/core/java_library.mk b/core/java_library.mk index fa40c0e23..904ef5fcb 100644 --- a/core/java_library.mk +++ b/core/java_library.mk @@ -14,9 +14,11 @@ ifneq (,$(LOCAL_ASSET_DIR)) $(error $(LOCAL_PATH): Target java libraries may not set LOCAL_ASSET_DIR) endif +ifneq (true,$(LOCAL_IS_STATIC_JAVA_LIBRARY)) ifneq (,$(LOCAL_RESOURCE_DIR)) $(error $(LOCAL_PATH): Target java libraries may not set LOCAL_RESOURCE_DIR) endif +endif #xxx base_rules.mk looks at this all_res_assets := diff --git a/core/static_java_library.mk b/core/static_java_library.mk index 93d770add..3a1925dd8 100644 --- a/core/static_java_library.mk +++ b/core/static_java_library.mk @@ -21,5 +21,75 @@ LOCAL_UNINSTALLABLE_MODULE := true LOCAL_IS_STATIC_JAVA_LIBRARY := true + +# Hack to build static Java library with Android resource +# See bug 5714516 +all_resources := +ifdef LOCAL_RESOURCE_DIR +all_resources := $(strip \ + $(foreach dir, $(LOCAL_RESOURCE_DIR), \ + $(addprefix $(dir)/, \ + $(patsubst res/%,%, \ + $(call find-subdir-assets,$(dir)) \ + ) \ + ) \ + )) +endif + include $(BUILD_SYSTEM)/java_library.mk + +ifneq (,$(all_resources)) +R_file_stamp := $(LOCAL_INTERMEDIATE_SOURCE_DIR)/R.stamp + +ifeq ($(strip $(LOCAL_MANIFEST_FILE)),) +LOCAL_MANIFEST_FILE := AndroidManifest.xml +endif +full_android_manifest := $(LOCAL_PATH)/$(LOCAL_MANIFEST_FILE) + +framework_res_package_export := +framework_res_package_export_deps := +# Please refer to package.mk +ifneq ($(LOCAL_NO_STANDARD_LIBRARIES),true) +ifneq ($(filter-out current,$(LOCAL_SDK_RES_VERSION)),) +framework_res_package_export := \ + $(HISTORICAL_SDK_VERSIONS_ROOT)/$(LOCAL_SDK_RES_VERSION)/android.jar +framework_res_package_export_deps := $(framework_res_package_export) +else +framework_res_package_export := \ + $(call intermediates-dir-for,APPS,framework-res,,COMMON)/package-export.apk +framework_res_package_export_deps := \ + $(dir $(framework_res_package_export))src/R.stamp +endif +endif + +$(R_file_stamp): PRIVATE_MODULE := $(LOCAL_MODULE) +# add --non-constant-id to prevent inlining constants. +$(R_file_stamp): PRIVATE_AAPT_FLAGS := $(LOCAL_AAPT_FLAGS) --non-constant-id +$(R_file_stamp): PRIVATE_SOURCE_INTERMEDIATES_DIR := $(LOCAL_INTERMEDIATE_SOURCE_DIR) +$(R_file_stamp): PRIVATE_ANDROID_MANIFEST := $(full_android_manifest) +$(R_file_stamp): PRIVATE_RESOURCE_PUBLICS_OUTPUT := $(intermediates.COMMON)/public_resources.xml +$(R_file_stamp): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR) +$(R_file_stamp): PRIVATE_AAPT_INCLUDES := $(framework_res_package_export) +ifneq (,$(filter-out current, $(LOCAL_SDK_VERSION))) +$(R_file_stamp): PRIVATE_DEFAULT_APP_TARGET_SDK := $(LOCAL_SDK_VERSION) +else +$(R_file_stamp): PRIVATE_DEFAULT_APP_TARGET_SDK := $(DEFAULT_APP_TARGET_SDK) +endif +$(R_file_stamp): PRIVATE_ASSET_DIR := +$(R_file_stamp): PRIVATE_PROGUARD_OPTIONS_FILE := +$(R_file_stamp): PRIVATE_MANIFEST_PACKAGE_NAME := +$(R_file_stamp): PRIVATE_MANIFEST_INSTRUMENTATION_FOR := + +$(R_file_stamp) : $(all_resources) $(full_android_manifest) $(AAPT) $(framework_res_package_export_deps) + @echo "target R.java/Manifest.java: $(PRIVATE_MODULE) ($@)" + $(create-resource-java-files) + $(hide) find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name R.java | xargs cat > $@ + +$(LOCAL_BUILT_MODULE): $(R_file_stamp) +ifneq ($(full_classes_jar),) +$(full_classes_compiled_jar): $(R_file_stamp) +endif + +endif # $(all_resources) not empty + LOCAL_IS_STATIC_JAVA_LIBRARY := -- 2.11.0