Skip to content

Commit 08fc449

Browse files
author
Krzysztof Borowy
committed
move config, ksp selection
1 parent 7222a18 commit 08fc449

File tree

4 files changed

+130
-77
lines changed

4 files changed

+130
-77
lines changed
Lines changed: 20 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,38 @@
1-
import java.nio.file.Paths
2-
3-
def resolveModulePath(packageName) {
4-
def basePath = rootDir.toPath().normalize()
5-
6-
// Node's module resolution algorithm searches up to the root directory,
7-
// after which the base path will be null
8-
while (basePath) {
9-
def candidatePath = Paths.get(basePath.toString(), 'node_modules', packageName)
10-
if (candidatePath.toFile().exists()) {
11-
return candidatePath.toString()
12-
}
13-
14-
basePath = basePath.getParent()
15-
}
16-
17-
return null
18-
}
19-
20-
def safeExtGet(prop, fallback) {
21-
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
22-
}
23-
24-
def getFlagOrDefault(flagName, defaultValue) {
25-
rootProject.hasProperty(flagName) ? rootProject.properties[flagName] == "true" : defaultValue
26-
}
27-
28-
def getVersionOrDefault(String flagName, String defaultVersion) {
29-
rootProject.hasProperty(flagName) ? rootProject.properties[flagName] : defaultVersion
30-
}
31-
32-
def isNewArchitectureEnabled() {
33-
// To opt-in for the New Architecture, you can either:
34-
// - Set `newArchEnabled` to true inside the `gradle.properties` file
35-
// - Invoke gradle with `-newArchEnabled=true`
36-
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
37-
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
38-
}
39-
401
configurations {
412
compileClasspath
423
}
434

445
buildscript {
45-
// kotlin version is dictated by rootProject extension or property in gradle.properties
46-
ext.asyncStorageKtVersion = rootProject.ext.has('kotlinVersion')
47-
? rootProject.ext['kotlinVersion']
48-
: rootProject.hasProperty('AsyncStorage_kotlinVersion')
49-
? rootProject.properties['AsyncStorage_kotlinVersion']
50-
: '1.9.20'
51-
52-
def kspVersion = rootProject.hasProperty("AsyncStorage_next_kspVersion")
53-
? rootProject.properties["AsyncStorage_next_kspVersion"]
54-
: '1.9.20-1.0.14'
6+
apply from: "config.gradle"
7+
def kotlinVersion = ext.AsyncStorageConfig.kotlinVersion
8+
def kspVersion = ext.AsyncStorageConfig.kspVersion
559

5610
repositories {
5711
mavenCentral()
5812
google()
5913
}
6014
dependencies {
61-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$asyncStorageKtVersion"
15+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
6216
classpath "com.google.devtools.ksp:symbol-processing-gradle-plugin:$kspVersion"
6317
}
6418
}
6519

66-
// AsyncStorage has default size of 6MB.
67-
// This is a sane limit to protect the user from the app storing too much data in the database.
68-
// This also protects the database from filling up the disk cache and becoming malformed.
69-
// If you really need bigger size, please keep in mind the potential consequences.
70-
long dbSizeInMB = 6L
71-
def newDbSize = rootProject.properties['AsyncStorage_db_size_in_MB']
72-
if (newDbSize != null && newDbSize.isLong()) {
73-
dbSizeInMB = newDbSize.toLong()
74-
}
7520

76-
// Instead of reusing AsyncTask thread pool, AsyncStorage can use its own executor
77-
def useDedicatedExecutor = getFlagOrDefault('AsyncStorage_dedicatedExecutor', false)
21+
apply plugin: 'com.android.library'
22+
apply from: "config.gradle"
7823

79-
// Use next storage implementation
80-
def useNextStorage = getFlagOrDefault("AsyncStorage_useNextStorage", false)
24+
boolean isNewArchitectureEnabled = ext.AsyncStorageConfig.isNewArchitectureEnabled
25+
boolean useNextStorage = ext.AsyncStorageConfig.useNextStorage
26+
27+
println("[AsyncStorage] Config: ${ext.AsyncStorageConfig}")
8128

82-
apply plugin: 'com.android.library'
8329
if (useNextStorage) {
8430
apply plugin: 'com.google.devtools.ksp'
8531
apply plugin: 'kotlin-android'
8632
apply from: './testresults.gradle'
8733
}
8834

89-
if (isNewArchitectureEnabled()) {
35+
if (isNewArchitectureEnabled) {
9036
apply plugin: "com.facebook.react"
9137
}
9238

@@ -99,7 +45,7 @@ android {
9945
}
10046
}
10147

102-
compileSdkVersion safeExtGet('compileSdkVersion', 32)
48+
compileSdkVersion project.ext.AsyncStorageConfig.compileSdkVersion
10349
// Used to override the NDK path/version by allowing users to customize
10450
// the NDK path/version from their root project (e.g. for M1 support)
10551
if (rootProject.hasProperty("ndkPath")) {
@@ -111,10 +57,10 @@ android {
11157

11258

11359
defaultConfig {
114-
minSdkVersion safeExtGet('minSdkVersion', 23)
115-
targetSdkVersion safeExtGet('targetSdkVersion', 32)
116-
buildConfigField "Long", "AsyncStorage_db_size", "${dbSizeInMB}L"
117-
buildConfigField "boolean", "AsyncStorage_useDedicatedExecutor", "${useDedicatedExecutor}"
60+
minSdkVersion project.ext.AsyncStorageConfig.minSdkVersion
61+
targetSdkVersion project.ext.AsyncStorageConfig.targetSdkVersion
62+
buildConfigField "Long", "AsyncStorage_db_size", "${project.ext.AsyncStorageConfig.databaseSizeMB}L"
63+
buildConfigField "boolean", "AsyncStorage_useDedicatedExecutor", "${project.ext.AsyncStorageConfig.useDedicatedExecutor}"
11864
buildConfigField "boolean", "AsyncStorage_useNextStorage", "${useNextStorage}"
11965
}
12066
lintOptions {
@@ -138,7 +84,7 @@ android {
13884
srcDirs += 'src/javaPackage/java'
13985
}
14086

141-
if (!isNewArchitectureEnabled()) {
87+
if (!isNewArchitectureEnabled) {
14288
srcDirs += 'src/oldarch/java'
14389
}
14490
}
@@ -148,16 +94,15 @@ android {
14894
repositories {
14995
maven {
15096
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
151-
url "${resolveModulePath("react-native")}/android"
97+
url "${project.ext.resolveModulePath("react-native")}/android"
15298
}
15399
google()
154100
mavenCentral()
155101
}
156102

157103
dependencies {
158-
159104
if (useNextStorage) {
160-
def room_version = getVersionOrDefault('AsyncStorage_next_roomVersion', '2.4.3')
105+
def room_version = project.ext.AsyncStorageConfig.roomVersion
161106
def coroutines_version = "1.7.3"
162107
def junit_version = "4.13.2"
163108
def robolectric_version = "4.7.3"
@@ -181,4 +126,4 @@ dependencies {
181126
}
182127

183128
implementation 'com.facebook.react:react-native:+' // from node_modules
184-
}
129+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import java.nio.file.Paths
2+
3+
def DEFAULT_KOTLIN_VERSION = "1.9.20"
4+
def DEFAULT_ROOM_VERSION = "2.4.3"
5+
6+
project.ext.AsyncStorageConfig = [
7+
kotlinVersion : getKotlinVersion(),
8+
kspVersion : getKspVersion(kotlinVersion),
9+
roomVersion : getPropertyOfDefault('AsyncStorage_next_roomVersion', DEFAULT_ROOM_VERSION),
10+
minSdkVersion : safeExtGet('minSdkVersion', 23),
11+
targetSdkVersion : safeExtGet('targetSdkVersion', 32),
12+
compileSdkVersion : safeExtGet('compileSdkVersion', 32),
13+
useNextStorage : getFlagOrDefault("AsyncStorage_useNextStorage", false),
14+
databaseSizeMB : getDatabaseSize(),
15+
isNewArchitectureEnabled: isNewArchitectureEnabled(),
16+
useDedicatedExecutor : getFlagOrDefault('AsyncStorage_dedicatedExecutor', false),
17+
]
18+
19+
20+
def getKotlinVersion() {
21+
return rootProject.ext.has('kotlinVersion')
22+
? rootProject.ext['kotlinVersion']
23+
: rootProject.hasProperty('AsyncStorage_kotlinVersion')
24+
? rootProject.properties['AsyncStorage_kotlinVersion']
25+
: DEFAULT_KOTLIN_VERSION
26+
}
27+
28+
def isNewArchitectureEnabled() {
29+
// To opt-in for the New Architecture, you can either:
30+
// - Set `newArchEnabled` to true inside the `gradle.properties` file
31+
// - Invoke gradle with `-newArchEnabled=true`
32+
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
33+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
34+
}
35+
36+
String getKspVersion(String kotlinVersion) {
37+
38+
String overriddenKspVersion = getPropertyOfDefault("AsyncStorage_next_kspVersion", null)
39+
if (overriddenKspVersion != null) {
40+
return overriddenKspVersion
41+
}
42+
// https://github.com/google/ksp/releases
43+
return [
44+
"1.9.20-1.0.14",
45+
"1.9.10-1.0.13",
46+
"1.9.0-1.0.13",
47+
"1.8.22-1.0.11",
48+
"1.8.21-1.0.11",
49+
"1.8.20-1.0.11",
50+
"1.8.10-1.0.9",
51+
"1.8.0-1.0.9",
52+
"1.7.22-1.0.8",
53+
"1.7.21-1.0.8",
54+
"1.7.20-1.0.8",
55+
"1.7.10-1.0.6",
56+
"1.7.0-1.0.6",
57+
"1.6.21-1.0.6",
58+
"1.6.20-1.0.5",
59+
"1.6.10-1.0.4",
60+
"1.6.0-1.0.2",
61+
"1.5.31-1.0.1",
62+
"1.5.30-1.0.0",
63+
].find { it.startsWith(kotlinVersion) }
64+
}
65+
66+
// AsyncStorage has default size of 6MB.
67+
// This is a sane limit to protect the user from the app storing too much data in the database.
68+
// This also protects the database from filling up the disk cache and becoming malformed.
69+
// If you really need bigger size, please keep in mind the potential consequences.
70+
long getDatabaseSize() {
71+
long dbSizeInMB = 6L
72+
def newDbSize = getPropertyOfDefault('AsyncStorage_db_size_in_MB', null)
73+
if (newDbSize != null && newDbSize.isLong()) {
74+
dbSizeInMB = newDbSize.toLong()
75+
}
76+
return dbSizeInMB
77+
}
78+
79+
def safeExtGet(prop, fallback) {
80+
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
81+
}
82+
83+
def getFlagOrDefault(flagName, defaultValue) {
84+
rootProject.hasProperty(flagName) ? rootProject.properties[flagName] == "true" : defaultValue
85+
}
86+
87+
def getPropertyOfDefault(String flagName, String defaultVersion) {
88+
rootProject.hasProperty(flagName) ? rootProject.properties[flagName] : defaultVersion
89+
}
90+
91+
ext.resolveModulePath = { packageName ->
92+
def basePath = rootDir.toPath().normalize()
93+
94+
// Node's module resolution algorithm searches up to the root directory,
95+
// after which the base path will be null
96+
while (basePath) {
97+
def candidatePath = Paths.get(basePath.toString(), 'node_modules', packageName)
98+
if (candidatePath.toFile().exists()) {
99+
return candidatePath.toString()
100+
}
101+
102+
basePath = basePath.getParent()
103+
}
104+
105+
return null
106+
}

packages/default-storage/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"android/build.gradle",
1212
"android/src",
1313
"android/testresults.gradle",
14+
"android/config.gradle",
1415
"ios/",
1516
"jest/",
1617
"lib/",

packages/website/docs/advanced/Next.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ Currently, tested version is `2.4.3`. You can specify different version, by addi
7171
AsyncStorage_next_roomVersion=2.4.3
7272
```
7373

74-
KSP is enabled for symbol processing for Room.
75-
If you use different Kotlin version than default, you should also [update KSP version](https://github.com/google/ksp/releases) to keep compatibility:
74+
KSP is enabled for symbol processing for the Room library.
75+
KSP version will be selected based on Kotlin version in your project.
76+
If you want to use different KSP version, you can set a property in `gradle.properties`:
7677

7778
```groovy
7879
AsyncStorage_next_kspVersion=1.9.20-1.0.14

0 commit comments

Comments
 (0)