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 摘要。