Enabling ProGuard in Eclipse for Android

AndroidEclipseProguardObfuscation

Android Problem Overview


The new documentation on ProGuard for Android says to add a line to the default.properties file in the project home directory. However, on opening this file, I read at the top:

# This file is automatically generated by Android Tools. 
# Do not modify this file -- YOUR CHANGES WILL BE ERASED! 

Am I missing something?

Also, is there a way to enable ProGuard only for a production build from Eclipse (i.e., when exporting the finished product)?

Android Solutions


Solution 1 - Android

Android SDK (r20 or higher)

Please check the predefined proguard.config refered in project.properties

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt

More info: http://proguard.sourceforge.net/manual/examples.html#androidapplication

On Gradle:

buildTypes {
 release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            ...
  }
 }

Here you can check a proguard "default" file that I keep updating: https://medium.com/code-procedure-and-rants/android-my-standard-proguard-ffeceaf65521


Android SDK (r19 or lower)

You can add it to the default.properties. I've been adding manually without having a problem so far.

If you add the line:

proguard.config=proguard.cfg

As said it will only use ProGuard when exporting signed application (Android Tools => Export Signed Application)

If you start the project with the SDK before Android 2.3 the proguard.cfg file will not be created (next to default.properties as in 2.3>).

To enable automatic creation of it, just simply update to the SDK of Android 2.3 and create a new project with existing sources (which are the sources of the project you currently have).

Automagically the proguard.cfg fill will be created.

If still, you want to create it manually this is what it should contain:

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontwarn android.support.**
-verbose

-dontoptimize
-dontpreverify


-keepattributes *Annotation* 
-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 com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

 -keepclassmembers public class * extends android.view.View {
  void set*(***);
  *** get*();
 }

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-keepclassmembers class **.R$* {
  public static <fields>;
}

I think I've answered all the questions above.

UPDATE:

A line by line explanation:

#Use 5 step of optimization 
#-optimizationpasses 5

#When not preverifing in a case-insensitive filing system, such as Windows. This tool will unpack your processed jars,(if using windows you should then use):
-dontusemixedcaseclassnames

#Specifies not to ignore non-public library classes. As of version 4.5, this is the default setting
-dontskipnonpubliclibraryclasses

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).    
-dontoptimize
-dontpreverify

-dontwarn android.support.**

#Specifies to write out some more information during processing. If the program terminates with an exception, this option will print out the entire stack trace, instead of just the exception message.
-verbose

#The -optimizations option disables some arithmetic simplifications that Dalvik 1.0 and 1.5 can't handle. Note that the Dalvik VM also can't handle aggressive overloading (of static fields).
#To understand or change this check http://proguard.sourceforge.net/index.html#/manual/optimizations.html
#-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

#To repackage classes on a single package
#-repackageclasses ''

#Uncomment if using annotations to keep them.
#-keepattributes *Annotation*

#Keep classes that are referenced on the AndroidManifest
-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
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
#Compatibility library 
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment

#To maintain custom components names that are used on layouts XML.
#Uncomment if having any problem with the approach below
#-keep public class custom.components.package.and.name.**

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
 -keepclassmembers public class * extends android.view.View {
  void set*(***);
  *** get*();
}

#To remove debug logs:
-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** w(...);
}

#To avoid changing names of methods invoked on layout's onClick.
# Uncomment and add specific method names if using onClick on layouts
#-keepclassmembers class * {
# public void onClickButton(android.view.View);
#}

#Maintain java native methods 
-keepclasseswithmembernames class * {
    native <methods>;
}


#To maintain custom components names that are used on layouts XML:
-keep public class * extends android.view.View {
    public <init>(android.content.Context);
}
-keep public class * extends android.view.View {
    public <init>(android.content.Context, android.util.AttributeSet);
}
-keep public class * extends android.view.View {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

#Maintain enums
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

#To keep parcelable classes (to serialize - deserialize objects to sent through Intents)
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

#Keep the R
-keepclassmembers class **.R$* {
    public static <fields>;
}

###### ADDITIONAL OPTIONS NOT USED NORMALLY

#To keep callback calls. Uncomment if using any
#http://proguard.sourceforge.net/index.html#/manual/examples.html#callback
#-keep class mypackage.MyCallbackClass {
#   void myCallbackMethod(java.lang.String);
#}

#Uncomment if using Serializable 
#-keepclassmembers class * implements java.io.Serializable {
#    private static final java.io.ObjectStreamField[] serialPersistentFields;
#    private void writeObject(java.io.ObjectOutputStream);
#    private void readObject(java.io.ObjectInputStream);
#    java.lang.Object writeReplace();
#    java.lang.Object readResolve();
#}

UPDATE 2:

In the most recent ADT/Proguard use -keepclasseswithmembers instead of -keepclasseswithmembernames

Solution 2 - Android

just a follow-up because I was searching for the same thing - and the answers here are outdated - lately the base proguard config is here in the sdk dir - so you only have to put this into your project.properties:

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt

if you want to make project-specific modifications, create a proguard-project.txt and change the line to:

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 

Solution 3 - Android

As of ADT 16 at least, you can indeed add the line in project.properties, and it will be preserved. You can try changing the target SDK version, and see that project.properties is updated accordingly but the added line is still there. So, I think the warning is just badly worded; it means to say that settings in the file such as target will be overwritten with project settings, rather than vice versa.

Solution 4 - Android

Changes to ProGuard configuration came about with ADT version 17. ProGuard was updated from 4.4 to 4.7 and the difference in the configuration file reference already note was introduced. Note that existing projects would remain unchanged, leaving them without the newer rule set included in this and newer ADT versions. Relevant doc for newer configuration arrangement, already noted by ligi above, are available at:-

http://tools.android.com/recent/proguardimprovements "Second, we've changed the way configuration files are handled."

Solution 5 - Android

You can add the line to build.properties, as mentioned in default.properties.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTed HoppView Question on Stackoverflow
Solution 1 - AndroidneteinsteinView Answer on Stackoverflow
Solution 2 - AndroidligiView Answer on Stackoverflow
Solution 3 - Androidusethe4ceView Answer on Stackoverflow
Solution 4 - AndroidphillxnetView Answer on Stackoverflow
Solution 5 - AndroidEric LafortuneView Answer on Stackoverflow