Skip to content

Commit

Permalink
feat(android): Bump versions for Next storage (#1028)
Browse files Browse the repository at this point in the history
- Bumped default Kotlin version to 1.9.20
- Coroutines bumped to 1.7.3
- Dropped kapt in favor of ksp, which version can be set via gradle property
  • Loading branch information
krizzu committed Nov 28, 2023
1 parent 278c3ff commit 14489a7
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 126 deletions.
128 changes: 33 additions & 95 deletions packages/default-storage/android/build.gradle
@@ -1,87 +1,38 @@
import java.nio.file.Paths

def resolveModulePath(packageName) {
def basePath = rootDir.toPath().normalize()

// Node's module resolution algorithm searches up to the root directory,
// after which the base path will be null
while (basePath) {
def candidatePath = Paths.get(basePath.toString(), 'node_modules', packageName)
if (candidatePath.toFile().exists()) {
return candidatePath.toString()
}

basePath = basePath.getParent()
}

return null
}

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def getFlagOrDefault(flagName, defaultValue) {
rootProject.hasProperty(flagName) ? rootProject.properties[flagName] == "true" : defaultValue
}

def getVersionOrDefault(String flagName, String defaultVersion) {
rootProject.hasProperty(flagName) ? rootProject.properties[flagName] : defaultVersion
}

def isNewArchitectureEnabled() {
// To opt-in for the New Architecture, you can either:
// - Set `newArchEnabled` to true inside the `gradle.properties` file
// - Invoke gradle with `-newArchEnabled=true`
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

configurations {
compileClasspath
}

buildscript {
// kotlin version is dictated by rootProject extension or property in gradle.properties
ext.asyncStorageKtVersion = rootProject.ext.has('kotlinVersion')
? rootProject.ext['kotlinVersion']
: rootProject.hasProperty('AsyncStorage_kotlinVersion')
? rootProject.properties['AsyncStorage_kotlinVersion']
: '1.8.10'
apply from: "config.gradle"
def kotlinVersion = ext.AsyncStorageConfig.kotlinVersion
def kspVersion = ext.AsyncStorageConfig.kspVersion

repositories {
mavenCentral()
google()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$asyncStorageKtVersion"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
classpath "com.google.devtools.ksp:symbol-processing-gradle-plugin:$kspVersion"
}
}

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

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

boolean isNewArchitectureEnabled = ext.AsyncStorageConfig.isNewArchitectureEnabled
boolean useNextStorage = ext.AsyncStorageConfig.useNextStorage

// Use next storage implementation
def useNextStorage = getFlagOrDefault("AsyncStorage_useNextStorage", false)
logger.info("[AsyncStorage] Config used: {}", ext.AsyncStorageConfig)

apply plugin: 'com.android.library'
if (useNextStorage) {
apply plugin: 'com.google.devtools.ksp'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply from: './testresults.gradle'
}

if (isNewArchitectureEnabled()) {
if (isNewArchitectureEnabled) {
apply plugin: "com.facebook.react"
}

Expand All @@ -94,7 +45,7 @@ android {
}
}

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


defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 23)
targetSdkVersion safeExtGet('targetSdkVersion', 32)
buildConfigField "Long", "AsyncStorage_db_size", "${dbSizeInMB}L"
buildConfigField "boolean", "AsyncStorage_useDedicatedExecutor", "${useDedicatedExecutor}"
minSdkVersion project.ext.AsyncStorageConfig.minSdkVersion
targetSdkVersion project.ext.AsyncStorageConfig.targetSdkVersion
buildConfigField "Long", "AsyncStorage_db_size", "${project.ext.AsyncStorageConfig.databaseSizeMB}L"
buildConfigField "boolean", "AsyncStorage_useDedicatedExecutor", "${project.ext.AsyncStorageConfig.useDedicatedExecutor}"
buildConfigField "boolean", "AsyncStorage_useNextStorage", "${useNextStorage}"
}
lintOptions {
Expand All @@ -133,7 +84,7 @@ android {
srcDirs += 'src/javaPackage/java'
}

if (!isNewArchitectureEnabled()) {
if (!isNewArchitectureEnabled) {
srcDirs += 'src/oldarch/java'
}
}
Expand All @@ -143,43 +94,30 @@ android {
repositories {
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "${resolveModulePath("react-native")}/android"
url "${project.ext.resolveModulePath("react-native")}/android"
}
google()
mavenCentral()
}

dependencies {

if (useNextStorage) {
def room_version = getVersionOrDefault('AsyncStorage_next_roomVersion', '2.4.3')
def coroutines_version = "1.6.4"
def coroutinesTest_version = "1.6.4"
// if we don't provide explicit dependency on reflection, kotlin plugin
// would add one automatically, probably a version that is not compatible with
// used kotlin
def kotlinReflect_version = project.ext.asyncStorageKtVersion
def junit_version = "4.13.2"
def robolectric_version = "4.7.3"
def truth_version = "1.1.3"
def androidxtest_version = "1.4.0"
def androidtest_junit_version = "1.1.3"
def room_version = project.ext.AsyncStorageConfig.roomVersion

implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlinReflect_version"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
kapt "androidx.room:room-compiler:$room_version"

testImplementation "junit:junit:$junit_version"
testImplementation "androidx.test:runner:$androidxtest_version"
testImplementation "androidx.test:rules:$androidxtest_version"
testImplementation "androidx.test.ext:junit:$androidtest_junit_version"
testImplementation "org.robolectric:robolectric:$robolectric_version"
testImplementation "com.google.truth:truth:$truth_version"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesTest_version"
ksp "androidx.room:room-compiler:$room_version"

implementation project.ext.AsyncStorageLibs.coroutines

testImplementation project.ext.AsyncStorageLibs.testCoroutines
testImplementation project.ext.AsyncStorageLibs.testJunit
testImplementation project.ext.AsyncStorageLibs.testExtJunit
testImplementation project.ext.AsyncStorageLibs.testRunner
testImplementation project.ext.AsyncStorageLibs.testRules
testImplementation project.ext.AsyncStorageLibs.testRobolectric
testImplementation project.ext.AsyncStorageLibs.testTruth
}

implementation 'com.facebook.react:react-native:+' // from node_modules
}
}
119 changes: 119 additions & 0 deletions packages/default-storage/android/config.gradle
@@ -0,0 +1,119 @@
import java.nio.file.Paths

def DEFAULT_KOTLIN_VERSION = "1.9.20"
def DEFAULT_ROOM_VERSION = "2.4.3"

project.ext.AsyncStorageConfig = [
kotlinVersion : getKotlinVersion(DEFAULT_KOTLIN_VERSION),
kspVersion : getKspVersion(kotlinVersion),
roomVersion : getPropertyOfDefault('AsyncStorage_next_roomVersion', DEFAULT_ROOM_VERSION),
minSdkVersion : safeExtGet('minSdkVersion', 23),
targetSdkVersion : safeExtGet('targetSdkVersion', 32),
compileSdkVersion : safeExtGet('compileSdkVersion', 32),
useNextStorage : getFlagOrDefault("AsyncStorage_useNextStorage", false),
databaseSizeMB : getDatabaseSize(),
isNewArchitectureEnabled: isNewArchitectureEnabled(),
useDedicatedExecutor : getFlagOrDefault('AsyncStorage_dedicatedExecutor', false),
]

project.ext.AsyncStorageLibs = [
coroutines : "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3",
testCoroutines : "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3",
testJunit : "junit:junit:4.13.2",
testRunner : "androidx.test:runner:1.4.0",
testRules : "androidx.test:rules:1.4.0",
testExtJunit : "androidx.test.ext:junit:1.1.3",
testRobolectric: "org.robolectric:robolectric:4.11.1",
testTruth : "com.google.truth:truth:1.1.3",
]


def getKotlinVersion(String defaultVersion) {
return rootProject.ext.has('kotlinVersion')
? rootProject.ext['kotlinVersion']
: rootProject.hasProperty('AsyncStorage_kotlinVersion')
? rootProject.properties['AsyncStorage_kotlinVersion']
: defaultVersion
}

def isNewArchitectureEnabled() {
// To opt-in for the New Architecture, you can either:
// - Set `newArchEnabled` to true inside the `gradle.properties` file
// - Invoke gradle with `-newArchEnabled=true`
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

String getKspVersion(String kotlinVersion) {

String overriddenKspVersion = getPropertyOfDefault("AsyncStorage_next_kspVersion", null)
if (overriddenKspVersion != null) {
return overriddenKspVersion
}
// https://github.com/google/ksp/releases
def kspVersions = [
"1.9.20-1.0.14",
"1.9.10-1.0.13",
"1.9.0-1.0.13",
"1.8.22-1.0.11",
"1.8.21-1.0.11",
"1.8.20-1.0.11",
"1.8.10-1.0.9",
"1.8.0-1.0.9",
"1.7.22-1.0.8",
"1.7.21-1.0.8",
"1.7.20-1.0.8",
"1.7.10-1.0.6",
"1.7.0-1.0.6",
"1.6.21-1.0.6",
"1.6.20-1.0.5",
"1.6.10-1.0.4",
"1.6.0-1.0.2",
"1.5.31-1.0.1",
"1.5.30-1.0.0",
]

return kspVersions.find { it.startsWith(kotlinVersion) } ?: kspVersions.first()
}

// AsyncStorage has default size of 6MB.
// This is a sane limit to protect the user from the app storing too much data in the database.
// This also protects the database from filling up the disk cache and becoming malformed.
// If you really need bigger size, please keep in mind the potential consequences.
long getDatabaseSize() {
long dbSizeInMB = 6L
def newDbSize = getPropertyOfDefault('AsyncStorage_db_size_in_MB', null)
if (newDbSize != null && newDbSize.isLong()) {
dbSizeInMB = newDbSize.toLong()
}
return dbSizeInMB
}

def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def getFlagOrDefault(flagName, defaultValue) {
rootProject.hasProperty(flagName) ? rootProject.properties[flagName] == "true" : defaultValue
}

def getPropertyOfDefault(String flagName, String defaultVersion) {
rootProject.hasProperty(flagName) ? rootProject.properties[flagName] : defaultVersion
}

ext.resolveModulePath = { packageName ->
def basePath = rootDir.toPath().normalize()

// Node's module resolution algorithm searches up to the root directory,
// after which the base path will be null
while (basePath) {
def candidatePath = Paths.get(basePath.toString(), 'node_modules', packageName)
if (candidatePath.toFile().exists()) {
return candidatePath.toString()
}

basePath = basePath.getParent()
}

return null
}
2 changes: 1 addition & 1 deletion packages/default-storage/example/android/gradle.properties
Expand Up @@ -42,7 +42,7 @@ newArchEnabled=true
#ANDROID_NDK_VERSION=21.4.7075529

# Version of Kotlin to build against.
KOTLIN_VERSION=1.8.10
KOTLIN_VERSION=1.9.20

# This is an example of how you can change default DB size (6MB) to 10MB
#AsyncStorage_db_size_in_MB=10
Expand Down
8 changes: 4 additions & 4 deletions packages/default-storage/package.json
Expand Up @@ -8,9 +8,9 @@
"types": "lib/typescript/index.d.ts",
"files": [
"RNCAsyncStorage.podspec",
"android/build.gradle",
"android/src",
"android/testresults.gradle",
"android/",
"!android/.gradle",
"!android/build",
"ios/",
"jest/",
"lib/",
Expand Down Expand Up @@ -97,7 +97,7 @@
"react-native-builder-bob": "^0.18.0",
"react-native-codegen": "^0.71.5",
"react-native-macos": "^0.71.0",
"react-native-test-app": "^2.5.8",
"react-native-test-app": "^2.5.33",
"react-native-web": "~0.18.10",
"react-native-windows": "^0.71.0",
"react-test-renderer": "18.2.0",
Expand Down
14 changes: 11 additions & 3 deletions packages/website/docs/advanced/Next.md
Expand Up @@ -47,19 +47,19 @@ AsyncStorage_useNextStorage=true

**Kotlin version**

Next storage is tested against Kotlin version `1.8.10`.
Next storage is tested against Kotlin version `1.9.20`.
You can specify different version, in one of two ways:

- add `kotlinVersion` extension to the `rootProject`:

```groovy
rootProject.ext.kotlinVersion = '1.8.10'
rootProject.ext.kotlinVersion = '1.9.20'
```

- specify `AsyncStorage_kotlinVersion` in `gradle.properties`:

```groovy
AsyncStorage_kotlinVersion=1.8.10
AsyncStorage_kotlinVersion=1.9.20
```

**Room**
Expand All @@ -71,6 +71,14 @@ Currently, tested version is `2.4.3`. You can specify different version, by addi
AsyncStorage_next_roomVersion=2.4.3
```

KSP is enabled for symbol processing for the Room library.
KSP version will be selected based on Kotlin version in your project.
If you want to use different KSP version, you can set a property in `gradle.properties`:

```groovy
AsyncStorage_next_kspVersion=1.9.20-1.0.14
```

### Notable changes

Alongside of a warning regarding `key`/`value`, errors are thrown when:
Expand Down

0 comments on commit 14489a7

Please sign in to comment.