-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.gradle.kts
371 lines (306 loc) · 11.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
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
/*
* Copyright © 2024 Mark Raynsford <[email protected]> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
import com.android.build.api.dsl.ApplicationExtension
import com.android.build.gradle.LibraryExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
val gradleVersionRequired = "8.10"
val gradleVersionReceived = gradle.gradleVersion
if (gradleVersionRequired != gradleVersionReceived) {
throw GradleException(
"Gradle version $gradleVersionRequired is required to run this build. You are using Gradle $gradleVersionReceived",
)
}
plugins {
signing
id("org.jetbrains.kotlin.jvm")
.version("2.0.20")
.apply(false)
id("org.jetbrains.kotlin.android")
.version("2.0.20")
.apply(false)
id("com.android.library")
.version("8.5.0")
.apply(false)
id("com.android.application")
.version("8.5.0")
.apply(false)
}
/*
* The various paths used during the build.
*/
val io7mRootBuildDirectory =
"$rootDir/build"
val io7mDeployDirectory =
"$io7mRootBuildDirectory/maven"
/**
* Convenience functions to read strongly-typed values from property files.
*/
fun property(
project: Project,
name: String,
): String {
return project.extra[name] as String
}
fun propertyOptional(project: Project, name: String): String? {
val map = project.extra
if (map.has(name)) {
return map[name] as String?
}
return null
}
fun propertyInt(
project: Project,
name: String,
): Int {
val text = property(project, name)
return text.toInt()
}
fun propertyBoolean(
project: Project,
name: String,
): Boolean {
val text = property(project, name)
return text.toBooleanStrict()
}
fun propertyBooleanOptional(
project: Project,
name: String,
defaultValue: Boolean,
): Boolean {
val value = propertyOptional(project, name) ?: return defaultValue
return value.toBooleanStrict()
}
allprojects {
/*
* Configure the project metadata.
*/
this.group =
property(this, "GROUP")
this.version =
property(this, "VERSION_NAME")
val jdkBuild =
propertyInt(this, "com.io7m.build.jdkBuild")
val jdkBytecodeTarget =
propertyInt(this, "com.io7m.build.jdkBytecodeTarget")
/*
* Configure builds and tests for various project types.
*/
when (extra["POM_PACKAGING"]) {
"pom" -> {
logger.info("Configuring ${this.project} $version as a pom project")
}
"apk" -> {
logger.info("Configuring ${this.project} $version as an apk project")
apply(plugin = "com.android.application")
apply(plugin = "org.jetbrains.kotlin.android")
/*
* Configure the JVM toolchain version that we want to use for Kotlin.
*/
val kotlin: KotlinAndroidProjectExtension =
this.extensions["kotlin"] as KotlinAndroidProjectExtension
val java: JavaPluginExtension =
this.extensions["java"] as JavaPluginExtension
kotlin.jvmToolchain(jdkBuild)
java.toolchain.languageVersion.set(JavaLanguageVersion.of(jdkBuild))
/*
* Configure the various required Android properties.
*/
val android: ApplicationExtension =
this.extensions["android"] as ApplicationExtension
android.namespace =
property(this, "POM_ARTIFACT_ID")
android.compileSdk =
propertyInt(this, "com.io7m.build.androidSDKCompile")
android.defaultConfig {
multiDexEnabled = true
targetSdk =
propertyInt(this@allprojects, "com.io7m.build.androidSDKTarget")
minSdk =
propertyInt(this@allprojects, "com.io7m.build.androidSDKMinimum")
}
/*
* Produce JDK bytecode of the correct version.
*/
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = jdkBytecodeTarget.toString()
}
java.sourceCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
java.targetCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
android.compileOptions {
encoding = "UTF-8"
sourceCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
targetCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
}
}
"aar" -> {
logger.info("Configuring ${this.project} $version as an aar project")
apply(plugin = "com.android.library")
apply(plugin = "org.jetbrains.kotlin.android")
/*
* Configure the JVM toolchain version that we want to use for Kotlin.
*/
val kotlin: KotlinAndroidProjectExtension =
this.extensions["kotlin"] as KotlinAndroidProjectExtension
val java: JavaPluginExtension =
this.extensions["java"] as JavaPluginExtension
kotlin.jvmToolchain(jdkBuild)
java.toolchain.languageVersion.set(JavaLanguageVersion.of(jdkBuild))
/*
* Configure the various required Android properties.
*/
val android: LibraryExtension =
this.extensions["android"] as LibraryExtension
android.namespace =
property(this, "POM_ARTIFACT_ID")
android.compileSdk =
propertyInt(this, "com.io7m.build.androidSDKCompile")
android.defaultConfig {
multiDexEnabled = true
minSdk = propertyInt(this@allprojects, "com.io7m.build.androidSDKMinimum")
}
/*
* Produce JDK bytecode of the correct version.
*/
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = jdkBytecodeTarget.toString()
}
java.sourceCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
java.targetCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
android.compileOptions {
encoding = "UTF-8"
sourceCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
targetCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
}
}
"jar" -> {
logger.info("Configuring ${this.project} $version as a jar project")
apply(plugin = "java-library")
apply(plugin = "org.jetbrains.kotlin.jvm")
/*
* Configure the JVM toolchain versions that we want to use for Kotlin and Java.
*/
val kotlin: KotlinProjectExtension =
this.extensions["kotlin"] as KotlinProjectExtension
val java: JavaPluginExtension =
this.extensions["java"] as JavaPluginExtension
kotlin.jvmToolchain(jdkBuild)
java.toolchain.languageVersion.set(JavaLanguageVersion.of(jdkBuild))
/*
* Produce JDK bytecode of the correct version.
*/
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions.jvmTarget = jdkBytecodeTarget.toString()
}
java.sourceCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
java.targetCompatibility = JavaVersion.toVersion(jdkBytecodeTarget)
/*
* Configure JUnit tests.
*/
tasks.named<Test>("test") {
useJUnitPlatform()
// Required for the Mockito ByteBuddy agent on modern VMs.
systemProperty("jdk.attach.allowAttachSelf", "true")
testLogging {
events("passed")
}
this.reports.html.required = true
this.reports.junitXml.required = true
}
}
}
/*
* Configure some aggressive version resolution behaviour. The listed configurations have
* transitive dependency resolution enabled; all other configurations do not. This forces
* projects to be extremely explicit about what is imported.
*/
val transitiveConfigurations = setOf(
"androidTestDebugImplementation",
"androidTestDebugImplementationDependenciesMetadata",
"androidTestImplementation",
"androidTestImplementationDependenciesMetadata",
"androidTestReleaseImplementation",
"androidTestReleaseImplementationDependenciesMetadata",
"annotationProcessor",
"debugAndroidTestCompilationImplementation",
"debugAndroidTestImplementation",
"debugAndroidTestImplementationDependenciesMetadata",
"debugAnnotationProcessor",
"debugAnnotationProcessorClasspath",
"debugUnitTestCompilationImplementation",
"debugUnitTestImplementation",
"debugUnitTestImplementationDependenciesMetadata",
"kotlinBuildToolsApiClasspath",
"kotlinCompilerClasspath",
"kotlinCompilerPluginClasspath",
"kotlinCompilerPluginClasspathDebug",
"kotlinCompilerPluginClasspathDebugAndroidTest",
"kotlinCompilerPluginClasspathDebugUnitTest",
"kotlinCompilerPluginClasspathMain",
"kotlinCompilerPluginClasspathRelease",
"kotlinCompilerPluginClasspathReleaseUnitTest",
"kotlinCompilerPluginClasspathTest",
"kotlinKlibCommonizerClasspath",
"kotlinNativeCompilerPluginClasspath",
"kotlinScriptDef",
"kotlinScriptDefExtensions",
"mainSourceElements",
"releaseAnnotationProcessor",
"releaseAnnotationProcessorClasspath",
"releaseUnitTestCompilationImplementation",
"releaseUnitTestImplementation",
"releaseUnitTestImplementationDependenciesMetadata",
"testDebugImplementation",
"testDebugImplementationDependenciesMetadata",
"testFixturesDebugImplementation",
"testFixturesDebugImplementationDependenciesMetadata",
"testFixturesImplementation",
"testFixturesImplementationDependenciesMetadata",
"testFixturesReleaseImplementation",
"testFixturesReleaseImplementationDependenciesMetadata",
"testImplementation",
"testImplementationDependenciesMetadata",
"testReleaseImplementation",
"testReleaseImplementationDependenciesMetadata",
)
/*
* Write the set of available configurations to files, for debugging purposes. Plugins can
* add new configurations at any time, and so it's nice to have a list of the available
* configurations visible.
*/
val configurationsActual = mutableSetOf<String>()
afterEvaluate {
configurations.all {
configurationsActual.add(this.name)
}
// File("configurations.txt").writeText(configurationsActual.joinToString("\n"))
}
afterEvaluate {
configurations.all {
isTransitive = transitiveConfigurations.contains(name)
// resolutionStrategy.failOnVersionConflict()
}
}
/*
* Configure all "test" tasks to be disabled. The tests are enabled only in those modules
* that specifically ask for them. Why do this? Because the Android plugins do lots of
* expensive per-module configuration for tests that don't exist.
*/
afterEvaluate {
tasks.matching { task -> task.name.contains("UnitTest") }
.forEach { task -> task.enabled = false }
}
}