Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proguard config for facebook sdk. Strip everything except analytics

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

like image 522
dmarin Avatar asked Jul 17 '17 08:07

dmarin


1 Answers

When it comes down to huge size, it is mostly about the resources rather than worrying about minimising java code itself. So you could actually try out a few things mentioned below.

  1. Proguard works on the Java code. Unfortunately, it doesn’t work on the resources folder. As a consequence, if an image my_image in res/drawable is not used, Proguard only strips it’s reference in the R class but keeps the associated image in place.
  2. Lint is a static code analyzer that helps you to detect all unused resources with a simple call to ./gradlew lint. It generates an HTML-report and gives you the exhaustive list of resources that look unused under the “UnusedResources: Unused resources” section. It is safe to remove these resources as long as you don’t access them through reflection in your code.

However Lint can tell you where are unused resources, but with fb sdk it will be hard to delete the resources as it comes from maven repository.

Minimising Resource Configuration (build.gradle) For eg Fb sdk provides support all languages which you might not need, or all folders images like mdpi which may not be useful for you.

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

If all of this doesn't work that means either native code is bloating your apk, where application binary interface split might help reducing your apk size.

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 could be done here as I opened facebook sdk gradle file ... it has few transitive dependencies, which is redundant and might conflict with your support version so you could either import the same in 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 could be removed from final fat jar as you might be already using the support dependencies in your project that too different or conflicting ones .. so you could ideally exclude transitive dependencies based on your requirements something like below

compile ('com.facebook.android:facebook-android-sdk:4.+') {
        exclude group: 'com.android.support' //by group
    }
like image 112
Anukalp Katyal Avatar answered Oct 19 '22 17:10

Anukalp Katyal