Skip to content
Android InMemory and persistent Cookie Store for HttpURLConnection and OkHttp, with extensions to easily sync cookies in Android WebViews.
Kotlin Shell
Branch: master
Clone or download
Latest commit 9c569c6 Jan 14, 2020

Android Cookie Store

Android Arsenal Android Weekly Build Status ktlint Download PRs Welcome Donate

Android InMemory and persistent Cookie Store for HttpURLConnection and OkHttp, with extensions to easily sync cookies in Android WebViews.


Neither HttpURLConnection nor OkHttp provides a native and rapid way of storing cookies persistently on Android. This library aims to fill this gap, by implementing the standard in Kotlin, with extendability in mind.

With this library you have:

  • super tiny footprint (the library is only a bunch of classes)
  • an in memory only cookie store
  • a shared preferences backed cookie store which can survive app reboots
  • possibility to extend both to provide your own custom implementation which best fits your needs without reinventing the wheel for cookie management


Android API 16+

Getting started

Add this to your dependencies:

implementation "net.gotev:cookie-store:1.2.2"


Create your Cookie Manager:

// Example extension function to demonstrate how to create both cookie stores
fun Context.createCookieStore(name: String, persistent: Boolean) = if (persistent) {
    SharedPreferencesCookieStore(applicationContext, name)
} else {

val cookieManager = CookieManager(
    createCookieStore(name = "myCookies", persistent = true),


To setup the default Cookie Manager:



Add the following dependency (suitable for JVM and Android):

implementation "net.gotev:cookie-store-okhttp:$cookieStoreVersion"

And when you build your OkHttpClient, set the Cookie Jar:

val okHttpClient = OkHttpClient.Builder()


It's a common thing to obtain a cookie from an API and to open an authenticated web page inside an app which needs the cookie. You can find a complete working example in the demo app.

You have two ways of doing this:

  • Using WebKitSyncCookieManager
  • Using standard

Using WebKitSyncCookieManager

val cookieManager = WebKitSyncCookieManager(
    createCookieStore(name = "myCookies", persistent = true),

Then follow standard instructions from the Usage section to setup HttpURLConnection or OkHttp according to your needs.

Incoming Cookies will be automatically synced to WebKit's CookieManager. Syncing is unidirectional from WebKitSyncCookieManager to android.webkit.CookieManager to have a single source of truth and to prevent attacks coming from URLs loaded in WebViews. If you need bi-directional sync, think twice before doing it.

To clear cookies:


This will clear both the CookieStore and WebKit's Cookie Manager.

Using standard

Cookies syncing is entirely up to you and manual.

To copy all cookies from the cookie store to the WebKit Cookie Manager:


Remember to do this before loading any URL in your web view.

To remove all cookies from the Cookie Store:


To remove all cookies from WebKit Cookie Manager:


That's all folks!

You can’t perform that action at this time.