-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
370 lines (318 loc) · 13 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
@file:Suppress("NOTHING_TO_INLINE", "FunctionName", "UnstableApiUsage", "UNCHECKED_CAST")
import Build_gradle.CommonComponentsExtensionT
import Build_gradle.CommonExtensionT
import com.android.build.api.dsl.CommonExtension
import com.android.build.api.dsl.LibraryExtension
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.squareup.anvil.plugin.AnvilExtension
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.konan.target.Architecture
import org.jetbrains.kotlin.konan.target.Family
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.noarg) apply false
alias(libs.plugins.kotlin.compose) apply false
alias(libs.plugins.anvil) apply false
alias(libs.plugins.molecule) apply false
alias(libs.plugins.ktor) apply false
alias(libs.plugins.versions)
}
subprojects {
JvmTargetConvention()
AllTheBoms()
TestsConvention()
AndroidConvention()
KotlinConvention()
AnvilConvention()
ComposeConvention()
UseMoshiVersion()
SimpleLayoutConvention()
}
// region Conventions
fun Project.JvmTargetConvention() {
val target by lazy { libs.versions.jvmTarget.get() }
withPlugin("java") {
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.toVersion(target)
sourceCompatibility = JavaVersion.toVersion(target)
}
}
withAnyKotlinPlugin {
kotlin.jvmCompilerOptions {
jvmTarget = JvmTarget.fromTarget(target)
}
}
withAnyAndroidPlugin {
android.compileOptions {
targetCompatibility = JavaVersion.toVersion(target)
targetCompatibility = JavaVersion.toVersion(target)
}
}
}
fun Project.AllTheBoms() {
configurations.named { "kotlinCompiler" !in it }.configureEach {
val configuration = this
dependencies {
withAnyKotlinPlugin {
configuration(platform(libs.ktor.bom))
configuration(platform(libs.kotlin.bom))
configuration(platform(libs.kotlinx.coroutines.bom))
configuration(platform(libs.kotlinx.serialization.bom))
}
withAnyAndroidPlugin {
configuration(platform(libs.okio.bom))
configuration(platform(libs.okHttp.bom))
}
}
}
}
fun Project.TestsConvention() {
tasks.withType<Test>().configureEach {
useJUnit()
testLogging.showStandardStreams = true
}
}
fun Project.AndroidConvention() {
withAnyAndroidPlugin {
android {
compileSdk = 34
defaultConfig.minSdk = 26
compileOptions { isCoreLibraryDesugaringEnabled = true }
dependencies { "coreLibraryDesugaring"(libs.android.tools.desugar) }
}
}
}
fun Project.KotlinConvention() {
withAnyKotlinPlugin {
kotlin {
jvmToolchain(libs.versions.jvm.get().toInt())
compilerOptions {
verbose = true
progressiveMode = true
optIn.appendAll(
"kotlin.RequiresOptIn",
"kotlin.ExperimentalStdlibApi",
"kotlin.ExperimentalMultiplatform",
"kotlin.time.ExperimentalTime",
"kotlin.experimental.ExperimentalTypeInference",
"kotlinx.coroutines.FlowPreview",
"kotlinx.coroutines.ExperimentalCoroutinesApi",
"coil.annotation.ExperimentalCoilApi",
"dev.efemoney.lexiko.engine.internal.InternalEngineApi",
)
freeCompilerArgs.appendAll(
"-XXLanguage:+ValueClasses",
"-XXLanguage:+ContextReceivers",
"-XXLanguage:+BreakContinueInInlineLambdas",
"-XXLanguage:+UnitConversionsOnArbitraryExpressions",
"-XXLanguage:+PackagePrivateFileClassesWithAllPrivateMembers", // KT-10884
"-Xself-upper-bound-inference",
"-Xunrestricted-builder-inference",
"-Xjsr305=strict",
"-Xassertions=jvm", // https://publicobject.com/2019/11/18/kotlins-assert-is-not-like-javas-assert/
"-Xjvm-default=all", // https://blog.jetbrains.com/kotlin/2020/07/kotlin-1-4-m3-generating-default-methods-in-interfaces/
"-Xemit-jvm-type-annotations", // useful for static analysis tools or annotation processors.
"-Xproper-ieee754-comparisons",
)
}
jvmCompilerOptions {
javaParameters = true
}
}
}
}
fun Project.AnvilConvention() {
withPlugin("com.squareup.anvil") {
configure<AnvilExtension> {
// We configure Anvil for modules that do not @MergeComponents to:
// - generate factories, so that we can skip dagger compiler entirely for the module
// - we also disable component merging: the idea here is that modules either merge components
// or they contribute dependencies, never both.
val mergesComponent = project.name in listOf("integration")
generateDaggerFactories = !mergesComponent
disableComponentMerging = !mergesComponent
/*syncGeneratedSources = true*/
}
dependencies {
"anvil"(projects.libs.di.codegen)
"implementation"(libs.dagger)
}
}
}
fun Project.SimpleLayoutConvention() {
withPlugin("java") {
the<JavaPluginExtension>().sourceSets.configureEach {
java.setSrcDirs(listOf(simpleName(name, "src")))
resources.setSrcDirs(listOf(simpleName(name, "resources")))
}
}
withAnyKotlinPlugin {
kotlin.sourceSets.configureEach {
kotlin.setSrcDirs(listOf(simpleName(name, "src")))
resources.setSrcDirs(listOf(simpleName(name, "resources")))
}
}
withAnyAndroidPlugin {
android.sourceSets.configureEach {
// AS & IntelliJ map (roughly) a source set to a 'module'
// This is important because in AS, whatever folder the manifest.xml file lives in becomes a source root
// according to this comment https://issuetracker.google.com/issues/232007221#comment13
// We keep manifest files in their own unique folders so intellij does not choke on "duplicate source roots"
manifest.srcFile("$name/AndroidManifest.xml")
java.setSrcDirs(listOf(simpleName(name, "src")))
kotlin.setSrcDirs(listOf(simpleName(name, "src")))
res.setSrcDirs(listOf(simpleName(name, "res")))
resources.setSrcDirs(listOf(simpleName(name, "resources")))
assets.setSrcDirs(listOf(simpleName(name, "assets")))
aidl.setSrcDirs(listOf(simpleName(name, "aidl")))
renderscript.setSrcDirs(listOf(simpleName(name, "renderscript")))
jniLibs.setSrcDirs(listOf(simpleName(name, "jniLibs")))
shaders.setSrcDirs(listOf(simpleName(name, "shaders")))
mlModels.setSrcDirs(listOf(simpleName(name, "mlModels")))
baselineProfiles.setSrcDirs(listOf(simpleName(name, "baselineProfiles")))
}
}
}
fun Project.ComposeConvention() {
withPlugin("org.jetbrains.kotlin.plugin.compose") {
configure<ComposeCompilerGradlePluginExtension> {
val reporting = the<ReportingExtension>()
val reportsEnabled = providers.gradleProperty("lexiko.compose.enableReports")
val reportsDir = reporting.baseDirectory.dir("compose").filter { reportsEnabled.orNull == "true" }
reportsDestination = reportsDir
metricsDestination = reportsDir
enableIntrinsicRemember = true
enableExperimentalStrongSkippingMode = true
stabilityConfigurationFile = rootProject.file("compose-stability-config.txt")
}
dependencies {
"implementation"(libs.jetbrains.compose.runtime)
}
}
}
fun Project.UseMoshiVersion() {
// MoshiX add a moshi dependency with version 1.13.0, we override with our own higher version
withPlugin("dev.zacsweers.moshix") {
dependencies {
"implementation"(libs.moshi)
}
}
}
fun simpleName(name: String, suffix: String) = if (name == "main") suffix else "$name-$suffix"
// endregion
tasks {
register<Delete>("clean") {
delete(rootProject.layout.buildDirectory)
}
dependencyUpdates {
rejectVersionIf { "ide" in candidate.version || "dev" in candidate.version }
}
}
// region DSL
private typealias CommonExtensionT = CommonExtension<*, *, *, *, *, *>
private typealias CommonComponentsExtensionT<T> = AndroidComponentsExtension<T, *, *>
inline val Project.buildDirectory get() = layout.buildDirectory
inline val ProviderFactory.isCi get() = environmentVariable("CI").map(String::toBoolean).getOrElse(false)
inline fun Project.withAnyImplementationAndApiConfigurations(action: Action<Configuration>) {
configurations
.named { it.contains("api", true) || it.contains("implementation", true) }
.configureEach(action)
}
inline fun Project.withPlugin(plugin: String, action: Action<AppliedPlugin>) =
pluginManager.withPlugin(plugin, action)
inline fun Project.withAnyPlugin(vararg plugins: String, action: Action<AppliedPlugin>) =
plugins.forEach { withPlugin(it, action) }
inline fun Project.withAnyKotlinPlugin(action: Action<AppliedKotlinPlugin<KotlinProjectExtension>>) {
withAnyPlugin(
"org.jetbrains.kotlin.jvm",
"org.jetbrains.kotlin.android",
"org.jetbrains.kotlin.multiplatform",
) { action.execute(AppliedKotlinPlugin(this)) }
}
inline fun Project.withAnyAndroidPlugin(
action: Action<AppliedAndroidPlugin<CommonExtensionT, CommonComponentsExtensionT<CommonExtensionT>>>
) {
withAnyPlugin("com.android.application", "com.android.library") {
action.execute(AppliedAndroidPlugin(this))
}
}
inline fun Project.withAndroidLibrary(
action: Action<AppliedAndroidPlugin<LibraryExtension, LibraryAndroidComponentsExtension>>
) {
withPlugin("com.android.library") {
action.execute(AppliedAndroidPlugin(this))
}
}
inline fun Project.withKotlinMultiplatformPlugin(action: Action<AppliedKotlinPlugin<KotlinMultiplatformExtension>>) {
withPlugin("org.jetbrains.kotlin.multiplatform") {
action.execute(AppliedKotlinPlugin(this))
}
}
inline fun Project.withKotlinJvmOrAndroidPlugins(action: Action<AppliedKotlinPlugin<KotlinProjectExtension>>) {
withAnyPlugin("org.jetbrains.kotlin.jvm", "org.jetbrains.kotlin.android") {
action.execute(AppliedKotlinPlugin(this))
}
}
inline fun Configuration.withDependency(
dependency: Provider<MinimalExternalModuleDependency>,
action: Action<ExternalModuleDependency>,
) {
incoming.dependencies.configureEach {
if (this is ExternalModuleDependency && this.module == dependency.get().module) action(this)
}
}
inline fun Configuration.withDependency(
dependency: ProviderConvertible<MinimalExternalModuleDependency>,
action: Action<ExternalModuleDependency>,
) = withDependency(dependency.asProvider(), action)
class AppliedAndroidPlugin<T : CommonExtensionT, U : CommonComponentsExtensionT<T>>(
appliedPlugin: AppliedPlugin
) : AppliedPlugin by appliedPlugin {
inline val Project.android get() = extensions.getByName("android") as T
inline val Project.androidComponents get() = extensions.getByName("androidComponents") as U
inline fun Project.android(action: Action<T>) = extensions.configure("android", action)
inline fun Project.androidComponents(action: Action<U>) = extensions.configure("androidComponents", action)
}
class AppliedKotlinPlugin<T : KotlinProjectExtension>(appliedPlugin: AppliedPlugin) : AppliedPlugin by appliedPlugin {
inline val Project.kotlin get() = extensions.getByName("kotlin") as T
inline fun Project.kotlin(action: Action<T>) = extensions.configure("kotlin", action)
inline fun KotlinProjectExtension.compilerOptions(action: Action<KotlinCommonCompilerOptions>) {
if (this is KotlinJvmProjectExtension) action.execute(compilerOptions)
if (this is KotlinAndroidProjectExtension) action.execute(compilerOptions)
@OptIn(ExperimentalKotlinGradlePluginApi::class)
if (this is KotlinMultiplatformExtension) action.execute(compilerOptions)
}
inline fun KotlinProjectExtension.jvmCompilerOptions(action: Action<KotlinJvmCompilerOptions>) {
if (this is KotlinJvmProjectExtension) compilerOptions(action)
if (this is KotlinAndroidProjectExtension) compilerOptions(action)
}
inline fun KotlinMultiplatformExtension.nativeTargets(
family: Family? = null,
architecture: Architecture? = null,
action: Action<KotlinNativeTarget>,
) {
targets.withType<KotlinNativeTarget>()
.matching {
(family == null || it.konanTarget.family == family) &&
(architecture == null || it.konanTarget.architecture == architecture)
}
.configureEach(action)
}
inline fun KotlinMultiplatformExtension.iosTargets(action: Action<KotlinNativeTarget>) {
nativeTargets(Family.IOS, action = action)
}
}
// endregion