Skip to content

Commit 4a4e271

Browse files
committed
chore: group kotlin config
1 parent 898ec0a commit 4a4e271

File tree

3 files changed

+83
-20
lines changed

3 files changed

+83
-20
lines changed

packages/async-storage/android/build.gradle

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
2-
ext.getExtOrDefault = { name ->
3-
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['AsyncStorage_' + name]
4-
}
2+
apply from: "config.gradle"
3+
def kotlinVersion = ext.AsyncStorage.kotlinVersion
4+
def kspVersion = ext.AsyncStorage.kspVersion
55

66
repositories {
77
google()
@@ -11,35 +11,29 @@ buildscript {
1111
dependencies {
1212
classpath "com.android.tools.build:gradle:8.7.2"
1313
// noinspection DifferentKotlinGradleVersion
14-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${getExtOrDefault('KOTLIN_VERSION')}"
15-
classpath "com.google.devtools.ksp:symbol-processing-gradle-plugin:${getExtOrDefault('KSP_VERSION')}"
14+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
15+
classpath "com.google.devtools.ksp:symbol-processing-gradle-plugin:${kspVersion}"
1616
}
1717
}
1818

1919

20+
apply from: "config.gradle"
2021
apply plugin: "com.android.library"
2122
apply plugin: "kotlin-android"
2223
apply plugin: "com.facebook.react"
2324

2425
// ksp/room is required to support legacy storage
2526
apply plugin: 'com.google.devtools.ksp'
2627

27-
def getExtOrDefault(name) {
28-
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["AsyncStorage_" + name])
29-
}
30-
31-
def isNewArchitectureEnabled() {
32-
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
33-
}
3428

3529
android {
3630
namespace "org.asyncstorage"
3731

38-
compileSdkVersion getExtOrDefault("compileSdkVersion").toInteger()
32+
compileSdkVersion project.ext.AsyncStorage.compileSdk
3933

4034
defaultConfig {
41-
minSdkVersion getExtOrDefault("minSdkVersion").toInteger()
42-
targetSdkVersion getExtOrDefault("targetSdkVersion").toInteger()
35+
minSdkVersion project.ext.AsyncStorage.minSdk
36+
targetSdkVersion project.ext.AsyncStorage.targetSdk
4337
}
4438

4539
buildFeatures {
@@ -73,7 +67,7 @@ android {
7367
"generated/jni"
7468
]
7569

76-
if (isNewArchitectureEnabled()) {
70+
if (project.ext.AsyncStorage.isNewArch) {
7771
kotlin.srcDirs += ['src/newarch']
7872
java.srcDirs += ['src/newarch']
7973
} else {
@@ -95,7 +89,7 @@ dependencies {
9589

9690

9791
// ksp/room is required to support legacy storage
98-
def room_version = getExtOrDefault("ROOM_VERSION")
92+
def room_version = project.ext.AsyncStorage.roomVersion
9993
implementation "androidx.room:room-runtime:$room_version"
10094
implementation "androidx.room:room-ktx:$room_version"
10195
ksp "androidx.room:room-compiler:$room_version"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
project.ext.AsyncStorage = [
2+
isNewArch : project.hasProperty("newArchEnabled") && project.newArchEnabled == "true",
3+
roomVersion: getConfigValueOrDefault("roomVersion"),
4+
5+
compileSdk : getConfigValueOrDefault("compileSdkVersion").toInteger(),
6+
minSdk : getConfigValueOrDefault("minSdkVersion").toInteger(),
7+
targetSdk : getConfigValueOrDefault("targetSdkVersion").toInteger()
8+
]
9+
10+
project.ext.AsyncStorage.kotlinVersion = getKotlinVersion()
11+
project.ext.AsyncStorage.kspVersion = getKspVersion(project.ext.AsyncStorage.kotlinVersion)
12+
13+
String getKotlinVersion() {
14+
String kotlin = getConfigValueOrDefault("kotlinVersion")
15+
def (major, minor) = kotlin.tokenize('.')
16+
if (major.toInteger() < 2 || minor.toInteger() < 1) {
17+
project.logger.warn("[AsyncStorage] Min. supported version of Kotlin is 2.1.0, current: $kotlin.")
18+
}
19+
20+
21+
logger.info("[AsyncStorage] Using kotlin version: $kotlin")
22+
return kotlin
23+
}
24+
25+
String getKspVersion(String kotlinVersion) {
26+
String overriddenKspVersion = getConfigValueOrDefault("kspVersion")
27+
if (overriddenKspVersion != null) {
28+
logger.info("[AsyncStorage] Overriden ksp version: ${overriddenKspVersion}")
29+
return overriddenKspVersion
30+
}
31+
32+
// https://kotlinlang.org/docs/releases.html#release-details
33+
// https://github.com/google/ksp/releases
34+
def kspVersions = [
35+
"2.2.20-2.0.4",
36+
"2.2.10-2.0.2",
37+
"2.2.0-2.0.2",
38+
"2.1.21-2.0.2",
39+
"2.1.20-1.0.31",
40+
"2.1.10-1.0.29",
41+
"2.1.0-1.0.28",
42+
]
43+
44+
String found = kspVersions.find { it.startsWith(kotlinVersion) }
45+
if (found != null) {
46+
logger.info("[AsyncStorage] Using ksp version: ${found}")
47+
return found
48+
}
49+
50+
String defKsp = kspVersions.last()
51+
logger.warn("[AsyncStorage] Could not find ksp version for $kotlinVersion. Min. supported kotlin is 2.1.0. Using default ksp: $defKsp")
52+
return defKsp
53+
}
54+
55+
def getConfigValueOrDefault(name) {
56+
if (rootProject.ext.has(name)) {
57+
return rootProject.ext.get(name)
58+
}
59+
if (rootProject.hasProperty(name)) {
60+
return rootProject.properties[name]
61+
}
62+
if (rootProject.hasProperty("AsyncStorage_$name")) {
63+
return rootProject.properties["AsyncStorage_$name"]
64+
}
65+
66+
return project.properties["AsyncStorage_$name"]
67+
}
68+
69+
70+
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
AsyncStorage_KOTLIN_VERSION=2.2.10
2-
AsyncStorage_KSP_VERSION=2.2.10-2.0.2
3-
AsyncStorage_ROOM_VERSION=2.7.2
1+
AsyncStorage_kotlinVersion=2.2.10
2+
AsyncStorage_roomVersion=2.7.2
43
AsyncStorage_minSdkVersion=24
54
AsyncStorage_targetSdkVersion=36
65
AsyncStorage_compileSdkVersion=36

0 commit comments

Comments
 (0)