Set proguard rules in android, is it possible to simply encrypt the code?

I have several libraries used in my application, and after minifyEnabled is true , it cannot generate an APK. After some research, I found the rules and added them to the .pro file one by one.

Here is a list of libraries

 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 JAR, this is PayPalAndroidSDK-2.9.11.jar

In short, I wonder if itโ€™s possible not to add a rule one at a time for libraries, since some library doesnโ€™t mention how to configure proguard for them? Can it just encrypt rather than optimize the code and cut out some useful code?

Many thanks.

+6
source share
3 answers

I wonder if itโ€™s possible not to add the rule one after another for the library, as some libraries do not seem to mention how to configure proguard for them?

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

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

The idea is to simply put a proguard-rules.pro with the regular expression that you use in your proguard-rules.pro .

But what's the point of using proguard if you don't mess up your code. You might want to keep some classes in your project unchanged after obfuscation. You just need to keep them like other libraries. For instance -

 // 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.** { *; } 

In any case, itโ€™s not very difficult to add rules one by one, since you will have more control when confusing. Here is my proguard-rules.pro . You can take 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(...); } 
+8
source

You can simply not add any proguard rules, but if some part of the application or its library uses reflection, Generics, Injection Dependency Injection, or any form of soft / indirect links, the application will fail. Thus, the best option is to create the application once, check it all and verify that something goes kaboom. If so, add proguard links for each library. If the library does not have any recommended configuration, check the vulnerable classes from the logarithm in the dictionary (outputs / proguard).

+3
source

If you want to obfuscate without minimizing, you can use the -dontshrink option. As with the @Reaz Murshed argument made in his answer, it's worth the time to properly configure proguard for each library. This can be annoying at times, but shirnking offers tangible benefits in small APKs and lower methods.

+1
source

All Articles