This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 772
/
build.gradle.kts
238 lines (219 loc) · 8.62 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
import com.android.build.api.dsl.ApplicationExtension
import com.android.build.api.variant.ApplicationAndroidComponentsExtension
import com.android.build.gradle.BaseExtension
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.internal.storage.file.FileRepository
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
plugins {
alias(libs.plugins.agp.lib) apply false
alias(libs.plugins.agp.app) apply false
alias(lspatch.plugins.kotlin.android) apply false
}
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath("org.eclipse.jgit:org.eclipse.jgit:6.3.0.202209071007-r")
}
}
val commitCount = run {
val repo = FileRepository(rootProject.file(".git"))
val refId = repo.refDatabase.exactRef("refs/remotes/origin/master").objectId!!
Git(repo).log().add(refId).call().count()
}
val (coreCommitCount, coreLatestTag) = FileRepositoryBuilder().setGitDir(rootProject.file(".git/modules/core"))
.runCatching {
build().use { repo ->
val git = Git(repo)
val coreCommitCount =
git.log()
.add(repo.refDatabase.exactRef("HEAD").objectId)
.call().count() + 4200
val ver = git.describe()
.setTags(true)
.setAbbrev(0).call().removePrefix("v")
coreCommitCount to ver
}
}.getOrNull() ?: (1 to "1.0")
// sync from https://github.com/LSPosed/LSPosed/blob/master/build.gradle.kts
val defaultManagerPackageName by extra("org.lsposed.lspatch")
val apiCode by extra(93)
val verCode by extra(commitCount)
val verName by extra("0.6")
val coreVerCode by extra(coreCommitCount)
val coreVerName by extra(coreLatestTag)
val androidMinSdkVersion by extra(28)
val androidTargetSdkVersion by extra(34)
val androidCompileSdkVersion by extra(34)
val androidCompileNdkVersion by extra("25.2.9519653")
val androidBuildToolsVersion by extra("34.0.0")
val androidSourceCompatibility by extra(JavaVersion.VERSION_17)
val androidTargetCompatibility by extra(JavaVersion.VERSION_17)
tasks.register<Delete>("clean") {
delete(rootProject.buildDir)
}
listOf("Debug", "Release").forEach { variant ->
tasks.register("build$variant") {
description = "Build LSPatch with $variant"
dependsOn(projects.jar.dependencyProject.tasks["build$variant"])
dependsOn(projects.manager.dependencyProject.tasks["build$variant"])
}
}
tasks.register("buildAll") {
dependsOn("buildDebug", "buildRelease")
}
fun Project.configureBaseExtension() {
extensions.findByType(BaseExtension::class)?.run {
compileSdkVersion(androidCompileSdkVersion)
ndkVersion = androidCompileNdkVersion
buildToolsVersion = androidBuildToolsVersion
externalNativeBuild.cmake {
version = "3.22.1+"
}
defaultConfig {
minSdk = androidMinSdkVersion
targetSdk = androidTargetSdkVersion
versionCode = verCode
versionName = verName
signingConfigs.create("config") {
val androidStoreFile = project.findProperty("androidStoreFile") as String?
if (!androidStoreFile.isNullOrEmpty()) {
storeFile = rootProject.file(androidStoreFile)
storePassword = project.property("androidStorePassword") as String
keyAlias = project.property("androidKeyAlias") as String
keyPassword = project.property("androidKeyPassword") as String
}
}
externalNativeBuild {
cmake {
arguments += "-DEXTERNAL_ROOT=${File(rootDir.absolutePath, "core/external")}"
arguments += "-DCORE_ROOT=${File(rootDir.absolutePath, "core/core/src/main/jni")}"
abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64")
val flags = arrayOf(
"-Wall",
"-Qunused-arguments",
"-Wno-gnu-string-literal-operator-template",
"-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__",
)
cppFlags("-std=c++20", *flags)
cFlags("-std=c18", *flags)
arguments(
"-DANDROID_STL=none",
"-DVERSION_CODE=$verCode",
"-DVERSION_NAME=$verName",
)
}
}
}
compileOptions {
targetCompatibility(androidTargetCompatibility)
sourceCompatibility(androidSourceCompatibility)
}
buildTypes {
all {
signingConfig = if (signingConfigs["config"].storeFile != null) signingConfigs["config"] else signingConfigs["debug"]
}
named("debug") {
externalNativeBuild {
cmake {
arguments.addAll(
arrayOf(
"-DCMAKE_CXX_FLAGS_DEBUG=-Og",
"-DCMAKE_C_FLAGS_DEBUG=-Og",
)
)
}
}
}
named("release") {
externalNativeBuild {
cmake {
val flags = arrayOf(
"-Wl,--exclude-libs,ALL",
"-ffunction-sections",
"-fdata-sections",
"-Wl,--gc-sections",
"-fno-unwind-tables",
"-fno-asynchronous-unwind-tables",
"-flto=thin",
"-Wl,--thinlto-cache-policy,cache_size_bytes=300m",
"-Wl,--thinlto-cache-dir=${buildDir.absolutePath}/.lto-cache",
)
cppFlags.addAll(flags)
cFlags.addAll(flags)
val configFlags = arrayOf(
"-Oz",
"-DNDEBUG"
).joinToString(" ")
arguments.addAll(
arrayOf(
"-DCMAKE_CXX_FLAGS_RELEASE=$configFlags",
"-DCMAKE_CXX_FLAGS_RELWITHDEBINFO=$configFlags",
"-DCMAKE_C_FLAGS_RELEASE=$configFlags",
"-DCMAKE_C_FLAGS_RELWITHDEBINFO=$configFlags",
"-DDEBUG_SYMBOLS_PATH=${buildDir.absolutePath}/symbols",
)
)
}
}
}
}
}
extensions.findByType(ApplicationExtension::class)?.lint {
abortOnError = true
checkReleaseBuilds = false
}
extensions.findByType(ApplicationAndroidComponentsExtension::class)?.let { androidComponents ->
val optimizeReleaseRes = task("optimizeReleaseRes").doLast {
val aapt2 = File(
androidComponents.sdkComponents.sdkDirectory.get().asFile,
"build-tools/${androidBuildToolsVersion}/aapt2"
)
val zip = java.nio.file.Paths.get(
project.buildDir.path,
"intermediates",
"optimized_processed_res",
"release",
"resources-release-optimize.ap_"
)
val optimized = File("${zip}.opt")
val cmd = exec {
commandLine(
aapt2, "optimize",
"--collapse-resource-names",
"--enable-sparse-encoding",
"-o", optimized,
zip
)
isIgnoreExitValue = false
}
if (cmd.exitValue == 0) {
delete(zip)
optimized.renameTo(zip.toFile())
}
}
tasks.configureEach {
if (name == "optimizeReleaseResources") {
finalizedBy(optimizeReleaseRes)
}
}
}
}
subprojects {
plugins.withId("com.android.application") {
configureBaseExtension()
}
plugins.withId("com.android.library") {
configureBaseExtension()
}
}