Skip to content
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

Use node package dependency to manage JSC version #24276

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions RNTester/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ def enableSeparateBuildPerCPUArchitecture = true
*/
def enableProguardInReleaseBuilds = true

/**
* Use the international variant of JavaScriptCore
* This variant includes the ICU i18n library to make APIs like `Date.toLocaleString`
* and `String.localeCompare` work when using with locales other than en-US.
* Note that this variant is about 6MiB larger per architecture than the default.
*/
def useIntlJsc = false

android {
compileSdkVersion 28

Expand Down Expand Up @@ -132,11 +140,23 @@ android {
signingConfig signingConfigs.release
}
}
packagingOptions {
pickFirst '**/armeabi-v7a/libc++_shared.so'
pickFirst '**/x86/libc++_shared.so'
pickFirst '**/x86_64/libc++_shared.so'
pickFirst '**/arm64-v8a/libc++_shared.so'
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

// Build React Native from source
implementation project(':ReactAndroid')

if (useIntlJsc) {
implementation 'org.webkit:android-jsc-intl:+'
} else {
implementation 'org.webkit:android-jsc:+'
}
}
18 changes: 5 additions & 13 deletions ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,14 @@ task prepareGlog(dependsOn: dependenciesPath ? [] : [downloadGlog], type: Copy)
}
}

task downloadJSC(dependsOn: createNativeDepsDirectories, type: Download) {
src("https://registry.npmjs.org/jsc-android/-/jsc-android-${JSC_VERSION}.tgz")
onlyIfNewer(true)
overwrite(false)
dest(new File(downloadsDir, "jsc-${JSC_VERSION}.tar.gz"))
}

// Create Android.mk library module based on jsc from npm
task prepareJSC(dependsOn: downloadJSC) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit confused about removing downloadJSC dependency.

What makes it appear jsc-android at "$projectDir/../node_modules/jsc-android/dist" location by the time this script runs?

FYI I don't see build.gradle invoking npm at any point, so unless I'm missing it somewhere this may make builds without invoking npm install first always fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inside of node_modules/react-native when using React Native, so JSC should definitely be there.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant when building RN from source, not when consuming it. While consuming none of ReactAndroid/build.gradle matters anyways.

The current instructions to build from source is simple download RN and run ./gradlew :ReactAndroid:installArchives I am asking will this work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Is there a way we can invoke "yarn" from gradle just to make sure node modules are in sync?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment from when new JSC was checked in #22263 (comment)

Using npm is just not the right way to manage jsc as it's a native dependency. After fighting with it for a while I abandoned this approach. The correct approach (which will require some work in jsc-android-buildscripts) is to manage publications of jsc in maven. This way the user will be able to override / use other versions of jsc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my point of view, RN itself is tightly coupled with node_modules, i.e. you cannot simply get a AAR or APK to run without metro or node_modules stuffs.

I also prefer to maintain even native dependencies in single source of truth, the pakcage.json.
Otherwise, we currently have multiple place to declare dependency version.
E.g. folly version 2018.10.22.00 defines in multiple places like gradle, CocoaPods, and Xcodeproj.

Invoking yarn by gradle is good to me.
Or at least we could check if node_modules is not there and prompt user to run yarn manually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this also broke the docker build yarn test-android-unit. I tried changing the Dockerfile.android to RUN yarn before ./gradlew :ReactAndroid:packageReactNdkLibsForBuck (and removed the dead reference to downloadJSC) but that didn't work, it still failed with

> Expected directory '/app/ReactAndroid/../node_modules/jsc-android/dist' to contain exactly one file, however, it contains no files.

task prepareJSC {
doLast {
def jscTar = tarTree(downloadJSC.dest)
def jscAAR = jscTar.matching({ it.include "**/android-jsc/**/*.aar" }).singleFile
def jscPackageRoot = fileTree("$projectDir/../node_modules/jsc-android/dist")
def jscAAR = jscPackageRoot.matching({ it.include "**/android-jsc/**/*.aar" }).singleFile
def soFiles = zipTree(jscAAR).matching({ it.include "**/*.so" })

def headerFiles = jscTar.matching({ it.include "**/include/*.h" })
def headerFiles = jscPackageRoot.matching({ it.include "**/include/*.h" })

copy {
from(soFiles)
Expand All @@ -168,7 +161,6 @@ task downloadNdkBuildDependencies {
dependsOn(downloadDoubleConversion)
dependsOn(downloadFolly)
dependsOn(downloadGlog)
dependsOn(downloadJSC)
}

def getNdkBuildName() {
Expand Down Expand Up @@ -255,8 +247,8 @@ task cleanReactNdkLib(type: Exec) {

task packageReactNdkLibs(dependsOn: buildReactNdkLib, type: Copy) {
from("$buildDir/react-ndk/all")
from("$thirdPartyNdkDir/jsc/jni")
into("$buildDir/react-ndk/exported")
exclude("**/libjsc.so")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this redundant ? By default gradle deps won't bundled in the aar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libjsc.so will be copied into $buildDir/react-ndk/all during ndkbuild.
If we don't exclude libjsc.so here, they will be copied to exported folder and packed in AAR.
This exclusion is pretty much same as what we did in old JSC, e.g. 0.57

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also exclude libc++_shared.so too so that app packages don't have to do that pickFirst thing?

It's a dangerous thing in case there are other dependencies than RN that bring in libc++_shared.so as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good point and I don't have a good solution right now.
The problem is that jsc-android-buildscripts packed AAR with a C++ runtime (libc++_shared.so).
Old JSC excludes C++ runtime to AAR.
That's the difference why old JSC does not have to use pickFirst.

There is also an assumption that NDK maintains ABI compatibilities because RN and JSC may build from different NDK version, e.g. old JSC is built by NDK r10c.
Fortunately, we don't have problems from ABI incompatibilities during these years.

Possible solutions like:

  1. Built JSC as static library. This will increase libjsc.so binary size and impact to application APK size.
  2. Move jsc-android-buildscripts to RN core and build together. This is increase build time incredibly. (e.g. It takes about 2 hours to build a jsc-android-buildscripts job on CicleCI.)

IMO, excluding libc++_shared.so from jsc-android-buildscripts is better than pickFirst as pickFirst may impact to application level.
But this needs changes from jsc-android-buildscripts side.

}

task packageReactNdkLibsForBuck(dependsOn: packageReactNdkLibs, type: Copy) {
Expand Down
1 change: 0 additions & 1 deletion ReactAndroid/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ BOOST_VERSION=1_63_0
DOUBLE_CONVERSION_VERSION=1.1.6
FOLLY_VERSION=2018.10.22.00
GLOG_VERSION=0.3.5
JSC_VERSION=236355.1.1

android.useAndroidX=true
android.enableJetifier=true
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ buildscript {
allprojects {
repositories {
mavenLocal()
maven {
url("$rootDir/node_modules/jsc-android/dist")
}

google()
jcenter()

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"fbjs": "^1.0.0",
"fbjs-scripts": "^1.1.0",
"invariant": "^2.2.4",
"jsc-android": "^236355.1.1",
"metro-babel-register": "0.52.0",
"metro-react-native-babel-transformer": "0.52.0",
"nullthrows": "^1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion scripts/circleci/gradle_download_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

set -e

./gradlew :ReactAndroid:downloadBoost :ReactAndroid:downloadDoubleConversion :ReactAndroid:downloadFolly :ReactAndroid:downloadGlog :ReactAndroid:downloadJSC
./gradlew :ReactAndroid:downloadBoost :ReactAndroid:downloadDoubleConversion :ReactAndroid:downloadFolly :ReactAndroid:downloadGlog
23 changes: 23 additions & 0 deletions template/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,15 @@ def enableSeparateBuildPerCPUArchitecture = false
*/
def enableProguardInReleaseBuilds = false

/**
* Use international variant JavaScriptCore
* International variant includes ICU i18n library and necessary data allowing to use
* e.g. Date.toLocaleString and String.localeCompare that give correct results
* when using with locales other than en-US.
* Note that this variant is about 6MiB larger per architecture than default.
*/
def useIntlJsc = false

android {
compileSdkVersion rootProject.ext.compileSdkVersion

Expand Down Expand Up @@ -149,11 +158,25 @@ android {
}
}
}

packagingOptions {
pickFirst '**/armeabi-v7a/libc++_shared.so'
pickFirst '**/x86/libc++_shared.so'
pickFirst '**/arm64-v8a/libc++_shared.so'
pickFirst '**/x86_64/libc++_shared.so'
}
}

dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.facebook.react:react-native:+" // From node_modules

// JSC from node_modules
if (useIntlJsc) {
implementation 'org.webkit:android-jsc-intl:+'
} else {
implementation 'org.webkit:android-jsc:+'
}
}

// Run this once to be able to run the application with BUCK
Expand Down
11 changes: 8 additions & 3 deletions template/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ buildscript {
allprojects {
repositories {
mavenLocal()
google()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
url("$rootDir/../node_modules/react-native/android")
}
maven {
// Android JSC is installed from npm
url("$rootDir/../node_modules/jsc-android/dist")
}

google()
jcenter()
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4192,6 +4192,11 @@ jsbn@~0.1.0:
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-0.1.1.tgz#a5e654c2e5a2deb5f201d96cefbca80c0ef2f513"
integrity sha1-peZUwuWi3rXyAdls77yoDA7y9RM=

jsc-android@^236355.1.1:
version "236355.1.1"
resolved "https://registry.yarnpkg.com/jsc-android/-/jsc-android-236355.1.1.tgz#43e153b722e3c60dd0595be4e7430baf65e67c9c"
integrity sha512-2py4f0McZIl/oH6AzPj1Ebutc58fyeLvwq6gyVYp1RsWr4qeLNHAPfW3kmfeVMz44oUBJMQ0lECZg9n4KBhHbQ==

jscodeshift@^0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/jscodeshift/-/jscodeshift-0.6.2.tgz#bb648e6bce717a597d165781158b0d73b7fa99c3"
Expand Down