Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin 2.0 #45

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ plugins {

android {
namespace = "com.jithub.app.android"
compileSdk = Versions.compileSdk
defaultConfig {
applicationId = "com.jithub.app.android"
minSdk = Versions.minSdk
targetSdk = Versions.targetSdk
versionCode = Versions.versionCode
versionName = Versions.versionName
}
Expand All @@ -23,12 +20,6 @@ android {
buildConfig = true
}

packaging {
resources {
excludes += Resources.excludes
}
}

val propertyFile by lazy { project.rootProject.file("keystore.properties") }
val currentSigning = if (propertyFile.exists() && propertyFile.canRead()) {
val properties = Properties().apply { propertyFile.inputStream().use { load(it) } }
Expand Down Expand Up @@ -72,30 +63,13 @@ android {
signingConfig = currentSigning
}
}

compileOptions {
sourceCompatibility = Versions.javaVersion
targetCompatibility = Versions.javaVersion
}

kotlinOptions {
jvmTarget = Versions.javaVersion.toString()
}
}

composeCompiler {
enableStrongSkippingMode = true
reportsDestination = layout.buildDirectory.dir("compose_compiler")
}

androidComponents {
onVariants(selector().withBuildType("release")) {
// Only exclude *.version files in release mode as debug mode requires
// these files for layout inspector to work.
it.packaging.resources.excludes.add("META-INF/*.version")
}
}

dependencies {
implementation(projects.shared)
// implementation(projects.sharedCompose)
Expand Down
5 changes: 3 additions & 2 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ dependencies {

gradlePlugin {
plugins {
val prefix = "com.jithub.gradle"
register("build-logic") {
id = "com.jithub.build-logic"
id = "$prefix.build-logic"
implementationClass = "BuildLogic"
}
register("build-support") {
id = "com.jithub.build-support"
id = "$prefix.build-support"
implementationClass = "BuildSupportPlugin"
}
}
Expand Down
123 changes: 117 additions & 6 deletions build-logic/convention/src/main/kotlin/BuildSupportPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import com.android.build.api.dsl.LibraryExtension
import com.android.build.api.dsl.Lint
import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.TestExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.KotlinJsCompile
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
Expand All @@ -11,14 +18,109 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

class BuildSupportPlugin : BasePlugin() {

override fun apply(target: Project) {
log("apply target: ${target.displayName}")
override fun apply(project: Project) {
log("apply target: ${project.displayName}")

target.group = "com.jithub.build-support"
target.version = "0.1"
project.group = "com.jithub.app.build-support"
project.version = Versions.versionName

target.configureCommonKotlin()
target.configureCommonCompose()
project.configureCommonAndroid()
project.configureCommonKotlin()
project.configureCommonCompose()
}

private fun Project.configureCommonAndroid() {
plugins.withId("com.android.base") {
val android = extensions.getByName("android") as BaseExtension
android.apply {
compileSdkVersion(Versions.compileSdk)
defaultConfig {
minSdk = Versions.minSdk
targetSdk = Versions.targetSdk
}

testOptions.animationsDisabled = true

sourceSets.configureEach {
java.srcDirs("src/$name/kotlin")
}

packagingOptions.apply {
resources {
excludes += Resources.excludes
}
}

compileOptions {
sourceCompatibility = Versions.javaVersion
targetCompatibility = Versions.javaVersion
}

tasks.withType(KotlinJvmCompile::class.java).configureEach {
compilerOptions {
// Treat all Kotlin warnings as errors (disabled by default)
allWarningsAsErrors.set(properties["warningsAsErrors"] as? Boolean ?: false)

freeCompilerArgs.set(
freeCompilerArgs.getOrElse(emptyList()) + listOf(
"-Xcontext-receivers",
"-opt-in=kotlin.RequiresOptIn",
// Enable experimental coroutines APIs, including Flow
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
// Enable experimental compose APIs
"-opt-in=androidx.compose.material3.ExperimentalMaterial3Api",
"-opt-in=androidx.lifecycle.compose.ExperimentalLifecycleComposeApi",
"-opt-in=androidx.compose.animation.ExperimentalSharedTransitionApi"
)
)

jvmTarget.set(Versions.jvmTarget)
}
}

with(project) {
when (android) {
is BaseAppModuleExtension -> configure<BaseAppModuleExtension> {
lint(lintConfigure())
}

is LibraryExtension -> configure<LibraryExtension> {
lint(lintConfigure())
}

is TestExtension -> configure<TestExtension> {
lint(lintConfigure())
}

else -> {
pluginManager.apply("com.android.lint")
configure<Lint>(lintConfigure())
}
}
}
}
}

plugins.withId("com.android.application") {
val android = extensions.getByName("android") as BaseAppModuleExtension
android.apply {
dependenciesInfo {
// Disables dependency metadata when building APKs.
includeInApk = false
// Disables dependency metadata when building Android App Bundles.
includeInBundle = false
}
}

val androidComponents = extensions.getByType(AndroidComponentsExtension::class.java)
with(androidComponents) {
onVariants(selector().withBuildType("release")) {
// Only exclude *.version files in release mode as debug mode requires
// these files for layout inspector to work.
it.packaging.resources.excludes.add("META-INF/*.version")
}
}
}
}

// https://github.com/cashapp/redwood/blob/trunk/build-support/src/main/kotlin/app/cash/redwood/buildsupport/RedwoodBuildPlugin.kt
Expand Down Expand Up @@ -105,4 +207,13 @@ class BuildSupportPlugin : BasePlugin() {
}
}
}

private fun lintConfigure(): Lint.() -> Unit = {
abortOnError = true
warningsAsErrors = false
ignoreTestSources = true
checkDependencies = true
checkReleaseBuilds = false // Full lint runs as part of 'build' task.
htmlReport = true
}
}
7 changes: 2 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
@file:Suppress("UnstableApiUsage")

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

buildscript {
repositories {
mavenCentral()
Expand All @@ -29,7 +26,7 @@ plugins {
// alias(libs.plugins.jetbrains.compose) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.spotless) apply false
id("com.jithub.build-logic")
id("com.jithub.gradle.build-logic")
}

allprojects {
Expand All @@ -44,7 +41,7 @@ allprojects {
}
maven("https://jitpack.io")
}
apply(plugin = "com.jithub.build-support")
apply(plugin = "com.jithub.gradle.build-support")
}

subprojects {
Expand Down
7 changes: 0 additions & 7 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,13 @@ multiplatformResources {

android {
namespace = "com.jithub.app.shared"
compileSdk = Versions.compileSdk

defaultConfig {
minSdk = Versions.minSdk
}

buildFeatures {
buildConfig = true
}

compileOptions {
sourceCompatibility = Versions.javaVersion
targetCompatibility = Versions.javaVersion
}
}

/* task 'testClasses' not found in project */
Expand Down
40 changes: 30 additions & 10 deletions spotless/spotless.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: "com.diffplug.spotless"

// https://github.com/pinterest/ktlint
def ktlintVersion = "1.3.1"

spotless {
java {
target("**/*.java")
targetExclude("**/build/**/*.java", "${rootProject.rootDir}/spotless/**")
targetExclude(
"**/build/**/*.java",
"${rootProject.rootDir}/spotless/**"
)
toggleOffOn("@formatter:off", "@formatter:on")
importOrder()
removeUnusedImports()
Expand All @@ -17,10 +17,19 @@ spotless {
formatAnnotations()
}

// https://github.com/pinterest/ktlint
def ktlintVersion = "1.3.1"

kotlin {
target("**/*.kt")
targetExclude("**/build/**/*.kt", "${rootProject.rootDir}/spotless/**")
ktlint(ktlintVersion).setEditorConfigPath(
targetExclude(
"**/build/**/*.kt",
"${rootProject.rootDir}/spotless/**"
)
ktlint(ktlintVersion).customRuleSets(
// https://github.com/mrmans0n/compose-rules
["io.nlopez.compose.rules:ktlint:0.4.5"]
).setEditorConfigPath(
"${rootProject.rootDir}/.editorconfig"
).editorConfigOverride(
[
Expand All @@ -33,20 +42,31 @@ spotless {
"ktlint_function_naming_ignore_when_annotated_with": "Composable",
"ktlint_compose_compositionlocal-allowlist" : "disabled"
]
).customRuleSets(
// https://github.com/mrmans0n/compose-rules
["io.nlopez.compose.rules:ktlint:0.4.5"]
)
}

kotlinGradle {
target("**/*.gradle.kts")
targetExclude(
"**/build/**/*.kts",
"${rootProject.rootDir}/spotless/**"
)
ktlint(ktlintVersion).setEditorConfigPath("${rootProject.rootDir}/.editorconfig")
}

format("xml") {
target("**/*.xml")
targetExclude("**/build/**/*.xml", "**/lint-config/**/*.xml", "${rootProject.rootDir}/spotless/**")
targetExclude(
"**/.idea/**/*.xml",
"**/.run/**/*.xml",
"**/.kotlin/**/*.xml",
"**/build/**/*.xml",
"**/lint-config/**/*.xml",
"${rootProject.rootDir}/spotless/**"
)
indentWithSpaces(2)
trimTrailingWhitespace()
endWithNewline()
// Look for the first XML tag that isn't a comment (<!--) or the xml declaration (<?xml)
licenseHeaderFile(rootProject.file("spotless/copyright.xml"), "(<[^!?])")
}
Expand Down
Loading