From a240fd5d5f1cbe1bdf90f39c6bebdeeb0a05a1c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Borcz?= Date: Sat, 9 Oct 2021 18:37:27 +0200 Subject: [PATCH] Add build timestamp as build config field (#1567) --- app/build.gradle | 11 +++++++---- app/src/main/AndroidManifest.xml | 12 ++++-------- .../java/io/github/wulkanowy/utils/AppInfo.kt | 16 +++------------- .../data/db/migrations/AbstractMigrationTest.kt | 4 ++-- .../data/db/migrations/Migration35Test.kt | 6 +++--- .../wulkanowy/data/repositories/StudentTest.kt | 2 +- 6 files changed, 20 insertions(+), 31 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 91ce5f24..031385fe 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -29,10 +29,7 @@ android { resValue "string", "app_name", "Wulkanowy" - manifestPlaceholders = [ - firebase_enabled: project.hasProperty("enableFirebase"), - buildTimestamp: String.valueOf(System.currentTimeMillis()) - ] + manifestPlaceholders = [firebase_enabled: project.hasProperty("enableFirebase")] javaCompileOptions { annotationProcessorOptions { arguments += [ @@ -41,6 +38,12 @@ android { ] } } + + if (System.env.SET_BUILD_TIMESTAMP) { + buildConfigField "long", "BUILD_TIMESTAMP", String.valueOf(System.currentTimeMillis()) + } else { + buildConfigField "long", "BUILD_TIMESTAMP", "1486235849000" + } } sourceSets { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index bebe33c6..8c7eb852 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -153,14 +153,6 @@ android:resource="@xml/provider_paths" /> - - - - + + diff --git a/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt b/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt index 4875425f..a3961aed 100644 --- a/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt +++ b/app/src/main/java/io/github/wulkanowy/utils/AppInfo.kt @@ -1,12 +1,10 @@ package io.github.wulkanowy.utils -import android.content.Context -import android.content.pm.PackageManager import android.content.res.Resources import android.os.Build.MANUFACTURER import android.os.Build.MODEL import android.os.Build.VERSION.SDK_INT -import dagger.hilt.android.qualifiers.ApplicationContext +import io.github.wulkanowy.BuildConfig.BUILD_TIMESTAMP import io.github.wulkanowy.BuildConfig.DEBUG import io.github.wulkanowy.BuildConfig.FLAVOR import io.github.wulkanowy.BuildConfig.VERSION_CODE @@ -15,21 +13,13 @@ import javax.inject.Inject import javax.inject.Singleton @Singleton -open class AppInfo @Inject constructor( - @ApplicationContext private val context: Context, -) { +open class AppInfo @Inject constructor() { open val isDebug get() = DEBUG open val versionCode get() = VERSION_CODE - open val buildTimestamp: Long - get() { - val info = context.packageManager.getApplicationInfo( - context.packageName, PackageManager.GET_META_DATA, - ) - return info.metaData?.getFloat("buildTimestamp")?.toLong() ?: 0 - } + open val buildTimestamp get() = BUILD_TIMESTAMP open val buildFlavor get() = FLAVOR diff --git a/app/src/test/java/io/github/wulkanowy/data/db/migrations/AbstractMigrationTest.kt b/app/src/test/java/io/github/wulkanowy/data/db/migrations/AbstractMigrationTest.kt index 5897af73..cc31d893 100644 --- a/app/src/test/java/io/github/wulkanowy/data/db/migrations/AbstractMigrationTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/db/migrations/AbstractMigrationTest.kt @@ -16,7 +16,7 @@ abstract class AbstractMigrationTest { val dbName = "migration-test" - val context: Context get() = ApplicationProvider.getApplicationContext() + val context: Context get() = ApplicationProvider.getApplicationContext() @get:Rule val helper: MigrationTestHelper = MigrationTestHelper( @@ -33,7 +33,7 @@ abstract class AbstractMigrationTest { ).addMigrations( *AppDatabase.getMigrations( SharedPrefProvider(PreferenceManager.getDefaultSharedPreferences(context)), - AppInfo(context) + AppInfo() ) ).build() // close the database and release any stream resources when the test finishes diff --git a/app/src/test/java/io/github/wulkanowy/data/db/migrations/Migration35Test.kt b/app/src/test/java/io/github/wulkanowy/data/db/migrations/Migration35Test.kt index 9a38f428..883cdb81 100644 --- a/app/src/test/java/io/github/wulkanowy/data/db/migrations/Migration35Test.kt +++ b/app/src/test/java/io/github/wulkanowy/data/db/migrations/Migration35Test.kt @@ -29,15 +29,15 @@ class Migration35Test : AbstractMigrationTest() { close() } - helper.runMigrationsAndValidate(dbName, 35, true, Migration35(AppInfo(context))) + helper.runMigrationsAndValidate(dbName, 35, true, Migration35(AppInfo())) val db = getMigratedRoomDatabase() val students = runBlocking { db.studentDao.loadAll() } assertEquals(2, students.size) - assertTrue { students[0].avatarColor in AppInfo(context).defaultColorsForAvatar } - assertTrue { students[1].avatarColor in AppInfo(context).defaultColorsForAvatar } + assertTrue { students[0].avatarColor in AppInfo().defaultColorsForAvatar } + assertTrue { students[1].avatarColor in AppInfo().defaultColorsForAvatar } } private fun createStudent(db: SupportSQLiteDatabase, id: Long) { diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/StudentTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/StudentTest.kt index 39ebb4a1..9d3d7a2e 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/StudentTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/StudentTest.kt @@ -37,7 +37,7 @@ class StudentTest { studentDb = studentDb, semesterDb = semesterDb, sdk = mockSdk, - appInfo = AppInfo(mockk()), + appInfo = AppInfo(), appDatabase = mockk() ) }