Fix to Cordova/PhoneGap Apps targeting Android 5 (Lollipop) or later on default disallowing third party cookies

When I updated my Cordova app to the latest Android 5.0 (Lollipop), I noticed, that the login does not work anymore. In prior versions of Android it works (app login is based on session cookies).

On further investigation found that, if your app targets API levels lower than 21: The system allows mixed content and third party cookies, and always renders the whole document at once.

If your app targets API level 21 or higher and if the app rely on session cookies it will stop working as expected. Reference here.


Here is a fix to make session cookies work again on Android 5.0 (Lollipop) and above:
Highlighted lines is the new code to be added for this fix

package com.joash.testApp;

import android.os.Bundle;
import org.apache.cordova.*;
import android.os.Build;
import android.webkit.CookieManager;
import android.webkit.WebView;

public class testApp extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();

        // Allow third party cookies for Android Lollipop
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                WebView webView = (WebView)super.appView;
                CookieManager cookieManager = CookieManager.getInstance();
                cookieManager.setAcceptThirdPartyCookies(webView,true);
        }
        super.loadUrl(Config.getStartUrl());
    }
}

Note: Clean the project before compiling. And make sure that you use the latest Android SDK to compile the source code.