score:76

Accepted answer

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 

score:3

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

score:4

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."

score:10

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.

score:113

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


Related Query

More Query from same tag