Skip to content

Commit

Permalink
Refactors BuildSrc to Version Catalog
Browse files Browse the repository at this point in the history
Adds Gradle Convention Plugins
  • Loading branch information
LloydBlv committed Feb 23, 2024
1 parent 633586f commit 1399d97
Show file tree
Hide file tree
Showing 28 changed files with 780 additions and 204 deletions.
64 changes: 26 additions & 38 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
plugins {
id("com.android.application")
kotlin("android")
kotlin("kapt")
id("dagger.hilt.android.plugin")
alias(libs.plugins.flickersearch.android.application)
alias(libs.plugins.flickersearch.hilt)
alias(libs.plugins.sgp.base)
alias(libs.plugins.kotlin.kapt)
}

android {
compileSdkVersion(Config.compileSdkVersion)
buildToolsVersion(Config.buildToolsVersion)

namespace = "ir.zinutech.android.flickrsearch"

defaultConfig {
applicationId = "ir.zinutech.android.flickrsearch"
minSdkVersion(Config.minSdkVersion)
targetSdkVersion(Config.targetSdkVersion)
versionCode = Config.versionCode
versionName = Config.versionName
testInstrumentationRunner = Config.androidTestInstrumentation

versionCode = 1
versionName = "1.0"

configure<BasePluginExtension> { archivesName.set("flickrSearch") }
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

buildConfigField("String", "FLICKR_API_KEY", "\"5a2cc90782760b3a6b3eca570dfaf5c3\"")
}
Expand All @@ -32,43 +33,30 @@ android {

buildFeatures {
dataBinding = true
buildConfig = true
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(project(":domain"))
implementation(project(":core"))
implementation(project(":data"))
implementation(projects.domain)
runtimeOnly(projects.data)
implementation(projects.core)

/*Kotlin*/
implementation(Dependencies.Kotlin.stdlib)
implementation(Dependencies.Kotlin.coroutinesCore)
implementation(libs.kotlin.coroutines)

/*AndroidX*/
implementation(Dependencies.AndroidX.appCompat)
implementation(Dependencies.AndroidX.fragmentKtx)
implementation(Dependencies.AndroidX.coreKtx)
implementation(Dependencies.AndroidX.material)
implementation(Dependencies.AndroidX.constraintLayout)
implementation(Dependencies.AndroidX.viewModelKtx)
implementation(Dependencies.AndroidX.lifeCycleCommon)
implementation(libs.androidx.appCompat)
implementation(libs.androidx.fragment.ktx)
implementation(libs.androidx.coreKtx)
implementation(libs.androidx.coreKtx)
implementation(libs.androidx.constraintlayout)
implementation(libs.google.material)

/*Misc*/
implementation(Dependencies.Misc.timber)
implementation(Dependencies.Misc.viewBindingDelegate)
implementation(Dependencies.Misc.coil)

/*Dagger*/
implementation(Dependencies.Dagger.Hilt.android)
implementation(Dependencies.Dagger.Hilt.viewModel)
kapt(Dependencies.Dagger.Hilt.compiler)
implementation(libs.misc.timber)
implementation(libs.viewbindingpropertydelegate)
implementation(libs.coil.default)
}
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
android:supportsRtl="true"
android:name=".FlickrSearch"
android:theme="@style/Theme.FlickrSearch">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class SearchFragment : Fragment(R.layout.fragment_search_layout) {
SearchViewModel.SearchUiState.EmptyResult -> {
showEmptyResultUi()
}

null -> {}
}

}
Expand Down
51 changes: 51 additions & 0 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
`kotlin-dsl`
}

group = "ir.zinutech.android.flickrSearch.buildlogic"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}
}

dependencies {
compileOnly(libs.android.gradlePlugin)
compileOnly(libs.android.tools.common)
compileOnly(libs.kotlin.gradlePlugin)
}

tasks {
validatePlugins {
enableStricterValidation = true
failOnWarning = true
}
}

gradlePlugin {
plugins {
register("androidApplication") {
id = "flickrsearch.android.application"
implementationClass = "AndroidApplicationConventionPlugin"
}
register("androidLibrary") {
id = "flickrsearch.android.library"
implementationClass = "AndroidLibraryConventionPlugin"
}
register("androidHilt") {
id = "flickrsearch.android.hilt"
implementationClass = "AndroidHiltConventionPlugin"
}
register("jvmLibrary") {
id = "flickrsearch.jvm.library"
implementationClass = "JvmLibraryConventionPlugin"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import com.android.build.api.dsl.ApplicationExtension
import ir.zinutech.android.flickrSearch.configureKotlinAndroid
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure

class AndroidApplicationConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("com.android.application")
apply("org.jetbrains.kotlin.android")
}

extensions.configure<ApplicationExtension> {
configureKotlinAndroid(this)
defaultConfig.targetSdk = 34
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import ir.zinutech.android.flickrSearch.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.dependencies

class AndroidHiltConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("com.google.devtools.ksp")
apply("dagger.hilt.android.plugin")
}

dependencies {
"implementation"(libs.findLibrary("hilt.android").get())
"ksp"(libs.findLibrary("hilt.compiler").get())
}

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import com.android.build.api.variant.LibraryAndroidComponentsExtension
import com.android.build.gradle.LibraryExtension
import ir.zinutech.android.flickrSearch.configureKotlinAndroid
import ir.zinutech.android.flickrSearch.disableUnnecessaryAndroidTests
import ir.zinutech.android.flickrSearch.libs
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin

class AndroidLibraryConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("com.android.library")
apply("org.jetbrains.kotlin.android")
}

extensions.configure<LibraryExtension> {
configureKotlinAndroid(this)
defaultConfig.targetSdk = 34
}
extensions.configure<LibraryAndroidComponentsExtension> {
disableUnnecessaryAndroidTests(target)
}
dependencies {
add("testImplementation", kotlin("test"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import ir.zinutech.android.flickrSearch.configureKotlinJvm
import org.gradle.api.Plugin
import org.gradle.api.Project

class JvmLibraryConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
with(pluginManager) {
apply("org.jetbrains.kotlin.jvm")
}
configureKotlinJvm()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ir.zinutech.android.flickrSearch

import com.android.build.api.variant.LibraryAndroidComponentsExtension
import org.gradle.api.Project

/**
* Disable unnecessary Android instrumented tests for the [project] if there is no `androidTest` folder.
* Otherwise, these projects would be compiled, packaged, installed and ran only to end-up with the following message:
*
* > Starting 0 tests on AVD
*
* Note: this could be improved by checking other potential sourceSets based on buildTypes and flavors.
*/
internal fun LibraryAndroidComponentsExtension.disableUnnecessaryAndroidTests(
project: Project,
) = beforeVariants {
it.enableAndroidTest = it.enableAndroidTest
&& project.projectDir.resolve("src/androidTest").exists()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package ir.zinutech.android.flickrSearch

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.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.withType
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

/**
* Configure base Kotlin with Android options
*/
internal fun Project.configureKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *, *, *>,
) {
commonExtension.apply {
compileSdk = 34

defaultConfig {
minSdk = 21
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}

configureKotlin()

dependencies {
// add("coreLibraryDesugaring", libs.findLibrary("android.desugarJdkLibs").get())
}
}

/**
* Configure base Kotlin options for JVM (non-Android)
*/
internal fun Project.configureKotlinJvm() {
extensions.configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

configureKotlin()
}

/**
* 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",
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ir.zinutech.android.flickrSearch

import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalog
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.getByType

val Project.libs
get(): VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
4 changes: 4 additions & 0 deletions build-logic/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Gradle properties are not passed to included builds https://github.com/gradle/gradle/issues/2534
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configureondemand=true
14 changes: 14 additions & 0 deletions build-logic/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
}
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}

rootProject.name = "build-logic"
include(":convention")
Loading

0 comments on commit 1399d97

Please sign in to comment.