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.

proguard-rules.pro

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

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

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

-dontusemixedcaseclassnames
#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.

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

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

-dontpreverify
#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 com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
#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 android.app.Activity {
   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 android.support.v7.** { *; }
#for supporting android v7 classes

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

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

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

-dontwarn android.support.v7.**
-dontwarn android.support.**
-dontwarn javax.management.**
-dontwarn java.lang.management.**
-dontwarn org.apache.log4j.**
-dontwarn org.apache.commons.logging.**
-dontwarn org.slf4j.**
-dontwarn org.json.*
-dontwarn com.google.android.gms.**
# 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.

Comments

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;
 @Override
 public void onCreate() {
  super.onCreate();
  instance = this;
 }
 public static MyApplication getInstance(){
  return instance;
 }
 public void clearApplicationData() {
  File cache = getCacheDir();
  File appDir = new File(cache.getParent());
  if(appDir.exists()){
   String[] children = app…

convert an integer to a string in java

Hi, have a great day. Hope It's like to be homework question or to be interview question. Yes, it's right. In future posts, I'm gonna be post a interview questions and answers, algorithms and some critical problems.

Requirement:
1) return an empty string if value is negative.
2) return an empty string if the conversion fails.
3) return the proper string of the value using characer '0' to '9' and 'A' to 'F'
for example:
MyIntToString(254, 16) return "FE"
MyIntToString(254,  8) return "376"
MyIntToString(254,  2) return "11111110"


Rules:
1) You SHALL NOT import any Java libraries for the implementation.
2) You are allowed to using the string class related functions, like: insert(),
appened, trim()and etc for the implementation
3) You SHALL NOT invoke static functions from java.lang.Integer class to perform the conversion.
4) Your code style is graded, hence it is encouraged to structure the code professionally
and equip…

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.in);         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]…