Set buildTimestamp through manifest meta (#1556)

This commit is contained in:
Mikołaj Pich 2021-10-03 14:13:42 +02:00 committed by GitHub
parent 8e607d48f7
commit 60501fcd72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 19 deletions

View File

@ -27,10 +27,10 @@ android {
vectorDrawables.useSupportLibrary = true vectorDrawables.useSupportLibrary = true
resValue "string", "app_name", "Wulkanowy" resValue "string", "app_name", "Wulkanowy"
buildConfigField "long", "BUILD_TIMESTAMP", String.valueOf(System.currentTimeMillis())
manifestPlaceholders = [ manifestPlaceholders = [
firebase_enabled: project.hasProperty("enableFirebase") firebase_enabled: project.hasProperty("enableFirebase"),
buildTimestamp: String.valueOf(System.currentTimeMillis())
] ]
javaCompileOptions { javaCompileOptions {
annotationProcessorOptions { annotationProcessorOptions {

View File

@ -106,7 +106,8 @@
</service> </service>
<service <service
android:name=".services.messaging.AppMessagingService" android:name=".services.messaging.AppMessagingService"
android:exported="false"> android:exported="false"
tools:ignore="MissingClass">
<intent-filter> <intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" /> <action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter> </intent-filter>
@ -152,6 +153,10 @@
android:resource="@xml/provider_paths" /> android:resource="@xml/provider_paths" />
</provider> </provider>
<meta-data
android:name="buildTimestamp"
android:value="${buildTimestamp}" />
<meta-data <meta-data
android:name="install_channel" android:name="install_channel"
android:value="${install_channel}" /> android:value="${install_channel}" />
@ -162,7 +167,8 @@
android:name="com.google.firebase.provider.FirebaseInitProvider" android:name="com.google.firebase.provider.FirebaseInitProvider"
android:authorities="${applicationId}.firebaseinitprovider" android:authorities="${applicationId}.firebaseinitprovider"
android:enabled="${firebase_enabled}" android:enabled="${firebase_enabled}"
android:exported="false" /> android:exported="false"
tools:ignore="MissingClass" />
<meta-data <meta-data
android:name="firebase_analytics_collection_enabled" android:name="firebase_analytics_collection_enabled"

View File

@ -1,10 +1,12 @@
package io.github.wulkanowy.utils package io.github.wulkanowy.utils
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.Resources import android.content.res.Resources
import android.os.Build.MANUFACTURER import android.os.Build.MANUFACTURER
import android.os.Build.MODEL import android.os.Build.MODEL
import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION.SDK_INT
import io.github.wulkanowy.BuildConfig.BUILD_TIMESTAMP import dagger.hilt.android.qualifiers.ApplicationContext
import io.github.wulkanowy.BuildConfig.DEBUG import io.github.wulkanowy.BuildConfig.DEBUG
import io.github.wulkanowy.BuildConfig.FLAVOR import io.github.wulkanowy.BuildConfig.FLAVOR
import io.github.wulkanowy.BuildConfig.VERSION_CODE import io.github.wulkanowy.BuildConfig.VERSION_CODE
@ -13,13 +15,21 @@ import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
open class AppInfo @Inject constructor() { open class AppInfo @Inject constructor(
@ApplicationContext private val context: Context,
) {
open val isDebug get() = DEBUG open val isDebug get() = DEBUG
open val versionCode get() = VERSION_CODE open val versionCode get() = VERSION_CODE
open val buildTimestamp get() = BUILD_TIMESTAMP 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 buildFlavor get() = FLAVOR open val buildFlavor get() = FLAVOR

View File

@ -16,6 +16,8 @@ abstract class AbstractMigrationTest {
val dbName = "migration-test" val dbName = "migration-test"
val context: Context get() = ApplicationProvider.getApplicationContext<Context>()
@get:Rule @get:Rule
val helper: MigrationTestHelper = MigrationTestHelper( val helper: MigrationTestHelper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(), InstrumentationRegistry.getInstrumentation(),
@ -24,7 +26,6 @@ abstract class AbstractMigrationTest {
) )
fun getMigratedRoomDatabase(): AppDatabase { fun getMigratedRoomDatabase(): AppDatabase {
val context = ApplicationProvider.getApplicationContext<Context>()
val database = Room.databaseBuilder( val database = Room.databaseBuilder(
ApplicationProvider.getApplicationContext(), ApplicationProvider.getApplicationContext(),
AppDatabase::class.java, AppDatabase::class.java,
@ -32,7 +33,7 @@ abstract class AbstractMigrationTest {
).addMigrations( ).addMigrations(
*AppDatabase.getMigrations( *AppDatabase.getMigrations(
SharedPrefProvider(PreferenceManager.getDefaultSharedPreferences(context)), SharedPrefProvider(PreferenceManager.getDefaultSharedPreferences(context)),
AppInfo() AppInfo(context)
) )
).build() ).build()
// close the database and release any stream resources when the test finishes // close the database and release any stream resources when the test finishes

View File

@ -29,15 +29,15 @@ class Migration35Test : AbstractMigrationTest() {
close() close()
} }
helper.runMigrationsAndValidate(dbName, 35, true, Migration35(AppInfo())) helper.runMigrationsAndValidate(dbName, 35, true, Migration35(AppInfo(context)))
val db = getMigratedRoomDatabase() val db = getMigratedRoomDatabase()
val students = runBlocking { db.studentDao.loadAll() } val students = runBlocking { db.studentDao.loadAll() }
assertEquals(2, students.size) assertEquals(2, students.size)
assertTrue { students[0].avatarColor in AppInfo().defaultColorsForAvatar } assertTrue { students[0].avatarColor in AppInfo(context).defaultColorsForAvatar }
assertTrue { students[1].avatarColor in AppInfo().defaultColorsForAvatar } assertTrue { students[1].avatarColor in AppInfo(context).defaultColorsForAvatar }
} }
private fun createStudent(db: SupportSQLiteDatabase, id: Long) { private fun createStudent(db: SupportSQLiteDatabase, id: Long) {

View File

@ -32,13 +32,13 @@ class StudentTest {
fun initApi() { fun initApi() {
MockKAnnotations.init(this) MockKAnnotations.init(this)
studentRepository = StudentRepository( studentRepository = StudentRepository(
mockk(), context = mockk(),
TestDispatchersProvider(), dispatchers = TestDispatchersProvider(),
studentDb, studentDb = studentDb,
semesterDb, semesterDb = semesterDb,
mockSdk, sdk = mockSdk,
AppInfo(), appInfo = AppInfo(mockk()),
mockk() appDatabase = mockk()
) )
} }