Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write rules for proguard?

Tags:

java

android

I was need to make implementation of proguard in my project

my default proguard settings were like this

android {
...///

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

....////
}

Then I made some little changes

android {
...///

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

....////
}

There is my proguard-ruler.pro file https://ideone.com/ccPvPv

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/Shahar/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
 #and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

#If you not sure about proguard process so uncomment next line
#-dontobfuscate

# view res/layout/design_layout_snackbar_include.xml #generated:18
#-keep class android.support.design.internal.SnackbarContentLayout { <init>
(...); }

# view res/layout/activity_main.xml #generated:11
#-keep class android.support.design.widget.AppBarLayout { <init>(...); }

# view AndroidManifest.xml #generated:19
#-keep class ru.jollydroid.athdemo.MainActivity { <init>(...); }

#

# Proguard configuration for Jackson 2.x (fasterxml package instead of 
codehaus  package)
#-keep class com.fasterxml.jackson.databind.ObjectMapper {
#    public <methods>;
#    protected <methods>;
#}
#-keep class com.fasterxml.jackson.databind.ObjectWriter {
#    public ** writeValueAsString(**);
#}
#-keepnames class com.fasterxml.jackson.** { *; }


#-----------------
#-keepnames com.fasterxml.jackson.databind.** { *; }
#
#-keepnames com.squareup.okhttp.** { *; }
#
#-keepnames cryptix.util.test.** { *; }
#
#-keepnames jp.wasabeef.recyclerview.animators.** { *; }
#
#-keepnames cryptix.util.gui.** { *; } 
#
#-keepnames ui.activities.** { *; }
#
#-keepnames ui.adapters.** { *; } 
#
#-keepnames ui.fragments.** { *; }
#
#-keepnames webServices.controllers.** { *; }
#-------------------------

#Was worked
#-----------------
#-dontwarn com.fasterxml.jackson.databind.**
#
#-dontwarn com.squareup.okhttp.**
#
#-dontwarn cryptix.util.test.**
#
#-dontwarn jp.wasabeef.recyclerview.animators.**
#
#-dontwarn cryptix.util.gui.**
#
# -dontwarn ui.activities.**
#
#-dontwarn ui.adapters.**
#
#-dontwarn ui.fragments.**
#
#-dontwarn webServices.controllers.**
#-------------------------

#-dontwarn java.awt.**
#-dontwarn java.beans.Beans
#-dontwarn javax.security.**
#-keep class javamail.** {*;}
#-keep class javax.mail.** {*;}
#-keep class javax.activation.** {*;}
#-keep class com.sun.mail.dsn.** {*;}
#-keep class com.sun.mail.handlers.** {*;}
#-keep class com.sun.mail.smtp.** {*;}
#-keep class com.sun.mail.util.** {*;}
#-keep class mailcap.** {*;}
#-keep class mimetypes.** {*;}
#-keep class myjava.awt.datatransfer.** {*;}
#-keep class org.apache.harmony.awt.** {*;}
#-keep class org.apache.harmony.misc.** {*;}

# Proguard configuration for Jackson 2.x (fasterxml package instead of 
codehaus 
 package)


#-keep class com.fasterxml.jackson.annotation.** { *; }
#
#-dontwarn com.fasterxml.jackson.databind.**
#
#-keepclassmembers class com.myapp.models.** { *; }
#
#-keepattributes SourceFile,LineNumberTable
#-keep class com.parse.*{ *; }
#-keep class android.content.res.Xm.ResourceParser.** { *; }
#-keep class com.googlecode.** { *; }
#-dontwarn com.parse.**
#-dontwarn com.squareup.picasso.**
#-keepclasseswithmembernames class * {
#    native <methods>;
#}

I tried a lot of different way to make a rules for libs, but doesn't work

And there output that I get if I am trying make build apk in release mode.

https://ideone.com/Lg8tOT

So as far as I understand I get this issue because I need to add rules for external libs.

I saw this resource

https://github.com/krschultz/android-proguard-snippets

but it doesn't help me...

I am not a strong in proguard , so tell me please how I can add this rules?

If I understand correctly I need to write my custom rules, but how to do it?

Feel free to ask

Thanks in advance

EDIT

Make changes https://gist.github.com/albinmathew/c4436f8371c9c41461ab

Now gradle looks like

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

My proguard-rules.pro

https://ideone.com/kxsCEh

My errors now

https://ideone.com/JhCE3o

like image 280
Aleksey Timoshchenko Avatar asked Oct 25 '25 08:10

Aleksey Timoshchenko


2 Answers

shrinkResources true will compress your images (png, jpeg, etc)

minifyEnabled true will obfuscate you code.

Those two rules will shrink you code and the apk will be more light.

Since minify is enabled, you need to put some rules, rules that you put in proguard-rules.pro in order to keep classes unobfuscated because some methods needs to be visible for use.

For example, you have a class used by Firebase, User. Methods and fields from that class need to be visible in order to be functional. Firebase will convert response to that class and needs to be visible.

#-keep class .User.** {*;}

Some rules are defined by libraries and you need to add them in order to build the project.

Warnings can be avoided with -dontwarn rule.

-dontwarn cryptix.**
like image 143
Florescu Cătălin Avatar answered Oct 26 '25 23:10

Florescu Cătălin


Why do you want to set rules for Proguard, is default configuration giving you any error ?. Anyway, check this links for detail information about Proguard

https://gist.github.com/albinmathew/c4436f8371c9c41461ab https://gist.github.com/Jackgris/c4a71328b1ae346cba04

Default configuration means you just put lines that is,

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

By this lines Gradle use default Proguard rules.

Dexguard (commercial version of Proguard)

If want to make more secure your code you can use Dexguard. Dexguard is a commercial version of Proguard, because dexguard comes with string encryption feature that makes code more secure.

like image 37
Aditya Avatar answered Oct 26 '25 22:10

Aditya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!