title: Android webview: throwing some token error. google.android.gms. something Plus webcookies not caching properly
body: <p>I have a webview application. I actually refactored package name an old application. That one was actually using firebase messaging. I removed all references to the firebase in my version. But its still throwing an error.</p>
  
  <pre><code>    2019-07-15 23:34:49.191 13878-13878/? E/volley is: 0/
  2019-07-16 02:24:51.997 30248-1014/? E/NetRec: [351] alcs.a: Could not retrieve server token for package com.google.android.apps.gcs
      java.util.concurrent.ExecutionException: rdg: 29503: 
          at avgq.b(:com.google.android.gms@17785040@17.7.85 (100700-253824076):3)
          at avgq.a(:com.google.android.gms@17785040@17.7.85 (100700-253824076):20)
          at alcs.a(:com.google.android.gms@17785040@17.7.85 (100700-253824076):1)
          at alcs.a(:com.google.android.gms@17785040@17.7.85 (100700-253824076):4)
          at alcr.getHeaders(:com.google.android.gms@17785040@17.7.85 (100700-253824076):2)
          at com.android.volley.toolbox.HttpClientStack.performRequest(:com.google.android.gms@17785040@17.7.85 (100700-253824076):9)
          at sdc.performRequest(:com.google.android.gms@17785040@17.7.85 (100700-253824076):1)
          at bwt.executeRequest(:com.google.android.gms@17785040@17.7.85 (100700-253824076):1)
          at com.android.volley.toolbox.BasicNetwork.performRequest(:com.google.android.gms@17785040@17.7.85 (100700-253824076):5)
          at sdf.performRequest(:com.google.android.gms@17785040@17.7.85 (100700-253824076):13)
          at com.android.volley.NetworkDispatcher.a(:com.google.android.gms@17785040@17.7.85 (100700-253824076):7)
          at com.android.volley.NetworkDispatcher.run(:com.google.android.gms@17785040@17.7.85 (100700-253824076):2)
       Caused by: rdg: 29503: 
          at rjf.a(:com.google.android.gms@17785040@17.7.85 (100700-253824076):4)
          at aodi.b(:com.google.android.gms@17785040@17.7.85 (100700-253824076):2)
          at aofv.a(:com.google.android.gms@17785040@17.7.85 (100700-253824076):6)
          at aaew.run(:com.google.android.gms@17785040@17.7.85 (100700-253824076):30)
          at bkng.run(:com.google.android.gms@17785040@17.7.85 (100700-253824076):2)
          at skq.b(:com.google.android.gms@17785040@17.7.85 (100700-253824076):37)
          at skq.run(:com.google.android.gms@17785040@17.7.85 (100700-253824076):21)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
          at sqo.run(Unknown Source:7)
          at java.lang.Thread.run(Thread.java:919)
  </code></pre>
  
  <p>App level gradle</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 28
      defaultConfig {
          applicationId "com.storeuncle.studyapp"
          minSdkVersion 22
          targetSdkVersion 28
          versionCode 3
          versionName "1.2"
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
  
  }
  
  dependencies {
      implementation fileTree(dir: 'libs', include: ['*.jar'])
      implementation 'com.android.support:appcompat-v7:28.0.0'
      // implementation 'com.android.support:support-v4:28.0.0'
      implementation 'com.android.support.constraint:constraint-layout:1.1.3'
      //implementation 'com.google.firebase:firebase-messaging:17.3.4'
      testImplementation 'junit:junit:4.12'
      androidTestImplementation 'com.android.support.test:runner:1.0.2'
      androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
      //implementation 'com.google.firebase:firebase-core:17.0.0'
  
     // compile "me.leolin:ShortcutBadger:1.1.19@aar"
  }
  
  //apply plugin: 'com.google.gms.google-services'
  </code></pre>
  
  <p>Top level gradle</p>
  
  <pre><code>buildscript {  
      repositories {
          google()
          jcenter()
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:3.4.2'
          // NOTE: Do not place your application dependencies here; they belong
          // in the individual module build.gradle files
        //  classpath 'com.google.gms:google-services:3.1.0'
      }
  }
  allprojects {
      repositories {
          google()
          jcenter()
      }
  }
  
  task clean(type: Delete) {
      delete rootProject.buildDir
  }
  </code></pre>
  
  <p>The original pacakge name was com.xyz.appname. At first I changed just the appname. It worked fine. The I changed the folder name from xyz to something else. Thats when this error popped up. I have just one java file with all the code in it. </p>
  
  <p>There are no errors in any on the lines in any files. manifest or gradle or any other files. The app crashed as soon as it launches. Can someboby please help me understand what should be causing this ? It would really make my day. Thank you.</p>
  
  <p><strong>UPDATE On My Sufferings :-)</strong> 
  The app stopped crashing. But the error is still there. Also please have a look at my Code I have implemented for caching browser cookies. Cos this is also not working as expected either. The site works fine on all browsers. But in webview the cookies are inaccessible to the loaded pages on the first login. It only get the cookies after the second login.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      WebView mWebView;
      private static final int INPUT_FILE_REQUEST_CODE = 1;
      private static final int FILECHOOSER_RESULTCODE = 1;
      private static final String TAG = MainActivity.class.getSimpleName();
      private ValueCallback&lt;Uri&gt; mUploadMessage;
      private Uri mCapturedImageURI = null;
      private ValueCallback&lt;Uri[]&gt; mFilePathCallback;
      private String mCameraPhotoPath;
  
      Toolbar toolbar;
      SharedPreferences sharedPref;
      SwipeRefreshLayout swipeContainer;
      CookieManager cookieManager;
  
      String URLS = "https://xyz.co.in/zyx/panel/index.php";
      int is_url = 0;
  
      @RequiresApi(api = Build.VERSION_CODES.ECLAIR_MR1)
      @SuppressLint("SetJavaScriptEnabled")
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          sharedPref = getApplicationContext().getSharedPreferences("MyPref", 0);
  
          mWebView = (WebView) findViewById(R.id.webView);
          toolbar = (Toolbar) findViewById(R.id.toolbar);
          swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer);
  
          mWebView.setWebViewClient(new NewWebViewClient());
          mWebView.setWebChromeClient(new ChromeClient());
          mWebView.clearCache(false);
  
          WebSettings webSettings = mWebView.getSettings();
          webSettings.setJavaScriptEnabled(true);
          webSettings.setDomStorageEnabled(true);
          webSettings.setAllowFileAccess(true);
          webSettings.setLightTouchEnabled(true);
          webSettings.setSupportZoom(true);
          webSettings.setBuiltInZoomControls(true);
          webSettings.setAppCacheEnabled(true);
          setSupportActionBar(toolbar);
  
          cookieManager = CookieManager.getInstance();
          cookieManager.setAcceptCookie(true);
  
          Intent intent = getIntent();
          is_url = intent.getIntExtra("is_url", 0);
          if (is_url == 1) {
              URLS = intent.getStringExtra("url");
          }
          if (isNetworkConnected()) {
              loadWebsite();
          }
          if (savedInstanceState == null) {
              mWebView.post(new Runnable() {
                  @Override
                  public void run() {
                      loadWebsite();
                  }
              });
          }
      }
  
      private void loadWebsite() {
          ConnectivityManager cm = (ConnectivityManager) getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo netInfo = cm.getActiveNetworkInfo();
          if (netInfo != null &amp;&amp; netInfo.isConnectedOrConnecting()) {
              mWebView.loadUrl(URLS);
          } else {
              mWebView.setVisibility(View.GONE);
          }
      }
  
  public class NewWebViewClient extends WebViewClient {
          String uid="";
          String pswd = "";
          String exp="";
          String usrnm = "";
          String cookies ="";
          @Override
          public void onPageStarted(WebView view, String url, Bitmap favicon) {
              super.onPageStarted(view, url, favicon);
              if(url.contains("login")){
                 // view.clearCache(true);
                  //view.clearHistory();
                  //view.clearFormData();
                  Log.i("Listener", "Start");
              }
  
          }
  
          @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD)
          @Override
          public void onPageFinished(WebView view, String url) {
              super.onPageFinished(view, url);
              String cookies = cookieManager.getCookie(url);
              Log.d(TAG, "All the cookies in a string:" + cookies);
              try {
  
                  if (cookies == null) {
                      return;
                  }
  
                  String[] splitCookies = cookies.split(";");
                  for (String cookie : splitCookies) {
                      String[] cookieParts = cookie.split("=");
                      if (cookieParts.length &gt; 0) {
                          String newCookie = cookieParts[0].trim() +
                                  "=;expires=Sat, 1 Jan 2000 00:00:01 UTC;";
                          cookieManager.setCookie(url, newCookie);
                      }
                  }
  
  
                  uid = getCookie(url, "user_id");
                      pswd = getCookie(url, "login_string");
                      usrnm = getCookie(url, "username");
                      exp = getCookie(url, "exp");
                      if (uid != null &amp;&amp; !uid.isEmpty()) {
                          SharedPreferences.Editor editor = sharedPref.edit();
                          editor.putString("user_id", uid);
                          editor.putString("login_string", pswd);
                          editor.putString("username", usrnm);
                          editor.putString("exp", exp);
                          //  Log.e("4444 ", ss.toString());
                          editor.commit();
                          Log.v(TAG, "cook page fin:" + cookies);
                      }
  
  
              }catch(Exception e){
  
              }
              if(url.contains("logout")){
                  Toast.makeText(MainActivity.this, "Logout successful", Toast.LENGTH_SHORT).show();
                  view.clearCache(true);
                  view.clearHistory();
                  view.clearFormData();
  
                  //sharedPref.edit().clear().apply();
                  Log.i("Listener", "Finish");
              }
          }
          @Override
          public void onLoadResource(WebView view, String url){
              Log.v(TAG, "onLoadResource url: " + url);
              String cookies = CookieManager.getInstance().getCookie(url);
  
          }
          public String getCookie(String siteName,String cookieName){
              String CookieValue = null;
  
              CookieManager cookieManager = CookieManager.getInstance();
              String cookies = cookieManager.getCookie(siteName);
              String[] temp=cookies.split(";");
              for (String ar1 : temp ){
                  if(ar1.contains(cookieName)){
                      String[] temp1=ar1.split("=");
                      CookieValue = temp1[1];
                      break;
                  }
              }
              return CookieValue;
          }
      }
  }
  </code></pre>
  
  <p>I can see logs of cookies as below</p>
  
  <pre><code>D/MainActivity: All the cookies in a string:PHPSESSID=p6puofnc250frclqh541cog5i6; user_id=4d236af1; username=Sachin; exp=2020-07-11; login_string=koko%40sert
  V/MainActivity: cook page fin:PHPSESSID=p6puofnc250frclqh541cog5i6; user_id=4d236af1; username=Sachin; exp=2020-07-11; login_string=koko%40sert
  </code></pre>