12 February 2025
Accurate time is crucial for a wide variety of app functionalities, from scheduling and event management to transaction logging and security protocols. However, a user can change the device’s time, so a more accurate source of time than the device’s local system time may be required. That's why we're introducing the TrustedTime API that leverages Google's infrastructure to deliver a trustworthy timestamp, independent of the device's potentially manipulated local time settings.
The new API leverages Google's secure infrastructure to provide a trusted time source to your app. TrustedTime periodically syncs its clock to Google's servers, which have access to a highly accurate time source, so that you do not need to make a server request every time you want to know the current network time. Additionally, we've integrated a unique model that calculates the device's clock drift. This will inform you when the time may be inaccurate between network synchronizations.
Many apps rely on the device's clock for various features. However, users can change their device's time settings, either intentionally or unintentionally, therefore changing the time that your app gets. This can lead to problems such as:
The TrustedTime API opens up a range of possibilities for enhancing the reliability and security of your apps, with use cases in areas such as:
The TrustedTime API is built on top of Google Play services, making integration seamless for most Android developers.
The simplest way to integrate is to initialize the TrustedTimeClient early in your app lifecycle, such as in the onCreate() method of your Application class. The following example uses dependency injection with Hilt to make the time client available to components throughout the app.
// TrustedTimeClientAccessor.kt import com.google.android.gms.tasks.Task import com.google.android.gms.time.TrustedTimeClient interface TrustedTimeClientAccessor { fun createClient(): Task<TrustedTimeClient> } // TrustedTimeModule.kt @Module @InstallIn(SingletonComponent::class) class TrustedTimeModule { @Provides fun provideTrustedTimeClientAccessor( @ApplicationContext context: Context ): TrustedTimeClientAccessor { return object : TrustedTimeClientAccessor { override fun createClient(): Task<TrustedTimeClient> { return TrustedTime.createClient(context) } } } }
// TrustedTimeDemoApplication.kt @HiltAndroidApp class TrustedTimeDemoApplication : Application() { @Inject lateinit var trustedTimeClientAccessor: TrustedTimeClientAccessor var trustedTimeClient: TrustedTimeClient? = null private set override fun onCreate() { super.onCreate() trustedTimeClientAccessor.createClient().addOnCompleteListener { task -> if (task.isSuccessful) { // Stash the client trustedTimeClient = task.result } else { // Handle error, maybe retry later val exception = task.exception } } // To use Kotlin Coroutine, you can use the await() method, // see https://developers.google.com/android/guides/tasks#kotlin_coroutine for more info. } } NOTE: If you don't use dependency injection in your app. You can simply call `TrustedTime.createClient(context)` instead of using a TrustedTimeClientAccessor.
// Retrieve the TrustedTimeClient from your application class val myApp = applicationContext as TrustedTimeDemoApplication // In this example, System.currentTimeMillis() is used as a fallback if the // client is null (i.e. client creation task failed) or when there is no time // signal available. You may not want to do this if using the system clock is // not suitable for your use case. val currentTimeMillis = myApp.trustedTimeClient?.computeCurrentUnixEpochMillis() ?: System.currentTimeMillis() // trustedTimeClient.computeCurrentInstant() can be used if Instant is // preferred to long for Unix epoch times and you are able to use the APIs.
@AndroidEntryPoint class MainActivity : AppCompatActivity() { @Inject lateinit var trustedTimeAccessor: TrustedTimeAccessor private var trustedTimeClient: TrustedTimeClient? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) ... trustedTimeAccessor.createClient().addOnCompleteListener { task -> if (task.isSuccessful) { // Stash the client trustedTimeClient = task.result } else { // Handle error, maybe retry later or use another time source. val exception = task.exception } } } private fun getCurrentTimeInMillis() : Long? { return trustedTimeClient?.computeCurrentUnixEpochMillis() } }
The TrustedTime API is available on all devices running Google Play services on Android 5 (Lollipop) and above. You need to add the dependency com.google.android.gms:play-services-time:16.0.1 (or above) to access the new API. No additional permission is required to use this API. However, TrustedTime needs an internet connection after the device starts up to provide timestamps. If the device hasn't connected to the internet since booting, the TrustedTime APIs won't return timestamps.
It’s important to note that the device's internal clock can drift due to factors like temperature, doze mode, and battery level. TrustedTime doesn't prevent this drift, but its APIs provide an error estimate for each timestamp. Use this estimate to determine if the timestamp's accuracy meets your application's requirements. While TrustedTime makes it more difficult for users to manipulate the time accessed by your app, it does not guarantee complete safety. Advanced techniques can still be used to tamper with the device’s time.
To learn more about the TrustedTime API, check out the following resources: