1+ // adapted from https://gist.github.com/CedricGatay/4e21ce855bd2562f9257
2+ // which was adapted from https://gist.github.com/dmarcato/d7c91b94214acd936e42
3+
4+ def toCamelCase (String string ) {
5+ String result = " "
6+ string. findAll(" [^\\ W]+" ) { String word ->
7+ result + = word. capitalize()
8+ }
9+ return result
10+ }
11+
12+ afterEvaluate { project ->
13+ Configuration runtimeConfiguration = project. configurations. getByName(' compile' )
14+ println runtimeConfiguration
15+ ResolutionResult resolution = runtimeConfiguration. incoming. resolutionResult
16+ // Forces resolve of configuration
17+ ModuleVersionIdentifier module = resolution. getAllComponents(). find {
18+ it. moduleVersion. name. equals(" play-services" )
19+ }. moduleVersion
20+
21+
22+ def playServicesLibName = toCamelCase(" ${ module.group} ${ module.name} ${ module.version} " )
23+ String prepareTaskName = " prepare${ playServicesLibName} Library"
24+ File playServiceRootFolder = project. tasks. find { it. name. equals(prepareTaskName) }. explodedDir
25+
26+
27+ def tmpDir = new File (project. buildDir, ' intermediates/tmp' )
28+ tmpDir. mkdirs()
29+ def libFile = new File (tmpDir, " ${ playServicesLibName} .marker" )
30+
31+ def strippedClassFileName = " ${ playServicesLibName} .jar"
32+ def classesStrippedJar = new File (tmpDir, strippedClassFileName)
33+
34+ def packageToExclude = [
35+ " com/google/ads/**" ,
36+ // "com/google/android/gms/actions/**",
37+ " com/google/android/gms/ads/**" ,
38+ " com/google/android/gms/analytics/**" ,
39+ // "com/google/android/gms/appindexing/**",
40+ // "com/google/android/gms/appstate/**",
41+ // "com/google/android/gms/auth/**",
42+ // "com/google/android/gms/cast/**",
43+ " com/google/android/gms/drive/**" ,
44+ " com/google/android/gms/fitness/**" ,
45+ " com/google/android/gms/games/**" ,
46+ // "com/google/android/gms/gcm/**",
47+ // "com/google/android/gms/identity/**",
48+ " com/google/android/gms/location/**" ,
49+ " com/google/android/gms/maps/**" ,
50+ " com/google/android/gms/panorama/**" ,
51+ " com/google/android/gms/plus/**" ,
52+ // "com/google/android/gms/security/**",
53+ // "com/google/android/gms/tagmanager/**",
54+ " com/google/android/gms/wallet/**" ,
55+ " com/google/android/gms/wearable/**"
56+ ]
57+
58+ Task stripPlayServices = project. tasks. create(name : ' stripPlayServices' , group : " Strip" ) {
59+ inputs. files new File (playServiceRootFolder, " classes.jar" )
60+ outputs. dir playServiceRootFolder
61+ description ' Strip useless packages from Google Play Services library to avoid reaching dex limit'
62+
63+ doLast {
64+ def packageExcludesAsString = packageToExclude. join(" ," )
65+ if (libFile. exists()
66+ && libFile. text == packageExcludesAsString
67+ && classesStrippedJar. exists()){
68+ println " Play services already stripped"
69+ copy {
70+ from(file(classesStrippedJar))
71+ into(file(playServiceRootFolder))
72+ rename { fileName ->
73+ fileName = " classes.jar"
74+ }
75+ }
76+ }else {
77+ copy {
78+ from(file(new File (playServiceRootFolder, " classes.jar" )))
79+ into(file(playServiceRootFolder))
80+ rename { fileName ->
81+ fileName = " classes_orig.jar"
82+ }
83+ }
84+ tasks. create(name : " stripPlayServices" + module. version, type : Jar ) {
85+ destinationDir = playServiceRootFolder
86+ archiveName = " classes.jar"
87+ from(zipTree(new File (playServiceRootFolder, " classes_orig.jar" ))) {
88+ exclude packageToExclude
89+ }
90+ }. execute()
91+ delete file(new File (playServiceRootFolder, " classes_orig.jar" ))
92+ copy {
93+ from(file(new File (playServiceRootFolder, " classes.jar" )))
94+ into(file(tmpDir))
95+ rename { fileName ->
96+ fileName = strippedClassFileName
97+ }
98+ }
99+ libFile. text = packageExcludesAsString
100+ }
101+ }
102+ }
103+
104+ project. tasks. findAll {
105+ it. name. startsWith(' prepare' ) && it. name. endsWith(' Dependencies' )
106+ }. each { Task task ->
107+ task. dependsOn stripPlayServices
108+ }
109+ }
0 commit comments