Skip to content

Commit 4542940

Browse files
committed
Fix unit tests showing warning about datastore_shared_counter
1 parent fcd270c commit 4542940

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/FirebaseSessionsComponent.kt

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ package com.google.firebase.sessions
1818

1919
import android.content.Context
2020
import android.util.Log
21+
import androidx.datastore.core.DataMigration
2122
import androidx.datastore.core.DataStore
23+
import androidx.datastore.core.DataStoreFactory
2224
import androidx.datastore.core.MultiProcessDataStoreFactory
25+
import androidx.datastore.core.Serializer
2326
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
2427
import androidx.datastore.dataStoreFile
2528
import com.google.android.datatransport.TransportFactory
@@ -43,6 +46,7 @@ import dagger.BindsInstance
4346
import dagger.Component
4447
import dagger.Module
4548
import dagger.Provides
49+
import java.io.File
4650
import javax.inject.Qualifier
4751
import javax.inject.Singleton
4852
import kotlin.coroutines.CoroutineContext
@@ -137,7 +141,7 @@ internal interface FirebaseSessionsComponent {
137141
appContext: Context,
138142
@Blocking blockingDispatcher: CoroutineContext,
139143
): DataStore<SessionConfigs> =
140-
MultiProcessDataStoreFactory.create(
144+
createDataStore(
141145
serializer = SessionConfigsSerializer,
142146
corruptionHandler =
143147
ReplaceFileCorruptionHandler { ex ->
@@ -154,7 +158,7 @@ internal interface FirebaseSessionsComponent {
154158
appContext: Context,
155159
@Blocking blockingDispatcher: CoroutineContext,
156160
): DataStore<SessionData> =
157-
MultiProcessDataStoreFactory.create(
161+
createDataStore(
158162
serializer = SessionDataSerializer,
159163
corruptionHandler =
160164
ReplaceFileCorruptionHandler { ex ->
@@ -164,6 +168,36 @@ internal interface FirebaseSessionsComponent {
164168
scope = CoroutineScope(blockingDispatcher),
165169
produceFile = { appContext.dataStoreFile("aqs/sessionDataStore.data") },
166170
)
171+
172+
private fun <T> createDataStore(
173+
serializer: Serializer<T>,
174+
corruptionHandler: ReplaceFileCorruptionHandler<T>,
175+
migrations: List<DataMigration<T>> = listOf(),
176+
scope: CoroutineScope,
177+
produceFile: () -> File,
178+
): DataStore<T> =
179+
if (loadDataStoreSharedCounter()) {
180+
MultiProcessDataStoreFactory.create(
181+
serializer,
182+
corruptionHandler,
183+
migrations,
184+
scope,
185+
produceFile,
186+
)
187+
} else {
188+
DataStoreFactory.create(serializer, corruptionHandler, migrations, scope, produceFile)
189+
}
190+
191+
/** This native library in unavailable in some conditions, for example, Robolectric tests */
192+
private fun loadDataStoreSharedCounter(): Boolean =
193+
try {
194+
System.loadLibrary("datastore_shared_counter")
195+
true
196+
} catch (_: UnsatisfiedLinkError) {
197+
false
198+
} catch (_: SecurityException) {
199+
false
200+
}
167201
}
168202
}
169203
}

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionEvents.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ internal object SessionEvents {
6363
fun getApplicationInfo(firebaseApp: FirebaseApp): ApplicationInfo {
6464
val context = firebaseApp.applicationContext
6565
val packageName = context.packageName
66-
@Suppress("DEPRECATION") // TODO(mrober): Use ApplicationInfoFlags when target sdk set to 33
6766
val packageInfo = context.packageManager.getPackageInfo(packageName, 0)
6867
val buildVersion =
6968
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {

0 commit comments

Comments
 (0)