Skip to content

Commit

Permalink
Move Android configuration to convention plugins (chrisbanes#1267)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikghonyan authored May 21, 2023
1 parent f032f3e commit 36e8b03
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 41 deletions.
38 changes: 0 additions & 38 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/


import com.android.build.gradle.BaseExtension
import com.diffplug.gradle.spotless.SpotlessExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask

Expand Down Expand Up @@ -99,41 +98,4 @@ allprojects {
}
}
}

// Configure Android projects
pluginManager.withPlugin("com.android.application") {
configureAndroidProject()
}
pluginManager.withPlugin("com.android.library") {
configureAndroidProject()
}
pluginManager.withPlugin("com.android.test") {
configureAndroidProject()
}
}

fun Project.configureAndroidProject() {
extensions.configure<BaseExtension> {
compileSdkVersion(libs.versions.compileSdk.get().toInt())

defaultConfig {
minSdk = libs.versions.minSdk.get().toInt()
targetSdk = libs.versions.targetSdk.get().toInt()
}

// Can remove this once https://issuetracker.google.com/issues/260059413 is fixed.
// See https://kotlinlang.org/docs/gradle-configure-project.html#gradle-java-toolchains-support
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

// https://developer.android.com/studio/write/java8-support
isCoreLibraryDesugaringEnabled = true
}
}

dependencies {
// https://developer.android.com/studio/write/java8-support
"coreLibraryDesugaring"(libs.tools.desugarjdklibs)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* http://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.
*/

package app.tivi.gradle

import com.android.build.gradle.BaseExtension
import org.gradle.api.JavaVersion
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.getByType

fun Project.configureAndroid() {
val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")

android {
compileSdkVersion(Versions.compileSdk)

defaultConfig {
minSdk = Versions.minSdk
targetSdk = Versions.targetSdk
}

// Can remove this once https://issuetracker.google.com/issues/260059413 is fixed.
// See https://kotlinlang.org/docs/gradle-configure-project.html#gradle-java-toolchains-support
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

// https://developer.android.com/studio/write/java8-support
isCoreLibraryDesugaringEnabled = true
}
}

dependencies {
// https://developer.android.com/studio/write/java8-support
"coreLibraryDesugaring"(libs.findLibrary("tools.desugarjdklibs").get())
}
}

private fun Project.android(action: BaseExtension.() -> Unit) = extensions.configure<BaseExtension>(action)
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
apply("com.android.application")
apply("org.gradle.android.cache-fix")
}

configureAndroid()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class AndroidLibraryConventionPlugin : Plugin<Project> {
apply("com.android.library")
apply("org.gradle.android.cache-fix")
}

configureAndroid()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class AndroidTestConventionPlugin : Plugin<Project> {
with(pluginManager) {
apply("com.android.test")
}

configureAndroid()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2023 Google LLC
*
* 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
*
* http://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.
*/

package app.tivi.gradle

object Versions {
const val compileSdk = 33
const val minSdk = 23
const val targetSdk = 31
}
3 changes: 0 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ ktor = "2.3.0"
paging = "3.1.1"
retrofit = "2.9.0"
sqldelight = "2.0.0-alpha05"
compileSdk = "33"
minSdk = "23"
targetSdk = "31"

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down

0 comments on commit 36e8b03

Please sign in to comment.