# LiquidCore **Repository Path**: dashixiong91/LiquidCore ## Basic Information - **Project Name**: LiquidCore - **Description**: Node.js virtual machine for Android and iOS - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2020-02-20 - **Last Updated**: 2022-01-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # The LiquidCore Project [](https://www.npmjs.com/package/liquidcore) [](https://nodei.co/npm/liquidcore/) LiquidCore enables [Node.js](https://nodejs.org) virtual machines to run inside Android and iOS apps. It provides a complete runtime environment, including a virtual file system. LiquidCore also provides a convenient way for Android developers to [execute raw JavaScript](https://github.com/LiquidPlayer/LiquidCore/wiki/LiquidCore-as-a-Raw-JavaScript-engine-for-Android-(v.-0.7.0-)) inside of their apps, as iOS developers can already do natively with JavaScriptCore. ## Installation #### Step 1: Make sure your project is configured for use with `npm` In the root directory of your project, you must have a `package.json` file. If you do not already have one, you can create it by running: ```bash $ npm init ``` and following the steps in the wizard. #### Step 2: Install LiquidCore and configure `package.json` ```bash $ npm i liquidcore $ node node_modules/liquidcore/lib/cli.js init ``` The `init` step will add some utility scripts and the `liquidcore` object to your `package.json` file. It will also create an example service called `example.js`, which will get packaged into your app. You can change / add files to be packaged by editing the `liquidcore.entry` property in your `package.json`. #### Step 3: Configure your mobile app project
| #### Android | #### iOS |
|
```bash
$ npm run gradle-config -- --module= |
```bash
$ npm run pod-config -- --target= |
| #### Android Kotlin | #### iOS Swift |
| ```kotlin val uri = MicroService.Bundle(androidContext, "example") val service = MicroService(androidContext, uri) service.start() ``` | ```swift import LiquidCore ... let url = LCMicroService.bundle("example") let service = LCMicroService(url: url) service?.start() ``` |
| #### Android Kotlin | #### iOS Swift |
| ```kotlin val uri = MicroService.Bundle(androidContext, "example") val startListener = MicroService.ServiceStartListener { // .. The environment is live, but the startup // JS code (from the URI) has not been executed yet. } val service = MicroService(androidContext, uri, startListener) service.start() ``` | Conform to `LCMicroServiceDelegate` ```swift let service = LCMicroService(url:url, delegate:self) service?.start() ... func onStart(_ service: LCMicroService) { // .. The environment is live, but the // startup JS code (from the URI) has // not been executed yet. } ``` |
| #### Android Kotlin | #### iOS Swift |
| ```kotlin val listener = MicroService.EventListener { service, event, payload -> android.util.Log.i("Event:" + event, payload.getString("foo")) // logs: I/Event:my_event: hello, world } service.addEventListener("my_event", listener) ``` |
Conform to `LCMicroServiceEventListener`
```swift
service.addEventListener("my_event", listener:self)
...
func onEvent(_ service: LCMicroService, event: String,
payload: Any?) {
var p = (payload as! Dictionary |
| #### Android Kotlin | #### iOS Swift |
| ```kotlin val payload = JSONObject() payload.put("hallo", "die Weld") service.emit("host_event", payload) ``` | ```swift var payload = ["hallo" : "die Weld"] service.emitObject("host_event", object:payload) ``` |
| #### Android Kotlin | #### iOS Swift |
|
```kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import org.liquidplayer.service.MicroService
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState:
Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val hello = findViewById |
```swift
import UIKit
import LiquidCore
class ViewController: UIViewController,
LCMicroServiceDelegate,
LCMicroServiceEventListener {
@IBOutlet weak var textBox: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let url = LCMicroService.bundle("example")
let service = LCMicroService(url: url,
delegate: self)
service?.start()
}
func onStart(_ service: LCMicroService) {
service.addEventListener("ready",listener: self)
service.addEventListener("pong", listener: self)
}
func onEvent(_ service: LCMicroService,
event: String,
payload: Any?) {
if event == "ready" {
service.emit("ping")
} else if event == "pong" {
let p = (payload as! Dictionary |