-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
445 lines (380 loc) · 14.7 KB
/
Copy pathbuild.gradle.kts
File metadata and controls
445 lines (380 loc) · 14.7 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
@file:Suppress("UnstableApiUsage")
import com.android.build.gradle.tasks.PackageAndroidArtifact
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import java.net.URI
import java.util.Properties
import java.io.File
import java.io.FileInputStream
plugins {
alias(libs.plugins.agp.app)
alias(libs.plugins.kotlin)
alias(libs.plugins.kotlin.compose.compiler)
alias(libs.plugins.ksp)
alias(libs.plugins.lsplugin.apksign)
alias(libs.plugins.lsplugin.resopt)
alias(libs.plugins.lsplugin.cmaker)
id("kotlin-parcelize")
}
val androidCompileSdkVersion: Int by rootProject.extra
val androidCompileNdkVersion: String by rootProject.extra
val androidBuildToolsVersion: String by rootProject.extra
val androidMinSdkVersion: Int by rootProject.extra
val androidTargetSdkVersion: Int by rootProject.extra
val androidSourceCompatibility: JavaVersion by rootProject.extra
val androidTargetCompatibility: JavaVersion by rootProject.extra
val managerVersionCode: Int by rootProject.extra
val managerVersionName: String by rootProject.extra
val branchName: String by rootProject.extra
val kernelPatchVersion: String by rootProject.extra
// Load keystore properties
val keystoreProperties = Properties()
val keystorePropertiesFile = rootProject.file("keystore.properties")
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(FileInputStream(keystorePropertiesFile))
}
// Load local properties
val localProperties = Properties()
val localPropertiesFile = rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localProperties.load(FileInputStream(localPropertiesFile))
}
apksign {
storeFileProperty = "KEYSTORE_FILE"
storePasswordProperty = "KEYSTORE_PASSWORD"
keyAliasProperty = "KEY_ALIAS"
keyPasswordProperty = "KEY_PASSWORD"
}
val ccache = System.getenv("PATH")?.split(File.pathSeparator)
?.map { File(it, "ccache") }?.firstOrNull { it.exists() }?.absolutePath
val baseFlags = listOf(
"-Wall", "-Qunused-arguments", "-fno-rtti", "-fvisibility=hidden",
"-fvisibility-inlines-hidden", "-fno-exceptions", "-fno-stack-protector",
"-fomit-frame-pointer", "-Wno-builtin-macro-redefined", "-Wno-unused-value",
"-D__FILE__=__FILE_NAME__",
"-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON", "-Wno-unused", "-Wno-unused-parameter",
"-Wno-unused-command-line-argument", "-Wno-incompatible-function-pointer-types",
"-U_FORTIFY_SOURCE", "-D_FORTIFY_SOURCE=0"
)
val baseArgs = mutableListOf(
"-DANDROID_STL=none", "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
"-DCMAKE_CXX_STANDARD=23", "-DCMAKE_C_STANDARD=23",
"-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON", "-DCMAKE_VISIBILITY_INLINES_HIDDEN=ON",
"-DCMAKE_CXX_VISIBILITY_PRESET=hidden", "-DCMAKE_C_VISIBILITY_PRESET=hidden"
).apply { if (ccache != null) add("-DANDROID_CCACHE=$ccache") }
android {
namespace = "me.bmax.apatch"
signingConfigs {
create("release") {
storeFile = file(keystoreProperties.getProperty("KEYSTORE_FILE") ?: "debug.keystore")
storePassword = keystoreProperties.getProperty("KEYSTORE_PASSWORD") ?: "android"
keyAlias = keystoreProperties.getProperty("KEY_ALIAS") ?: "androiddebugkey"
keyPassword = keystoreProperties.getProperty("KEY_PASSWORD") ?: "android"
enableV1Signing = true
enableV2Signing = true
enableV3Signing = true
enableV4Signing = true
}
}
buildTypes {
debug {
isDebuggable = true
isMinifyEnabled = false
isShrinkResources = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
externalNativeBuild {
cmake {
arguments += listOf("-DCMAKE_CXX_FLAGS_DEBUG=-Og", "-DCMAKE_C_FLAGS_DEBUG=-Og")
}
}
}
release {
isMinifyEnabled = true
isShrinkResources = true
isDebuggable = false
multiDexEnabled = true
vcsInfo.include = false
if (keystorePropertiesFile.exists()) {
signingConfig = signingConfigs.getByName("release")
}
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
externalNativeBuild {
cmake {
val relFlags = listOf(
"-flto", "-ffunction-sections", "-fdata-sections", "-Wl,--gc-sections",
"-fno-unwind-tables", "-fno-asynchronous-unwind-tables", "-Wl,--exclude-libs,ALL",
"-Ofast", "-fmerge-all-constants", "-flto=full", "-ffat-lto-objects",
"-fno-semantic-interposition", "-fno-threadsafe-statics"
)
cppFlags += relFlags
cFlags += relFlags
arguments += listOf("-DCMAKE_BUILD_TYPE=Release", "-DCMAKE_CXX_FLAGS_RELEASE=-O3 -DNDEBUG", "-DCMAKE_C_FLAGS_RELEASE=-O3 -DNDEBUG")
}
}
}
}
dependenciesInfo.includeInApk = false
buildFeatures {
aidl = true
buildConfig = true
compose = true
prefab = true
}
defaultConfig {
applicationId = "me.yuki.folk"
minSdk = androidMinSdkVersion
targetSdk = androidTargetSdkVersion
versionCode = managerVersionCode
versionName = managerVersionName
buildConfigField("String", "buildKPV", "\"$kernelPatchVersion\"")
buildConfigField("boolean", "DEBUG_FAKE_ROOT", localProperties.getProperty("debug.fake_root", "false"))
base.archivesName = "FolkPatch_${managerVersionCode}_${managerVersionName}_on_${branchName}"
ndk.abiFilters.addAll(arrayOf("arm64-v8a"))
externalNativeBuild {
cmake {
cppFlags += baseFlags + "-std=c++2b"
cFlags += baseFlags + "-std=c2x"
arguments += baseArgs
// Pass Token and Signature Hash to CMake
val authProps = Properties()
val authFile = rootProject.file("auth.properties")
if (authFile.exists()) {
authProps.load(FileInputStream(authFile))
}
val token = authProps.getProperty("api.token", "")
val signatureHash = authProps.getProperty("app.signature.hash", "")
// Pass to C++ compiler directly via flags
// Only add flags if values are non-empty to avoid compiler errors
if (token.isNotEmpty()) {
cppFlags += "-DAPI_TOKEN=\"$token\""
}
if (signatureHash.isNotEmpty()) {
cppFlags += "-DAPP_SIGNATURE_HASH=\"$signatureHash\""
}
cppFlags += "-DAPP_PACKAGE_NAME=\"$applicationId\""
abiFilters("arm64-v8a")
}
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
packaging {
jniLibs {
useLegacyPackaging = true
}
resources {
excludes += "**"
merges += "META-INF/com/google/android/**"
}
}
externalNativeBuild {
cmake {
version = "3.28.0+"
path("src/main/cpp/CMakeLists.txt")
}
}
androidResources {
generateLocaleConfig = true
}
compileSdk = androidCompileSdkVersion
ndkVersion = androidCompileNdkVersion
buildToolsVersion = androidBuildToolsVersion
lint {
abortOnError = false
checkReleaseBuilds = false
}
android.sourceSets.named("main") {
kotlin.directories += "build/generated/ksp/$name/kotlin"
jniLibs.directories += "libs"
}
}
// https://stackoverflow.com/a/77745844
tasks.withType<PackageAndroidArtifact> {
doFirst { appMetadata.asFile.orNull?.writeText("") }
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
kotlin {
jvmToolchain(21)
compilerOptions {
jvmTarget = JvmTarget.JVM_21
}
}
fun registerDownloadTask(
taskName: String, srcUrl: String, destPath: String, project: Project, version: String? = null
) {
project.tasks.register(taskName) {
val destFile = File(destPath)
val versionFile = File("$destPath.version")
doLast {
var forceDownload = false
if (version != null) {
if (!versionFile.exists() || versionFile.readText().trim() != version) {
forceDownload = true
}
}
if (!destFile.exists() || forceDownload || isFileUpdated(srcUrl, destFile)) {
println(" - Downloading $srcUrl to ${destFile.absolutePath}")
downloadFile(srcUrl, destFile)
if (version != null) {
versionFile.writeText(version)
}
println(" - Download completed.")
} else {
println(" - File is up-to-date, skipping download.")
}
}
}
}
fun isFileUpdated(url: String, localFile: File): Boolean {
val connection = URI.create(url).toURL().openConnection()
val remoteLastModified = connection.getHeaderFieldDate("Last-Modified", 0L)
return remoteLastModified > localFile.lastModified()
}
fun downloadFile(url: String, destFile: File) {
URI.create(url).toURL().openStream().use { input ->
destFile.outputStream().use { output ->
input.copyTo(output)
}
}
}
registerDownloadTask(
taskName = "downloadKpimg",
srcUrl = "https://github.com/LyraVoid/KernelPatch/releases/download/$kernelPatchVersion/kpimg-android",
destPath = "${project.projectDir}/src/main/assets/kpimg",
project = project,
version = kernelPatchVersion
)
registerDownloadTask(
taskName = "downloadKptools",
srcUrl = "https://github.com/LyraVoid/KernelPatch/releases/download/$kernelPatchVersion/kptools-android",
destPath = "${project.projectDir}/libs/arm64-v8a/libkptools.so",
project = project,
version = kernelPatchVersion
)
// Compat kp version less than 0.10.7
// TODO: Remove in future
registerDownloadTask(
taskName = "downloadCompatKpatch",
srcUrl = "https://github.com/bmax121/KernelPatch/releases/download/0.10.7/kpatch-android",
destPath = "${project.projectDir}/libs/arm64-v8a/libkpatch.so",
project = project,
version = "0.10.7"
)
tasks.register<Copy>("mergeScripts") {
into("${project.projectDir}/src/main/resources/META-INF/com/google/android")
from(rootProject.file("${project.rootDir}/scripts/update_binary.sh")) {
rename { "update-binary" }
}
from(rootProject.file("${project.rootDir}/scripts/update_script.sh")) {
rename { "updater-script" }
}
}
// Build fpd (FolkPatch service binary) for arm64
tasks.register<Exec>("buildFpd") {
executable("cargo")
args("ndk", "-t", "arm64-v8a", "build", "--release")
workingDir("${project.rootDir}/fpd")
doFirst {
println("Building fpd for arm64...")
}
doLast {
val fpdBinary = file("${project.rootDir}/fpd/target/aarch64-linux-android/release/fpd")
val serviceDir = file("src/main/assets/Service")
serviceDir.mkdirs()
fpdBinary.copyTo(file("${serviceDir}/fpd"), overwrite = true)
println("fpd binary built and copied to Service/fpd")
}
}
tasks.getByName("preBuild").dependsOn(
"downloadKpimg",
"downloadKptools",
"downloadCompatKpatch",
"mergeScripts",
"buildFpd",
)
// https://github.com/bbqsrc/cargo-ndk
// cargo ndk -t arm64-v8a build --release
tasks.register<Exec>("cargoBuild") {
executable("cargo")
args("ndk", "-t", "arm64-v8a", "build", "--release")
workingDir("${project.rootDir}/apd")
environment("APATCH_VERSION_CODE", "${managerVersionCode}")
environment("APATCH_VERSION_NAME", "${managerVersionCode}-Matsuzaka-yuki")
}
tasks.register<Copy>("buildApd") {
dependsOn("cargoBuild")
from("${project.rootDir}/apd/target/aarch64-linux-android/release/apd")
into("${project.projectDir}/libs/arm64-v8a")
rename("apd", "libapd.so")
}
tasks.configureEach {
if (name == "mergeDebugJniLibFolders" || name == "mergeReleaseJniLibFolders") {
dependsOn("buildApd")
}
}
tasks.register<Exec>("cargoClean") {
executable("cargo")
args("clean")
workingDir("${project.rootDir}/apd")
}
tasks.register<Delete>("apdClean") {
dependsOn("cargoClean")
delete(file("${project.projectDir}/libs/arm64-v8a/libapd.so"))
}
tasks.clean {
dependsOn("apdClean")
}
ksp {
arg("compose-destinations.defaultTransitions", "none")
}
dependencies {
implementation(libs.androidx.appcompat)
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.core.splashscreen)
implementation(libs.androidx.webkit)
implementation(libs.androidx.biometric)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.material.icons.extended)
implementation(libs.androidx.compose.material)
implementation(libs.androidx.compose.material3)
implementation("androidx.compose.material3:material3-android:1.5.0-alpha20")
implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.runtime.livedata)
debugImplementation(libs.androidx.compose.ui.test.manifest)
debugImplementation(libs.androidx.compose.ui.tooling)
implementation(libs.androidx.lifecycle.runtime.compose)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.lifecycle.viewmodel.compose)
implementation("androidx.lifecycle:lifecycle-process:2.10.0")
implementation(libs.compose.destinations.core)
ksp(libs.compose.destinations.ksp)
implementation(libs.com.github.topjohnwu.libsu.core)
implementation(libs.com.github.topjohnwu.libsu.service)
implementation(libs.com.github.topjohnwu.libsu.nio)
implementation(libs.com.github.topjohnwu.libsu.io)
implementation(libs.dev.rikka.rikkax.parcelablelist)
implementation(libs.io.coil.kt.coil.compose)
implementation(libs.io.coil.kt.coil.gif)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.me.zhanghai.android.appiconloader.coil)
implementation(libs.sheet.compose.dialogs.core)
implementation(libs.sheet.compose.dialogs.list)
implementation(libs.sheet.compose.dialogs.input)
implementation(libs.markdown)
implementation(libs.ini4j)
implementation(libs.google.code.gson)
implementation(libs.liquid)
compileOnly(libs.cxx)
}