Android Runtime for NativeScript
Contains the source code for the NativeScript's Android Runtime. NativeScript is a framework which enables developers to write truly native mobile applications for Android and iOS using JavaScript and CSS. Each mobile platform has its own ecosystem and offers completely different development tools and language(s) - Java for Android and Objective C (Swift) for iOS. In order to translate JavaScript code to the corresponding native APIs some kind of proxy mechanism is needed. This is exactly what the "Runtime" parts of NativeScript are responsible for. The Android Runtime may be thought of as "The Bridge" between the JavaScript and Android worlds. A NativeScript application for Android is a standard native package (apk) which besides the JavaScript files embed the runtime as well.
Additional docs
Several Wiki pages describe some internal topics about the runtime here
Main Projects
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.
Helper Projects
Architecture Diagram
The NativeScript Android Runtime architecture can be summarized in the following diagram.
For more details on how it works, read the documentation.
Build Prerequisites
Following are the minimal prerequisites to build the runtime package.
- Install the latest Android Studio.
- From the SDK Manager (Android Studio -> Tools -> Android -> SDK Manager) install the following components:
- Android API Level 23, 24, 25, 26, 27
- Android NDK
- Android Support Repository
- Download Build Tools
- CMake
- LLDB
- Clone this repository as well as the submodules:
git clone --recurse-submodules git@github.com:NativeScript/android-runtime.git
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.
How to Build
-
Install dependencies from NPM:
cd test-app/build-tools/jsparser
npm install
-
Set environment variables:
JAVA_HOME
such that $JAVA_HOME/bin/java
points to your Java executableANDROID_HOME
pointing to where you have installed the Android SDKANDROID_NDK_HOME
pointing to the version of the Android NDK needed for this version of NativeScript
-
Run command
Windows:
gradlew
Mac/Linux:
./gradlew
-
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
-
The result of the build will be in the dist folder.
Note:
To cut the build time in half and package only the optimized (stripped) version of the runtime package comment out 'tasks.generateRuntimeAar.execute()' in the build.gradle script.
How to Run Tests
-
Go to subfolder test-app after you built the runtime.
-
Start an emulator or connect a device.
Note: Keep in mind the device or emulator needs to have an sdcard mounted.
-
Run command
gradlew runtests
How to Connect the Inspector
- Install dependencies: run command
cd test-app
npm install
- Run the test app in Android Studio, or manually in the emulator.
- Run command
npx ns debug android --start
(If you instead have the nativescript
NPM module installed globally,
you can omit the npm install
and the npx
prefix.)
- This will print out a URL, such as
devtools://devtools/bundled/inspector.html?ws=localhost:40000
.
Open this URL in Chrome or Chromium.
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
.
Misc
Get Help
Please, use github issues strictly for reporting bugs or requesting features. For general questions and support, check out Stack Overflow or ask our experts in NativeScript community Slack channel.
8.5.1 (2023-07-24)
Bug Fixes
-
Avoid setting the same property on an ObjectTemplate twice (9e610c8)
-
Don't access super
property on implementation object (d8b8bc0)
-
Don't iterate properties in GetImplementedInterfaces (9dfae65)
-
intermediary fix for https://github.com/NativeScript/android/pull/1771 (32c7abb)
-
Leave context after Runtime::PrepareV8Runtime() (cd1d285)
-
memory leak on accessing static interface methods (88ce2d8)
-
memory leak on saving code cache (6d416a1)
-
Update common-runtime-tests-app (c8db3ca)
-
update legacy android version in package.json (#1744) (b4ad8e5)
-
Use Isolate::TryGetCurrent() (afe026a)
-
Remove weak callback from __postFrameCallback cache (#1755) (ff1b979), closes #1755
Features
Performance Improvements
- avoid unnecessary string copying when calling static properties of interfaces (8b53d02)
BREAKING CHANGES
- __startNDKProfiler() and __stopNDKProfiler() global
bindings no longer available.
The NDK profiler was not functional, since nothing in the build process
defined the NDK_PROFILER_ENABLED preprocessor symbol. The start and stop
functions were already no-ops.
- chore: Remove outdated comment
RunMicrotasks no longer exists, it's already been replaced in the code
with PerformMicrotaskCheckpoint.
- chore: Use unique_ptr for NewBackingStore in byte buffers
V8 doc: https://docs.google.com/document/d/1sTc_jRL87Fu175Holm5SV0kajkseGl2r8ifGY76G35k/edit
The V8 usage examples show unique_ptr here; it probably doesn't matter
much, but we don't need the backing store after creating the ArrayBuffer,
and I believe shared_ptr is slightly more overhead than unique_ptr.
For convenience, replace the manual empty deleter for direct buffers with
v8::BackingStore::EmptyDeleter.
- chore: Remove weak finalizer callback from __postFrameCallback()
Weak finalizer callbacks are going away in V8 11.x, so we have to remove
this one. Luckily, a finalizer callback is not necessary - it's never
needed to prevent the frame callback from being collected.
However, a weak callback is not necessary in the first place. We can just
clean up the cache entry after the callback is executed, since it is only
executed once.
Note that the call to erase() destructs the FrameCallbackCacheEntry
instance, making entry
a dangling pointer, so don't use it after the
erase(). There's probably a safer way to do this, although the way that
first occurred to me (pass the key to the callback instead of the entry,
and then use std::unordered_map::extract()) is not available on
robin_hood::unordered_map.
- fix: Make sure frame callbacks are not ignored
There was a bug where __postFrameCallback() would not always cause the
callback to be called. Without initializing removed
, sometimes it would
have a nonzero value, so the callback would be ignored.
- chore: Clear callback caches' persistent handles in destructor
Clearing the persistent handles in the destructor makes it a bit easier to
deal with the cache entry's lifetime: they are cleared whenever the entry
is removed from the cache.
We do this for both the main thread callback cache and the frame callback
cache.
Adding a destructor makes the cache entries non-movable. But the only
place where they were moved was when inserting them into the cache anyway.
We can use C++17's try_emplace() method to insert them without having to
move them.
- chore: Construct FrameCallbackCacheEntry with ID
This avoids the situation of forgetting to add an ID to the cache entry.
- chore: Improve usage of unordered_map APIs in CallbackHandlers
This fixes a few places where we can avoid double lookups:
-
In RunOnMainThreadFdCallback, we already have a valid iterator, so no
need to look up the same key again in RemoveKey (this is the only usage
of RemoveKey, so we can remove it.) (Additionally, we probably want to
remove the cache entry before throwing a NativeScript exception.)
-
In PostFrameCallback and RemoveFrameCallback, we should not do
contains() immediately followed by find().
We don't do anything with the return value from these callbacks, so it's
OK to ignore them and not convert them to a local handle.