https://github.com/gotev/android-cookie-store
Android InMemory and persistent Cookie Store for HttpURLConnection and OkHttp, with extensions to easily sync cookies in Android WebViews.
https://github.com/gotev/android-cookie-store
android cookie httpurlconnection kotlin library okhttp store webview
Last synced: 9 months ago
JSON representation
Android InMemory and persistent Cookie Store for HttpURLConnection and OkHttp, with extensions to easily sync cookies in Android WebViews.
- Host: GitHub
- URL: https://github.com/gotev/android-cookie-store
- Owner: gotev
- License: apache-2.0
- Created: 2019-12-28T10:48:05.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-12T12:55:46.000Z (over 1 year ago)
- Last Synced: 2025-03-31T10:07:00.004Z (9 months ago)
- Topics: android, cookie, httpurlconnection, kotlin, library, okhttp, store, webview
- Language: Kotlin
- Homepage:
- Size: 253 KB
- Stars: 194
- Watchers: 5
- Forks: 18
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Security: SECURITY.md
Awesome Lists containing this project
- awesome-list - gotev/android-cookie-store - Android InMemory and persistent Cookie Store for HttpURLConnection and OkHttp, with extensions to easily sync cookies in Android WebViews. (Kotlin)
README
# Android Cookie Store
[](https://android-arsenal.com/details/1/8000) [](https://androidweekly.net/issues/issue-394) [](https://ktlint.github.io/)  [](http://makeapullrequest.com)
#### [Latest version Release Notes and Demo App](https://github.com/gotev/android-cookie-store/releases/latest) | [Demo App Sources](https://github.com/gotev/android-cookie-store/tree/master/example/app/src/main/java/net/gotev/cookiestoredemo)
Android InMemory and persistent Cookie Store for `HttpURLConnection` and `OkHttp`, with extensions to easily sync cookies in Android WebViews.
## Why?
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 `java.net.InMemoryCookieStore` 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
## Compatibility
Android API 16+
## Getting started
Add this to your dependencies:
```groovy
implementation "net.gotev:cookie-store:x.y.z"
```
Replace `x.y.z` with 
## Usage
Create your Cookie Manager:
```kotlin
// Example extension function to demonstrate how to create both cookie stores
fun Context.createCookieStore(name: String, persistent: Boolean) = if (persistent) {
SharedPreferencesCookieStore(applicationContext, name)
} else {
InMemoryCookieStore(name)
}
val cookieManager = CookieManager(
createCookieStore(name = "myCookies", persistent = true),
CookiePolicy.ACCEPT_ALL
)
```
### HttpURLConnection
To setup the default Cookie Manager:
```kotlin
CookieManager.setDefault(cookieManager)
```
### OkHttp
Add the following dependency (suitable for JVM and Android):
```groovy
implementation "net.gotev:cookie-store-okhttp:$cookieStoreVersion"
```
And when you build your OkHttpClient, set the Cookie Jar:
```kotlin
val okHttpClient = OkHttpClient.Builder()
.cookieJar(JavaNetCookieJar(cookieManager))
.build()
```
### WebView
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 `java.net.CookieManager`
#### Using WebKitSyncCookieManager
```kotlin
val cookieManager = WebKitSyncCookieManager(
store = createCookieStore(name = "myCookies", persistent = true),
cookiePolicy = CookiePolicy.ACCEPT_ALL,
onWebKitCookieManagerError = { exception ->
// This gets invoked when there's internal webkit cookie manager exceptions
Log.e("COOKIE-STORE", "WebKitSyncCookieManager error", exception)
}
)
```
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:
```kotlin
cookieManager.removeAll()
```
This will clear both the `CookieStore` and WebKit's Cookie Manager.
#### Using standard java.net.CookieManager
Cookies syncing is entirely up to you and manual.
To copy all cookies from the cookie store to the WebKit Cookie Manager:
```kotlin
cookieManager.cookieStore.syncToWebKitCookieManager()
```
Remember to do this before loading any URL in your web view.
To remove all cookies from the Cookie Store:
```kotlin
cookieManager.cookieStore.removeAll()
```
To remove all cookies from WebKit Cookie Manager:
```kotlin
android.webkit.CookieManager.getInstance().removeAll()
```
That's all folks!