forked from wuyr/HookwormForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
executable file
·289 lines (257 loc) · 9.57 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
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
import org.apache.tools.ant.filters.FixCrLfFilter
import org.gradle.internal.os.OperatingSystem
import org.gradle.kotlin.dsl.support.zipTo
import java.security.MessageDigest
import java.util.*
import kotlin.concurrent.thread
plugins {
id("com.android.library")
kotlin("android")
kotlin("android.extensions")
}
val properties =
Properties().apply { load(file("src/main/assets/module.properties").inputStream()) }
val moduleId: String = properties.getProperty("moduleId")
val moduleName: String = properties.getProperty("moduleName")
val moduleAuthor: String = properties.getProperty("moduleAuthor")
val moduleDescription: String = properties.getProperty("moduleDescription")
val moduleVersion: String = properties.getProperty("moduleVersion")
val moduleDexPath: String = "system/framework/$moduleId.dex"
val moduleMainClass: String = properties.getProperty("moduleMainClass")
val targetProcessName: String = properties.getProperty("targetProcessName")
val automaticInstallation: Boolean = properties.getProperty("automaticInstallation") == "1"
val platform: OperatingSystem = OperatingSystem.current()
android {
compileSdkVersion(30)
buildToolsVersion("30.0.2")
defaultConfig {
minSdkVersion(23)
targetSdkVersion(30)
versionCode = 1
versionName = moduleVersion
externalNativeBuild {
cmake {
arguments("-DLIBRARY_NAME:STRING=riru_$moduleId")
}
}
checkProperties()
refreshMainHeader()
refreshConstants()
}
buildTypes {
named("release") {
isMinifyEnabled = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
externalNativeBuild {
cmake {
path = file("src/main/cpp/CMakeLists.txt")
}
}
libraryVariants.all {
if (name == "release") {
sdkDirectory.buildModule()
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.3.1")
}
fun checkProperties() {
if (moduleId.isEmpty()) {
error("moduleId must be fill out!")
}
if (moduleMainClass.isEmpty()) {
error("moduleMainClass must be fill out!")
}
}
fun refreshMainHeader() =
targetProcessName.split(";").filter { it.isNotBlank() && it.isNotEmpty() }.run {
file("src/main/cpp/main.h").writeText(
file("src/main/resource/template_main_header").readText()
.format(moduleDexPath, joinToString("\", \"", "\"", "\""), size)
)
}
fun refreshConstants() = file("src/main/java/com/wuyr/hookworm/core/Constants.java").writeText(
file("src/main/resource/template_constants").readText().format(moduleMainClass)
)
fun File.buildModule() {
val task = rootProject.getTasksByName("assemble", true)
.filter { it.project.name == "app" }.run { if (isEmpty()) null else get(0) }
?: error("Please dependent on to an app module!")
val buildDir = task.project.buildDir.also { it.deleteRecursively() }
task.doLast {
val zipPath = buildDir.resolve("intermediates/magisk/").apply {
deleteRecursively()
mkdirs()
}
copy {
into(zipPath)
processResource()
processScript()
processProperties()
zipTree(File(buildDir, "outputs/apk/release/app-release-unsigned.apk")
.also { if (!it.exists()) error("${it.name} not found!") }).let { apkFileTree ->
processLibs(apkFileTree)
processDex(apkFileTree)
}
}
zipPath.apply {
resolve("extras.files").writeText("${moduleDexPath}\n")
fixLineBreaks()
generateSHA256Sum()
}
buildDir.resolve("outputs/module").also { it.mkdirs() }.run {
val moduleFile = File(this, "${moduleId}.zip")
zipTo(moduleFile, zipPath)
if (automaticInstallation) {
if (isDeviceConnected(this@buildModule)) {
if (installModuleFailed(this@buildModule, moduleFile, zipPath)) {
openInGUI()
error("Module installation failed!")
}
} else {
openInGUI()
error("Device not connected or connected more than one device!")
}
} else {
openInGUI()
}
}
}
}
fun CopySpec.processResource() = from(file("src/main/resource")) {
exclude(
"riru.sh", "module.prop", "riru/module.prop.new",
"template_constants", "template_main_header"
)
}
fun CopySpec.processScript() =
from(file("src/main/resource/riru.sh")) {
filter { line ->
line.replace("%%%RIRU_MODULE_ID%%%", moduleId)
.replace("%%%RIRU_MIN_API_VERSION%%%", "7")
.replace("%%%RIRU_MIN_VERSION_NAME%%%", moduleVersion)
}
filter(FixCrLfFilter::class.java)
}
fun CopySpec.processProperties() {
from(file("src/main/resource/module.prop")) {
filter { line ->
line.replace("%%%MAGISK_ID%%%", moduleId)
.replace("%%%MAIGKS_NAME%%%", moduleName)
.replace("%%%MAGISK_VERSION_NAME%%%", moduleVersion)
.replace("%%%MAGISK_AUTHOR%%%", moduleAuthor)
.replace("%%%MAGISK_DESCRIPTION%%%", moduleDescription)
}
filter(FixCrLfFilter::class.java)
}
from(file("src/main/resource/riru/module.prop.new")) {
into("riru/")
filter { line ->
line.replace("%%%RIRU_NAME%%%", moduleName)
.replace("%%%RIRU_VERSION_NAME%%%", moduleVersion)
.replace("%%%RIRU_AUTHOR%%%", moduleAuthor)
.replace("%%%RIRU_DESCRIPTION%%%", moduleDescription)
.replace("%%%RIRU_API%%%", "7")
}
filter(FixCrLfFilter::class.java)
}
}
fun CopySpec.processLibs(apkFileTree: FileTree) = from(apkFileTree) {
include("lib/**")
eachFile {
path = path.replace("lib/armeabi-v7a", "system/lib")
.replace("lib/x86_64", "system_x86/lib64")
.replace("lib/x86", "system_x86/lib")
.replace("lib/arm64-v8a", "system/lib64")
}
}
fun CopySpec.processDex(apkFileTree: FileTree) = from(apkFileTree) {
include("classes.dex")
eachFile { path = moduleDexPath }
}
fun File.fixLineBreaks() {
val ignoreFiles = arrayOf("system", "system_x86")
fun walk(file: File) {
if (file.isDirectory) {
if (!ignoreFiles.contains(file.name)) {
file.listFiles()?.forEach { if (!ignoreFiles.contains(it.name)) walk(it) }
}
} else {
file.readText().run {
if (contains("\r\n")) file.writeText(replace("\r\n", "\n"))
}
}
}
walk(this)
}
fun File.generateSHA256Sum() = fileTree(this).matching {
exclude("customize.sh", "verify.sh", "META-INF")
}.filter { it.isFile }.forEach { file ->
File(file.absolutePath + ".sha256sum").writeText(
MessageDigest.getInstance("SHA-256").digest(file.readBytes()).run {
joinToString("") { String.format("%02x", it.toInt() and 0xFF) }
})
}
fun File.openInGUI() = platform.runCatching {
Runtime.getRuntime().exec(
"${
when {
isWindows -> "explorer"
isLinux -> "nautilus"
isMacOsX -> "open"
else -> ""
}
} ${this@openInGUI}"
)
}.isSuccess
val platformArgs: Array<String>
get() = if (platform.isWindows) arrayOf("cmd", "/C") else arrayOf("/bin/bash", "-c")
val File.adb: String get() = if (platform.isWindows) "SET Path=$this/platform-tools&&adb" else "$this/platform-tools/adb"
val File.adbWithoutSetup: String get() = if (platform.isWindows) "adb" else "$this/platform-tools/adb"
fun isDeviceConnected(sdkDirectory: File) = platform.runCatching {
Runtime.getRuntime().exec(arrayOf(*platformArgs, "${sdkDirectory.adb} devices")).run {
waitFor()
inputStream.reader().readLines().size == 3.also { destroy() }
}
}.getOrDefault(false)
fun installModuleFailed(sdkDirectory: File, moduleFile: File, zipPath: File) =
"${sdkDirectory.adb} push $moduleFile /data/local/tmp/&&${sdkDirectory.adbWithoutSetup} push $zipPath/META-INF/com/google/android/update-binary /data/local/tmp/&&${sdkDirectory.adbWithoutSetup} shell su".runCatching {
Runtime.getRuntime().exec(arrayOf(*platformArgs, this)).run {
thread(isDaemon = true) { readContentSafely(inputStream) { it.p() } }
thread(isDaemon = true) { readContentSafely(errorStream) { it.p() } }
outputStream.run {
write("cd /data/local/tmp&&BOOTMODE=true sh update-binary dummy 1 ${moduleFile.name}&&rm update-binary&&rm ${moduleFile.name}&&reboot\n".toByteArray())
flush()
close()
}
waitFor()
destroy()
}
false
}.getOrDefault(true)
fun Process.readContentSafely(inputStream: java.io.InputStream, onReadLine: (String) -> Unit) {
runCatching {
inputStream.bufferedReader().use { reader ->
var line = ""
while (runCatching { exitValue() }.isFailure && reader.readLine()
?.also { line = it } != null
) {
onReadLine(line)
}
}
}
}
fun Any?.p() = println(this)