-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
256 lines (220 loc) · 10.1 KB
/
build.gradle.kts
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
import java.util.*
plugins {
id("base")
id("java")
id("com.novoda.build-properties") version "0.4.1"
}
buildProperties {
create("local") {
using(project.file("local.properties"))
}
}
val localProperties = buildProperties["local"].asMap()
val fileSrcApk = layout.projectDirectory.dir("sourceApk").asFileTree.singleFile
val apkFilePath = fileSrcApk.path
val apkName = fileSrcApk.nameWithoutExtension
val packageName = "com.e1c.mobile"
val targetApi = 28
val minApi = 23
val androidSdkDir = localProperties["sdk.dir"]?.string?.trim() ?: error("Need define path to android sdk")
val pathToBaksmali = localProperties["baksmali.dir"]?.string?.trim() ?: error("define path to baksmali")
val keystorePath = localProperties["keystore.path"]?.string?.trim() ?: error("Need define path to keystore")
val keystorePassword = localProperties["keystore.password"]?.string?.trim() ?: error("Need define keystore password")
val keyAlias = localProperties["key.alias"]?.string?.trim() ?: error("Need define key alias")
val keyPassword = localProperties["key.password"]?.string?.trim() ?: error("Need define key password")
val buildToolsVersion = localProperties["buildToolsVersion"]?.string?.trim()
?: error("Need define sdk build tools version")
val runOnWindows = runOnWindows()
val separator = File.separator
sourceSets {
main {
java {
val androidSdkFiles by files("$androidSdkDir${separator}platforms${separator}android-$targetApi${separator}android.jar")
setSrcDirs(listOf("src/java", "src/stubJava"))
exclude("stubJava/**")
compileClasspath = androidSdkFiles
}
}
}
tasks {
val apktoolUnpackDestinationDir = layout.buildDirectory.dir("unpacked").get().asFile.path
val unpack = task("unpack", type = Exec::class) {
if (runOnWindows) {
commandLine("PowerShell", "apktool.bat", "-f", "d $apkFilePath", "-o $apktoolUnpackDestinationDir")
} else {
executable("sh")
args("-c", "apktool -f d $apkFilePath -o $apktoolUnpackDestinationDir")
}
}
val extractDex = task("extractDex", type = Copy::class) {
from(zipTree(layout.projectDirectory.file(fileSrcApk.path))) {
include("**/*.dex")
}
into(layout.buildDirectory)
}
val jadx = task("jadx", type = Exec::class) {
dependsOn(extractDex)
if (runOnWindows) {
commandLine(
"PowerShell",
"jadx.bat --no-debug-info --no-res --show-bad-code -j 1 --fs-case-sensitive -d ${layout.buildDirectory.dir("decompiledJava").get().asFile.path} ${layout.buildDirectory.file("classes.dex").get().asFile.path}"
)
} else {
executable("sh")
args("-c", "jadx --no-debug-info --no-res --show-bad-code -j 1 --fs-case-sensitive -d ${layout.buildDirectory.dir("decompiledJava").get().asFile.path} ${layout.buildDirectory.file("classes.dex").get().asFile.path}")
}
}
compileJava.get().sourceCompatibility = JavaVersion.VERSION_1_8.toString()
compileJava.get().targetCompatibility = JavaVersion.VERSION_1_8.toString()
compileJava.get().options.encoding = "UTF-8"
val compileToDex = task("compileToDex", type = Exec::class) {
dependsOn(compileJava.get())
val classesPath = layout.buildDirectory.dir("classes/java/main").get().asFile.invariantSeparatorsPath
if (runOnWindows) {
val filesToCompileToDex = files(layout.projectDirectory.dir("src/java").asFileTree.map {
it.path.replace(Regex(".*src\\\\java"), classesPath).replace(".java", ".class")
})
commandLine(
"PowerShell",
"${androidSdkDir}${separator}build-tools$separator$buildToolsVersion${separator}d8.bat ${filesToCompileToDex.fold("") { acc, file -> acc + file.path + " " }} --min-api ${minApi} --output ${layout.buildDirectory.get().asFile.path}"
)
} else {
executable("sh")
val filesToCompileToDex = files(layout.projectDirectory.dir("src/java").asFileTree.map {
it.path.replace(Regex(".*src/java"), classesPath).replace(".java", ".class")
})
args("-c", "${androidSdkDir}${separator}build-tools$separator$buildToolsVersion${separator}d8 ${filesToCompileToDex.fold("") { acc, file -> acc + file.path + " " }} --min-api ${minApi} --output ${layout.buildDirectory.get().asFile.path}")
}
}
val decompileOwnFilesToSmali = task("decompileOwnFilesToSmali", type = Exec::class) {
dependsOn(compileToDex)
if (runOnWindows) {
commandLine(
"PowerShell",
"java -jar ${pathToBaksmali}${separator}baksmali.jar d " +
"${layout.buildDirectory.file("classes.dex").get().asFile.path} " +
"-o ${layout.buildDirectory.dir("unpackedOwn").get().asFile.path}"
)
} else {
executable("sh")
args("-c", "java -jar ${pathToBaksmali}${separator}baksmali.jar d " +
"${layout.buildDirectory.file("classes.dex").get().asFile.path} " +
"-o ${layout.buildDirectory.dir("unpackedOwn").get().asFile.path}")
}
}
val copySmaliFiles = task("copySmaliFiles", type = Copy::class) {
dependsOn(unpack)
dependsOn(decompileOwnFilesToSmali)
from(layout.projectDirectory.dir("src/smali").asFile.path) {
include("**/*.smali")
}
from(layout.buildDirectory.dir("unpackedOwn").get().asFile.path) {
include("**/*.smali")
}
destinationDir = layout.buildDirectory.dir("unpacked/smali").get().asFile
}
val copyResFiles = task("copyResFiles", type = Copy::class) {
dependsOn(unpack)
from(layout.projectDirectory.dir("src/res").asFile.path) {
include("**/*")
}
destinationDir = layout.buildDirectory.dir("unpacked/res").get().asFile
}
val apktoolPackDestinationDir = "${layout.buildDirectory.get().asFile.path}${File.separator}$apkName.apk"
val pack = task("pack", type = Exec::class) {
dependsOn(copySmaliFiles)
dependsOn(copyResFiles)
if (runOnWindows) {
commandLine("PowerShell", "apktool.bat b $apktoolUnpackDestinationDir -o $apktoolPackDestinationDir")
} else {
executable("sh")
args("-c", "apktool b $apktoolUnpackDestinationDir -o $apktoolPackDestinationDir")
}
}
val sign = task("sign", type = Exec::class) {
dependsOn(pack)
if (runOnWindows) {
commandLine(
"PowerShell",
"${androidSdkDir}${separator}build-tools$separator$buildToolsVersion${separator}apksigner.bat sign"
+ " --ks $keystorePath "
+ " --ks-key-alias $keyAlias "
+ " --ks-pass pass:$keystorePassword "
+ " --key-pass pass:$keyPassword "
+ apktoolPackDestinationDir
)
} else {
executable("sh")
args("-c", "${androidSdkDir}${separator}build-tools$separator$buildToolsVersion${separator}apksigner sign"
+ " --ks $keystorePath "
+ " --ks-key-alias $keyAlias "
+ " --ks-pass pass:$keystorePassword "
+ " --key-pass pass:$keyPassword "
+ apktoolPackDestinationDir
)
}
}
assemble.get().dependsOn(sign)
val installApk = task("installApk", type = Exec::class) {
dependsOn(assemble.get())
if (runOnWindows) {
commandLine(
"PowerShell",
"adb install ${layout.buildDirectory.file("$apkName.apk").get().asFile.path}"
)
} else {
executable("sh")
args("-c", "adb install ${layout.buildDirectory.file("$apkName.apk").get().asFile.path}")
}
}
val grantPermissionReadStorage = task("grantPermissionReadStorage", type = Exec::class) {
dependsOn(installApk)
if (runOnWindows) {
commandLine(
"PowerShell",
"adb shell pm grant $packageName android.permission.READ_EXTERNAL_STORAGE"
)
} else {
executable("sh")
args("-c", "adb shell pm grant $packageName android.permission.READ_EXTERNAL_STORAGE")
}
}
val pathToCf = "/storage/emulated/0/Download"
val push1cCf = task("push1cCf", type = Exec::class) {
dependsOn(grantPermissionReadStorage)
if (runOnWindows) {
commandLine(
"PowerShell",
"adb push ${layout.projectDirectory.dir("src/1cConfiguration").asFile.path} $pathToCf"
)
} else {
executable("sh")
args("-c", "adb push ${layout.projectDirectory.dir("src/1cConfiguration").asFile.path} $pathToCf")
}
}
val run1cApk = task("run1cApk", type = Exec::class) {
dependsOn(push1cCf)
if (runOnWindows) {
commandLine(
"PowerShell",
"adb shell am start -n com.e1c.mobile/.App -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"
)
} else {
executable("sh")
args("-c", "adb shell am start -n com.e1c.mobile/.App -a android.intent.action.MAIN -c android.intent.category.LAUNCHER")
}
}
val runOnDevice = task("runOnDevice", type = Exec::class) {
dependsOn(run1cApk)
if (runOnWindows) {
commandLine(
"PowerShell",
"adb shell am broadcast -n com.e1c.mobile/.Starter -a com.e1c.mobile.START_TEMPLATE -e templatepath $pathToCf/1cConfiguration/1cema.xml "
)
} else {
executable("sh")
args("-c", "adb shell am broadcast -n com.e1c.mobile/.Starter -a com.e1c.mobile.START_TEMPLATE -e templatepath $pathToCf/1cConfiguration/1cema.xml ")
}
}
}
fun runOnWindows(): Boolean = System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("windows")