Coder Social home page Coder Social logo

pavel-corsaghin / androidjsmodule Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 1.0 70.74 MB

A simple library that helps Android developers to execute JavaScript code from Android native side easily without using Webview.

License: MIT License

Kotlin 0.65% Makefile 0.04% C++ 40.76% C 24.07% Objective-C++ 24.51% Objective-C 8.56% HTML 0.01% JavaScript 1.40%
javascript android native javascriptcore

androidjsmodule's Introduction

AndroidJSModule

Maven Central

A simple library that helps Android developers to execute JavaScript code from Android native side easily without using Webview.

Installation

The easiest way to add the library to your project is by adding it as a dependency to your build.gradle

dependencies {
    implementation "io.github.pavel-corsaghin:androidjs:${release_version}""
}

Usage

  • Usage library with plain javascript code:
// Load plain script
val plainScript = "var js_obj = {add: function(a,b) { return a+b; }, multiply: function(a,b) { return a*b; }};"
JsExecutor.loadPlainScript(plainScript)

// Execute function and get result
val add = JsExecutor.execute("js_obj.add", 1, 2) //3

val multiply = JsExecutor.execute("js_obj.multiply", 4, 5) //20
  • Load a js bundle and execute functions
// Load js bundle file
JsExecutor.loadJsBundle(this, "JsModule/dist/jsBundle.js")

// Get simple message
val message = JsExecutor.execute("JsModule.getMessage") //This is message from Javascript side

// Execute function and get result
val logStr = JsExecutor.execute("JsModule.MathJs.log", 10000, 10) //4
  • Get custom kotlin object
// Primitive types
val logDouble = JsExecutor.executeToGetObject<Double>("JsModule.MathJs.log", 1000, 10) //2.999

// Custom object
val inputPoint = Point(3.4, 5.2)
val outputPoint = JsExecutor.executeToGetObject<Point>(
    "JsModule.MathJs.doublePoint",
     JsExecutor.jsonConverter.encodeToString(inputPoint)
)

// Define Point class
@Serializable
data class Point(val lat: Double, val lon: Double)

Contributing

All contributions are welcomed. Please create your PR and send to me.

License

MIT

androidjsmodule's People

Contributors

pavel-corsaghin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

concurs-program

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.