Skip to content

Commit 4039097

Browse files
mroberDavid Motsonashvili
authored and
David Motsonashvili
committed
Fix NPE when no version name is set (#5198)
* Fix NPE when no version code is set * Update changelog * Simplify the null check * name * Add issue to changelog
1 parent 5e9a026 commit 4039097

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

firebase-sessions/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
# Unreleased
22

3+
* [fixed] Fixed NPE when no version name is
4+
set ([#5195](//github.com/firebase/firebase-android-sdk/issues/5195)).
5+
6+
# 1.0.0
7+
38
* [feature] Initial Firebase sessions library.

firebase-sessions/gradle.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
version=1.0.0
15+
version=1.0.1
16+
latestReleasedVersion=1.0.0

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ internal object SessionEvents {
5757
fun getApplicationInfo(firebaseApp: FirebaseApp): ApplicationInfo {
5858
val context = firebaseApp.applicationContext
5959
val packageName = context.packageName
60+
@Suppress("DEPRECATION") // TODO(mrober): Use ApplicationInfoFlags when target sdk set to 33
6061
val packageInfo = context.packageManager.getPackageInfo(packageName, 0)
6162
val buildVersion =
6263
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
@@ -74,7 +75,7 @@ internal object SessionEvents {
7475
androidAppInfo =
7576
AndroidApplicationInfo(
7677
packageName = packageName,
77-
versionName = packageInfo.versionName,
78+
versionName = packageInfo.versionName ?: buildVersion,
7879
appBuildVersion = buildVersion,
7980
deviceManufacturer = Build.MANUFACTURER,
8081
)

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/settings/LocalOverrideSettings.kt

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,21 @@ package com.google.firebase.sessions.settings
1818

1919
import android.content.Context
2020
import android.content.pm.PackageManager
21-
import android.os.Build
2221
import android.os.Bundle
2322
import kotlin.time.Duration
2423
import kotlin.time.DurationUnit
2524
import kotlin.time.toDuration
2625

2726
internal class LocalOverrideSettings(context: Context) : SettingsProvider {
27+
@Suppress("DEPRECATION") // TODO(mrober): Use ApplicationInfoFlags when target sdk set to 33
2828
private val metadata =
29-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
30-
context.packageManager
31-
.getApplicationInfo(
32-
context.packageName,
33-
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong()),
34-
)
35-
.metaData
36-
} else {
37-
@Suppress("DEPRECATION") // For older API levels.
38-
context.packageManager
39-
.getApplicationInfo(
40-
context.packageName,
41-
PackageManager.GET_META_DATA,
42-
)
43-
.metaData
44-
}
45-
// Default to an empty bundle, meaning no cached values.
46-
?: Bundle.EMPTY
29+
context.packageManager
30+
.getApplicationInfo(
31+
context.packageName,
32+
PackageManager.GET_META_DATA,
33+
)
34+
.metaData
35+
?: Bundle.EMPTY // Default to an empty bundle, meaning no cached values.
4736

4837
override val sessionEnabled: Boolean?
4938
get() =

firebase-sessions/src/test/kotlin/com/google/firebase/sessions/ApplicationInfoTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ import android.os.Build
2121
import androidx.test.core.app.ApplicationProvider
2222
import com.google.common.truth.Truth.assertThat
2323
import com.google.firebase.FirebaseApp
24+
import com.google.firebase.FirebaseOptions
25+
import com.google.firebase.ktx.Firebase
26+
import com.google.firebase.ktx.initialize
2427
import com.google.firebase.sessions.testing.FakeFirebaseApp
2528
import org.junit.After
2629
import org.junit.Test
@@ -51,6 +54,39 @@ class ApplicationInfoTest {
5154
)
5255
}
5356

57+
@Test
58+
fun applicationInfo_missiongVersionCode_populatesInfoCorrectly() {
59+
// Initialize Firebase with no version code set.
60+
val firebaseApp =
61+
Firebase.initialize(
62+
ApplicationProvider.getApplicationContext(),
63+
FirebaseOptions.Builder()
64+
.setApplicationId(FakeFirebaseApp.MOCK_APP_ID)
65+
.setApiKey(FakeFirebaseApp.MOCK_API_KEY)
66+
.setProjectId(FakeFirebaseApp.MOCK_PROJECT_ID)
67+
.build()
68+
)
69+
70+
val applicationInfo = SessionEvents.getApplicationInfo(firebaseApp)
71+
72+
assertThat(applicationInfo)
73+
.isEqualTo(
74+
ApplicationInfo(
75+
appId = FakeFirebaseApp.MOCK_APP_ID,
76+
deviceModel = Build.MODEL,
77+
sessionSdkVersion = BuildConfig.VERSION_NAME,
78+
osVersion = Build.VERSION.RELEASE,
79+
logEnvironment = LogEnvironment.LOG_ENVIRONMENT_PROD,
80+
AndroidApplicationInfo(
81+
packageName = ApplicationProvider.getApplicationContext<Context>().packageName,
82+
versionName = "0",
83+
appBuildVersion = "0",
84+
deviceManufacturer = Build.MANUFACTURER,
85+
)
86+
)
87+
)
88+
}
89+
5490
@After
5591
fun cleanUp() {
5692
FirebaseApp.clearInstancesForTest()

0 commit comments

Comments
 (0)