Skip to content

Commit

Permalink
Running jarjar on Java resources.
Browse files Browse the repository at this point in the history
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 140274707e31c9585aa28b0de2f1418c64ecd272)

Bug: 18837479
Change-Id: I15ecf282bfb65fd53dd03fbd03dd4c71927c186a
  • Loading branch information
Ying Wang authored and Chairshot215 committed Jan 23, 2015
1 parent c5695e3 commit 1df3707
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
34 changes: 16 additions & 18 deletions core/definitions.mk
Expand Up @@ -1634,6 +1634,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
Expand Down Expand Up @@ -1694,6 +1695,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

Expand Down Expand Up @@ -1741,6 +1743,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.
Expand Down Expand Up @@ -1792,24 +1803,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.
Expand Down Expand Up @@ -1853,7 +1852,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

###########################################################
Expand Down
7 changes: 2 additions & 5 deletions core/host_dalvik_java_library.mk
Expand Up @@ -95,13 +95,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 :=
10 changes: 2 additions & 8 deletions core/java_library.mk
Expand Up @@ -58,24 +58,18 @@ $(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)

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
Expand Down
13 changes: 8 additions & 5 deletions core/package_internal.mk
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1df3707

Please sign in to comment.