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.
When jack is enabled, in jack-java-to-dex we add the Java resources to
a temp jar using the PRIVATE_EXTRA_JAR_ARGS, and extrac the files in a
temp dir. Jack will process the resource files and output the result to
PRIVATE_JACK_INTERMEDIATES_DIR. When we package the final apk/jar, we
need to call add-carried-jack-resources to readd the resources.
(TODO: if jack can output all resources to a jar/zip file, we can use
that file as the initial package file as well.)
Bug: 18837479
Change-Id: I8d7296e30ec8d005054cf04c4f2aed6d7a0d823b
diff --git a/core/base_rules.mk b/core/base_rules.mk
index 8e423e4..883f330 100644
--- a/core/base_rules.mk
+++ b/core/base_rules.mk
@@ -409,7 +409,6 @@
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_RESOURCE_DIR := $(LOCAL_RESOURCE_DIR)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASSET_DIR := $(LOCAL_ASSET_DIR)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_STATIC_JAVA_LIBRARIES := $(full_static_java_libs)
-$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_STATIC_JACK_LIBRARIES := $(full_static_jack_libs)
# full_java_libs: The list of files that should be used as the classpath.
# Using this list as a dependency list WILL NOT WORK.
@@ -682,7 +681,7 @@
###########################################################
# JACK
###########################################################
-ifeq ($(strip $(LOCAL_USE_JACK)),true)
+ifeq ($(LOCAL_USE_JACK),true)
ifdef need_compile_java
full_static_jack_libs := \
diff --git a/core/definitions.mk b/core/definitions.mk
index e38e36d..587aab2 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -1702,6 +1702,7 @@
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
@@ -1733,6 +1734,12 @@
$(hide) echo -basedirectory $(CURDIR) > $@.flags; \
echo $(PRIVATE_JACK_PROGUARD_FLAGS) >> $@.flags; \
)
+$(if $(PRIVATE_EXTRA_JAR_ARGS),
+ $(hide) mkdir -p $@.res.tmp
+ $(hide) $(call create-empty-package-at,$@.res.tmp.zip)
+ $(hide) $(call add-java-resources-to,$@.res.tmp.zip)
+ $(hide) $(call unzip-jar-files,$@.res.tmp.zip,$@.res.tmp)
+ $(hide) rm $@.res.tmp.zip)
$(hide) if [ -s $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq ] ; then \
export tmpEcjArg="@$(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq"; \
else \
@@ -1746,6 +1753,7 @@
$(addprefix --classpath ,$(strip \
$(call normalize-path-list,$(PRIVATE_BOOTCLASSPATH_JAVA_LIBRARIES) $(PRIVATE_ALL_JACK_LIBRARIES)))) \
$(addprefix --import ,$(call reverse-list,$(PRIVATE_STATIC_JACK_LIBRARIES))) \
+ $(if $(PRIVATE_EXTRA_JAR_ARGS),--import-resource $@.res.tmp) \
-D jack.import.resource.policy=keep-first \
-D jack.import.type.policy=keep-first \
--output-jack $(PRIVATE_CLASSES_JACK) \
@@ -1757,6 +1765,7 @@
|| ( rm -rf $(PRIVATE_CLASSES_JACK); rm -rf $(PRIVATE_JACK_INTERMEDIATES_DIR); exit 41 )
$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/classes*.dex $(dir $@)
$(hide) rm -f $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list
+$(if $(PRIVATE_EXTRA_JAR_ARGS),$(hide) rm -rf $@.res.tmp)
$(hide) mv $(PRIVATE_JACK_INTERMEDIATES_DIR)/java-source-list-uniq $(PRIVATE_JACK_INTERMEDIATES_DIR).java-source-list
$(if $(PRIVATE_JAR_PACKAGES), $(hide) echo unsupported options PRIVATE_JAR_PACKAGES in $@; exit 53)
$(if $(PRIVATE_JAR_EXCLUDE_PACKAGES), $(hide) echo unsupported options JAR_EXCLUDE_PACKAGES in $@; exit 53)
@@ -1890,6 +1899,7 @@
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
@@ -1945,6 +1955,15 @@
$(call create-empty-package-at,$@)
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.
@@ -2004,25 +2023,6 @@
@rm -f $(1).jar-arg-list
endef
-# Add java resources added by the current module.
-#
-define add-java-resources-to-package
-$(call add-java-resources-to,$@)
-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
-endef
-
# Add resources carried by static Jack libraries.
#
define add-carried-jack-resources
@@ -2036,7 +2036,6 @@
fi
endef
-
# Sign a package using the specified key/cert.
#
define sign-package
@@ -2078,7 +2077,6 @@
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
###########################################################
diff --git a/core/dpi_specific_apk.mk b/core/dpi_specific_apk.mk
index aad4713..11c7c84 100644
--- a/core/dpi_specific_apk.mk
+++ b/core/dpi_specific_apk.mk
@@ -25,24 +25,31 @@
$(built_dpi_apk): PRIVATE_MANIFEST_INSTRUMENTATION_FOR := $(LOCAL_INSTRUMENTATION_FOR)
$(built_dpi_apk): PRIVATE_JNI_SHARED_LIBRARIES := $(jni_shared_libraries_with_abis)
$(built_dpi_apk): PRIVATE_JNI_SHARED_LIBRARIES_ABI := $(jni_shared_libraries_abis)
-$(built_dpi_apk): PRIVATE_DEX_FILE := $(built_dex)
-# Note that PRIVATE_CLASS_INTERMEDIATES_DIR points to the base apk's intermediate dir.
-$(built_dpi_apk): PRIVATE_CLASS_INTERMEDIATES_DIR := $(intermediates)/classes
-$(built_dpi_apk): PRIVATE_EXTRA_JAR_ARGS := $(extra_jar_args)
$(built_dpi_apk): PRIVATE_PRIVATE_KEY := $(private_key)
$(built_dpi_apk): PRIVATE_CERTIFICATE := $(certificate)
$(built_dpi_apk): PRIVATE_ADDITIONAL_CERTIFICATES := $(foreach c,\
$(LOCAL_ADDITIONAL_CERTIFICATES), $(c).x509.pem $(c).pk8)
+ifneq ($(full_classes_jar),)
+$(built_dpi_apk): PRIVATE_DEX_FILE := $(built_dex)
+# Use the jarjar processed arhive as the initial package file.
+$(built_dpi_apk): PRIVATE_SOURCE_ARCHIVE := $(full_classes_jarjar_jar)
+$(built_dpi_apk): $(built_dex)
+else
+$(built_dpi_apk): PRIVATE_DEX_FILE :=
+$(built_dpi_apk): PRIVATE_SOURCE_ARCHIVE :=
+endif # full_classes_jar
+
# Set up dependenncies and the build recipe.
$(built_dpi_apk) : $(R_file_stamp)
$(built_dpi_apk) : $(all_library_res_package_export_deps)
-$(built_dpi_apk) : $(built_dex)
$(built_dpi_apk) : $(private_key) $(certificate) $(SIGNAPK_JAR)
$(built_dpi_apk) : $(AAPT) | $(ZIPALIGN)
$(built_dpi_apk) : $(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)
@@ -50,10 +57,6 @@
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)
$(align-package)
diff --git a/core/host_dalvik_java_library.mk b/core/host_dalvik_java_library.mk
index 772245e..e8823be 100644
--- a/core/host_dalvik_java_library.mk
+++ b/core/host_dalvik_java_library.mk
@@ -97,21 +97,18 @@
@echo Copying: $@
$(hide) $(ACP) -fp $< $@
-ifneq ($(strip $(LOCAL_USE_JACK)),true)
+ifneq ($(LOCAL_USE_JACK),true)
$(built_dex): PRIVATE_INTERMEDIATES_DIR := $(intermediates.COMMON)
$(built_dex): PRIVATE_DX_FLAGS := $(LOCAL_DX_FLAGS)
$(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
else # LOCAL_USE_JACK
$(LOCAL_INTERMEDIATE_TARGETS): \
@@ -134,9 +131,6 @@
$(create-empty-package)
$(add-dex-to-package)
$(add-carried-jack-resources)
-ifneq ($(extra_jar_args),)
- $(add-java-resources-to-package)
-endif
endif # LOCAL_USE_JACK
diff --git a/core/java.mk b/core/java.mk
index 5e9a285..b80e8b8 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -471,7 +471,7 @@
-verbose \
$(legacy_proguard_flags)
# not supported with jack
-ifeq ($(strip $(LOCAL_USE_JACK)),true)
+ifeq ($(LOCAL_USE_JACK),true)
$(error $(LOCAL_MODULE): Build with jack of instrumentation when obfuscating is not yet supported)
endif
@@ -562,7 +562,7 @@
endif # full_classes_jar is defined
-ifeq ($(strip $(LOCAL_USE_JACK)),true)
+ifeq ($(LOCAL_USE_JACK),true)
$(LOCAL_INTERMEDIATE_TARGETS): \
PRIVATE_JACK_INTERMEDIATES_DIR := $(intermediates.COMMON)/jack
diff --git a/core/java_library.mk b/core/java_library.mk
index 0930234..586172b 100644
--- a/core/java_library.mk
+++ b/core/java_library.mk
@@ -58,9 +58,6 @@
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,17 +65,17 @@
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) ($@)"
+ifeq ($(LOCAL_USE_JACK),true)
$(create-empty-package)
- $(add-dex-to-package)
-ifneq ($(strip $(LOCAL_USE_JACK)),true)
- $(add-carried-java-resources)
else
- $(add-carried-jack-resources)
+ $(call initialize-package-file,$(PRIVATE_SOURCE_ARCHIVE),$@)
endif
-ifneq ($(extra_jar_args),)
- $(add-java-resources-to-package)
+ $(add-dex-to-package)
+ifeq ($(LOCAL_USE_JACK),true)
+ $(add-carried-jack-resources)
endif
ifdef LOCAL_DEX_PREOPT
diff --git a/core/package_internal.mk b/core/package_internal.mk
index 898a8df..059f121 100644
--- a/core/package_internal.mk
+++ b/core/package_internal.mk
@@ -162,7 +162,7 @@
endif # !custom
LOCAL_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_PROGUARD_FLAGS)
-ifeq ($(strip $(LOCAL_USE_JACK)),true)
+ifeq ($(LOCAL_USE_JACK),true)
ifndef LOCAL_JACK_PROGUARD_FLAGS
LOCAL_JACK_PROGUARD_FLAGS := $(LOCAL_PROGUARD_FLAGS)
endif
@@ -278,7 +278,7 @@
# they want to use this module's R.java file.
$(LOCAL_BUILT_MODULE): $(R_file_stamp)
-ifeq ($(strip $(LOCAL_USE_JACK)),true)
+ifeq ($(LOCAL_USE_JACK),true)
ifneq ($(built_dex_intermediate),)
$(built_dex_intermediate): $(R_file_stamp)
endif
@@ -339,7 +339,12 @@
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
@@ -400,7 +405,13 @@
endif
$(LOCAL_BUILT_MODULE): $(all_res_assets) $(jni_shared_libraries) $(full_android_manifest)
@echo "target Package: $(PRIVATE_MODULE) ($@)"
+ifeq ($(LOCAL_USE_JACK),true)
$(create-empty-package)
+else
+ $(if $(PRIVATE_SOURCE_ARCHIVE),\
+ $(call initialize-package-file,$(PRIVATE_SOURCE_ARCHIVE),$@),\
+ $(create-empty-package))
+endif
$(add-assets-to-package)
ifneq ($(jni_shared_libraries),)
$(add-jni-shared-libs-to-package)
@@ -408,14 +419,9 @@
ifneq ($(full_classes_jar),)
$(add-dex-to-package)
endif
-ifneq ($(strip $(LOCAL_USE_JACK)),true)
- $(add-carried-java-resources)
-else
+ifeq ($(LOCAL_USE_JACK),true)
$(add-carried-jack-resources)
endif
-ifneq ($(extra_jar_args),)
- $(add-java-resources-to-package)
-endif
$(sign-package)
ifdef LOCAL_DEX_PREOPT
ifneq (nostripping,$(LOCAL_DEX_PREOPT))
diff --git a/core/static_java_library.mk b/core/static_java_library.mk
index e1b9dd0..c692f9f 100644
--- a/core/static_java_library.mk
+++ b/core/static_java_library.mk
@@ -59,7 +59,7 @@
LOCAL_PROGUARD_FLAGS := $(addprefix -include ,$(proguard_options_file)) $(LOCAL_PROGUARD_FLAGS)
-ifeq ($(strip $(LOCAL_USE_JACK)),true)
+ifeq ($(LOCAL_USE_JACK),true)
ifndef LOCAL_JACK_PROGUARD_FLAGS
LOCAL_JACK_PROGUARD_FLAGS := $(LOCAL_PROGUARD_FLAGS)
endif
@@ -123,7 +123,7 @@
$(hide) find $(PRIVATE_SOURCE_INTERMEDIATES_DIR) -name R.java | xargs cat > $@
$(LOCAL_BUILT_MODULE): $(R_file_stamp)
-ifeq ($(strip $(LOCAL_USE_JACK)),true)
+ifeq ($(LOCAL_USE_JACK),true)
$(noshrob_classes_jack): $(R_file_stamp)
$(full_classes_jack): $(R_file_stamp)
endif # LOCAL_USE_JACK