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