Bug 709230 part 2 - Optimize Java .class files with ProGuard. r=dougt
authorChris Peterson <cpeterson@mozilla.com>
Thu, 26 Jan 2012 10:50:37 -0800
changeset 86969 823cd28154837eb1735c6f2c2673909e5bc74b76
parent 86968 1d4d44ecd75c705e3d7049d3248c183eb8161664
child 86970 cae76db91b49809be3579cfec47d26a7a36fa8ea
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdougt
bugs709230
milestone12.0a1
Bug 709230 part 2 - Optimize Java .class files with ProGuard. r=dougt
config/android-common.mk
mobile/android/base/Makefile.in
mobile/android/config/proguard.cfg
--- a/config/android-common.mk
+++ b/config/android-common.mk
@@ -43,16 +43,17 @@ endif
 
 ifndef JAVA_CLASSPATH
   $(error JAVA_CLASSPATH must be defined before including android-common.mk)
 endif
 
 DX=$(ANDROID_PLATFORM_TOOLS)/dx
 AAPT=$(ANDROID_PLATFORM_TOOLS)/aapt
 APKBUILDER=$(ANDROID_SDK)/../../tools/apkbuilder
+PROGUARD=java -jar $(ANDROID_SDK)/../../tools/proguard/lib/proguard.jar
 ZIPALIGN=$(ANDROID_SDK)/../../tools/zipalign
 
 ifdef JARSIGNER
   APKBUILDER_FLAGS += -u
 endif
 
 # For Android, this defaults to $(ANDROID_SDK)/android.jar
 ifndef JAVA_BOOTCLASSPATH
--- a/mobile/android/base/Makefile.in
+++ b/mobile/android/base/Makefile.in
@@ -581,17 +581,23 @@ include $(topsrcdir)/config/android-comm
 # Instead of on the .class files, since more than one .class file might be produced per .java file
 # Sync dependencies are provided in a single jar. Sync classes themselves are delivered as source,
 # because Android resource classes must be compiled together in order to avoid overlapping resource
 # indices.
 classes.dex: $(FENNEC_JAVA_FILES) $(FENNEC_PP_JAVA_FILES) $(SYNC_JAVA_FILES) $(SYNC_PP_JAVA_FILES) $(SYNC_THIRDPARTY_JAVA_FILES) R.java
 	$(NSINSTALL) -D classes
 	$(JAVAC) $(JAVAC_FLAGS) -d classes $(addprefix $(srcdir)/,$(SYNC_THIRDPARTY_JAVA_FILES))
 	$(JAVAC) $(JAVAC_FLAGS) -Xlint:all,-deprecation,-fallthrough -d classes -classpath classes $(addprefix $(srcdir)/,$(FENNEC_JAVA_FILES)) $(FENNEC_PP_JAVA_FILES) $(addprefix $(srcdir)/,$(SYNC_JAVA_FILES)) $(SYNC_PP_JAVA_FILES) R.java
-	$(DX) --dex --output=$@ classes
+	$(PROGUARD) @$(topsrcdir)/mobile/android/config/proguard.cfg \
+			-injars classes \
+			-outjars classes-proguarded \
+			-libraryjars $(ANDROID_SDK)/android.jar
+	# ProGuard may optimize away local variables, so dx --no-locals to ignore
+	# debug symbols for missing local variables.
+	$(DX) --dex --output=$@ --no-locals classes-proguarded
 
 PP_RES_XML=$(SYNC_PP_RES_XML)
 
 $(PP_RES_XML): $(subst res/,$(srcdir)/resources/, $(PP_RES_XML).in)
 	$(PYTHON) $(topsrcdir)/config/Preprocessor.py \
              $(AUTOMATION_PPARGS) $(DEFINES) $(ACDEFINES) $< > $@
 
 # AndroidManifest.xml includes these files, so they need to be marked as dependencies.
new file mode 100644
--- /dev/null
+++ b/mobile/android/config/proguard.cfg
@@ -0,0 +1,98 @@
+#
+# Rules from Google's example proguard.cfg in the Android SDK:
+# android-sdk-macosx/tools/lib/proguard.cfg
+#
+
+# TODO: We can remove -dontskipnonpubliclibraryclasses for ProGuard >= 4.5
+# because it is enabled by default.
+-dontskipnonpubliclibraryclasses
+-dontpreverify
+-verbose
+
+# Preserve all fundamental application classes.
+-keep public class * extends android.app.Activity
+-keep public class * extends android.app.Application
+-keep public class * extends android.app.Service
+-keep public class * extends android.content.BroadcastReceiver
+-keep public class * extends android.content.ContentProvider
+-keep public class * extends android.app.backup.BackupAgentHelper
+-keep public class * extends android.preference.Preference
+
+# Preserve all native method names and the names of their classes.
+-keepclasseswithmembernames class * {
+    native <methods>;
+}
+
+# Preserve all classes that have special context constructors, and the
+# constructors themselves.
+-keepclasseswithmembers class * {
+    public <init>(android.content.Context, android.util.AttributeSet);
+}
+
+# Preserve all classes that have special context constructors, and the
+# constructors themselves.
+-keepclasseswithmembers class * {
+    public <init>(android.content.Context, android.util.AttributeSet, int);
+}
+
+-keepclassmembers class * extends android.app.Activity {
+   public void *(android.view.View);
+}
+
+# Preserve the special static methods that are required in all enumeration
+# classes.
+-keepclassmembers enum * {
+    public static **[] values();
+    public static ** valueOf(java.lang.String);
+}
+
+#
+# Rules from ProGuard's Android example:
+# http://proguard.sourceforge.net/manual/examples.html#androidapplication
+#
+
+# Switch off some optimizations that trip older versions of the Dalvik VM.
+# https://code.google.com/p/android/issues/detail?id=964
+-optimizations !code/simplification/cast
+
+# Preserve all View implementations, their special context constructors, and
+# their setters.
+-keep public class * extends android.view.View {
+    public <init>(android.content.Context);
+    public <init>(android.content.Context, android.util.AttributeSet);
+    public <init>(android.content.Context, android.util.AttributeSet, int);
+    public void set*(...);
+}
+
+# Preserve the special fields of all Parcelable implementations.
+-keepclassmembers class * implements android.os.Parcelable {
+    static android.os.Parcelable$Creator CREATOR;
+}
+
+# Preserve static fields of inner classes of R classes that might be accessed
+# through introspection.
+-keepclassmembers class **.R$* {
+    public static <fields>;
+}
+
+#
+# Mozilla-specific rules
+#
+
+# Preserve public classes and methods accessed from C++ JNI.
+-keep public class org.mozilla.gecko.** { public *; }
+
+# Google defaults to 5 passes, but Fennec benefits from up to 9-10 passes.
+-optimizationpasses 10
+
+# Disable obfuscation because it makes exception stack traces more
+# difficult to debug. Plus Firefox's code is already open-source.
+-dontobfuscate
+
+# Silence warnings about "Note: duplicate definition of library class".
+-dontnote **,!org.mozilla.**
+
+# Allow private members to be made public to improve method inlining.
+# TODO: Enable -allowaccessmodification optimizations for ProGuard >= 4.6.
+# These optimizations trigger a ProGuard 4.4 bug when inlining private methods.
+##-allowaccessmodification