title: Android WebView evaluateJavascript Uncaught ReferenceError
body: <p>Currently I'm trying to figured out which is the best way to invoke javascript from android.</p>
  
  <ul>
  <li>AndroidJSCore. <a href="https://github.com/ericwlange/AndroidJSCore.git" rel="nofollow noreferrer">AndroidJSCore</a> </li>
  <li>WebView</li>
  </ul>
  
  <p>I've tried AndroidJSCore, it's great. But it would enlarge the apk size.
  Then I trying to dive into WebView to invoke javascript. But fail. </p>
  
  <p>Activity look like this:</p>
  
  <pre><code>public class WebViewActivity extends AppCompatActivity {
  
  private WebView webView;
  private static final String TAG = WebViewActivity.class.getSimpleName();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_web_view);
      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      webView = (WebView)findViewById(R.id.webview);
  
      WebSettings webSettings = webView.getSettings();
      webSettings.setJavaScriptEnabled(true);
      webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
      webSettings.setSupportZoom(true);
      webSettings.setDisplayZoomControls(true);
  
      webView.loadUrl("file:///android_asset/jsWeb/test.html");
      webView.setWebViewClient(new WebViewClient(){
          public void onPageFinished(WebView view, String url){
  
              String test1 = "permissionRegist(\"'function'\", [\"'319609831167500021'\",\"'319609831167500022'\",\"'319609831167500023'\",\"'419609839767500020'\",\"'644731700926740003'\",\"'644731700926740004'\",\"'854885785357389824'\",\"'854995673156816896'\",\"'855001385022918656'\",\"'855009454683459584'\",\"'855298661779902464'\",\"'855303143108513792'\",\"'855347061611171840'\",\"'855356859882803200'\"])";
              String test2 = "evalJSExpression()";
  
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.KITKAT) {
                  webView.evaluateJavascript(test1, new ValueCallback&lt;String&gt;() {
                      @Override
                      public void onReceiveValue(String s) {
                          Log.d(TAG, s);
                      }
                  });
  
                  webView.evaluateJavascript(test2, new ValueCallback&lt;String&gt;() {
                      @Override
                      public void onReceiveValue(String s) {
                          Log.d(TAG, s);
                      }
                  });
              }
          }
      });
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>there appending files are:</p>
  
  <p>android.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function permissionRegist(type, list) {
      var s_permissionMap = new Map();
      s_permissionMap.set(type, list);
  }
  
  function evalJSExpression() {
      var result = 'dddddd';
      return result;
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;title&gt;LogicalExpression&lt;/title&gt;
  
      &lt;!-- 导入script --&gt;
      &lt;script type="text/javascript" src="android.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;!--&lt;button onClick="permissionRegist()"&gt;点击读取文件&lt;/button&gt;--&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>but the result is :</p>
  
  <pre><code> I/chromium: [INFO:CONSOLE(2)] "Uncaught ReferenceError: Map is not defined", source: file:///android_asset/jsWeb/android.js (2)
   D/WebViewActivity: null
   D/WebViewActivity: "dddddd"
  </code></pre>