Forum Libraries, Components, and Plugins


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Embed V8 JavaScript Engine in Xamarin.Android


<PackageReference Include="Xamarin.Android.V8" Version="1.4.79" />

GitHub Repo

Inspector Protocol Port

Visual Studio > Tools > Android > Android Adb Command Prompt

adb forward tcp:9222 tcp:9222

If you want to change the default 9222 port, you can specify in the parameters.

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=


Create New Global Function

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

// JS : printf("a");

Evaluate Script with Location

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

Navigate Objects

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

Convert Native Objects

As V8 Engine has its own representation of native types, you need to create them in order to pass them as parameters for JavaScript methods.

   var jsString = context.CreateString("TEXT");
   var jsNumber = context.CreateNumber(100);

   context["console"].InvokeMethod("log", jsString);
   context["console"].InvokeMethod("log", jsNumber);

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.

Big Thanks to
A huge thanks to following github repo to help making this project a reality.

Sign In or Register to comment.