Skip to content

Latest commit

 

History

History
 
 

android

README.md

NativeScript Logo

Node-API Android Runtime for NativeScript

NPM Version (with dist tag) NPM Version (with dist tag) NPM Version (with dist tag) NPM Version (with dist tag)

NativeScript is an open-source framework for building truly native mobile applications using JavaScript. This repository contains the source code for the Node-API based Android Runtime used by NativeScript.

The Android Runtime is a key component of the NativeScript framework. It is responsible for executing JavaScript code on Android devices. The runtime is built on top of Node-API and provides a way to interact with the Android platform APIs from JavaScript.

The new runtime is based on the Node-API and is designed to be more stable, faster, and easier to maintain. It also supports multiple JavaScript engines, including V8, QuickJS, Hermes, and JavaScriptCore.

Project structure

The repo is structured in the following projects (ordered by dependencies):

  • android-metadata-generator - generates metadata necessary for the Android Runtime.
  • android-binding-generator - enables Java & Android types to be dynamically created at runtime. Needed by the extend routine.
  • android-runtime - contains the core logic behind the NativeScript's Android Runtime. This project contains native C++ code and needs the Android NDK to build properly.
  • android-runtime-testapp - this is a vanilla Android Application, which contains the tests for the runtime project.
  • native runtime sources - contains the shared runtime, JNI bridge, and engine-specific Node-API implementations.

Helper Projects

Build Prerequisites

Following are the minimal prerequisites to build the runtime package.

  • Install JDK 17 and set JAVA_HOME to it. Newer JDKs are not supported by the current Gradle toolchain.

  • Install the latest Android Studio.

  • From the SDK Manager (Android Studio -> Tools -> Android -> SDK Manager) install the following components:

    • Android SDK Platform 34
    • Android SDK Build Tools 35.0.0 or newer
    • Android NDK r27
    • CMake

How to Build

Clone the repo:

git clone https://github.com/NativeScript/runtimes.git
cd runtimes/platforms/android

Set the following environment variables:

  • JAVA_HOME such that $JAVA_HOME/bin/java points to a JDK 17 Java executable
  • ANDROID_HOME pointing to where you have installed the Android SDK
  • ANDROID_NDK_HOME pointing to the version of the Android NDK needed for this version of NativeScript

Run the following commands from the repository root -- the Android build entry points live in scripts/ alongside the Apple ones:

git submodule update --init --force vendor/quickjs/source_ng
npm run setup-android

The first command fetches the vendored QuickJS-NG source at its pinned commit -- a deliberate manual step, so nothing moves you off the pin behind your back (see vendor/quickjs/patches/README.md). npm run setup-android then applies the NativeScript patches to it and installs the jsparser build-tool dependencies.

npm run build-android

The command will let you interactively choose the JS engine you want to build with, i.e. V8, QuickJS-NG, Hermes, JSC, PrimJS or Static Hermes. Pass the engine directly to skip the prompt:

npm run build-android -- --engine=QUICKJS_NG
  • The build process includes building of the runtime package (both optimized and with unstripped v8 symbol table), as well as all supplementary tools used for the android builds: metadata-generator, binding-generator, metadata-generator, static-binding-generator
  • Output goes to the repo-root dist/ and build/ folders, next to the Apple build output:
    • staged package: dist/android_<engine>_<binding>/ -- e.g. dist/android_quickjs_napi/, dist/android_v8_13_jsi/
    • tarball: build/npm-tarballs/nativescript-android_<engine>_<binding>-<version>.tgz
  • <binding> is the runtime tree the AAR was compiled from, napi (default) or jsi (--binding=jsi). They are two complete parallel runtimes, so each variant gets its own folder.

How to Run Tests

  • Go to subfolder test-app after you built the runtime.

  • Start an emulator or connect a device.

  • Run command

gradlew runtests

Working with the Runtime in Android Studio

  • Open the test-app folder in Android Studio. It represents a valid Android project and you are able to build and run a test application working with the Runtime from the source.

Note: You might need to run the Android Studio from the command line in order to preserve the environment variables. This is in case you get errors like "missing npm" if starting the studio the usual way.

You can change the JS engine used by the runtime by setting the jsEngine property in the build.gradle file in the root of the project. The possible values are QUICKJS, HERMES, JSC or V8.

Contribute

We love PRs! Check out the contributing guidelines. If you want to contribute, but you are not sure where to start, look for issues labeled help wanted.

Get Help

Please use GitHub issues for reporting bugs or requesting features. For general questions and support, check out Stack Overflow or ask the NativeScript community on Discord.

License

This project is licensed under the Apache License Version 2.0. See the LICENSE file for more info.