-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathdexPatchWraper.gradle
More file actions
118 lines (102 loc) · 3.63 KB
/
dexPatchWraper.gradle
File metadata and controls
118 lines (102 loc) · 3.63 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
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import static Constants.*
/**
* Created by 种藏 on 2017/8/30.
*
* 1. 这个类只是对产物patches.json文件做个包装,用来模拟服务器控制dexPatch版本的逻辑,生成的产物是dexpatch-versionName.json
*
* 2. 正常开发情况,做dexPatch时,服务器会分配一个对应的dexPatchVersion。
* 客户端比较dexPatchVersion来判断要不要做patch。
*
* 3. 这里只是每次做dexPatch时,简单的将dexPatchVersion 加 1.
*
* 4. 具体字段含义,参考https://alibaba.github.io/atlas/update/dexpatch_use_guide.html
*
*/
tasks.whenTaskAdded { task ->
if (task.name == "assembleDebug" || task.name == "assembleRelease") {
task.doLast {
wrapDexPatchFile(task.name.contains("Debug"))
}
}
// if (task.name == "clean") {
// task.doLast { cleanPatchVersion() }
// }
}
class Constants {
static final ENCODE_UTF_8 = "UTF-8"
static final PATH_VERSION_FILE = "dexPatch.version"
}
task tesetm << {
wrapDexPatchFile(true)
}
def wrapDexPatchFile(boolean debug) {
println "root project :${rootProject.projectDir.path}"
def apVersion = getEnvValue("apVersion", "-1")
def versionName = getEnvValue("versionName", "-1")
if (apVersion == "-1" || versionName == "-1") {
println("jump dexPatch")
return
}
if (apVersion != versionName) {
return
}
println "DexPatch wrap ,version $versionName"
String path = rootProject.projectDir.path + "/app/build/outputs/tpatch-" + (debug ? "debug" : "release") + "/patchs.json"
String patchPath = path.replace('/', File.separator)
println "patchFile path :$patchPath"
String fileContents = new File(patchPath).getText(ENCODE_UTF_8)
def patchConfig = new JsonSlurper().parseText(fileContents)
def updateInfo = patchConfig.patches[0]
updateInfo.dexPatch = true
def bundles = updateInfo.bundles
assert bundles instanceof List
def patchVersion = getPatchVersion()
bundles.each {
assert it instanceof Map
it.dexpatchVersion = patchVersion
it.isMainDex = it.mainBundle
it.remove("mainBundle")
if (it.isMainDex) {
println "=================>add empty main_dex dependency ============="
it.dependency=[]
}
}
//写回到最终的dexPatch产物文件中
String wrapFilePath = "${rootProject.projectDir.path}/app/build/outputs/tpatch-" + (debug ? "debug" : "release") + "/dexpatch-${patchConfig.baseVersion}.json"
File wrapFile = new File(wrapFilePath.replace('/', File.separator))
if (wrapFile.exists()) wrapFile.delete()
wrapFile.createNewFile()
def dexPatchJson = JsonOutput.toJson(patchConfig)
wrapFile.setText(dexPatchJson, ENCODE_UTF_8)
}
//粗暴+1
def getPatchVersion() {
File versionFile = new File(rootProject.projectDir.path, PATH_VERSION_FILE)
if (!versionFile.exists()) versionFile.createNewFile()
String versionStr = versionFile.getText(ENCODE_UTF_8)
if (null == versionStr || versionStr.isEmpty()) {
versionStr = "1"
}
String nextVersion = String.valueOf(Integer.valueOf(versionStr) + 1)
versionFile.setText(nextVersion, ENCODE_UTF_8)
return versionStr
}
def cleanPatchVersion() {
File versionFile = new File(rootProject.projectDir.path, PATH_VERSION_FILE)
if (versionFile.exists()) {
versionFile.delete()
}
}
String getEnvValue(key, defValue) {
def val = System.getProperty(key)
if (null != val) {
return val
}
val = System.getenv(key)
if (null != val) {
return val
}
return defValue
}