- Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathgenerate_proguard.gradle
155 lines (139 loc) · 6.02 KB
/
generate_proguard.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Generates a proguard file for the given C++ library.
// Args:
// library: The C++ library to generate the proguard from.
// outCppPro: The path to the output proguard file.
defgenerateCppProguard(Filelibrary, StringoutCppPro) {
def stringsOutStream =newByteArrayOutputStream()
exec {
executable 'strings'
args "$library"
standardOutput = stringsOutStream
}
List<String> proguardLines =newArrayList<String>()
for (Stringline : stringsOutStream.toString().split("[\\r\\n]+")) {
// The classes to keep are prefixed with %PG%, so find those.
if (line.startsWith("%PG%")) {
proguardLines.add(
"-keep,includedescriptorclasses public class "+
// Remove the %PG%, and replace any slashes with dots.
line.replaceFirst("%PG%", "").replace('/', '.') +
" { *; }");
}
}
newFile("$outCppPro").text =String.join('\n', proguardLines.toSorted())
}
// Combines the given set of proguard files into a single one.
// Args:
// proguardSet: The set of proguard files to use.
// outputProguard: The path to the output proguard file.
defgenerateFinalProguard(Set<File>proguardSet, StringoutputProguard) {
Set<String> proguardLineSet =newHashSet<String>()
for (Filepro : proguardSet) {
for (Stringline : pro.text.split("[\\r\\n]+")) {
// Ignore commented out lines.
if (!line.startsWith("#")) {
proguardLineSet.add(line)
}
}
}
// Sort the lines, and then write it out to the file.
String proguardLines =String.join('\n', proguardLineSet.toSorted())
newFile("$outputProguard").text = proguardLines
}
// Adds the logic to generate a proguard file from the static library,
// and then combining the proguard files into a single file.
defdefineGenerateProguardFile(Stringsubproject, StringbuildType,
Action<File>callback) {
Task t = tasks.getByName("externalNativeBuild$buildType").with {
String outputProguard ="$buildDir/$buildType/${subproject}.pro"
outputs.file "$outputProguard"
doLast {
String nativeBuildDir =
project.android.externalNativeBuild.cmake.buildStagingDirectory
if (nativeBuildDir ==null|| nativeBuildDir.isEmpty()) {
nativeBuildDir = file('.cxx').absolutePath
}
// Find the static library that was built. Note that there are multiple
// versions, based on the architecture, but we just need any one of them.
Set<File> librarySet = fileTree("$nativeBuildDir")
.matching({ include "**/*firebase_${subproject}*" }).getFiles()
String cppProguard ="$buildDir/cpp_${subproject}.pro"
while (!librarySet.isEmpty()) {
File lib = librarySet.iterator().next()
librarySet.remove(lib)
if (lib.name.endsWith(".a")) {
generateCppProguard(lib, cppProguard)
}
}
// Combine the proguard files into a single file.
File proguardFile =newFile(outputProguard)
if (proguardFile.exists()) {
proguardFile.delete()
}
// If the Firebase C++ SDK directory is defined, so that, otherwise use
// the project directory.
String proguardSearchDir
if (project.hasProperty('firebase_cpp_sdk_dir')) {
proguardSearchDir ="${firebase_cpp_sdk_dir}/$subproject"
} else {
proguardSearchDir ="$projectDir"
}
FileTree proguardFileTree = fileTree("$proguardSearchDir")
.matching({ include "**/*.pro" })
// The app directory contains invites, which we don't want to include in
// the app proguard file.
if ("${subproject}"=="app") {
proguardFileTree = proguardFileTree.matching({ exclude "*invites*" })
}
// We don't want to include proguard files from any external libraries.
proguardFileTree =
proguardFileTree.matching({ exclude "**/external/src/**" })
Set<File> proguardSet = proguardFileTree.getFiles()
// On the other hand, if building dynamic_links or invites, we want to
// include the invites proguard file contained in app.
if ("${subproject}"=="dynamic_links"||"${subproject}"=="invites") {
FileTree invitesResourceTree = fileTree("$proguardSearchDir/../app")
.matching({ include "**/invites_resources.pro" })
proguardSet.addAll(invitesResourceTree.getFiles())
}
// The proguard file generated from the C++ library might not be in the
// search directory, so add it if it is missing.
File cppProguardFile =newFile(cppProguard)
if (!proguardSet.contains(cppProguardFile) && cppProguardFile.exists()) {
proguardSet.add(cppProguardFile)
}
generateFinalProguard(proguardSet, outputProguard)
if (callback !=null&& proguardFile.exists()) {
callback.execute(proguardFile)
}
}
}
}
// Set up the tasks to generate the proguard file for the subproject
defgenerateProguardFile(Stringsubproject) {
defineGenerateProguardFile(subproject, 'Debug', null)
defineGenerateProguardFile(subproject, 'Release', null)
}
// Set up the tasks to generate the proguard file for the subproject,
// passing along a callback to call when finished
defgenerateProguardFileWithCallback(Stringsubproject, Action<File>callback) {
defineGenerateProguardFile(subproject, 'Debug', callback)
defineGenerateProguardFile(subproject, 'Release', callback)
}
ext {
generateProguardFile =this.&generateProguardFile
generateProguardFileWithCallback =this.&generateProguardFileWithCallback
}