Home » Android » java – Setup proguard rules in android, can it just encrypt the code?

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

Posted by: admin June 15, 2020 Leave a comment

Questions:

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:[email protected]'
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.

How to&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(...);
}

Answer:

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).

Answer:

If you want to obfuscate without minimizing, you can use the -dontshrink option. As with the argument @Reaz Murshed made in his answer, it is worth your time to correctly configure proguard for each library. It can be annoying work sometimes, but shirnking provides tangible benefits in smaller APKs and lower method counts.