A lot has been written about the monolithic nature of the Google Play Services and why it should be split into more libraries. For now the workaround to keep your APK small is t
Thanks to Petr Nalevka's insightful answer I have come up with the following solution (a.k.a. ugly hack).
This code removes all PNG drawables under hdpi, xhdpi, xxhdpi, and so on, keeping only the drawables under mdpi (the ones with the smallest file size). Drawables are only removed if they have a sibling under mdpi, that is if they can be safely removed. Google Play Services should remain fully functional.
A task gets registered for each application variant, so this code works with multiple product flavors.
applicationVariants.all { variant ->
def variantCamelName = variant.name.substring(0, 1).toUpperCase() + variant.name.substring(1)
def stripResourcesTask = task("strip${variantCamelName}Resources") {
doFirst {
def resDir = "$buildDir/intermediates/res/merged/$variant.dirName"
def gpsDir = "$buildDir/intermediates/exploded-aar/com.google.android.gms"
fileTree(dir: "$gpsDir", include: "**/drawable-*/*.png").each { resFile ->
if (!resFile.parentFile.name.endsWith("-mdpi")) {
def resName = resFile.name
def resDirName = resFile.parentFile.name
def mdpiFile = "$resFile.parentFile.parentFile/drawable-mdpi/$resName"
if (file(mdpiFile).file) {
def files = fileTree(dir: "$resDir", include: "**/${resDirName}/${resName}")
// files.each { f -> println "Deleting $f..." }
delete files
}
}
}
}
}
tasks["merge${variantCamelName}Resources"].finalizedBy stripResourcesTask
}
Tested with Android Studio 1.3.0, Gradle plugin version 1.3.0, build tools version 22.0.1 and GPS version 7.5.0.
I'm a bit of a Groovy noob, so this is probably not the most elegant solution.