title: Android utilize V8 without WebView
body: <p>I'm exercising executing javascript from Java.  Rhino works very well for this on desktop, but has to fall back to (slow) interpreted mode on Android (due to dalvik being unable to execute the Java bytecode the Rhino JIT compiles).</p>
  
  <p>Android has its built-in V8 javascript engine which is accessed internally via JNI and ought to give much better performance than Rhino; however, the only way I can find to access it is indirectly through a WebView.</p>
  
  <p>Unfortunately, WebView requires a Context, and crashes with NPE with a null context, so I'm unable to even instantiate a dummy WebView to merely execute the code and return the result.  The nature of my exercise doesn't really allow me to provide a Context for WebView, so I'm hoping perhaps there's something I'm overlooking.</p>
  
  <p>Several of these V8Threads run in parallel, so it's not really feasible (as far as I'm aware) to add a WebView to my layout and hide it, as I don't believe a single WebView can execute functions in multiple threads.</p>
  
  <pre><code>private class V8Thread extends Thread
  {
      private WebView webView;
      private String source;
  
      private double pi;
      private int i, j;
  
      public V8Thread(int i, int j)
      {
          pi = 0.0;
          this.i = i;
          this.j = j;
  
          source = "";
  
          try {
              InputStreamReader isReader = new InputStreamReader(assetManager.open("pi.js"));
              int blah = isReader.read();
              while (blah != -1)
              {
                  source += (char)blah;
                  blah = isReader.read();
              }
  
              webView = new WebView(null);
              webView.loadData(source, "text/html", "utf-8");
              webView.getSettings().setJavaScriptEnabled(true);
              webView.addJavascriptInterface(this, "V8Thread");
          } catch (IOException e) {
              e.printStackTrace();
          }
  
      }
  
      public double getResult()
      {
          return pi;
      }
  
      @Override
      public void run() 
      {
          webView.loadUrl("javascript:Androidpicalc("+i+","+j+")");
      }
  }
  </code></pre>
  
  <p>Ideally there must be some supported way to call V8 directly, or at least execute javascript without requiring an actual WebView, as it seems a rather clunky and convoluted method just to run javascript code.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I've rearranged my code a bit, though unseen here is that now I am instantiating the V8Threads on the AsyncTasks's onPreExecute() while keeping everything else in doInBackground().  The source code is read in earlier in the program, so it's not redundantly re-read for each thread.</p>
  
  <p>Because now the V8Thread is instantiated on the UI Thread, I can pass it the current view's Context (I'm using fragments so I can't just pass it "this"), so it no longer crashes.</p>
  
  <pre><code>private class V8Thread extends Thread
  {
      private WebView webView;
  
      private double pi;
      private int i, j;
  
      public V8Thread(int i, int j)
      {
          pi = 0.0;
          this.i = i;
          this.j = j;
  
          source = "";
  
          webView = new WebView(v.getContext());
      }
  
      @SuppressWarnings("unused")
      public void setResult(String in)
      {
          Log.d("Pi",in);
      }
  
      public double getResult()
      {
          return pi;
      }
  
      @Override
      public void run()
      {
          webView.getSettings().setJavaScriptEnabled(true);
          webView.addJavascriptInterface(this, "V8Thread");
          webView.loadData(source, "text/html", "utf-8");
          //webView.loadUrl("javascript:Androidpicalc("+i+","+j+")");
          webView.loadUrl("javascript:test()");
          Log.d("V8Thread","Here");
      }
  }
  </code></pre>
  
  <p>However, when executing, logcat spits out one per thread of the error "Can't get the viewWidth after the first layout" and the javascript code never executes.  I know the thread fires completely, because the "Here" log message is sent.  Here's the relevant test() function in the js code.</p>
  
  <pre><code>function test()
  {
  V8Thread.setResult("blah");
  }
  </code></pre>
  
  <p>Working correctly, "blah" should show up four times in logcat, but it never shows up.  Could be my source code is read incorrectly, but I doubt that.</p>
  
  <pre><code>Scanner scan = new Scanner(assetManager.open("pi.js"));
  while (scan.hasNextLine()) source += scan.nextLine();
  </code></pre>
  
  <p>The only other thing I can imagine is that due to these aforementioned errors, the webView never actually gets around to executing the javascript.</p>
  
  <p>I'll also add that pi.js contains only javascript, no HTML whatsoever.  However, even when I wrap it in just enough HTML for it to qualify as a webpage, still no luck.</p>