Skip to content

Commit

Permalink
Migrated To Kotlin 2.0 (#1751)
Browse files Browse the repository at this point in the history
* Migrated To Kotlin 2.0

* Workflow CI Update
  • Loading branch information
niyajali authored Aug 24, 2024
1 parent a12b843 commit 2a6dafe
Show file tree
Hide file tree
Showing 45 changed files with 1,117 additions and 998 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/master_dev_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ jobs:

- name: Prevent updating Dependency Guard baselines if this is a fork
id: checkfork_dependencyguard
continue-on-error: false
continue-on-error: true
if: steps.dependencyguard_verify.outcome == 'failure' && github.event.pull_request.head.repo.full_name != github.repository
run: |
echo "::error::Dependency Guard failed, please update baselines with: ./gradlew dependencyGuardBaseline" && exit 1
Expand Down Expand Up @@ -111,13 +111,14 @@ jobs:
java-version: 17
- name: Run tests
run: |
./gradlew testDemoDebug
./gradlew testDemoDebug lintProdRelease
- name: Upload reports
if: always()
uses: actions/upload-artifact@v4
with:
name: test-and-lint-reports
path: |
**/build/reports/lint-results-*.html
**/build/test-results/test*UnitTest/**.xml
build:
Expand All @@ -131,10 +132,10 @@ jobs:
java-version: 17

- name: Build APKs
run: ./gradlew :app:assemble
run: ./gradlew :mifospay:assembleDemoDebug

- name: Check badging
run: ./gradlew :app:checkProdReleaseBadging
run: ./gradlew :mifospay:checkProdReleaseBadging

- name: Upload APKs
uses: actions/upload-artifact@v4
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ build/
# Gradle cache
.gradle

# Kotlin
.kotlin

# Android Studio captures folder
captures/

Expand Down
12 changes: 7 additions & 5 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
`kotlin-dsl`
}

group = "org.mifospay.buildlogic"

// Configure the build-logic plugins to target JDK 17
// Configure the build-logic plugins to target JDK 19
// This matches the JDK used to build the project, and is not related to what is running on device.
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()

kotlin {
compilerOptions {
jvmTarget = JvmTarget.JVM_17
}
}

dependencies {
compileOnly(libs.android.gradlePlugin)
compileOnly(libs.android.tools.common)
compileOnly(libs.compose.gradlePlugin)
compileOnly(libs.firebase.crashlytics.gradlePlugin)
compileOnly(libs.firebase.performance.gradlePlugin)
compileOnly(libs.kotlin.gradlePlugin)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import com.android.build.api.dsl.ApplicationExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.getByType
import org.mifospay.configureAndroidCompose

class AndroidApplicationComposeConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
pluginManager.apply("com.android.application")
apply(plugin = "com.android.application")
apply(plugin = "org.jetbrains.kotlin.plugin.compose")

val extension = extensions.getByType<ApplicationExtension>()
configureAndroidCompose(extension)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import com.android.build.gradle.LibraryExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.getByType
import org.mifospay.configureAndroidCompose

class AndroidLibraryComposeConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
pluginManager.apply("com.android.library")
apply(plugin = "org.jetbrains.kotlin.plugin.compose")

val extension = extensions.getByType<LibraryExtension>()
configureAndroidCompose(extension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package org.mifospay

import com.android.build.api.dsl.CommonExtension
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.assign
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.compose.compiler.gradle.ComposeCompilerGradlePluginExtension

/**
* Configure Compose-specific options
Expand All @@ -17,14 +19,11 @@ internal fun Project.configureAndroidCompose(
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = libs.findVersion("androidxComposeCompiler").get().toString()
}

dependencies {
val bom = libs.findLibrary("androidx-compose-bom").get()
add("implementation", platform(bom))
add("androidTestImplementation", platform(bom))

add("implementation", libs.findLibrary("androidx-compose-ui-tooling-preview").get())
add("debugImplementation", libs.findLibrary("androidx-compose-ui-tooling").get())
}
Expand All @@ -33,45 +32,33 @@ internal fun Project.configureAndroidCompose(
unitTests {
// For Robolectric
isIncludeAndroidResources = true

isReturnDefaultValues = true

all {
it.systemProperties["robolectric.pixelCopyRenderMode"] = "hardware"
}
}
}

}

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
freeCompilerArgs += buildComposeMetricsParameters()
freeCompilerArgs += stabilityConfiguration()
}
}
}
extensions.configure<ComposeCompilerGradlePluginExtension> {
fun Provider<String>.onlyIfTrue() = flatMap { provider { it.takeIf(String::toBoolean) } }
fun Provider<*>.relativeToRootProject(dir: String) = flatMap {
rootProject.layout.buildDirectory.dir(projectDir.toRelativeString(rootDir))
}.map { it.dir(dir) }

private fun Project.buildComposeMetricsParameters(): List<String> {
val metricParameters = mutableListOf<String>()
val enableMetricsProvider = project.providers.gradleProperty("enableComposeCompilerMetrics")
val relativePath = projectDir.relativeTo(rootDir)
val buildDir = layout.buildDirectory.get().asFile
val enableMetrics = (enableMetricsProvider.orNull == "true")
if (enableMetrics) {
val metricsFolder = buildDir.resolve("compose-metrics").resolve(relativePath)
metricParameters.add("-P")
metricParameters.add(
"plugin:androidx.compose.compiler.plugins.kotlin:metricsDestination=" + metricsFolder.absolutePath,
)
}
project.providers.gradleProperty("enableComposeCompilerMetrics").onlyIfTrue()
.relativeToRootProject("compose-metrics")
.let(metricsDestination::set)

val enableReportsProvider = project.providers.gradleProperty("enableComposeCompilerReports")
val enableReports = (enableReportsProvider.orNull == "true")
if (enableReports) {
val reportsFolder = buildDir.resolve("compose-reports").resolve(relativePath)
metricParameters.add("-P")
metricParameters.add(
"plugin:androidx.compose.compiler.plugins.kotlin:reportsDestination=" + reportsFolder.absolutePath
)
}
return metricParameters.toList()
}
project.providers.gradleProperty("enableComposeCompilerReports").onlyIfTrue()
.relativeToRootProject("compose-reports")
.let(reportsDestination::set)

private fun Project.stabilityConfiguration() = listOf(
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:stabilityConfigurationPath=${project.rootDir.absolutePath}/compose_compiler_config.conf",
)
stabilityConfigurationFile = rootProject.layout.projectDirectory.file("compose_compiler_config.conf")

enableStrongSkippingMode = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import com.android.build.api.dsl.CommonExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.kotlin.dsl.assign
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

/**
Expand All @@ -20,7 +25,7 @@ internal fun Project.configureKotlinAndroid(
compileSdk = 34

defaultConfig {
minSdk = 24
minSdk = 26
}

compileOptions {
Expand All @@ -32,7 +37,7 @@ internal fun Project.configureKotlinAndroid(
}
}

configureKotlin()
configureKotlin<KotlinAndroidProjectExtension>()

dependencies {
add("coreLibraryDesugaring", libs.findLibrary("android.desugarJdkLibs").get())
Expand All @@ -50,26 +55,29 @@ internal fun Project.configureKotlinJvm() {
targetCompatibility = JavaVersion.VERSION_17
}

configureKotlin()
configureKotlin<KotlinJvmProjectExtension>()
}

/**
* Configure base Kotlin options
*/
private fun Project.configureKotlin() {
// Use withType to workaround https://youtrack.jetbrains.com/issue/KT-55947
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
// Set JVM target to 11
jvmTarget = JavaVersion.VERSION_17.toString()
// Treat all Kotlin warnings as errors (disabled by default)
// Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties
val warningsAsErrors: String? by project
allWarningsAsErrors = warningsAsErrors.toBoolean()
freeCompilerArgs = freeCompilerArgs + listOf(
// Enable experimental coroutines APIs, including Flow
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
)
}
private inline fun <reified T : KotlinTopLevelExtension> Project.configureKotlin() = configure<T> {
// Treat all Kotlin warnings as errors (disabled by default)
// Override by setting warningsAsErrors=true in your ~/.gradle/gradle.properties
val warningsAsErrors: String? by project
when (this) {
is KotlinAndroidProjectExtension -> compilerOptions
is KotlinJvmProjectExtension -> compilerOptions
else -> TODO("Unsupported project extension $this ${T::class}")
}.apply {
jvmTarget = JvmTarget.JVM_17
allWarningsAsErrors = warningsAsErrors.toBoolean()
freeCompilerArgs.add(
// Enable experimental coroutines APIs, including Flow
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
)
freeCompilerArgs.add(
"-opt-in=androidx.compose.material3.ExperimentalMaterial3Api"
)
}
}
16 changes: 0 additions & 16 deletions build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
/*
* Copyright 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

dependencyResolutionManagement {
repositories {
google()
Expand Down
7 changes: 3 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.android.library) apply false
alias(libs.plugins.android.test) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.serialization) apply false
alias(libs.plugins.kotlin.parcelize) apply false
Expand All @@ -25,18 +26,16 @@ plugins {
alias(libs.plugins.secrets) apply false
alias(libs.plugins.room) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.detekt)
alias(libs.plugins.detekt.compiler)
alias(libs.plugins.detekt) apply true
// Plugin applied to allow module graph generation
alias(libs.plugins.module.graph) apply true
alias(libs.plugins.spotless)
alias(libs.plugins.spotless) apply true
}

val detektFormatting = libs.detekt.formatting
val twitterComposeRules = libs.twitter.detekt.compose
val ktlintVersion = "1.0.1"


subprojects {
apply {
plugin("io.gitlab.arturbosch.detekt")
Expand Down
33 changes: 33 additions & 0 deletions ci-prebuild.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@echo off
setlocal enabledelayedexpansion

rem Check if gradlew exists in the project
if not exist "%~dp0gradlew" (
echo Error: gradlew not found in the project.
exit /b 1
)

echo Starting all checks and tests...

call :run_gradle_task "check -p build-logic"
call :run_gradle_task "spotlessApply --no-configuration-cache"
call :run_gradle_task "dependencyGuardBaseline"
call :run_gradle_task "detekt"
call :run_gradle_task "testDemoDebug"
call :run_gradle_task "lintProdRelease"
call :run_gradle_task "build"
call :run_gradle_task "updateProdReleaseBadging"

echo All checks and tests completed successfully.
exit /b 0

:run_gradle_task
echo ########################################################
echo Running: %~1
call "%~dp0gradlew" %~1
if %ERRORLEVEL% neq 0 (
echo Error: Task %~1 failed
exit /b 1
)
echo ########################################################
exit /b 0
Loading

0 comments on commit 2a6dafe

Please sign in to comment.