Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup proguard rules in android, can it just encrypt the code?

I have several libraries used in my application and after the minifyEnabled is true, it can not generate the APK. After some studies , I found the rules and add it to the .pro file one by one.

Here is the library list

compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.slider:library:1.1.5@aar'
compile 'org.apache.httpcomponents:httpmime:4.3.5'//Volley
compile 'org.apache.httpcomponents:httpcore:4.2.4'//Volley
compile 'com.mcxiaoke.volley:library:1.0.17'//Volley
compile 'com.github.bumptech.glide:glide:3.6.1'//Gradle
compile 'com.baoyz.swipemenulistview:library:1.3.0'//Swipe Menu li stview
compile 'org.lucasr.twowayview:twowayview:0.1.4' //horizontal listview
compile 'com.android.support:recyclerview-v7:+'

For the JAR, it is PayPalAndroidSDK-2.9.11.jar

In short, I wonder is it possible not to add the rule one by one for the libraries, as some library seems does not mention about how to setup proguard for them? Can it just encrypt instead of optimize the code and stripe out some useful code?

Thanks a lot.

like image 830
user782104 Avatar asked Oct 14 '16 01:10

user782104


People also ask

Where do you put ProGuard rules?

When you create a new project or module using Android Studio, the IDE creates a <module-dir>/proguard-rules.pro file for you to include your own rules. You can also include additional rules from other files by adding them to the proguardFiles property in your module's build.gradle file.


2 Answers

I wonder is it possible not to add the rule one by one for the libraries, as some library seems does not mention about how to setup proguard for them?

Yes its possible, not to add rules one by one for each library you used in your project. Try adding the following in your proguard-rules.pro file.

-keep class !com.example.myproject.** { *; }

The idea is simply putting a negator with the regular expression you use in your proguard-rules.pro.

But what's the point of using proguard if you're not obfuscating your code. You might want to keep some of classes in your project unchanged after obfuscation. You just need to keep them like the other libraries. For example -

// I want to keep the classes in the `Models` package to remain unchanged
-keep class com.example.myproject.Models.** { *; }
-keepclassmembers class com.example.myproject.Model.** { *; }

Anyway, its not very hard to add the rules one by one as you'll have more control while obfuscating. Here's my proguard-rules.pro. You can have a look at it.

-useuniqueclassmembernames
-allowaccessmodification
-keep class com.google.** { *; }
-keep class com.journeyapps.** { *; }
-keep class com.makeramen.** { *; }
-keep class com.github.** { *; }
-keep class org.apache.** { *; }
-keep class com.flipboard.** { *; }
-keep class com.android.** { *; }
-keep class com.mikepenz.** { *; }
-keep class junit.** { *; }
-keep class org.mockito.** { *; }
-keep class android.support.v7.widget.SearchView { *; }
-keep class com.example.myproject.Models.** { *; }
-keepclassmembers class com.example.myproject.Model.** { *; }

-keepattributes Signature
-keepattributes *Annotation*

-dontwarn com.google.**
-dontwarn org.apache.**
-dontwarn android.support.**
-dontwarn org.junit.**
-dontwarn org.mockito.**
-dontwarn com.makeramen.**

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** w(...);
    public static *** i(...);
    public static *** e(...);
}
like image 139
Reaz Murshed Avatar answered Oct 25 '22 07:10

Reaz Murshed


You can just don't add any proguard rules, but if some part of the app or its libraries uses reflection, Generics, Dependency Injection or any form of soft/indirect references, the app will crash. So the best option is to build the app once, test all of it, and check if something goes kaboom. If it does, add the proguard references for each library. If the library hasn't any recommended configuration, check the afflicted classes from the logcat in the dictionary (outputs/proguard).

like image 31
Fco P. Avatar answered Oct 25 '22 06:10

Fco P.