1import java.nio.file.Paths
2
3def 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
20def safeExtGet(prop, fallback) {
21    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
22}
23
24def getFlagOrDefault(flagName, defaultValue) {
25    rootProject.hasProperty(flagName) ? rootProject.properties[flagName] == "true" : defaultValue
26}
27
28def getVersionOrDefault(String flagName, String defaultVersion) {
29    rootProject.hasProperty(flagName) ? rootProject.properties[flagName] : defaultVersion
30}
31
32configurations {
33    compileClasspath
34}
35
36buildscript {
37    // kotlin version is dictated by rootProject extension or property in gradle.properties
38    ext.asyncStorageKtVersion = rootProject.ext.has('kotlinVersion')
39            ? rootProject.ext['kotlinVersion']
40            : rootProject.hasProperty('AsyncStorage_kotlinVersion')
41            ? rootProject.properties['AsyncStorage_kotlinVersion']
42            : '1.8.10'
43
44    repositories {
45        mavenCentral()
46        google()
47    }
48    dependencies {
49        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$asyncStorageKtVersion"
50    }
51}
52
53// AsyncStorage has default size of 6MB.
54// This is a sane limit to protect the user from the app storing too much data in the database.
55// This also protects the database from filling up the disk cache and becoming malformed.
56// If you really need bigger size, please keep in mind the potential consequences.
57long dbSizeInMB = 6L
58def newDbSize = rootProject.properties['AsyncStorage_db_size_in_MB']
59if (newDbSize != null && newDbSize.isLong()) {
60    dbSizeInMB = newDbSize.toLong()
61}
62
63// Instead of reusing AsyncTask thread pool, AsyncStorage can use its own executor
64def useDedicatedExecutor = getFlagOrDefault('AsyncStorage_dedicatedExecutor', false)
65
66// Use next storage implementation
67def useNextStorage = getFlagOrDefault("AsyncStorage_useNextStorage", false)
68
69apply plugin: 'com.android.library'
70if (useNextStorage) {
71    apply plugin: 'kotlin-android'
72    apply plugin: 'kotlin-kapt'
73    apply from: './testresults.gradle'
74}
75
76android {
77    compileSdkVersion safeExtGet('compileSdkVersion', 32)
78    defaultConfig {
79        minSdkVersion safeExtGet('minSdkVersion', 23)
80        targetSdkVersion safeExtGet('targetSdkVersion', 32)
81        buildConfigField "Long", "AsyncStorage_db_size", "${dbSizeInMB}L"
82        buildConfigField "boolean", "AsyncStorage_useDedicatedExecutor", "${useDedicatedExecutor}"
83        buildConfigField "boolean", "AsyncStorage_useNextStorage", "${useNextStorage}"
84    }
85    lintOptions {
86        abortOnError false
87    }
88
89    if (useNextStorage) {
90        testOptions {
91            unitTests {
92                returnDefaultValues = true
93                includeAndroidResources = true
94            }
95        }
96    }
97}
98
99repositories {
100    maven {
101        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
102        url "${resolveModulePath("react-native")}/android"
103    }
104    google()
105    mavenCentral()
106}
107
108dependencies {
109
110    if (useNextStorage) {
111        def room_version = getVersionOrDefault('AsyncStorage_next_roomVersion', '2.4.3')
112        def coroutines_version = "1.6.4"
113        def coroutinesTest_version = "1.6.4"
114        // if we don't provide explicit dependency on reflection, kotlin plugin
115        // would add one automatically, probably a version that is not compatible with
116        // used kotlin
117        def kotlinReflect_version = project.ext.asyncStorageKtVersion
118        def junit_version = "4.13.2"
119        def robolectric_version = "4.7.3"
120        def truth_version = "1.1.3"
121        def androidxtest_version = "1.4.0"
122        def androidtest_junit_version = "1.1.3"
123
124        implementation "androidx.room:room-runtime:$room_version"
125        implementation "androidx.room:room-ktx:$room_version"
126        implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlinReflect_version"
127
128        implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"
129        kapt "androidx.room:room-compiler:$room_version"
130
131        testImplementation "junit:junit:$junit_version"
132        testImplementation "androidx.test:runner:$androidxtest_version"
133        testImplementation "androidx.test:rules:$androidxtest_version"
134        testImplementation "androidx.test.ext:junit:$androidtest_junit_version"
135        testImplementation "org.robolectric:robolectric:$robolectric_version"
136        testImplementation "com.google.truth:truth:$truth_version"
137        testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesTest_version"
138    }
139
140    //noinspection GradleDynamicVersion
141    implementation 'com.facebook.react:react-native:+'  // From node_modules
142}
143