Skip to content

Fix NPE when no version name is set #5198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions firebase-sessions/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Unreleased

* [fixed] Fixed NPE when no version name is
set ([#5195](//github.com/firebase/firebase-android-sdk/issues/5195)).

# 1.0.0

* [feature] Initial Firebase sessions library.
3 changes: 2 additions & 1 deletion firebase-sessions/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
# See the License for the specific language governing permissions and
# limitations under the License.

version=1.0.0
version=1.0.1
latestReleasedVersion=1.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ internal object SessionEvents {
fun getApplicationInfo(firebaseApp: FirebaseApp): ApplicationInfo {
val context = firebaseApp.applicationContext
val packageName = context.packageName
@Suppress("DEPRECATION") // TODO(mrober): Use ApplicationInfoFlags when target sdk set to 33
val packageInfo = context.packageManager.getPackageInfo(packageName, 0)
val buildVersion =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Expand All @@ -74,7 +75,7 @@ internal object SessionEvents {
androidAppInfo =
AndroidApplicationInfo(
packageName = packageName,
versionName = packageInfo.versionName,
versionName = packageInfo.versionName ?: buildVersion,
appBuildVersion = buildVersion,
deviceManufacturer = Build.MANUFACTURER,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,21 @@ package com.google.firebase.sessions.settings

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import kotlin.time.Duration
import kotlin.time.DurationUnit
import kotlin.time.toDuration

internal class LocalOverrideSettings(context: Context) : SettingsProvider {
@Suppress("DEPRECATION") // TODO(mrober): Use ApplicationInfoFlags when target sdk set to 33
private val metadata =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager
.getApplicationInfo(
context.packageName,
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA.toLong()),
)
.metaData
} else {
@Suppress("DEPRECATION") // For older API levels.
context.packageManager
.getApplicationInfo(
context.packageName,
PackageManager.GET_META_DATA,
)
.metaData
}
// Default to an empty bundle, meaning no cached values.
?: Bundle.EMPTY
context.packageManager
.getApplicationInfo(
context.packageName,
PackageManager.GET_META_DATA,
)
.metaData
?: Bundle.EMPTY // Default to an empty bundle, meaning no cached values.

override val sessionEnabled: Boolean?
get() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import android.os.Build
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.google.firebase.FirebaseApp
import com.google.firebase.FirebaseOptions
import com.google.firebase.ktx.Firebase
import com.google.firebase.ktx.initialize
import com.google.firebase.sessions.testing.FakeFirebaseApp
import org.junit.After
import org.junit.Test
Expand Down Expand Up @@ -51,6 +54,39 @@ class ApplicationInfoTest {
)
}

@Test
fun applicationInfo_missiongVersionCode_populatesInfoCorrectly() {
// Initialize Firebase with no version code set.
val firebaseApp =
Firebase.initialize(
ApplicationProvider.getApplicationContext(),
FirebaseOptions.Builder()
.setApplicationId(FakeFirebaseApp.MOCK_APP_ID)
.setApiKey(FakeFirebaseApp.MOCK_API_KEY)
.setProjectId(FakeFirebaseApp.MOCK_PROJECT_ID)
.build()
)

val applicationInfo = SessionEvents.getApplicationInfo(firebaseApp)

assertThat(applicationInfo)
.isEqualTo(
ApplicationInfo(
appId = FakeFirebaseApp.MOCK_APP_ID,
deviceModel = Build.MODEL,
sessionSdkVersion = BuildConfig.VERSION_NAME,
osVersion = Build.VERSION.RELEASE,
logEnvironment = LogEnvironment.LOG_ENVIRONMENT_PROD,
AndroidApplicationInfo(
packageName = ApplicationProvider.getApplicationContext<Context>().packageName,
versionName = "0",
appBuildVersion = "0",
deviceManufacturer = Build.MANUFACTURER,
)
)
)
}

@After
fun cleanUp() {
FirebaseApp.clearInstancesForTest()
Expand Down