NativeScript / NativeScript/nativescript-cli

Can't create Android plugins based on compiling source from node_modules

未关闭
#5,488 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
1.1k
派生
204
平均合并
1 天 9 小时
30 天内合并 PR
8

描述

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.

  1. some include statements need to be written into the settings.gradle file to proceed (yet there is no API to edit it - so far I've hacked it together using hooks);
  2. 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 gradle directory 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 :)

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先复现链接的 expo-nativescript monorepo 中 expo-permissions Android 插件的构建,重点关注 android/build.gradle 和生成的 settings.gradle。检查 package.json 中提议的 includedProjects 字段,以及 settings.gradle 的组装方式。当 node_modules 中基于 Java 或 Kotlin 的 source plugin 能够连同其所需的项目别名成功构建时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
android, javascript, kotlin, react-native
领域
build-system, cli, mobile-dev
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
34/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。