BY Akash Kava16 Jul 2021 Edit
Embed V8 JavaScript Engine in Xamarin.Android

Isn't there any existing JavaScript engine for Xamarin.Android?

Well, there is LiquidCore project, which has Java bindings for V8, you can use it to embed V8 in Xamarin.Android by natively embedding Android Native Project (AAR) files of LiquidCore project.

Also there are other implementations such as DuckTape, JavaScriptCore etc.

The problem with all of them is, your C# code actually calls (Marshals all parameters) Java code which in turn calls (Marshals all parameters) again to V8.

   // Marshall all parameters 
   CLR Code -> Java Code -> V8 Native

   // Marshal result
   CLR Code <- Java Code <- V8 Native

This in turn slows your execution if your code frequently access C# code.

So I decided to remove "Java" bridge to call V8.

V8 is Embedded as a Native Library

Since V8 is embedded as native library, calls from C# does not go through Java bride.

   CLR Code -> V8 Native
   CLR Code <- V8 Native

V8 Inspector Protocol Support

Currently LiquidCore does not support Inspector protocol, that means you cannot debug your JavaScript. And other libraries such as DuckTape etc does not have any support for debugging at all.

Xamarin.Android.V8 (Name is subjected to change in future)

NuGet Package

NuGet Package is available with ID Xamarin.Android.V8

Inspector Protocol Port

To open Inspector protocol port from your device, you have to click Visual Studio > Tools > Android > Android Adb Command Prompt. Then type,

    adb forward tcp:9222 tcp:9222

Create Context

using(var context = new JSContext( /*Enable Debugging*/ true)) {

  // you can connect to dev tools by visiting url
  // devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=

    context.Evaluate("console.log('I am ready')", "vm");


Now you have full debugging support in the given link


Create New Global Function

Context itself is a global object. So you can store/retrive values on it.

context["printf"] = context.CreateFunction(0, (c, a) => {
  // first parameter is context isself
  // second parameter is an array as IJSValue
  return c.Undefined;

context.Evaluate("  printf('This is from JS Coe'); ", "vm");

// this works as well
context.Evaluate("  global.printf('This is from JS Coe'); ", "vm");

Evaluate Script with Location

// script location is useful for debugging
context.Evaluate(scriptText, scriptLocation);

Navigate Objects

Each JavaScript object is exposed to CLR as IJSValue, you can access properties and methods on this object directly.

   // Object.create() JavaScript Equivalent in c#
   var obj = context["Object"].InvokeMethod("create");
   var obj = context.CreateObject();
   obj["name"] = context.CreateString("Akash");

   // Object.keys(obj) JavaScript Equivalent in c#
   var keys = context["Object"].InvokeMethod("keys", obj);

   for(var i = 0; i < keys.Length; i++) {
       var key = keys[i].ToString();
       var value = obj[key].ToString();

       //... you have key and value here

Serialize C# Object

When you use method context.Convert method to automatically create native JS values from native types, it will only wrap C# custom object, you cannot call any method or access property from JavaScript on wrapped object. This is done to improve performance. So when you pass C# objects in and out, engine will not create methods and properties on them.

In order to access methods and properties of C# object, you have to serialize them.

  // you can access all properties, no methods
   var jsDictObject = context.Serialize( customClrObject , SerializationMode.Copy);

   // you can access all properties and invoke method as well
   var jsClrObject = context.Serialize( customClrObject , SerializationMode.Reference);

Serialization Modes


This method will create a deep copy of CLR Object as dictionary which you can easily access inside JavaScript code. This method will fail if there are self referencing objects in the object graph. This limitation may be removed in future, but right now it will throw an exception.

This method is also very slow as deep copy operation will take more time.

Deserialization will also be slow as it will completely construct new object with all properties.


Keeps reference along with serialization, every property is serialized as getter/setter, upon deserialization, same object will be returned.

This method is useful for self referencing objects, but this may cause memory leak if you keep reference in JavaScript and JavaScript garbage collector fails to dispose object.

Deserialization is faster as it simply returns referenced object.


Same as Reference but it only keeps weak reference, you will get object disposed if you try to access object in JavaScript and it is disposed in CLR. CLR is very aggressive while disposing objects, so this may not work if you do not keep reference in CLR. This is also recommended method as it will avoid memory leaks.


This is default serialization method for any object. Object will simply be wrapped and no methods/properties are exposed.

Thread Safety

This library is not thread safe and it is recommended that you use JSContext on the same thread as you created on. Most likely it is better to call in UI Thread.

This is done to avoid unnecessary locking when you don't need them. You can achieve locking in the CLR when you need them.


MIT License

Big Thanks to

A huge thanks to following GitHub repo to help making this project a reality.

  1. LiquidCore
  2. v8-android-buildscripts
  3. v8dotnet
BY Akash Kava
Developer Guides
Web Atoms Updates

17 Mar 2021
Simmi Kava
commented this post.
Simmi Kava
liked this post.
Show more
javascript (56)
developer (25)
javascriptdeveloper (16)
Xamarin.Forms (16)
Html (14)
typescript (12)
webatoms (12)
xamarin (11)
coding (10)
web-atoms (10)
arrays (9)
android (8)
javascript-developer (8)
csharp (7)
dotnet (7)
css (6)
update (6)
dotnet-standard (5)
function (5)
iOS (5)
methods (4)

Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository