I'm trying to upgrade a WebView-wrapped Android app that was built using Trigger.io to one built with Apache Cordova. While both frameworks are similar, the session cookies are stored differently in Trigger vs Cordova.
To fix this, I've written some code that will copy all cookies from the app's CookieManager instance to the CordovaWebView's cookie manager:
public void fetchTriggerCookies() {
// Note: I also tried using a CookieStore implementation for
// retrieving the old cookies, but it didn't seem to make
// a difference.
CookieManager cm = CookieManager.getInstance();
String cookies = cm.getCookie("my.example.com");
// Note: cm.hasCookies() is always false when cookies is empty,
// so I know I'm checking the right URL
System.out.println("WebView CM has cookies? " + cm.hasCookies());
System.out.println("WebView CM cookies: " + cookies);
if (cookies == null || cookies.length() == 0) { return; }
this.mTriggerCookieString = cookies;
}
public void restoreTriggerCookies(CordovaWebView webView) {
if (this.mTriggerCookieString == null) { return; }
webView.getCookieManager().setCookie("my.example.com", this.mTriggerCookieString);
}
Much to my confusion, calling fetchTriggerCookies() in the Cordova app's onCreate() (both before and after the super call) only sometimes finds cookies to restore. (I've tried running this from a few other places around the app init code without any difference.)
It looks like the cookies are being stored in the app_webview/Cookies file (a SQLite DB), but that file is somehow being removed directly after the new APK is installed! I can test this with:
# Uninstall and install Trigger app:
adb uninstall com.my.example && adb install path/to/old/trigger-apk.apk
# Sign in to trigger app to load session cookies
# Extract a backup of the trigger app from my device...
adb backup -noapk com.my.example
dd if=backup.ab bs=1 skip=24 > compressed-data
printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" | cat - compressed-data | gunzip -c > decompressed-data.tar
tar xf decompressed-data.tar
# ...and verify that the sessionid cookie exists
cat apps/com.my.example/r/app_webview/Cookies | grep sessionid # is found!
# Install the new Cordova app
adb install -r path/to/new/android-debug.apk
# Before running the app, shell into the device and check if cookies file exists
adb shell
run-as com.my.example
ls app_webview/Cookies # No such file or directory
What's extremely strange about this is that it sometimes works fine; sometimes the Cookie file exists and my code is able to copy them to the new WebView. But other times it does not—even with the same APKs signed by the same debug key—and I can see that the Cookie file doesn't exist after installing the new app (and before running it).
So, is there anything I'm not understanding about how cookies are stored/cached in Android and why I might not be able to access them? Why would installing a new app clear the Cookies file? Is there a more stable way to request cookies than through the CookieManager?
Thanks in advance to any helpful feedback! :)