-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathbuild.gradle.kts
executable file
·377 lines (347 loc) · 14.3 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import com.android.build.api.dsl.ExternalNativeCmakeOptions
import javassist.ClassPool
import javassist.CtMethod
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")
}
buildscript {
dependencies {
classpath("javassist:javassist:3.9.0.GA")
}
}
if (!file("module.properties").exists()) {
error("Please copy \"module.properties.sample\" and rename to \"module.properties\" and fill in the module information!")
}
val versionsProp =
Properties().apply { load(file("src/main/resource/versions.properties").inputStream()) }
val moduleCompileSdkVersion = versionsProp.getProperty("compileSdkVersion").toInt()
val moduleMinSdkVersion = versionsProp.getProperty("minSdkVersion").toInt()
val moduleTargetSdkVersion = versionsProp.getProperty("targetSdkVersion").toInt()
val cmakeVersion: String = versionsProp.getProperty("cmakeVersion")
val corektxVersion: String = versionsProp.getProperty("core-ktxVersion")
val maxRiruApiVersionCode = versionsProp.getProperty("maxRiruApiVersionCode").toInt()
val minRiruApiVersionCode = versionsProp.getProperty("minRiruApiVersionCode").toInt()
val minRiruApiVersionName: String = versionsProp.getProperty("minRiruApiVersionName")
val moduleProp =
Properties().apply { load(file("module.properties").inputStream()) }
val moduleId: String = moduleProp.getProperty("moduleId")
val moduleName: String = moduleProp.getProperty("moduleName")
val moduleAuthor: String = moduleProp.getProperty("moduleAuthor")
val moduleDescription: String = moduleProp.getProperty("moduleDescription")
val moduleVersionName: String = moduleProp.getProperty("moduleVersionName")
val moduleVersionCode: String = moduleProp.getProperty("moduleVersionCode")
val moduleMainClass: String = moduleProp.getProperty("moduleMainClass")
val targetProcessName: String = moduleProp.getProperty("targetProcessName")
val libraryPath: String =
moduleProp.getProperty("libraryPath").let { if (it.isEmpty()) moduleId else it }
val automaticInstallation: Boolean =
moduleProp.getProperty("automaticInstallation").let { it == "1" || it == "true" }
val debug: Boolean = moduleProp.getProperty("debug").let { it == "1" || it == "true" }
val moduleDexPath: String = "${if (debug) "data/local/tmp" else "system/framework"}/$moduleId.dex"
val hookwormMainClass = "com/wuyr/hookworm/core/Main"
val targetProcessNameList =
targetProcessName.split(";").filter { it.isNotBlank() && it.isNotEmpty() }
val processNameArray = targetProcessNameList.joinToString("\", \"", "{\"", "\"}")
val processNameArraySize = targetProcessNameList.size
val platform: OperatingSystem = OperatingSystem.current()
checkProperties()
rootProject.allprojects.forEach { it.buildDir.deleteRecursively() }
android {
compileSdkVersion(moduleCompileSdkVersion)
defaultConfig {
minSdkVersion(moduleMinSdkVersion)
targetSdkVersion(moduleTargetSdkVersion)
externalNativeBuild { cmake { addDefinitions() } }
}
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")
version = cmakeVersion
}
}
libraryVariants.all {
if (name == "release") {
sdkDirectory.buildModule()
}
}
}
dependencies {
implementation("androidx.core:core-ktx:$corektxVersion")
}
fun checkProperties() {
if (moduleId.isEmpty()) {
error("moduleId must be fill out!")
}
if (moduleMainClass.isEmpty()) {
error("moduleMainClass must be fill out!")
}
}
fun ExternalNativeCmakeOptions.addDefinitions() = arguments(
"-DDEX_PATH=\"$moduleDexPath\"",
"-DMAIN_CLASS=\"$hookwormMainClass\"",
"-DPROCESS_NAME_ARRAY=$processNameArray",
"-DPROCESS_NAME_ARRAY_SIZE=$processNameArraySize",
"-DMODULE_NAME=riru_$moduleId",
"-DRIRU_MODULE_API_VERSION=$maxRiruApiVersionCode",
"-DRIRU_MODULE_VERSION=$moduleVersionCode",
"-DRIRU_MODULE_VERSION_NAME=\"$moduleVersionName\""
)
var magiskDir = ""
fun File.buildModule() {
initModuleInfo()
val task = rootProject.project("app").tasks.find { it.name == "assemble" }
?: 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()
magiskDir = absolutePath
mkdirs()
}
copy {
into(zipPath)
processResource()
processScript()
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 {
val prop = "name=$moduleName\n" +
"version=$moduleVersionName\n" +
"versionCode=$moduleVersionCode\n" +
"author=$moduleAuthor\n" +
"description=$moduleDescription"
resolve("module.prop").writeText(
"id=$moduleId\n$prop\ntarget_process_name=$targetProcessName\nlibrary_path=$libraryPath"
)
resolve("riru").apply {
mkdir()
resolve("module.prop.new").writeText(
"$prop\nminApi=$minRiruApiVersionCode"
)
}
resolve("extras.files").run {
if (debug) {
createNewFile()
} else {
writeText("${moduleDexPath}\n")
}
}
fixLineBreaks()
generateSHA256Sum()
}
buildDir.resolve("outputs/module").also { it.mkdirs() }.run {
val moduleFile = File(this, "${moduleId}_$moduleVersionName.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 initModuleInfo() = (project.tasks.find { it.name == "compileReleaseJavaWithJavac" }
?: error("Task 'compileReleaseJavaWithJavac' not found!"))
.doLast {
val classPool = ClassPool.getDefault()
val moduleInfoClassPath =
buildDir.resolve("intermediates/javac/release/classes").absolutePath
classPool.insertClassPath(moduleInfoClassPath)
classPool.getCtClass("com.wuyr.hookworm.core.ModuleInfo").run {
getDeclaredMethod("getMainClass").name = "getMainClassOld"
addMethod(
CtMethod.make("static String getMainClass(){ return \"$moduleMainClass\"; }", this)
)
getDeclaredMethod("getDexPath").name = "getDexPathOld"
addMethod(
CtMethod.make("static String getDexPath(){ return \"$moduleDexPath\"; }", this)
)
if (debug) {
getDeclaredMethod("isDebug").name = "isDebugOld"
addMethod(CtMethod.make("static boolean isDebug(){ return true; }", this))
}
val hasSOFile =
(rootProject.project("app").tasks.find { it.name == "mergeReleaseNativeLibs" }
?: error("Task 'mergeReleaseNativeLibs' not found!")).run {
this is com.android.build.gradle.internal.tasks.MergeNativeLibsTask && externalLibNativeLibs.files.any {
it.isDirectory && it.list()?.isNotEmpty() == true
}
}
if (hasSOFile) {
getDeclaredMethod("hasSOFile").name = "hasSOFileOld"
addMethod(CtMethod.make("static boolean hasSOFile(){ return true; }", this))
getDeclaredMethod("getSOPath").name = "getSOPathOld"
addMethod(
CtMethod.make(
"static String getSOPath(){ return \"$libraryPath\"; }",
this
)
)
}
writeFile(moduleInfoClassPath)
detach()
}
}
fun CopySpec.processResource() = from(file("src/main/resource")) {
exclude("riru.sh", "versions.properties")
}
fun CopySpec.processScript() =
from(file("src/main/resource/riru.sh")) {
filter { line ->
line.replace("%%%RIRU_MODULE_ID%%%", moduleId)
.replace("%%%RIRU_MIN_API_VERSION%%%", minRiruApiVersionCode.toString())
.replace("%%%RIRU_MIN_VERSION_NAME%%%", minRiruApiVersionName)
}
filter(FixCrLfFilter::class.java)
}
fun CopySpec.processLibs(apkFileTree: FileTree) = from(apkFileTree) {
include("lib/**")
eachFile {
path = path.replace("lib/armeabi-v7a", "system/lib")
.replace("lib/armeabi", "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 ignoreDirs = arrayOf("system", "system_x86")
val ignoreSuffix = arrayOf("so", "dex")
fun walk(file: File) {
if (file.isDirectory) {
if (!ignoreDirs.contains(file.name)) {
file.listFiles()?.forEach { if (!ignoreDirs.contains(it.name)) walk(it) }
}
} else {
if (ignoreSuffix.none { file.absolutePath.endsWith(it) }) {
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) =
exec("${sdkDirectory.adb} devices").count { it == '\n' } == 3
fun exec(command: String) = runCatching {
Runtime.getRuntime().exec(arrayOf(*platformArgs, command)).run {
waitFor()
inputStream.reader().readText().also { destroy() }
}
}.getOrDefault("")
fun installModuleFailed(sdkDirectory: File, moduleFile: File, zipPath: File) =
if (debug && (exec("${sdkDirectory.adb} shell ls $moduleDexPath&&echo 1").contains("1"))) {
if (exec("${sdkDirectory.adb} push $magiskDir/$moduleDexPath /data/local/tmp/&&echo 1".also { it.p() })
.contains("1")
) {
targetProcessNameList.forEach { processName ->
exec("adb shell su -c killall $processName".also { it.p() })
}
"*********************************".p()
"Module installation is completed.".p()
"*********************************".p()
false
} else {
"*********************************".p()
"Module installation failed! Please try again.".p()
"*********************************".p()
true
}
} else {
exec("${sdkDirectory.adb} shell rm /data/local/tmp/$moduleId.dex".also { it.p() })
if (debug) {
exec("${sdkDirectory.adb} push $magiskDir/$moduleDexPath /data/local/tmp/").p()
}
"${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)