Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7311a344 authored by Ying Wang's avatar Ying Wang
Browse files

Make it easier to enable obfuscation and optimization.

With this change, use:
* LOCAL_PROGUARD_ENABLED := obfuscation  # to enable obfuscation
* LOCAL_PROGUARD_ENABLED := optimization # to enable optimization
* LOCAL_PROGUARD_ENABLED := obfuscation optimization # to enable both

Now the meaning of the LOCAL_PROGUARD_ENABLED options:
* full:
    Use the build system's default configurations:
    with shrink but no obfuscation or optimization,
    global proguard flags in build/core/proguard.flags
    are applied.
* custom:
    The same as "full" except no aapt-generated resource-related
    proguard flags.
* nosystem:
    Don't use any build system's default configurations; but
    aapt-generated proguard flags are still applied. You are
    responsible for any other flags.
* disabled:
    Disable proguard.
* obfuscation:
    The same as "full" but with obfuscation enabled.
* optimization:
    The same as "full" but with optimization enabled.
* no value (the default):
    The build system chooses the proper value: "full" if it's an
    app; "disabled" if it's a library.

You can use more than 1 of them in a meaningful combination,
for example:
LOCAL_PROGUARD_ENABLED := obfuscation optimization

Bug: 10307372
Change-Id: Id248caca3048e99547f16559fae74f4afe85c354
parent f4723fa4
Loading
Loading
Loading
Loading
+10 −11
Original line number Diff line number Diff line
@@ -387,19 +387,20 @@ ifdef LOCAL_IS_HOST_MODULE
ifeq ($(LOCAL_BUILD_HOST_DEX),true)
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH := -bootclasspath $(call java-lib-files,core-hostdex,$(LOCAL_IS_HOST_MODULE))

full_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
full_shared_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
full_java_lib_deps := $(call java-lib-deps,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
else
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_BOOTCLASSPATH :=

full_java_libs := $(addprefix $(HOST_OUT_JAVA_LIBRARIES)/,$(addsuffix $(COMMON_JAVA_PACKAGE_SUFFIX),$(LOCAL_JAVA_LIBRARIES)))
full_java_lib_deps := $(full_java_libs)
full_shared_java_libs := $(addprefix $(HOST_OUT_JAVA_LIBRARIES)/,\
    $(addsuffix $(COMMON_JAVA_PACKAGE_SUFFIX),$(LOCAL_JAVA_LIBRARIES)))
full_java_lib_deps := $(full_shared_java_libs)
endif # LOCAL_BUILD_HOST_DEX
else
full_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
full_shared_java_libs := $(call java-lib-files,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
full_java_lib_deps := $(call java-lib-deps,$(LOCAL_JAVA_LIBRARIES),$(LOCAL_IS_HOST_MODULE))
endif # !LOCAL_IS_HOST_MODULE
full_java_libs += $(full_static_java_libs) $(LOCAL_CLASSPATH)
full_java_libs := $(full_shared_java_libs) $(full_static_java_libs) $(LOCAL_CLASSPATH)
full_java_lib_deps += $(full_static_java_libs) $(LOCAL_CLASSPATH)

# This is set by packages that are linking to other packages that export
@@ -426,14 +427,12 @@ ifdef LOCAL_INSTRUMENTATION_FOR
        $(LOCAL_PATH): Multiple LOCAL_INSTRUMENTATION_FOR members defined)
  endif

  link_instr_intermediates_dir := $(call intermediates-dir-for, \
      APPS,$(LOCAL_INSTRUMENTATION_FOR))
  link_instr_intermediates_dir.COMMON := $(call intermediates-dir-for, \
      APPS,$(LOCAL_INSTRUMENTATION_FOR),,COMMON)

  # link against the jar with full original names (before proguard processing).
  full_java_libs += $(link_instr_intermediates_dir.COMMON)/classes.jar
  full_java_lib_deps += $(link_instr_intermediates_dir.COMMON)/classes.jar
  link_instr_classes_jar := $(link_instr_intermediates_dir.COMMON)/classes.jar
  full_java_libs += $(link_instr_classes_jar)
  full_java_lib_deps += $(link_instr_classes_jar)
endif

jar_manifest_file :=
@@ -444,7 +443,7 @@ else
$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_JAR_MANIFEST :=
endif

endif
endif  # PRIVATE java vars


###########################################################
+1 −1
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ LOCAL_SDK_VERSION:=
LOCAL_SDK_RES_VERSION:=
LOCAL_NDK_STL_VARIANT:=
LOCAL_EMMA_INSTRUMENT:=
LOCAL_PROGUARD_ENABLED:= # '',full,custom,nosystem,disabled
LOCAL_PROGUARD_ENABLED:= # '',full,custom,nosystem,disabled,obfuscation,optimization
LOCAL_PROGUARD_FLAGS:=
LOCAL_PROGUARD_FLAG_FILES:=
LOCAL_EMMA_COVERAGE_FILTER:=
+2 −54
Original line number Diff line number Diff line
@@ -1715,35 +1715,6 @@ $(call compile-java,$(HOST_JAVAC),$(PRIVATE_BOOTCLASSPATH))
$(if $(PRIVATE_EXTRA_JAR_ARGS), $(call add-java-resources-to-package))
endef

###########################################################
## Obfuscate a jar file
###########################################################

# PRIVATE_KEEP_FILE is a file containing a list of classes
# PRIVATE_INTERMEDIATES_DIR is a directory we can use for temporary files
# The module using this must depend on
#        $(HOST_OUT_JAVA_LIBRARIES)/proguard-4.0.1.jar
define obfuscate-jar
@echo "Obfuscate jar: $(notdir $@) ($@)"
@mkdir -p $(dir $@)
@rm -f $@
@mkdir -p $(PRIVATE_INTERMEDIATES_DIR)
$(hide) sed -e 's/^/-keep class /' < $(PRIVATE_KEEP_FILE) > \
		$(PRIVATE_INTERMEDIATES_DIR)/keep.pro
$(hide) java -Xmx512M -jar $(HOST_OUT_JAVA_LIBRARIES)/proguard-4.0.1.jar \
		-injars $< \
		-outjars $@ \
		-target 1.5 \
		-dontnote -dontwarn \
		-printmapping $(PRIVATE_INTERMEDIATES_DIR)/out.map \
		-forceprocessing \
		-renamesourcefileattribute SourceFile \
		-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod \
		-repackageclasses \
		-keepclassmembers "class * { public protected *; }" \
		@$(PRIVATE_INTERMEDIATES_DIR)/keep.pro
endef

###########################################################
## Commands for copying files
###########################################################
@@ -1887,34 +1858,11 @@ endif
###########################################################
## Commands to call Proguard
###########################################################

# Command to copy the file with acp, if proguard is disabled.
define proguard-disabled-commands
@echo Copying: $@
$(hide) $(ACP) -fp $< $@
endef

# Command to call Proguard
# $(1): extra flags for instrumentation.
define proguard-enabled-commands
@echo Proguard: $@
$(hide) $(PROGUARD) -injars $< -outjars $@ $(PRIVATE_PROGUARD_FLAGS) $(1)
endef

# Figure out the proguard dictionary file of the module that is instrumentationed for.
define get-instrumentation-proguard-flags
$(if $(PRIVATE_INSTRUMENTATION_FOR),$(if $(ALL_MODULES.$(PRIVATE_INSTRUMENTATION_FOR).PROGUARD_ENABLED),-applymapping $(call intermediates-dir-for,APPS,$(PRIVATE_INSTRUMENTATION_FOR),,COMMON)/proguard_dictionary))
endef

define transform-jar-to-proguard
$(eval _instrumentation_proguard_flags:=$(call get-instrumentation-proguard-flags))
$(eval _enable_proguard:=$(PRIVATE_PROGUARD_ENABLED)$(_instrumentation_proguard_flags))
$(if $(_enable_proguard),$(call proguard-enabled-commands,$(_instrumentation_proguard_flags)),$(call proguard-disabled-commands))
$(eval _instrumentation_proguard_flags:=)
$(eval _enable_proguard:=)
@echo Proguard: $@
$(hide) $(PROGUARD) -injars $< -outjars $@ $(PRIVATE_PROGUARD_FLAGS)
endef


###########################################################
## Stuff source generated from one-off tools
###########################################################
+52 −20
Original line number Diff line number Diff line
@@ -88,7 +88,6 @@ full_classes_compiled_jar_leaf := classes-full-debug.jar
built_dex_intermediate_leaf := classes-with-local.dex
endif

LOCAL_PROGUARD_ENABLED:=$(strip $(LOCAL_PROGUARD_ENABLED))
ifeq ($(LOCAL_PROGUARD_ENABLED),disabled)
LOCAL_PROGUARD_ENABLED :=
endif
@@ -386,42 +385,75 @@ $(full_classes_jar): $(full_classes_emma_jar) | $(ACP)
	$(hide) $(ACP) -fp $< $@

# Run proguard if necessary, otherwise just copy the file.
ifdef LOCAL_PROGUARD_ENABLED
ifneq ($(filter-out full custom nosystem obfuscation optimization,$(LOCAL_PROGUARD_ENABLED)),)
    $(warning while processing: $(LOCAL_MODULE))
    $(error invalid value for LOCAL_PROGUARD_ENABLED: $(LOCAL_PROGUARD_ENABLED))
endif
proguard_dictionary := $(intermediates.COMMON)/proguard_dictionary
# Proguard doesn't like a class in both library and the jar to be processed.
proguard_full_java_libs := $(filter-out $(full_static_java_libs),$(full_java_libs))
proguard_flags := $(addprefix -libraryjars ,$(proguard_full_java_libs)) \
proguard_flags := $(addprefix -libraryjars ,$(full_shared_java_libs)) \
                  -forceprocessing \
                  -printmapping $(proguard_dictionary)
ifneq ($(LOCAL_PROGUARD_ENABLED),nosystem)

ifeq ($(filter nosystem,$(LOCAL_PROGUARD_ENABLED)),)
proguard_flags += -include $(BUILD_SYSTEM)/proguard.flags
ifeq ($(LOCAL_EMMA_INSTRUMENT),true)
proguard_flags += -include $(BUILD_SYSTEM)/proguard.emma.flags
endif
# If this is a test package, add proguard keep flags for tests.
ifneq ($(strip $(LOCAL_INSTRUMENTATION_FOR)$(filter tests,$(LOCAL_MODULE_TAGS))$(filter android.test.runner,$(LOCAL_JAVA_LIBRARIES))),)
proguard_flags := $(proguard_flags) -include $(BUILD_SYSTEM)/proguard_tests.flags
ifneq ($(LOCAL_INSTRUMENTATION_FOR)$(filter tests,$(LOCAL_MODULE_TAGS)),)
proguard_flags += -include $(BUILD_SYSTEM)/proguard_tests.flags
endif # test package
else  # LOCAL_PROGUARD_ENABLED is nosystem
proguard_flags += -include $(BUILD_SYSTEM)/proguard_basic_keeps.flags
ifeq ($(filter obfuscation,$(LOCAL_PROGUARD_ENABLED)),)
# By default no obfuscation
proguard_flags += -dontobfuscate
endif  # No obfuscation
ifeq ($(filter optimization,$(LOCAL_PROGUARD_ENABLED)),)
# By default no optimization
proguard_flags += -dontoptimize
endif  # No optimization

ifdef LOCAL_INSTRUMENTATION_FOR
ifeq ($(filter obfuscation,$(LOCAL_PROGUARD_ENABLED)),)
# If no obfuscation, link in the instrmented package's classes.jar as a library.
# link_instr_classes_jar is defined in base_rule.mk
proguard_flags += -libraryjars $(link_instr_classes_jar)
else # obfuscation
# If obfuscation is enabled, the main app must be obfuscated too.
# We need to run obfuscation using the main app's dictionary,
# and treat the main app's class.jar as injars instead of libraryjars.
proguard_flags := -injars  $(link_instr_classes_jar) \
    -outjars $(intermediates.COMMON)/proguard.$(LOCAL_INSTRUMENTATION_FOR).jar \
    -include $(link_instr_intermediates_dir.COMMON)/proguard_options \
    -applymapping $(link_instr_intermediates_dir.COMMON)/proguard_dictionary \
    -verbose \
    $(proguard_flags)

# Sometimes (test + main app) uses different keep rules from the main app -
# apply the main app's dictionary anyway.
proguard_flags += -ignorewarnings

# Make sure we run Proguard on the main app first
$(full_classes_proguard_jar) : $(link_instr_intermediates_dir.COMMON)/proguard.classes.jar

endif # no obfuscation
endif # LOCAL_INSTRUMENTATION_FOR
endif  # LOCAL_PROGUARD_ENABLED is not nosystem

ifneq ($(LOCAL_PROGUARD_ENABLED),)
ifeq ($(filter full custom nosystem, $(LOCAL_PROGUARD_ENABLED)),)
    $(warning while processing: $(LOCAL_MODULE))
    $(error invalid value for LOCAL_PROGUARD_ENABLED: $(LOCAL_PROGUARD_ENABLED))
endif # not a legal value
endif # LOCAL_PROGUARD_ENABLED

proguard_flag_files := $(addprefix $(LOCAL_PATH)/, $(LOCAL_PROGUARD_FLAG_FILES))
LOCAL_PROGUARD_FLAGS += $(addprefix -include , $(proguard_flag_files))

$(full_classes_proguard_jar): PRIVATE_PROGUARD_ENABLED:=$(LOCAL_PROGUARD_ENABLED)
$(full_classes_proguard_jar): PRIVATE_PROGUARD_FLAGS := $(proguard_flags) $(LOCAL_PROGUARD_FLAGS)
$(full_classes_proguard_jar): PRIVATE_INSTRUMENTATION_FOR:=$(strip $(LOCAL_INSTRUMENTATION_FOR))
$(full_classes_proguard_jar) : $(full_classes_jar) $(proguard_flag_files) | $(ACP) $(PROGUARD)
	$(call transform-jar-to-proguard)

ALL_MODULES.$(LOCAL_MODULE).PROGUARD_ENABLED:=$(LOCAL_PROGUARD_ENABLED)
else  # LOCAL_PROGUARD_ENABLED not defined
$(full_classes_proguard_jar) : $(full_classes_jar)
	@echo Copying: $@
	$(hide) $(ACP) -fp $< $@

endif # LOCAL_PROGUARD_ENABLED defined


# Override PRIVATE_INTERMEDIATES_DIR so that install-dex-debug
# will work even when intermediates != intermediates.COMMON.
+3 −2
Original line number Diff line number Diff line
# We have moved -dontobfuscate and -dontoptimize to the makefiles.
# dex does not like code run through proguard optimize and preverify steps.
-dontoptimize
# -dontoptimize
-dontpreverify

# Don't obfuscate. We only need dead code striping.
-dontobfuscate
# -dontobfuscate

# Add this flag in your package's own configuration if it's needed.
#-flattenpackagehierarchy
Loading