Proguard configuration for facebook sdk. Share everything except analytics

I want to use facebook sdk only for analytics, is there an optimized proguard configuration that I could use to highlight the rest?

+8
android facebook proguard
source share
4 answers

My question was wrong, there is no way to remove code in proguard, maybe this can be done with exceptions in gradle, but I do not think this is doable using only proguard

In any case, facebook finally modeled its sdk, so for people like me who are inspected only by analytics, facebook-core is the only problem we need to import

0
source share

When it comes to enormous proportions, it is mainly about resources, not about minimizing java code. Therefore, you can try a few things mentioned below.

  • Proguard is working on Java code. Unfortunately, it does not work in the resources folder. As a result, if the my_image image is not used in res / drawable, Proguard only shares its reference in the R class, but keeps the linked image in place.
  • Lint is a static code analyzer that helps you discover all unused resources with a simple call. / gradlew lint. It creates an HTML report and provides an exhaustive list of resources that look unused in the "UnusedResources: Unused resources" section. It’s safe to delete these resources until you access them through reflection in your code.

However, Lint can tell you where the unused resources are located, but using fb sdk it will be difficult to delete the resources, since it comes from the maven repository.

Minimizing resource configurations (build.gradle) For example, Fb sdk provides support for all languages ​​that you may not need, or for all folder images, such as mdpi, that may not be useful to you.

defaultConfig { resConfigs "en", "de", "fr", "it" resConfigs "nodpi", "hdpi", "xhdpi", "xxhdpi", "xxxhdpi" } 

If all this does not work, it means that either your own code inflates your apk, where splitting the application binary interface can help reduce the size of your apk.

ABI Split: -

  splits { density { enable true reset() include "ldpi", "mdpi" } abi { // Enables building multiple APKs per ABI. enable true // By default all ABIs are included, so use reset() and include to specify that we only // want APKs for x86, armeabi-v7a, and mips. // Resets the list of ABIs that Gradle should create APKs for to none. reset() // Specifies a list of ABIs that Gradle should create APKs for. include "x86", "armeabi-v7a", "mips" // Specifies that we do not want to also generate a universal APK that includes all ABIs. universalApk false } } 

I think something can be done here when I opened the facebook sdk gradle file ... it has several transitive dependencies, which is redundant and may conflict with your support version so that you can either import them into your files.

 dependencies { // Facebook Dependencies compile 'com.android.support:support-v4:25.3.1' compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' compile 'com.android.support:customtabs:25.3.1'} 

It can be removed from the final thick jar, as you can already use support dependencies in your project that are too different or conflicting. That way, you could ideally exclude transitive dependencies based on your requirements, as shown below.

 compile ('com.facebook.android:facebook-android-sdk:4.+') { exclude group: 'com.android.support' //by group } 
+1
source share

The configuration of ProGuard has been updated , which is included in the SDK for Facebook , and therefore it will correctly delete all classes that are not used by your application (for example, all are not in Google Analytics).

... with the exception of anything Serializable that Facebook believes is unsafe to delete ...

Now it is very similar to the old answers:

 -keepclassmembers class * implements java.io.Serializable { private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve(); } 

( May 20, 2016 )

This means that we no longer need our own ProGuard settings for Facebook . Gradle will automatically use the rules provided by the SDK.


Of course, this still requires your build.gradle configured to run ProGuard:

 minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-release.pro' 
-one
source share
 -keep class com.facebook.** { *; } -keepattributes Signature 

I use these 2 lines and work great

-one
source share

All Articles