Skip to content

Instantly share code, notes, and snippets.

@Takhion
Forked from dmarcato/strip_play_services.gradle
Last active May 28, 2020 11:13
Show Gist options
  • Save Takhion/10a37046b9e6d259bb31 to your computer and use it in GitHub Desktop.
Save Takhion/10a37046b9e6d259bb31 to your computer and use it in GitHub Desktop.
Gradle task to strip unused packages on Google Play Services library, so you don't have to use necessarily Proguard or MultiDex
apply from: 'strip_play_services.gradle'
-dontwarn com.google.android.gms.**
// taken from https://gist.github.com/Takhion/10a37046b9e6d259bb31
def toCamelCase(String string) {
String result = ""
string.findAll("[^\\W]+") { String word ->
result += word.capitalize()
}
return result
}
afterEvaluate { project ->
Configuration runtimeConfiguration = project.configurations.getByName('compile')
ResolutionResult resolution = runtimeConfiguration.incoming.resolutionResult
// Forces resolve of configuration
ModuleVersionIdentifier module = resolution.getAllComponents().find { it.moduleVersion.name.equals("play-services") }.moduleVersion
String prepareTaskName = "prepare${toCamelCase("${module.group} ${module.name} ${module.version}")}Library"
Task prepareTask = project.tasks.findByName(prepareTaskName)
File playServiceRootFolder = prepareTask.explodedDir
// Add the stripping to the existing task that extracts the AAR containing the original classes.jar
prepareTask.doLast {
// First create a copy of the GMS classes.jar
copy {
from(file(new File(playServiceRootFolder, "classes.jar")))
into(file(playServiceRootFolder))
rename { fileName ->
fileName = "classes_orig.jar"
}
}
// Then create a new .jar file containing everything from the first one except the stripped packages
tasks.create(name: "stripPlayServices" + module.version, type: Jar) {
destinationDir = playServiceRootFolder
archiveName = "classes.jar"
from(zipTree(new File(playServiceRootFolder, "classes_orig.jar"))) {
exclude "com/google/ads/**"
exclude "com/google/android/gms/actions/**"
exclude "com/google/android/gms/ads/**"
exclude "com/google/android/gms/analytics/**"
exclude "com/google/android/gms/appindexing/**"
exclude "com/google/android/gms/appstate/**"
exclude "com/google/android/gms/auth/**"
exclude "com/google/android/gms/cast/**"
exclude "com/google/android/gms/drive/**"
exclude "com/google/android/gms/fitness/**"
exclude "com/google/android/gms/games/**"
exclude "com/google/android/gms/gcm/**"
exclude "com/google/android/gms/identity/**"
exclude "com/google/android/gms/location/**"
exclude "com/google/android/gms/maps/**"
exclude "com/google/android/gms/panorama/**"
exclude "com/google/android/gms/plus/**"
exclude "com/google/android/gms/security/**"
exclude "com/google/android/gms/tagmanager/**"
exclude "com/google/android/gms/wallet/**"
exclude "com/google/android/gms/wearable/**"
}
}.execute()
delete file(new File(playServiceRootFolder, "classes_orig.jar"))
}
}
@twobitlabs
Copy link

Nice work, much appreciated, worked great for us!

@hoavt-54
Copy link

Hi Takhion, thank you for your script.

I got this error: Error:Cannot get property 'moduleVersion' on null object
on the line "ModuleVersionIdentifier module = resolution.getAllComponents().find { it.moduleVersion.name.equals("play-services") }.moduleVersion"

I'm using google service as library which I imported in Android Studio as a module
Could you help me on this

@hoavt-54
Copy link

Just wanna clarify my problem which I solved some days ago. My project was migrated from Eclipse to Studio, I'm not sure it's reason or not but the jar file is located in libs/google-play-services.jar and the jar file named: google-play-services.jar
Hope this help somebody else

@ericntd-legacy
Copy link

Thanks for sharing but I ran into an issue, could you help?

I assume that "minifyEnabled true" in the Gradle build script should be used?

I got the following error:
Error:Gradle: Execution failed for task ':waach:proguardDevDebug'.

java.io.IOException: Please correct the above warnings first.

When I ran "./gradlew :waach:proguardDevDebug" I got a lot of warnings:
Warning: butterknife.internal.ButterKnifeProcessor: can't find superclass or interface javax.annotation.processing.AbstractProcessor
...
Warning: com.actionbarsherlock.internal.ActionBarSherlockCompat: can't find referenced class com.actionbarsherlock.BuildConfig
Warning: com.google.common.cache.Striped64: can't find referenced class sun.misc.Unsafe
...
Warning: com.google.common.primitives.UnsignedBytes$LexicographicalComparatorHolder$UnsafeComparator: can't find referenced class sun.misc.Unsafe
...

@Takhion
Copy link
Author

Takhion commented Jan 15, 2015

@fuzzybee that's a Proguard specific issue, you should add things like:

-dontwarn com.actionbarsherlock.internal.ActionBarSherlockCompat

@positivelymade
Copy link

It worked for me from 5.18 MB to 4.72MB

@mineshpatel1511
Copy link

Hi What gradle API is required for this? I am not able to import ResolutionResult, ModuleVersionIdentifier and Task? I am not gradle 1.0.0. Please help.

@3c71
Copy link

3c71 commented May 28, 2020

Hi Takhion, thank you for your script.

I got this error: Error:Cannot get property 'moduleVersion' on null object
on the line "ModuleVersionIdentifier module = resolution.getAllComponents().find { it.moduleVersion.name.equals("play-services") }.moduleVersion"

I'm using google service as library which I imported in Android Studio as a module
Could you help me on this

Same issue here, few years later. Did you find a solution to this? I assumed it refers to project, but replacing doesn't work because property moduleVersion doesn't exist in project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment