Running jarjar on Java resources.

Before this change, Java resources are added as a separate step
(add-java-resources-to-package) after dex is run, so jarjar isn't run on
the resource files.
With this change, we add Java resources immediately after we call javac,
so jarjar is run on the resource files (the module's own resource, as
well as resources carried by static Java libraries).
When we generate the final apk/jar, we use the jarjar'ed jar as the
inital pacakge file, with class files and empty folders removed.

(cherry-picked from commit 140274707e)

Bug: 18837479
Change-Id: I15ecf282bfb65fd53dd03fbd03dd4c71927c186a
This commit is contained in:
Ying Wang 2015-01-14 14:23:56 -08:00
parent 17967eb72f
commit 07acdbfd32
4 changed files with 28 additions and 36 deletions

View File

@ -1663,6 +1663,7 @@ $(if $(PRIVATE_JAR_MANIFEST), \
jar -cfm $@ $(dir $@)/manifest.mf \
-C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ., \
$(hide) jar -cf $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .)
$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
endef
define transform-java-to-classes.jar
@ -1723,6 +1724,7 @@ $(if $(PRIVATE_JAR_MANIFEST), \
jar -cfm $@ $(dir $@)/manifest.mf \
-C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ., \
$(hide) jar -cf $@ -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) .)
$(if $(PRIVATE_EXTRA_JAR_ARGS),$(call add-java-resources-to,$@))
$(hide) mv $(PRIVATE_CLASS_INTERMEDIATES_DIR)/newstamp $(PRIVATE_CLASS_INTERMEDIATES_DIR)/stamp
endef
@ -1770,6 +1772,15 @@ $(hide) zip -qd $@ dummy
$(hide) rm $(dir $@)dummy
endef
# Copy an arhchive file and delete any class files and empty folders inside.
# $(1): the source archive file.
# $(2): the destination archive file.
define initialize-package-file
@mkdir -p $(dir $(2))
$(hide) cp -f $(1) $(2)
$(hide) zip -qd $(2) "*.class" "*/" || true # Ignore the error when nothing to delete.
endef
#TODO: we kinda want to build different asset packages for
# different configurations, then combine them later (or something).
# Per-locale, etc.
@ -1821,24 +1832,12 @@ $(hide) zip -qj $@ $(dir $(PRIVATE_DEX_FILE))classes*.dex
endef
# Add java resources added by the current module.
# $(1) destination package
#
define add-java-resources-to-package
$(call dump-words-to-file, $(PRIVATE_EXTRA_JAR_ARGS), $(dir $@)jar-arg-list)
$(hide) jar uf $@ @$(dir $@)jar-arg-list
@rm -f $(dir $@)jar-arg-list
endef
# Add java resources carried by static Java libraries.
#
define add-carried-java-resources
$(hide) if [ -d $(PRIVATE_CLASS_INTERMEDIATES_DIR) ] ; then \
java_res_jar_flags=$$(find $(PRIVATE_CLASS_INTERMEDIATES_DIR) -type f -a -not -name "*.class" \
| sed -e "s?^$(PRIVATE_CLASS_INTERMEDIATES_DIR)/? -C $(PRIVATE_CLASS_INTERMEDIATES_DIR) ?"); \
if [ -n "$$java_res_jar_flags" ] ; then \
echo $$java_res_jar_flags >$(dir $@)java_res_jar_flags; \
jar uf $@ $$java_res_jar_flags; \
fi; \
fi
define add-java-resources-to
$(call dump-words-to-file, $(PRIVATE_EXTRA_JAR_ARGS), $(1).jar-arg-list)
$(hide) jar uf $(1) @$(1).jar-arg-list
@rm -f $(1).jar-arg-list
endef
# Sign a package using the specified key/cert.
@ -1882,7 +1881,6 @@ endef
define transform-host-java-to-package
@echo "host Java: $(PRIVATE_MODULE) ($(PRIVATE_CLASS_INTERMEDIATES_DIR))"
$(call compile-java,$(HOST_JAVAC),$(PRIVATE_BOOTCLASSPATH))
$(if $(PRIVATE_EXTRA_JAR_ARGS), $(call add-java-resources-to-package))
endef
###########################################################

View File

@ -101,13 +101,10 @@ $(built_dex): $(full_classes_jar) $(DX)
$(transform-classes.jar-to-dex)
$(LOCAL_BUILT_MODULE): PRIVATE_DEX_FILE := $(built_dex)
$(LOCAL_BUILT_MODULE): PRIVATE_SOURCE_ARCHIVE := $(full_classes_jarjar_jar)
$(LOCAL_BUILT_MODULE): $(built_dex) $(java_resource_sources)
@echo "Host Jar: $(PRIVATE_MODULE) ($@)"
$(create-empty-package)
$(call initialize-package-file,$(PRIVATE_SOURCE_ARCHIVE),$@)
$(add-dex-to-package)
$(add-carried-java-resources)
ifneq ($(extra_jar_args),)
$(add-java-resources-to-package)
endif
USE_CORE_LIB_BOOTCLASSPATH :=

View File

@ -58,9 +58,6 @@ $(common_javalib.jar) : $(full_classes_jar)
endif
@echo "target Static Jar: $(PRIVATE_MODULE) ($@)"
$(copy-file-to-target)
ifneq ($(extra_jar_args),)
$(add-java-resources-to-package)
endif
$(LOCAL_BUILT_MODULE): $(common_javalib.jar)
$(copy-file-to-target)
@ -68,14 +65,11 @@ $(LOCAL_BUILT_MODULE): $(common_javalib.jar)
else # !LOCAL_IS_STATIC_JAVA_LIBRARY
$(common_javalib.jar): PRIVATE_DEX_FILE := $(built_dex)
$(common_javalib.jar): PRIVATE_SOURCE_ARCHIVE := $(full_classes_jarjar_jar)
$(common_javalib.jar) : $(built_dex) $(java_resource_sources)
@echo "target Jar: $(PRIVATE_MODULE) ($@)"
$(create-empty-package)
$(call initialize-package-file,$(PRIVATE_SOURCE_ARCHIVE),$@)
$(add-dex-to-package)
$(add-carried-java-resources)
ifneq ($(extra_jar_args),)
$(add-java-resources-to-package)
endif
ifdef LOCAL_DEX_PREOPT
ifneq ($(dexpreopt_boot_jar_module),) # boot jar

View File

@ -324,7 +324,12 @@ endif # LOCAL_NO_STANDARD_LIBRARIES
ifneq ($(full_classes_jar),)
$(LOCAL_BUILT_MODULE): PRIVATE_DEX_FILE := $(built_dex)
# Use the jarjar processed arhive as the initial package file.
$(LOCAL_BUILT_MODULE): PRIVATE_SOURCE_ARCHIVE := $(full_classes_jarjar_jar)
$(LOCAL_BUILT_MODULE): $(built_dex)
else
$(LOCAL_BUILT_MODULE): PRIVATE_DEX_FILE :=
$(LOCAL_BUILT_MODULE): PRIVATE_SOURCE_ARCHIVE :=
endif # full_classes_jar
include $(BUILD_SYSTEM)/install_jni_libs.mk
@ -385,17 +390,15 @@ endif
endif
$(LOCAL_BUILT_MODULE): $(all_res_assets) $(jni_shared_libraries) $(full_android_manifest)
@echo "target Package: $(PRIVATE_MODULE) ($@)"
$(create-empty-package)
$(if $(PRIVATE_SOURCE_ARCHIVE),\
$(call initialize-package-file,$(PRIVATE_SOURCE_ARCHIVE),$@),\
$(create-empty-package))
$(add-assets-to-package)
ifneq ($(jni_shared_libraries),)
$(add-jni-shared-libs-to-package)
endif
ifneq ($(full_classes_jar),)
$(add-dex-to-package)
endif
$(add-carried-java-resources)
ifneq ($(extra_jar_args),)
$(add-java-resources-to-package)
endif
$(sign-package)
ifdef LOCAL_DEX_PREOPT