Android CookieManager.setCookie doesn't sync across different process?

570 views Asked by At

In Android, we can have different process (e.g. a Service in another process).

However, I notice the CookieManager (from android.webkit.CookieManager) doesn't sync its newly set Cookie across the process.

I have a function in the MainActivity that set the Cookie

cookieManager.setCookie(url, cookieString)
cookieManager.flush()

If I read it in the MainActivity or MainService (service in the same process), then I can get it read.

val cookie = cookieManager.getCookie(url)

However, if I read it in the OtherService (service in the other process), then it might not get it.

To demonstrate this clearly, I have created an App as shown below. You can get the code here

enter image description here

1. Freshly Install App

  1. Install a freshly created App
  2. Click Main Process Service: Log (TrackingMe) shows no Cookie Set <Correct>
  3. Click Other Process Service: Log (TrackingMe) shows no Cookie Set <Correct>
  4. Click Write Cookie to write cookie into CookieManager
  5. Click Main Process Service: Log (TrackingMe) shows Cookie Set <Correct>
  6. Click Other Process Service: Log (TrackingMe) shows no Cookie Set <Incorrect>

2. Kill the App and Restart the App

  1. Kill and Restart the App (don't remove the App data)
  2. Click Main Process Service: Log (TrackingMe) shows Cookie Set <Correct>
  3. Click Other Process Service: Log (TrackingMe) shows Cookie Set <Correct>
  4. Click Clear Cookie to clear all Cookies into CookieManager
  5. Click Main Process Service: Log (TrackingMe) shows no Cookie Set <Correct>
  6. Click Other Process Service: Log (TrackingMe) shows Cookie Set <Incorrect>

The finding

  • The Other Process Service will not get the updated cookies from the CookieManager. It can only get it after the App is killed and restart.

The question:

  • How can we get the Other Process Service get the Cookies from the CookieManager immediately? (without need to restart the App?)
0

There are 0 answers