Proguard usage in Android

Hi friends, have a great day. Now I'm going to explain the what is proguard and it's usage in android. Believe android is big sea, there is lot of things.

What is Proguard?
Proguard is a simple java tools to shrink apk, prevent re-engineering from hackers. It's a reduce the size of android apps and run faster. Proguard renames the classes, methods and objects to prevent the re-engineering the APK.

Now hackers easily to hack your code and run for their uses. Proguard stops them and protect your code.

In android project, proguard located in apps folder.

To enable the code shrinking, just add simple line in gradle. It's under

      minifyEnabled true
      proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

Please follow the below lines, that's very important to make it.

#Specifies not to generate mixed-case class names while obfuscating. By default, obfuscated class names can contain a mix of upper-case characters and lower-case characters. This creates perfectly acceptable and usable jars. Only if a jar is unpacked on a platform with a case-insensitive filing system (say, Windows), the unpacking tool may let similarly named class files overwrite each other.

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

#Specifies not to optimize the input class files. By default, optimization is enabled; all methods are optimized at a bytecode level.

#Specifies not to preverify the processed class files. By default, class files are preverified if they are targeted at Java Micro Edition or at Java 6 or higher. For Java Micro Edition, preverification is required, so you will need to run an external preverifier on the processed code if you specify this option. For Java 6, preverification is optional, but as of Java 7, it is required. Only when eventually targeting Android, it is not necessary, so you can then switch it off to reduce the processing time a bit.

-keepattributes [attribute_filter] example (-keepattributes *Annotation*)
#Specifies any optional attributes to be preserved. The attributes can be specified with one or more -keepattributes directives. The optional filter is a comma-separated list of attribute names that Java virtual machines and ProGuard support. Attribute names can contain ?, *, and ** wildcards, and they can be preceded by the ! negator. For example, you should at least keep the Exceptions, InnerClasses, and Signature attributes when processing a library. You should also keep the SourceFile and LineNumberTable attributes for producing useful obfuscated stack traces. Finally, you may want to keep annotations if your code depends on them. Only applicable when obfuscating.

-keep public class
-keep public class
#For google in purchase library.

keepclasseswithmembernames [class_specification] 
example (-keepclasseswithmembernames class * {
    native <methods>;
#Short for -keepclasseswithmembers,allowshrinking class_specification
#Specifies classes and class members whose names are to be preserved, on the condition that all of the specified class members are present after the shrinking phase. For example, you may want to keep all native method names and the names of their classes, so that the processed code can still link with the native library code. Native methods that aren't used at all can still be removed. If a class file is used, but none of its native methods are, its name will still be obfuscated. Only applicable when obfuscating.

-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
#keep setters in Views so that animations can still work.

-keepclassmembers class * extends {
   public void *(android.view.View);
-keepattributes EnclosingMethod
#We want to keep methods in Activity that could be used in the XML attribute onClick

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
#For enumeration classes

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

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

-keep class** { *; }
#for supporting android v7 classes

-keep interface** { *; }
#For supporting android v7 interfaces.

-keep class javax.** { *; }
-keep class org.** { *; }
-keep public class**
#for keep google play services

-keep [package name of libraries]
# for keep your libraries

-dontwarn org.apache.log4j.**
-dontwarn org.apache.commons.logging.**
-dontwarn org.slf4j.**
-dontwarn org.json.*
# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.

Once you plan to shrink your apk, all classes, methods, objects and third party libraries will be protect by proguard.

class name like A.class, B.class, C.class.

Sometimes you getting error, because you might be missing out to write some lines in proguard. So please be carefully to handle this. All the best.


Popular posts from this blog

Clear the app data programmatically in android

convert an integer to a string in java

Print the large string values in android log cat.