Skip to main content

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

Clear the App Data Programmatically in Android

Application data has been created due to use shared preference data, databases and network caches data. This data has been manually clear on Settings -- > Apps (or) Application Manager --> Select the app you want to clear the data. --> Then click button clear data to erase the app from the Phone and SDCARD.

Applications like facebook, google+, gmail and some games captures more data on phone and SDCARD.

Once you clear the data of your app, all passwords and saved settings in app has been lost. So carefull to use this method.

Create the Class MyApplication

public class MyApplication extends Application {
 private static MyApplication instance;
 public void onCreate() {
  instance = this;
 public static MyApplication getInstance(){
  return instance;
 public void clearApplicationData() {
  File cache = getCacheDir();
  File appDir = new File(cache.getParent());
   String[] children = app…

Sort the numbers using If, Else and Loop.

This is very interesting. You need to sort the numbers, but not using the SORT functions. You can use the If, Else or Loop to do the functions.

See the solution:

import java.util.Scanner;
public class OrderNumbers {
    public static void main(String[] args) {         Scanner in = new Scanner(;         System.out.println("Enter the size of an Array");         int num = in.nextInt();         int [] numbers = new int[num];         for (int i = 0; i < numbers.length; i++) {             System.out.println("Enter the number "+(i+1)+" : ");             numbers[i] = in.nextInt();         }         sortNumbers(numbers);     }     static void sortNumbers(int[] numbers){         for (int i = 0; i < numbers.length; i++) {             int newValue;             for (int j = 0; j < numbers.length; j++) {                 if (numbers[i] < numbers[j]) {                     newValue = numbers[j];                     numbers[j] = numbers[i];                     numbers[i]…

Record the mobile screens through MyMobiler

Dedicate to all android developers, MyMobiler is very useful for android app developers.


Records the mobile screens.
Screen Capture.
Control your mobile device from the desktop.
Transfer the file between desktop/device
USB/Wifi Connection
Supports from Android OS 2.2 version.

Download the desktop application.

Download the mobile application.

Watch the tutorials about connecting the device and desktop in YouTube: