Skip to content

Commit b8eb608

Browse files
committed
build: Introduce API Gradle Convention Plugin
Likely only one consumer: `:api`, added to standardize .kts files The API has separate requirements, but these are noise in the main gradle.kts file. Issue 20775 Assisted-by: Claude Opus 4.7 - full refactor
1 parent 6f71c73 commit b8eb608

2 files changed

Lines changed: 61 additions & 22 deletions

File tree

api/build.gradle.kts

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,22 @@ import com.android.build.api.dsl.LibraryExtension
22
import com.android.build.gradle.internal.tasks.factory.dependsOn
33

44
plugins {
5-
// Use `id` to avoid classpath conflicts. Versions are pinned by buildSrc/.
6-
id("com.android.library")
5+
id("ankidroid.android-api-library")
76
id("maven-publish")
87
}
98

109
group = "com.ichi2.anki"
1110
version = "2.0.0"
1211

13-
kotlin {
14-
explicitApi()
15-
compilerOptions {
16-
// enable explicit api mode for additional checks related to the public api
17-
// see https://kotlinlang.org/docs/whatsnew14.html#explicit-api-mode-for-library-authors
18-
freeCompilerArgs.add("-Xexplicit-api=strict")
19-
}
20-
}
21-
2212
configure<LibraryExtension> {
2313
namespace = "com.ichi2.anki.api"
24-
compileSdk =
25-
libs.versions.compileSdk
26-
.get()
27-
.toInt()
2814

2915
buildFeatures {
3016
buildConfig = true
3117
}
3218

3319
defaultConfig {
20+
// TODO: This may be incorrect (#20777)
3421
minSdk =
3522
libs.versions.minSdk
3623
.get()
@@ -56,11 +43,6 @@ configure<LibraryExtension> {
5643
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
5744
}
5845
}
59-
compileOptions {
60-
// API remains on VERSION_11 for compatibility
61-
sourceCompatibility = JavaVersion.VERSION_11
62-
targetCompatibility = JavaVersion.VERSION_11
63-
}
6446

6547
publishing {
6648
singleVariant("release") {
@@ -70,8 +52,6 @@ configure<LibraryExtension> {
7052
}
7153
}
7254

73-
apply(from = "../lint.gradle")
74-
7555
dependencies {
7656
implementation(libs.androidx.annotation)
7757
implementation(libs.kotlin.stdlib)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2026 David Allison <davidallisongithub@gmail.com>
3+
*
4+
* This program is free software; you can redistribute it and/or modify it under
5+
* the terms of the GNU General Public License as published by the Free Software
6+
* Foundation; either version 3 of the License, or (at your option) any later
7+
* version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
10+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11+
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License along with
14+
* this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
// Convention plugin: Android library published as a public API artifact.
18+
//
19+
// Distinct from ankidroid.android-library:
20+
// * Java source/target compat is 11 (not 17) for consumer compatibility.
21+
// * Kotlin explicit-API strict mode is enabled (library-author quality check).
22+
// * minSdk is not set.
23+
//
24+
// Publishing (maven-publish, singleVariant setup, POM metadata) is left to
25+
// the consuming module — it's intrinsically per-module configuration.
26+
27+
import com.android.build.api.dsl.LibraryExtension
28+
import org.gradle.api.artifacts.VersionCatalogsExtension
29+
import org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
30+
31+
plugins {
32+
id("com.android.library")
33+
}
34+
35+
val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")
36+
fun versionInt(alias: String): Int =
37+
libs.findVersion(alias).get().requiredVersion.toInt()
38+
39+
extensions.configure<LibraryExtension> {
40+
compileSdk = versionInt("compileSdk")
41+
42+
compileOptions {
43+
// API remains on VERSION_11 for consumer compatibility.
44+
sourceCompatibility = JavaVersion.VERSION_11
45+
targetCompatibility = JavaVersion.VERSION_11
46+
}
47+
}
48+
49+
extensions.configure<KotlinAndroidProjectExtension> {
50+
explicitApi()
51+
compilerOptions {
52+
// Stricter checks on public API shape for library authors.
53+
// See https://kotlinlang.org/docs/whatsnew14.html#explicit-api-mode-for-library-authors
54+
freeCompilerArgs.add("-Xexplicit-api=strict")
55+
}
56+
}
57+
58+
// Shared project-wide lint configuration.
59+
apply(from = "${rootDir}/lint.gradle")

0 commit comments

Comments
 (0)