Skip to content

Commit

Permalink
move config, ksp selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Krzysztof Borowy committed Nov 21, 2023
1 parent e7a996e commit 5a4c5ea
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 77 deletions.
95 changes: 20 additions & 75 deletions packages/default-storage-backend/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,92 +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.9.20'

def kspVersion = rootProject.hasProperty("AsyncStorage_next_kspVersion")
? rootProject.properties["AsyncStorage_next_kspVersion"]
: '1.9.20-1.0.14'
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"

// Use next storage implementation
def useNextStorage = getFlagOrDefault("AsyncStorage_useNextStorage", false)
boolean isNewArchitectureEnabled = ext.AsyncStorageConfig.isNewArchitectureEnabled
boolean useNextStorage = ext.AsyncStorageConfig.useNextStorage

println("[AsyncStorage] Config: ${ext.AsyncStorageConfig}")

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

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

Expand All @@ -99,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 @@ -111,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 @@ -138,7 +84,7 @@ android {
srcDirs += 'src/javaPackage/java'
}

if (!isNewArchitectureEnabled()) {
if (!isNewArchitectureEnabled) {
srcDirs += 'src/oldarch/java'
}
}
Expand All @@ -148,16 +94,15 @@ 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 room_version = project.ext.AsyncStorageConfig.roomVersion
def coroutines_version = "1.7.3"
def junit_version = "4.13.2"
def robolectric_version = "4.7.3"
Expand All @@ -181,4 +126,4 @@ dependencies {
}

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

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

project.ext.AsyncStorageConfig = [
kotlinVersion : getKotlinVersion(),
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),
]


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

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
return [
"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",
].find { it.startsWith(kotlinVersion) }
}

// 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
}
1 change: 1 addition & 0 deletions packages/default-storage-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"android/build.gradle",
"android/src",
"android/testresults.gradle",
"android.config.gradle",
"ios/",
"jest/",
"lib/",
Expand Down
5 changes: 3 additions & 2 deletions packages/website/docs/advanced/Next.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ 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 Room.
If you use different Kotlin version than default, you should also [update KSP version](https://github.com/google/ksp/releases) to keep compatibility:
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
Expand Down

0 comments on commit 5a4c5ea

Please sign in to comment.