1
+ afterEvaluate {
2
+ for (variant in android. applicationVariants) {
3
+ def scope = variant. getVariantData(). getScope()
4
+ String mergeTaskName = scope. getMergeResourcesTask(). name
5
+ println (" insertPublicXML task has insert " + mergeTaskName)
6
+ def publicXMLMergeTask = tasks. getByName(mergeTaskName)
7
+ publicXMLMergeTask. doLast {
8
+ def forInsertModule = MODULE_NAME_INSERT_PUBLIC_XML
9
+ String saveInsertModule = " ${ rootDir} /" + forInsertModule
10
+ String resPath = saveInsertModule + " /build/intermediates/bundles/release/R.txt"
11
+ MakeResFile (resPath)
12
+ String insertXMLPath = " ${ rootDir} /build/public.xml"
13
+ def resFile = new File (insertXMLPath)
14
+ if (! resFile. exists()) {
15
+ new IllegalArgumentException (" public.xml File is not exist please check at" + insertXMLPath). printStackTrace()
16
+ }
17
+ copy {
18
+ int i = 0
19
+ from(android. sourceSets. main. res. srcDirs) {
20
+ include insertXMLPath
21
+ rename ' public.xml' , (i++ == 0 ? " public.xml" : " public_${ i} .xml" )
22
+ }
23
+ into(publicXMLMergeTask. outputDir)
24
+ }
25
+ }
26
+ }
27
+ }
28
+
29
+
30
+ def MakeResFile (final rPath ) {
31
+ def resFile = new File (rPath)
32
+ if (! resFile. exists()) {
33
+ print " res File is not exist please check" + rPath
34
+ return false
35
+ }
36
+ def sbRRes = new StringBuffer ()
37
+ sbRRes. append(" <?xml version=\" 1.0\" encoding=\" utf-8\" ?>\n " )
38
+ sbRRes. append(" <resources>\n " )
39
+ resFile. withInputStream() {
40
+ is ->
41
+ resFile. eachLine {
42
+ line ->
43
+ def lineRes = line. split(" (" + (char ) 32 + " |" + (char ) 9 + " )+" )
44
+ def sbLine = new StringBuffer ()
45
+ if (lineRes. length == 4 && lineRes[3 ]. matches(" [0-9a-fA-Fx]{10}" )) {
46
+ sbLine. append(" \t <public type=\" " )
47
+ sbLine. append(lineRes[1 ])
48
+ sbLine. append(" \" name=\" " )
49
+ sbLine. append(lineRes[2 ])
50
+ sbLine. append(" \" id=\" " )
51
+ sbLine. append(lineRes[3 ])
52
+ sbLine. append(" \" />\n " )
53
+ }
54
+ sbRRes. append(sbLine. toString())
55
+
56
+ }
57
+ }
58
+ sbRRes. append(" </resources>" )
59
+ // println sbRRes.toString()
60
+ File rootBuild = new File (" ${ rootDir} /build/" )
61
+ if (! rootBuild. exists()) {
62
+ rootBuild. mkdirs()
63
+ }
64
+ new File (" ${ rootDir} /build/public.xml" ). write(sbRRes. toString())
65
+ return true
66
+ }
0 commit comments