Skip to content

Commit 559e318

Browse files
committed
add gralde public.xml public file
1 parent ac32267 commit 559e318

9 files changed

+1228
-1
lines changed

AndoridVariants/gradleTest/R.txt

+1,030
Large diffs are not rendered by default.
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
apply from: 'publicXML-Creater.gradle'
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.security.MessageDigest
2+
3+
def generateMD5(final file) {
4+
def String[] HEX_DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
5+
MessageDigest digest = MessageDigest.getInstance("MD5")
6+
file.withInputStream(){
7+
is->byte[] buffer = new byte[1024]
8+
int read = 0
9+
while( (read = is.read(buffer)) > 0) {
10+
digest.update(buffer, 0, read);
11+
}
12+
}
13+
byte[] md5sum = digest.digest()
14+
StringBuilder sb = new StringBuilder(md5sum.length * 2);
15+
for (int i = 0; i < md5sum.length; i++) {
16+
sb.append(HEX_DIGITS[(md5sum[i] & 0xf0) >>> 4]);
17+
sb.append(HEX_DIGITS[md5sum[i] & 0x0f]);
18+
}
19+
return sb.toString()
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def MakeResFileRead(final path) {
2+
print "res R path is: " + path
3+
def resFile = new File(path)
4+
if (!resFile.exists()) {
5+
print "res File is not exist"
6+
return
7+
}
8+
def sbRRes = new StringBuffer()
9+
sbRRes.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
10+
sbRRes.append("<resources>\n")
11+
resFile.withInputStream() {
12+
is -> resFile.eachLine {
13+
line ->
14+
def lineRes = line.split("(" + (char)32 + "|" + (char)9 + ")+")
15+
def sbLine = new StringBuffer()
16+
if (lineRes.length == 4 && lineRes[3].matches("[0-9a-fA-Fx]{10}")) {
17+
sbLine.append("\t<public type=\"")
18+
sbLine.append(lineRes[1])
19+
sbLine.append("\" name=\"")
20+
sbLine.append(lineRes[2])
21+
sbLine.append("\" id=\"")
22+
sbLine.append(lineRes[3])
23+
sbLine.append("\" />\n")
24+
}
25+
sbRRes.append(sbLine.toString())
26+
27+
}
28+
}
29+
sbRRes.append("</resources>")
30+
// println sbRRes.toString()
31+
new File("${rootDir}/build/public.xml").write(sbRRes.toString())
32+
return true
33+
}
34+
35+
task TestResRReader {
36+
doLast {
37+
println 'start test ResRReader!'
38+
def rPath = "${projectDir}/R.txt"
39+
MakeResFileRead(rPath)
40+
}
41+
}

AndoridVariants/settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// Sets the output JAR filename
22
//rootProject.name = 'AndroidVeriants'
33
include 'ResMerge'
4+
include 'gradleTest'

androidUtils/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,20 @@
55
```gradle
66
// outAPKDebug outAPKRelease outAPKDebugARMv7a outAPKReleaseARMv7a
77
apply from: "https://github.com/sinlov/Gradle_Zoo/raw/master/androidUtils/outBuildApk.gradle"
8-
```
8+
```
9+
10+
# public.xml 插件
11+
12+
- 固化public.xml 资源插件
13+
14+
application-public-xml.gradle
15+
library-public-xml.gradle
16+
17+
- 生成pubic.xml资源表插件
18+
19+
public-XML-Saver.gradle
20+
21+
- 从一个module注入固化资源的插件
22+
23+
insert-public-xml.gradle
24+

androidUtils/gradle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MODULE_NAME_INSERT_PUBLIC_XML = Lib_Project

androidUtils/insert-public-xml.gradle

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}

androidUtils/public-XML-Saver.gradle

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
afterEvaluate {
2+
for (variant in android.libraryVariants) {
3+
def scope = variant.getVariantData().getScope()
4+
String mergeTaskName = scope.getMergeResourcesTask().name
5+
println("public_XML_saver Task has insert " + mergeTaskName)
6+
def publicXMLMergeTask = tasks.getByName(mergeTaskName)
7+
8+
publicXMLMergeTask.doLast {
9+
String resPath = "${projectDir}/build/intermediates/bundles/release/R.txt"
10+
MakeResFile(resPath)
11+
}
12+
}
13+
}
14+
15+
def MakeResFile(final rPath) {
16+
def resFile = new File(rPath)
17+
if (!resFile.exists()) {
18+
print "res File is not exist please check" + rPath
19+
return false
20+
}
21+
def sbRRes = new StringBuffer()
22+
sbRRes.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
23+
sbRRes.append("<resources>\n")
24+
resFile.withInputStream() {
25+
is ->
26+
resFile.eachLine {
27+
line ->
28+
def lineRes = line.split("(" + (char) 32 + "|" + (char) 9 + ")+")
29+
def sbLine = new StringBuffer()
30+
if (lineRes.length == 4 && lineRes[3].matches("[0-9a-fA-Fx]{10}")) {
31+
sbLine.append("\t<public type=\"")
32+
sbLine.append(lineRes[1])
33+
sbLine.append("\" name=\"")
34+
sbLine.append(lineRes[2])
35+
sbLine.append("\" id=\"")
36+
sbLine.append(lineRes[3])
37+
sbLine.append("\" />\n")
38+
}
39+
sbRRes.append(sbLine.toString())
40+
41+
}
42+
}
43+
sbRRes.append("</resources>")
44+
// println sbRRes.toString()
45+
File rootBuild = new File("${rootDir}/build/")
46+
if (!rootBuild.exists()) {
47+
rootBuild.mkdirs()
48+
}
49+
new File("${rootDir}/build/public.xml").write(sbRRes.toString())
50+
return true
51+
}

0 commit comments

Comments
 (0)