Add a (currently trivial) convention plugin.

Apparently gradle's new version catalogs only work in certain contexts,
so we still need a convention plugin to be able to centralize decisions
like compileSdk and targetSdk. I'm starting with those for now to prove
it out, then will expand it to cover other interesting things like NDK
version.
This commit is contained in:
Dan Albert
2024-05-07 16:30:52 -07:00
committed by Dan Albert
parent 1667d98d26
commit 2fadc89708
7 changed files with 91 additions and 0 deletions

11
build-logic/README.md Normal file
View File

@@ -0,0 +1,11 @@
# Convention plugins
This directory contains [convention plugins] used by the NDK samples. These are
used to remove Gradle boiler plate from individual samples in favor of common
configuration here. Using convention plugins for single module projects is
overkill, but any non-trivial app will likely need their own eventually. See
[Now In Android's build-logic][nia-build-logic] for a more thorough example of
building convention plugins for Android projects.
[convention plugins]: https://docs.gradle.org/current/samples/sample_convention_plugins.html
[nia-build-logic]: https://github.com/android/nowinandroid/blob/main/build-logic/README.md

View File

@@ -0,0 +1,31 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("java-gradle-plugin")
`kotlin-dsl`
alias(libs.plugins.jetbrains.kotlin.jvm)
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}
}
dependencies {
compileOnly(libs.android.gradlePlugin)
}
gradlePlugin {
plugins {
register("androidApplication") {
id = "ndksamples.android.application"
implementationClass = "com.android.ndk.samples.buildlogic.AndroidApplicationConventionPlugin"
}
}
}

View File

@@ -0,0 +1,13 @@
dependencyResolutionManagement {
repositories {
google()
mavenCentral()
}
versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
}
rootProject.name = "build-logic"

View File

@@ -0,0 +1,24 @@
package com.android.ndk.samples.buildlogic
import com.android.build.api.dsl.ApplicationExtension
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")
}
extensions.configure<ApplicationExtension> {
compileSdk = Versions.COMPILE_SDK
defaultConfig {
targetSdk = Versions.TARGET_SDK
}
}
}
}
}

View File

@@ -0,0 +1,6 @@
package com.android.ndk.samples.buildlogic
object Versions {
const val COMPILE_SDK = 34
const val TARGET_SDK = 34
}

View File

@@ -6,6 +6,7 @@ junitVersion = "1.1.5"
espressoCore = "3.5.1"
appcompat = "1.6.1"
material = "1.12.0"
jetbrainsKotlinJvm = "1.7.21"
[libraries]
junit = { group = "junit", name = "junit", version.ref = "junit" }
@@ -14,7 +15,11 @@ espresso-core = { group = "androidx.test.espresso", name = "espresso-core", vers
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
# build-logic dependencies
android-gradlePlugin = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
jetbrainsKotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
jetbrains-kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "jetbrainsKotlinJvm" }

View File

@@ -1,4 +1,5 @@
pluginManagement {
includeBuild("build-logic")
repositories {
google {
content {