Add the ability to turn on proguard for java targets, and to call aapt
to generate a rules file for proguard for stuff from the manifest and
other resources that need to be kept.
diff --git a/core/clear_vars.mk b/core/clear_vars.mk
index 28044c4..a7eba3f 100644
--- a/core/clear_vars.mk
+++ b/core/clear_vars.mk
@@ -87,6 +87,8 @@
 LOCAL_SDK_VERSION:=
 LOCAL_NO_EMMA_INSTRUMENT:=
 LOCAL_NO_EMMA_COMPILE:=
+LOCAL_PROGUARD_ENABLED:= # '',optonly,full,custom
+LOCAL_PROGUARD_FLAGS:=
 
 # Trim MAKEFILE_LIST so that $(call my-dir) doesn't need to
 # iterate over thousands of entries every time.
diff --git a/core/config.mk b/core/config.mk
index 77239cd..37ca404 100644
--- a/core/config.mk
+++ b/core/config.mk
@@ -199,6 +199,7 @@
 TUNE2FS := tune2fs
 E2FSCK := e2fsck
 JARJAR := java -jar $(HOST_OUT_JAVA_LIBRARIES)/jarjar.jar
+PROGUARD := external/proguard/bin/proguard.sh
 
 # dx is java behind a shell script; no .exe necessary.
 DX := $(HOST_OUT_EXECUTABLES)/dx
diff --git a/core/definitions.mk b/core/definitions.mk
index ccefafa..83834bf 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -1167,6 +1167,7 @@
     $(addprefix -S , $(PRIVATE_RESOURCE_DIR)) \
     $(addprefix -A , $(PRIVATE_ASSET_DIR)) \
     $(addprefix -I , $(PRIVATE_AAPT_INCLUDES)) \
+    $(addprefix -G , $(PRIVATE_PROGUARD_OPTIONS_FILE)) \
     $(addprefix --min-sdk-version , $(DEFAULT_APP_TARGET_SDK)) \
     $(addprefix --target-sdk-version , $(DEFAULT_APP_TARGET_SDK)) \
     $(addprefix --version-code , $(PLATFORM_SDK_VERSION)) \
diff --git a/core/java.mk b/core/java.mk
index 658b173..19b4d63 100644
--- a/core/java.mk
+++ b/core/java.mk
@@ -68,7 +68,8 @@
 # the emma tool
 full_classes_emma_jar := $(emma_intermediates_dir)/lib/$(full_classes_compiled_jar_leaf)
 full_classes_stubs_jar := $(intermediates.COMMON)/stubs.jar
-full_classes_jarjar_jar := $(full_classes_jar)
+full_classes_jarjar_jar := $(intermediates.COMMON)/classes-jarjar.jar
+full_classes_proguard_jar := $(full_classes_jar)
 built_dex := $(intermediates.COMMON)/classes.dex
 
 LOCAL_INTERMEDIATE_TARGETS += \
@@ -174,15 +175,14 @@
 $(PRIVATE_EMMA_COVERAGE_FILE): $(full_classes_emma_jar)
 else
 $(full_classes_emma_jar): $(full_classes_compiled_jar) | $(ACP)
-	@echo Copying $<
+	@echo Copying: $<
 	$(copy-file-to-target)
 endif
 
-# Run jarjar if necessary, otherwise just copy the file.  This is the last
-# part of this step, so the output of this command is full_classes_jar.
+# Run jarjar if necessary, otherwise just copy the file.
 ifneq ($(strip $(LOCAL_JARJAR_RULES)),)
 $(full_classes_jarjar_jar): PRIVATE_JARJAR_RULES := $(LOCAL_JARJAR_RULES)
-$(full_classes_jarjar_jar): $(full_classes_emma_jar) | jarjar
+$(full_classes_jarjar_jar): $(full_classes_emma_jar) | $(JARJAR)
 	@echo JarJar: $@
 	$(hide) $(JARJAR) process $(PRIVATE_JARJAR_RULES) $< $@
 else
@@ -191,6 +191,40 @@
 	$(hide) $(ACP) $< $@
 endif
 
+# Run proguard if necessary, otherwise just copy the file.  This is the last
+# part of this step, so the output of this command is full_classes_jar.
+ifneq ($(strip $(LOCAL_PROGUARD_ENABLED)),)
+proguard_dictionary := $(intermediates.COMMON)/proguard_dictionary
+proguard_flags := $(addprefix -libraryjars ,$(full_java_libs)) \
+                  -include $(BUILD_SYSTEM)/proguard.flags \
+                  -forceprocessing \
+                  -printmapping $(proguard_dictionary)
+ifeq ($(strip $(LOCAL_PROGUARD_ENABLED)),full)
+    # full
+else
+ifeq ($(strip $(LOCAL_PROGUARD_ENABLED)),optonly)
+    # optonly
+    proguard_flags += -dontobfuscate
+else
+ifeq ($(strip $(LOCAL_PROGUARD_ENABLED)),custom)
+    # custom
+else
+    $(warning while processing: $(LOCAL_MODULE))
+    $(error invalid value for LOCAL_PROGUARD_ENABLED: $(LOCAL_PROGUARD_ENABLED))
+endif
+endif
+endif
+
+$(full_classes_proguard_jar): PRIVATE_PROGUARD_FLAGS := $(proguard_flags) $(LOCAL_PROGUARD_FLAGS)
+$(full_classes_proguard_jar): $(full_classes_emma_jar) | $(PROGUARD)
+	@echo Proguard: $@
+	$(hide) $(PROGUARD) -injars $< -outjars $@ $(PRIVATE_PROGUARD_FLAGS)
+else
+$(full_classes_proguard_jar): $(full_classes_emma_jar) | $(ACP)
+	@echo Copying: $@
+	$(hide) $(ACP) $< $@
+endif
+
 # Override PRIVATE_INTERMEDIATES_DIR so that install-dex-debug
 # will work even when intermediates != intermediates.COMMON.
 $(built_dex): PRIVATE_INTERMEDIATES_DIR := $(intermediates.COMMON)
diff --git a/core/package.mk b/core/package.mk
index 6b09bda..828f4c7 100644
--- a/core/package.mk
+++ b/core/package.mk
@@ -111,6 +111,11 @@
 
 LOCAL_BUILT_MODULE_STEM := package.apk
 
+proguard_options_file := $(package_expected_intermediates_COMMON)/proguard_options
+ifneq ($(strip $(LOCAL_PROGUARD_ENABLED)),custom)
+    LOCAL_PROGUARD_FLAGS := -include $(proguard_options_file) $(LOCAL_PROGUARD_FLAGS)
+endif
+
 # The dex files go in the package, so we don't
 # want to install them separately for this module.
 old_DONT_INSTALL_DEX_FILES := $(DONT_INSTALL_DEX_FILES)
@@ -143,6 +148,7 @@
 
 $(R_file_stamp): PRIVATE_RESOURCE_PUBLICS_OUTPUT := \
 			$(intermediates.COMMON)/public_resources.xml
+$(R_file_stamp): PRIVATE_PROGUARD_OPTIONS_FILE := $(proguard_options_file)
 $(R_file_stamp): $(all_res_assets) $(full_android_manifest) $(AAPT) | $(ACP)
 	@echo "target R.java/Manifest.java: $(PRIVATE_MODULE) ($@)"
 	@rm -f $@
@@ -164,6 +170,8 @@
 		$(ACP) -fpt $$GENERATED_R_FILE $@ || exit 32; \
 	done; \
 
+$(proguard_options_file): $(R_file_stamp)
+
 ifdef LOCAL_EXPORT_PACKAGE_RESOURCES
 # Put this module's resources into a PRODUCT-agnositc package that
 # other packages can use to build their own PRODUCT-agnostic R.java (etc.)
diff --git a/core/proguard.flags b/core/proguard.flags
new file mode 100644
index 0000000..afd1548
--- /dev/null
+++ b/core/proguard.flags
@@ -0,0 +1,7 @@
+# see http://sourceforge.net/tracker/?func=detail&aid=2787465&group_id=54750&atid=474707
+-optimizations !code/simplification/arithmetic
+-allowaccessmodification
+
+# Some classes in the libraries extend package private classes to chare common functionality
+# that isn't explicitly part of the API
+-dontskipnonpubliclibraryclasses