-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Android Native C++ Libraries Build #423
Comments
One thing of pain point I've found is relative paths used by native code. They expect to be in exact location while this is not always true. Especially if workspaces are used (which makes packages be symlinked). So in monorepo native package have different path then when installed as a dependency. It makes native development broken. See this example And experience Would be very cool to think about this use case (and make yarn v3 completely working). This would allow testing code under different RN versions easily. |
Hey all, Starting from React Native 0.70, we released the full CMake support for Android builds. This benefits apps on New Architecture. Specifically, it means that:
Moreover, we migrated the whole RN build to CMake. This comes with a couple of nice benefits:
EDIT: Typo |
awesome ^ think meant
|
Hey all,
In 0.71, the React Native Android artifacts will be prefab capable. This allowed us to:
In 0.71, the New Architecture setup for Android will be using prebuilts and not building React Native from source anymore. This allowed us to reduce the build time of Android Apps on New Architecture drastically (5x and more). We had reports of user waiting more than 1h to finish their build. In 0.71, building Android app in Old/New Arch will similar build times.
In 0.71, we're distributing a debug and a release prebuilt. This means that:
|
Introduction
The goal of this issue to bring awareness of what is the current status of the Android native builds, express some of the improvements we shipped, and collect feedback on some of the potential direction we might take.
With the new architecture, and specifically with the TurboModule capability (see #40) we expect more users having to deal with C++ code, therefore we believe it's important to bring clarity on this topic.
Details
Currently the
react-native
project needs to build both Java/Kotlin (app layer) code and C++ (native layer) in order to be shipped and be used by Android developers.Whenever we release a new version of
react-native
we:ndk-build
)..so
files) together with the .class files and so on inside an Android Archive (aka a.aar
file) and we place it inside theandroid/com/facebook/react/react-native/<version>
folder of our NPM module.This will make possible to build a React Native project with Gradle using the aforementioned folder as a Maven Repository (see here).
As long as you don't need to compile C++ code, you will be good with the current setup. If you need to build C++ code instead (say because you want to add a TurboModule or you're developing a react-native libraries that relies on native code), you need to setup a native build with the Android NDK.
Sadly, setting up an Android native build is not as easy as we would like it to be. Here are some of the friction points and potential improvements.
Discussion points
AGP Android NDK Apis
What: The
ReactAndroid
projectis(was) not using the AGP NDK ApisWho is affected: react-native contributors that are building the Android project.
Status: ✅ Shipped
Historically, the
react-native
project was using a custom Gradle task to invoke the Android NDK (see here) rather than AGP's APIexternalNativeBuild
.This had a variety of implications, namely:
doLast
and would have been re-executed at every builds).Some of the complexity here was that the
ReactAndroid
native build relies on several third party libraries that should be downloaded and prepared afterwards. We cleaned up theReactAndroid
build with the following PRs that are aimed at extracting those Gradle tasks to separate classes and fix the cache misses:The switch to the NDK Apis was done in this PR:
If you are a
react-native
contributor, please note the implications on the build time (and how to reduce it if its too long for you) in the commit message of #32443Cmake Support
What: The react-native Android ecosystem is relying on
ndk-build
.Who is affected: Any developer which is building a module with native code (e.g. a TurboModule or similar).
Status:
💬 Open for discussion✅ Shipped in RN 0.70Currently all the C++ code for Android is built using
ndk-build
. Google is anyway advertising to do not usendk-build
for new project but preferCmake
instead (see here).We haven't yet evaluated what would be the impact of migrating the builds to Cmake and we're happy to collect feedback from the community.
Prefab Support
What: The react-native .aar is not exposing headers/dynamic libraries and is forcing consumers to rebuild everything from source.
Who is affected: Any developer which is building a module with native code (e.g. a TurboModule or similar).
Status:
💬 Open for discussion✅ Shipped in RN 0.71AGP 4.1 added the support for publishing prefabs inside an .aar (see here). We're not using prefab in any from at this stage:
Consuming Prefabs
Some of the native libraries we depend on are offering prefab support, but we're not actively using it. An example is
fbjni
- see facebookincubator/fbjni#35. We're instead relying on a custom Gradle machinery to extract headers and.so
s from the fbjni tarball: facebook/react-native#32426Publishing Prefabs
Publishing prefabs inside the
react-native
.aar was blocked by theAGP Android NDK Apis
task.We should investigate what would be the impact of shipping all/some of the native dependencies that we're bundling inside the
react-native
.aar. Specifically users that are adding aTurboModule
will need to have the following .so files + related headers:libjsi
,libfbjni
,libglog
,libfolly_json
,libyoga
,libreact_nativemodule_core
,libturbomodulejsijni
,librrc_view
,libreact_render_core
,libreact_render_graphics
,libreact_codegen_rncore
.The impact of this in terms of bundle size and build time hasn't been assessed yet.
Specifically if you're a react-native library author you might benefit from depending on
react-native
as you won't need to fetch and build all the shared libraries (such as Glog, Folly and so on). This has however implications in terms of ABI stability that we haven't yet fully assessed.Build from source vs build from artifacts.
There has been some discussions and confusions in the community between prefabs and the build from source v. build from artifacts. We would like to clarify that we believe in:
ReactAndroid
builds to build from source, with all the dependencies code available locally. TheConsuming Prefabs
point presented earlier should be considered as an improvement to speedup built time and will anyway be an opt-in feature (i.e. you will still be able to build everything from source).react-native
is distributed as prebuilt (an.aar
) and, as we evolve our framework/architecture, we will try to reduce the build impact on consumers as much as possible while still allowing the flexibility to build from source to some extent (see this wiki page).Variant Aware Packaging
What: The react-native .aar needs to be manually patched to remove the unnecessary dynamic libaries.
Who is affected: All the react-native users to some extent.
Status:
💬 Open for discussion✅ Shipped in RN 0.71We currently rely on the
enableVmCleanup
flag (enabled by default) to run a cleanup of all the.so
files that we suppose they won't be needed in the final APK. For instance if you're building a Debug build using Hermes, you will not needlibhermes-executor-debug.so
, and so on.This has to be performed manually with a
FileTree.visit
during the build which is really error prone. Some alternatives to this approach could be:react-native
.aar so that they will be matched accordingly to the app build (e.g. areact-native-debug.aar
and so on).The impact of this hasn't been assessed yet.
The text was updated successfully, but these errors were encountered: