-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.gradle
More file actions
190 lines (170 loc) · 4.81 KB
/
Copy pathutils.gradle
File metadata and controls
190 lines (170 loc) · 4.81 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
def copyFile(String src, String des) {
// 拷贝文件
def s = new File(src)
if (!s.exists()) {
return
}
def d = new File(des)
if (d.exists()) {
rmFile(des)
} else {
d.createNewFile()
}
d.withDataOutputStream { os ->
s.withDataInputStream { is ->
os << is
}
}
}
def isEmpty(String src) {
if (src == null || src.length() == 0) {
return true
}
return false
}
def rmFile(String src) {
// 删除文件
def file = new File(src)
if (!file.exists()) {
return
}
if (file.isDirectory()) {
file.deleteDir()
} else {
file.delete()
}
}
def cleanOutput(boolean output = true) {
// 清除输出
if (output) {
println "cleanOutput..."
}
}
def copyOutput(boolean output = true) {
// 拷贝输出
println("copyOutput...")
if (output) {
copyOutputAAR(output)
copyOutputAPK(output)
copyOutputJAR(output)
copyOutputSO(output)
}
}
def copy(String product, String des,
final boolean filter = false, final String filterSuffix = null) {
if (product == null || product.isEmpty()) {
println("product==null")
return false
}
def s = new File(product)
if (!s.exists()) {
println("product not exists")
return false;
}
def d = new File(des)
if (d.exists() && !d.isDirectory()) {
return false
}
if (!d.exists()) {
d.mkdirs()
}
println("product:" + product + ",des:" + des)
s.listFiles(new FilenameFilter() {
@Override
boolean accept(File dir, String name) {
if (filter) {
return !name.endsWith(filterSuffix)
}
return true
}
}).each {
def path = it.absolutePath;
def desPath = d.absolutePath;
def name = it.name
println("path:" + path + ",desPath:" + desPath + ",name:" + name)
if (it.isDirectory()) {
copy(path, desPath + "/" + name)
} else {
def index = name.lastIndexOf(".");
String versionName = project.versionName
if (isEmpty(versionName)) {
versionName = getVersionName();
}
name = name.substring(0, index).concat("-").concat(versionName).concat(name.substring(index))
copyFile(path, desPath + "/" + name)
}
}
}
def copyOutputAAR(boolean bAAR = true) {
if (bAAR) {
def destination = gradle.aar + "/" + project.name;
def product = new File(getBuildDir(), "outputs/aar").getAbsolutePath()
copy(product, destination)
}
}
def copyOutputAPK(boolean bAPK = true) {
if (bAPK) {
def destination = gradle.apps + "/" + project.name;
def product = new File(getBuildDir(), "outputs/apk").getAbsolutePath()
copy(product, destination)
}
}
def copyOutputJAR(boolean bJar = true) {
if (bJar) {
def destination = gradle.javaLib + "/" + project.name;
def product = new File(getBuildDir(), "libs").getAbsolutePath()
copy(product, destination)
}
}
def copyOutputSO(boolean bSO = true) {
if (bSO) {
def destination = gradle.nativeLib + "/" + project.name;
def product = new File(project.getProjectDir(), "libs").getAbsolutePath()
copy(product, destination, true, ".jar")
}
}
def getVersionName() {
// 获取版本名
def androidManifest = new XmlSlurper().parse(project.projectDir.getAbsolutePath() + "/src/main/AndroidManifest.xml")
def versionName = androidManifest.@'android:versionName';
println versionName
return versionName
}
def getPackageName() {
// 获取版本名
def androidManifest = new XmlSlurper().parse(project.projectDir.getAbsolutePath() + "/src/main/AndroidManifest.xml")
def packageName = androidManifest.@'package';
println packageName
return packageName
}
def getOSName() {
def osName = System.getProperty("os.name", "Mac")
println("os:" + osName)
return osName
}
def disableDebugBuild() {
// 注意增量编译导致以前文件不会删除
def tasks = project.tasks.findAll { task ->
task.name.contains("debug") || task.name.contains("Debug")
}
tasks.each {
println "disable task debug: $it.name"
it.enabled false
}
}
ext {
copyFile = this.©File
rmFile = this.&rmFile
cleanOutput = this.&cleanOutput
copyOutput = this.©Output
copy = this.©
copyOutputAAR = this.©OutputAAR
copyOutputAPK = this.©OutputAPK
copyOutputJAR = this.©OutputJAR
copyOutputSO = this.©OutputSO
getVersionName = this.&getVersionName
getPackageName = this.&getPackageName
disableDebugBuild = this.&disableDebugBuild
isEmpty = this.&isEmpty
getOSName = this.&getOSName
}