ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Member-only story

Testing Jetpack Security with Robolectric

Matthew Dolan
ProAndroidDev
Published in
4 min readAug 7, 2020

When updating my Android caching library, layercache, with built-in support for the new Jetpack SecurityEncryptedSharedPreferences I started to write unit tests using Robolectric, but soon came across the exception java.security.KeyStoreException: AndroidKeyStore not found.

Usually, shadows come to mind when faced with issues like this, but in this article, we will discuss why this won’t work and how we can get around the problem to continue to write unit tests without requiring a test device.

What is Jetpack Security

The Security library implements crypto security best practices for storing data at rest and currently provides the classes EncryptedSharedPreferences and EncryptedFile.

EncryptedSharedPreferences

EncryptedSharedPreferences is an implementation of SharedPreferences where the keys and values are both encrypted.

To use, add the following dependency into yourbuild.gradle file:

dependencies {
implementation("androidx.security:security-crypto:1.1.0-alpha01")
}

To create EncryptedSharedPreferences, you first create a MasterKey, default settings can be achieved with:

val masterKey = MasterKey.Builder(context)

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Matthew Dolan

Matt Dolan has been eating doughnuts and developing with Android since the dark days of v1.6.

Responses (3)

Write a response