Can't create Android plugins based on compiling source from node_modules
Is your feature request related to a problem? Please describe.
I'm trying to re-implement React Native native modules as NativeScript plugins.
React Native native modules typically consist of some JS/TS source in a src folder and some native code, i.e. Obj-C/Swift code in an ios folder, and Java/Kotlin code in an android folder. expo-permissions is one example of a React Native native module that I'm trying to port.
The key difference from the NativeScript ecosystem is that React Native native modules very often don't rely on ready-packaged native code (e.g. .jar files) downloaded from a package repository - instead, a React Native native module (which, to be clear, is an npm package such as expo-permissions) may distribute the native source files (e.g. .java files) with the intention that the React Native app then compiles them itself. In other words, the .jar file (or iOS equivalent - clang module?) would be built locally.
This presents a few problems which I may not be able to express coherently in a single attempt... For iOS, at least, I have a fully working example (see my expo-permissions-nativescript-plugin project) - the build process was relatively pain-free. But for Android, I've spent weeks failing to get anything working.
Android is problematic principally for two reasons.
- some include statements need to be written into the
settings.gradlefile to proceed (yet there is no API to edit it - so far I've hacked it together using hooks); - at least with this particular Kotlin-based project, I've not had any luck compiling it. The best I managed was to produce a jar corresponding to the package, but when I extracted it, no symbols relating to the source code were found. I am not sure whether this is a problem of Kotlin, or perhaps the need for a
gradledirectory to be provided before anything will start to work (as @triniwiz mentioned - he would have more knowledge on this).
Describe the solution you'd like
Whether the project is made from .java or .kotlin files, NativeScript should be able to build plugins from source found in `node_modules.
When those projects bear a build.gradle file (such as this one in expo-permissions) that refers to any projects such as :unimodules-core, NativeScript must provide some solution for populating the app's settings.gradle file with the corresponding project alias:
include :unimodules-permissions-interface;
project(':unimodules-permissions-interface').projectDir = new File(rootProject.projectDir, '../../node_modules/unimodules-permissions-interface/android')
include :expo-permissions;
project(':expo-permissions').projectDir = new File(rootProject.projectDir, '../../node_modules/@nativescript-community/expo-permissions/platforms/android')
To be clear, settings.gradle is the only place where these include statements can go - I have tried placing them in build.gradle and include.gradle and whatever else, but those other options don't work.
This would probably best be done by the plugin defining, somewhere in its package.json, a field for any includedProjects, implicitly relative to the location of rootProject.projectDir, e.g.:
"includedProjects": {
":unimodules-permissions-interface": "../../node_modules/unimodules-permissions-interface/android",
":expo-permissions": "../../node_modules/@nativescript-community/expo-permissions/platforms/android"
},
Additional context
The difficulties should become much clearer if you have a go at trying to install the npm module expo-permissions and try yourself to make an Android plugin from it (as far as I can tell, it's impossible at the moment, even with hacks). You could use my expo-nativescript monorepo to experiment.
The end goal - being able to seamlessly consume React Native (and Flutter, I guess) native modules in NativeScript projects :)
FYI @rigor789 and @triniwiz, as we were discussing this on Slack yesterday.