-
Notifications
You must be signed in to change notification settings - Fork 33
/
build.gradle
483 lines (420 loc) · 22.2 KB
/
build.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven {
url 'https://maven.aliyun.com/repository/public'
}
maven {
url 'https://maven.aliyun.com/repository/central'
}
maven {
url 'https://maven.aliyun.com/repository/gradle-plugin'
}
maven {
url 'https://maven.aliyun.com/repository/google'
}
maven {
url 'https://maven.aliyun.com/repository/jcenter'
}
maven {
url 'https://dl.google.com/dl/android/maven2/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'io.github.lizhangqu:core-publish:1.4.0'
// classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.7"
}
configurations.all {
it.resolutionStrategy.cacheDynamicVersionsFor(5, 'minutes')
it.resolutionStrategy.cacheChangingModulesFor(0, 'seconds')
}
}
allprojects {
repositories {
maven {
url 'https://maven.aliyun.com/repository/public'
}
maven {
url 'https://maven.aliyun.com/repository/central'
}
maven {
url 'https://maven.aliyun.com/repository/gradle-plugin'
}
maven {
url 'https://maven.aliyun.com/repository/google'
}
maven {
url 'https://maven.aliyun.com/repository/jcenter'
}
maven {
url 'https://dl.google.com/dl/android/maven2/'
}
}
configurations.all {
it.resolutionStrategy.cacheDynamicVersionsFor(5, 'minutes')
it.resolutionStrategy.cacheChangingModulesFor(0, 'seconds')
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext {
global_compileSdkVersion = 28
global_buildToolsVersion = "28.0.3"
global_minSdkVersion = 16
global_targetSdkVersion = 28
global_javaVersion = JavaVersion.VERSION_1_7
}
void writeProperties(File file, String key, version) {
try {
// we use replace here as other ant tasks escape and modify the whole file
project.ant.replaceregexp(file: file, byline: true) {
regexp(pattern: "^(\\s*)$key((\\s*[=|:]\\s*)|(\\s+)).+\$")
substitution(expression: "\\1$key\\2$version")
}
} catch (Exception e) {
}
}
void releaseFiles(boolean fromSrc) {
File preBuiltDir = new File(project.rootDir, "prebuilt")
File apiDestDir = new File(project.rootDir, "cronet-api${File.separator}src${File.separator}main${File.separator}java")
File commonDestDir = new File(project.rootDir, "cronet-common${File.separator}src${File.separator}main${File.separator}java")
File nativeDestDir = new File(project.rootDir, "cronet-native${File.separator}src${File.separator}main${File.separator}java")
File platformDestDir = new File(project.rootDir, "cronet-platform${File.separator}src${File.separator}main${File.separator}java")
File processorDestDir = new File(project.rootDir, "cronet-processor${File.separator}src${File.separator}main${File.separator}java")
File apiJarDestFile = new File(project.rootDir, "cronet-api${File.separator}libs${File.separator}cronet_api.jar")
File commonJarDestFile = new File(project.rootDir, "cronet-common${File.separator}libs${File.separator}cronet_impl_common_java.jar")
File nativeJarDestFile = new File(project.rootDir, "cronet-native${File.separator}libs${File.separator}cronet_impl_native_java.jar")
File platformJarDestFile = new File(project.rootDir, "cronet-platform${File.separator}libs${File.separator}cronet_impl_platform_java.jar")
//==================================================java=========================================================
if (fromSrc) {
GFileUtils.deleteQuietly(apiJarDestFile)
GFileUtils.deleteQuietly(apiDestDir)
GFileUtils.mkdirs(apiDestDir)
File apiSrcFile = new File(preBuiltDir, "cronet_api-src.jar")
project.logger.error "releasePreBuilt ${apiSrcFile}"
project.zipTree(apiSrcFile).visit { def visit ->
File file = visit.getFile()
String path = visit.getRelativePath()
if (path.startsWith("android${File.separator}")
|| path.contains("${File.separator}library_loader${File.separator}")
|| path.contains("${File.separator}multidex${File.separator}")
|| path.contains("${File.separator}process_launcher${File.separator}")
|| path.endsWith("${File.separator}SysUtils.java")
|| path.contains("${File.separator}CachedMetrics")
|| path.contains("${File.separator}MemoryPressureMonitor")) {
project.logger.error "exclude ${path}"
return
}
GFileUtils.copyFile(file, new File(apiDestDir, path))
project.logger.error "copy ${path} to cronet-api"
}
GFileUtils.deleteQuietly(commonJarDestFile)
GFileUtils.deleteQuietly(commonDestDir)
GFileUtils.mkdirs(commonDestDir)
File commonSrcFile = new File(preBuiltDir, "cronet_impl_common_java-src.jar")
project.logger.error "releasePreBuilt ${commonSrcFile}"
project.zipTree(commonSrcFile).visit { def visit ->
File file = visit.getFile()
String path = visit.getRelativePath()
if (path.startsWith("android${File.separator}")
|| path.contains("${File.separator}library_loader${File.separator}")
|| path.contains("${File.separator}multidex${File.separator}")
|| path.contains("${File.separator}process_launcher${File.separator}")
|| path.endsWith("${File.separator}SysUtils.java")
|| path.contains("${File.separator}CachedMetrics")
|| path.contains("${File.separator}MemoryPressureMonitor")) {
project.logger.error "exclude ${path}"
return
}
GFileUtils.copyFile(file, new File(commonDestDir, path))
project.logger.error "copy ${path} to cronet-common"
}
GFileUtils.deleteQuietly(platformJarDestFile)
GFileUtils.deleteQuietly(platformDestDir)
GFileUtils.mkdirs(platformDestDir)
File platformSrcFile = new File(preBuiltDir, "cronet_impl_platform_java-src.jar")
project.logger.error "releasePreBuilt ${platformSrcFile}"
project.zipTree(platformSrcFile).visit { def visit ->
File file = visit.getFile()
String path = visit.getRelativePath()
if (path.startsWith("android${File.separator}")
|| path.contains("${File.separator}library_loader${File.separator}")
|| path.contains("${File.separator}multidex${File.separator}")
|| path.contains("${File.separator}process_launcher${File.separator}")
|| path.endsWith("${File.separator}SysUtils.java")
|| path.contains("${File.separator}CachedMetrics")
|| path.contains("${File.separator}MemoryPressureMonitor")) {
project.logger.error "exclude ${path}"
return
}
GFileUtils.copyFile(file, new File(platformDestDir, path))
project.logger.error "copy ${path} to cronet-platform"
}
GFileUtils.deleteQuietly(nativeJarDestFile)
GFileUtils.deleteQuietly(nativeDestDir)
GFileUtils.mkdirs(nativeDestDir)
File nativeSrcFile = new File(preBuiltDir, "cronet_impl_native_java-src.jar")
project.logger.error "releasePreBuilt ${nativeSrcFile}"
project.zipTree(nativeSrcFile).visit { def visit ->
File file = visit.getFile()
String path = visit.getRelativePath()
if (path.startsWith("android${File.separator}")
|| path.contains("${File.separator}library_loader${File.separator}")
|| path.contains("${File.separator}multidex${File.separator}")
|| path.contains("${File.separator}process_launcher${File.separator}")
|| path.endsWith("${File.separator}SysUtils.java")
|| path.contains("${File.separator}CachedMetrics")
|| path.contains("${File.separator}MemoryPressureMonitor")) {
project.logger.error "exclude ${path}"
return
}
if (path.contains("org${File.separator}chromium${File.separator}jni_generator")) {
project.logger.error "exclude annotationProcessor ${path}"
return
}
if (path.contains("J${File.separator}N")) {
project.logger.error "exclude annotationProcessor J.N ${path}"
return
}
GFileUtils.copyFile(file, new File(nativeDestDir, path))
project.logger.error "copy ${path} to cronet-native"
}
GFileUtils.deleteQuietly(processorDestDir)
GFileUtils.mkdirs(processorDestDir)
File processorSrcFile = new File(preBuiltDir, "cronet_impl_native_java-src.jar")
project.logger.error "releasePreBuilt ${processorSrcFile}"
project.zipTree(processorSrcFile).visit { def visit ->
File file = visit.getFile()
String path = visit.getRelativePath()
boolean shouldCopy = false
if (path.contains("org${File.separator}chromium${File.separator}jni_generator")) {
shouldCopy = true
}
if (path.contains("org${File.separator}chromium${File.separator}base${File.separator}annotations${File.separator}NativeMethods")) {
shouldCopy = true
}
if (path.contains("org${File.separator}chromium${File.separator}base${File.separator}JniStaticTestMocker")) {
shouldCopy = true
}
if (!shouldCopy) {
project.logger.error "exclude not annotationProcessor ${path}"
return
}
GFileUtils.copyFile(file, new File(processorDestDir, path))
project.logger.error "copy ${path} to cronet-processor"
}
} else {
GFileUtils.deleteQuietly(apiDestDir)
GFileUtils.deleteQuietly(commonDestDir)
GFileUtils.deleteQuietly(platformDestDir)
GFileUtils.deleteQuietly(nativeDestDir)
File apiJarSrcFile = new File(preBuiltDir, "cronet_api.jar")
File commonJarSrcFile = new File(preBuiltDir, "cronet_impl_common_java.jar")
File nativeJarSrcFile = new File(preBuiltDir, "cronet_impl_native_java.jar")
File platformJarSrcFile = new File(preBuiltDir, "cronet_impl_platform_java.jar")
GFileUtils.copyFile(apiJarSrcFile, apiJarDestFile)
project.logger.error "copy ${apiJarSrcFile} to ${apiJarDestFile}"
GFileUtils.copyFile(commonJarSrcFile, commonJarDestFile)
project.logger.error "copy ${commonJarSrcFile} to ${commonJarDestFile}"
GFileUtils.copyFile(nativeJarSrcFile, nativeJarDestFile)
project.logger.error "copy ${nativeJarSrcFile} to ${nativeJarDestFile}"
GFileUtils.copyFile(platformJarSrcFile, platformJarDestFile)
project.logger.error "copy ${platformJarSrcFile} to ${platformJarDestFile}"
}
//==================================================proguard=========================================================
File commonProguardSrcFile = new File(preBuiltDir, "cronet_impl_common_proguard.cfg")
File commonProguardDestFile = new File(project.rootDir, "cronet-common${File.separator}consumer-proguard-rules.pro")
GFileUtils.deleteQuietly(commonProguardDestFile)
GFileUtils.copyFile(commonProguardSrcFile, commonProguardDestFile)
project.logger.error "copy proguard from ${commonProguardSrcFile} to ${commonProguardDestFile}"
File nativeProguardSrcFile = new File(preBuiltDir, "cronet_impl_native_proguard.cfg")
File nativeProguardDestFile = new File(project.rootDir, "cronet-native${File.separator}consumer-proguard-rules.pro")
GFileUtils.deleteQuietly(nativeProguardDestFile)
GFileUtils.copyFile(nativeProguardSrcFile, nativeProguardDestFile)
project.logger.error "copy proguard from ${nativeProguardSrcFile} to ${nativeProguardDestFile}"
File platformProguardSrcFile = new File(preBuiltDir, "cronet_impl_platform_proguard.cfg")
File platformProguardDestFile = new File(project.rootDir, "cronet-platform${File.separator}consumer-proguard-rules.pro")
GFileUtils.deleteQuietly(platformProguardDestFile)
GFileUtils.copyFile(platformProguardSrcFile, platformProguardDestFile)
project.logger.error "copy proguard from ${platformProguardSrcFile} to ${platformProguardDestFile}"
//==================================================res=========================================================
File apiResourcesSrcFile = new File(preBuiltDir, "res")
File apiResourcesDestFile = new File(project.rootDir, "cronet-api${File.separator}src${File.separator}main${File.separator}res")
GFileUtils.deleteQuietly(apiResourcesDestFile)
GFileUtils.copyDirectory(apiResourcesSrcFile, apiResourcesDestFile)
project.logger.error "copy res from ${apiResourcesSrcFile} to ${apiResourcesDestFile}"
//==================================================jniLibs=========================================================
File nativeLibsSrcFile = new File(preBuiltDir, "libs")
File nativeLibsDestFile = new File(project.rootDir, "cronet-so${File.separator}src${File.separator}main${File.separator}jniLibs")
GFileUtils.deleteQuietly(nativeLibsDestFile)
GFileUtils.copyDirectory(nativeLibsSrcFile, nativeLibsDestFile)
project.logger.error "copy libs from ${nativeLibsSrcFile} to ${nativeLibsDestFile}"
if (project.hasProperty("rewriteArmeabi") && project.getProperties().get("rewriteArmeabi").toBoolean()) {
File nativeLibsArmeabiDestFile = new File(project.rootDir, "cronet-native${File.separator}src${File.separator}main${File.separator}jniLibs${File.separator}armeabi")
File nativeLibsArmeabiV7ASrcFile = new File(project.rootDir, "cronet-native${File.separator}src${File.separator}main${File.separator}jniLibs${File.separator}armeabi-v7a")
GFileUtils.deleteQuietly(nativeLibsArmeabiDestFile)
GFileUtils.copyDirectory(nativeLibsArmeabiV7ASrcFile, nativeLibsArmeabiDestFile)
project.logger.error "rewrite armeabi with armeabi-v7a"
}
uploadChromiumSo()
//==================================================version=========================================================
File versionSrcFile = new File(preBuiltDir, "VERSION")
Properties versionProperties = new Properties()
versionProperties.load(versionSrcFile.newDataInputStream())
String major = versionProperties.getProperty("MAJOR")
String minor = versionProperties.getProperty("MINOR")
String build = versionProperties.getProperty("BUILD")
String patch = versionProperties.getProperty("PATCH")
String cronetVersion = "${major}.${minor}.${build}.${patch}-SNAPSHOT"
project.logger.error "version ${cronetVersion}"
File apiPropertiesFile = new File(project.rootDir, "cronet-api${File.separator}gradle.properties")
File commonPropertiesFile = new File(project.rootDir, "cronet-common${File.separator}gradle.properties")
File nativePropertiesFile = new File(project.rootDir, "cronet-native${File.separator}gradle.properties")
File platformPropertiesFile = new File(project.rootDir, "cronet-platform${File.separator}gradle.properties")
File processorPropertiesFile = new File(project.rootDir, "cronet-processor${File.separator}gradle.properties")
writeProperties(apiPropertiesFile, "version", cronetVersion)
writeProperties(commonPropertiesFile, "version", cronetVersion)
writeProperties(nativePropertiesFile, "version", cronetVersion)
writeProperties(platformPropertiesFile, "version", cronetVersion)
writeProperties(processorPropertiesFile, "version", cronetVersion)
}
void uploadChromiumSo() {
File nativeLibsSrcFile = new File(project.rootDir, "cronet-so${File.separator}src${File.separator}main${File.separator}jniLibs")
Map<String, Map<String, String>> jsonMap = new HashMap<>()
//noinspection UnnecessaryQualifiedReference
nativeLibsSrcFile.eachFileRecurse(groovy.io.FileType.FILES) { File file ->
if (!file.name.endsWith(".so")) {
return;
}
String fileMd5 = getMD5(file)
Map<String, String> result = uploadFile(file)
project.logger.error "${file.getParentFile().getName()} ${result}"
if (result == null || !result.get("md5").equalsIgnoreCase(fileMd5)) {
throw new Exception("文件上传失败")
}
jsonMap.put(file.getParentFile().getName(), result)
}
//noinspection UnnecessaryQualifiedReference
String json = new groovy.json.JsonOutput().toJson(jsonMap)
project.logger.error "json ${json}"
File nativeDestDir = new File(project.rootDir, "cronet-native")
File configFile = new File(nativeDestDir, "remote.json")
//noinspection UnnecessaryQualifiedReference
org.gradle.util.GFileUtils.deleteQuietly(configFile)
//noinspection UnnecessaryQualifiedReference
org.gradle.util.GFileUtils.writeFile("${json}", configFile)
}
task releasePreBuiltFromSrc() {
setGroup("preBuilt")
doLast {
releaseFiles(true)
}
}
task releasePreBuiltFromJar() {
setGroup("preBuilt")
doLast {
releaseFiles(false)
}
}
task uploadSoToCDN() {
setGroup("preBuilt")
doLast {
uploadChromiumSo()
}
}
task checkJava8() {
setGroup("preBuilt")
doLast {
File apiDestDir = new File(project.rootDir, "cronet-api${File.separator}src${File.separator}main${File.separator}java")
File commonDestDir = new File(project.rootDir, "cronet-common${File.separator}src${File.separator}main${File.separator}java")
File nativeDestDir = new File(project.rootDir, "cronet-native${File.separator}src${File.separator}main${File.separator}java")
File platformDestDir = new File(project.rootDir, "cronet-platform${File.separator}src${File.separator}main${File.separator}java")
File processorDestDir = new File(project.rootDir, "cronet-processor${File.separator}src${File.separator}main${File.separator}java")
List<File> checkDirList = Arrays.asList(apiDestDir, commonDestDir, nativeDestDir, platformDestDir, processorDestDir)
checkDirList.each { File dir ->
//noinspection UnnecessaryQualifiedReference
dir.eachFileRecurse(groovy.io.FileType.FILES) { File file ->
file.eachLine { String line ->
if (line.contains("try (") && !line.trim().startsWith("*")) {
//try with resource
//要求不是注释行开头
project.logger.error "file contains try with resource, please replease it. file path: ${file}"
project.logger.error "${line}"
} else if (line.contains("implements AutoCloseable")) {
//AutoCloseable
project.logger.error "file contains AutoCloseable, please replease it. file path: ${file}"
project.logger.error "${line}"
}
//lambda在编译期可以检测出来,所以交由编译期处理
}
}
}
}
}
static String getMD5(File file) {
String md5 = file?.withInputStream {
//noinspection UnnecessaryQualifiedReference
new java.security.DigestInputStream(it, java.security.MessageDigest.getInstance('MD5')).withStream {
it.eachByte {}
it.messageDigest.digest().encodeHex() as String
}
}
return md5
}
static Map<String, String> uploadFile(File file) {
HttpURLConnection httpURLConnection = null
String BOUNDARY = "---------------------------" + UUID.randomUUID()
try {
URL url = new URL("https://repacedThisWithYourCDN")
httpURLConnection = (HttpURLConnection) url.openConnection()
httpURLConnection.setConnectTimeout(30000)
httpURLConnection.setReadTimeout(30000)
httpURLConnection.setDoOutput(true)
httpURLConnection.setDoInput(true)
httpURLConnection.setUseCaches(false)
httpURLConnection.setRequestMethod("POST")
httpURLConnection.setRequestProperty("Connection", "Keep-Alive")
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)")
httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY)
OutputStream out = new DataOutputStream(httpURLConnection.getOutputStream())
StringBuffer outBuffer = new StringBuffer();
outBuffer.append("\r\n").append("--").append(BOUNDARY).append("\r\n")
outBuffer.append("Content-Disposition: form-data; name=\"" + "file" + "\"; filename=\"" + "cronet-so" + "\"\r\n")
outBuffer.append("Content-Type: application/octet-stream\r\n\r\n")
out.write(outBuffer.toString().getBytes())
DataInputStream dis = new DataInputStream(new FileInputStream(file))
int bytes = 0
byte[] bufferOut = new byte[10240]
while ((bytes = dis.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes)
}
dis.close()
byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes()
out.write(endData)
out.flush()
out.close()
StringBuffer inBuf = new StringBuffer()
BufferedReader reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()))
String line = null
while ((line = reader.readLine()) != null) {
inBuf.append(line).append("\n")
}
String json = inBuf.toString()
//noinspection UnnecessaryQualifiedReference
def map = new groovy.json.JsonSlurper().parseText(json)
Map<String, String> result = new HashMap<>()
result.put("name", file.getName())
result.put("url", map.result)
result.put("md5", map.status.message?.split(":")[1])
return result
} catch (Exception e) {
e.printStackTrace()
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect()
}
}
return null
}