Android, Development, Kotlin, Test

Clear all Android SharedPreferences

It may happen you would need to clear your all of your SharedPreferences without knowing in advance their keys.


This can happen when you are writing tests: you don’t want your production code to publicly expose your SharedPreferences keys neither you need a clear() method, so you didn’t implement it. You may also need to clear third party SharedPreferences to which you don’t have direct access.

Without changing your production code there is something you can do:

  • access the app SharedPreferences folder
  • get the SharedPreferences editor for each file
  • clear the SharedPreferences

You can use the following code, called in a @BeforeEach annotated method, to be sure each of your tests will run in a clean environment.

private fun clearAllSharedPreferences(context: Context) {
    val sharedPreferencesPath = File(context.filesDir.parentFile!!.absolutePath + File.separator + "shared_prefs")
    sharedPreferencesPath.listFiles()?.forEach { file ->
        context.getSharedPreferences(file.nameWithoutExtension, Context.MODE_PRIVATE).edit { clear() }


  • Be sure to include androidx.core:core-ktx in your project to have that edit() method
  • If you are running your tests using Espresso you can access the app Context using InstrumentationRegistry.getInstrumentation().targetContext

Happy coding!

Android, Development, Java, Kotlin

How to debug an Annotation Processor in Android Studio

Writing an Annotation Processor in Java/Kotlin is a very interesting task and debugging comes very handy, but unfortunately it seems not so easy to start a debug session.

rubber ducks

The following information are valid for Android Studio 3.6.3 and Kotlin 1.3.71. Gradle is the build tool used for the project.

First thing to do is setup a new Run/Debug Configuration in Android Studio:

  • Choose Run from the main menu
  • Choose Edit Configurations
  • Now click on the + symbol in the top left corner to add a new Configuration
  • Choose Remote from the list
  • Give it a friendly name (e.g. AnnotationProcessorDebugger)

Setup your breakpoints wherever you need them and then run this command from the terminal:

./gradlew --no-daemon -Dorg.gradle.debug=true :APP_MODULE_NAME:clean :APP_MODULE_NAME:compileDebugJavaWithJavac

Replace APP_MODULE_NAME with the name of the module having annotations which you want to debug. You will see the process will start and suddenly stop on > Starting Daemon waiting for you to do something.

Now, click on the Debug icon in the Toolbar (having selected the previously created AnnotationProcessorDebugger configuration) and the debugger will hit your breakpoints!

Additional info:

The clean task seems to be necessary.

Unfortunately, sometimes these steps are not enough, or simply don’t work, the debugger won’t hit your breakpoints. I see somebody else solved the issue after adding the following line to their file.

Development, Java

Java concurrency: a few examples

Sometimes we write very high level code, sometimes we don’t. I want to put here a link to a simple project which shows how to deal with concurrency in Java with a few examples.

The project is available on Github here

Goal of the project is to retrieve the details and the balance of a player and show them at the same time: until you have collected both of them you have to wait before showing the user the information.

There are two different packages: in the first, Futures and Callables are being used, in the other, Threads/Runnables and a CountDownLatch are.

Futures and Callables are the best option when you need to run a task which has a return value. I also made it in a different way using Threads/Runnables, a CountDownLatch and some listeners but these classes are usually used when you have tasks which won’t return any value.

Use the two Main classes to run the examples. Have fun!


ApplicationContextException using SpringBoot with Gradle and IntelliJ IDEA

In my spare time I decided to have a better look to SpringBoot and in doing so I decided to leave back Maven and Eclipse and to move to Gradle and IntelliJ, which I already use to develop Android applications.

During the first phase of my development I encountered a strange issue: it seems that IntelliJ IDEA will add wrong dependencies to your SpringBoot project which will cause it to crash at runtime.

This was the exception thrown and the full stack trace:

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh( ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh( ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.refresh( [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.SpringApplication.createAndRefreshContext( [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at com.sampleapp.SampleAppApplication.main( [main/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_72]
	at sun.reflect.NativeMethodAccessorImpl.invoke( ~[na:1.8.0_72]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke( ~[na:1.8.0_72]
	at java.lang.reflect.Method.invoke( ~[na:1.8.0_72]
	at com.intellij.rt.execution.application.AppMain.main( [idea_rt.jar:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory( ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer( ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh( ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
	... 13 common frames omitted

This stacktrace is related to SpringBoot v1.3.3 but I experienced the same issue even with SpringBoot v1.4.0.

In the end I found that IntelliJ was putting this line in the build.gradle file:


After removing that line, the project ran without issues.
So if you are experiencing an ApplicationContextException and you couldn’t find its cause, try to have a look at your build.gradle file.