Skip to content

Commit

Permalink
Setup an android library project inside ReactAndroid/hermes-engine
Browse files Browse the repository at this point in the history
Summary:
This commits sets up an Android library by applying the plugin
and configuring all the necessary flags.

Flags have been adapted from:
https://github.com/facebook/hermes/blob/main/android/hermes/build.gradle
Removing the unnecesary ones and adapting the build to conform
to the React Native build system.

Changelog:
[Internal] [Changed] - Setup an android library project inside `ReactAndroid/hermes-engine`

Reviewed By: hramos

Differential Revision: D34213534

fbshipit-source-id: c2e7b810bf4c4b1831a764a6f76cb73722da2125
  • Loading branch information
cortinico authored and facebook-github-bot committed Mar 4, 2022
1 parent d96cd6d commit a211089
Showing 1 changed file with 112 additions and 1 deletion.
113 changes: 112 additions & 1 deletion ReactAndroid/hermes-engine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.apache.tools.ant.taskdefs.condition.Os

plugins {
id("de.undercouch.download")
id("com.android.library")
}

def customDownloadDir = System.getenv("REACT_NATIVE_DOWNLOADS_DIR")
Expand All @@ -21,6 +22,9 @@ if (hermesVersionFile.exists()) {
}
def ndkBuildJobs = Runtime.runtime.availableProcessors().toString()

// We inject the JSI directory used inside the Hermes build with the -DJSI_DIR config.
def jsiDir = rootProject.file("ReactCommon/jsi")

task downloadHermes(type: Download) {
src("https://github.com/facebook/hermes/tarball/${hermesVersion}")
onlyIfNewer(true)
Expand Down Expand Up @@ -49,7 +53,13 @@ task unzipHermes(dependsOn: downloadHermes, type: Copy) {

task configureNinjaForHermes(dependsOn: unzipHermes, type: org.gradle.api.tasks.Exec) {
workingDir(hermesDir)
commandLine(windowsAwareCommandLine("python3", "utils/build/configure.py", "./ninja_build"))
commandLine(windowsAwareCommandLine(
"python3",
"utils/build/configure.py",
"--jsidir",
jsiDir.absolutePath,
"./ninja_build"
))
}

task buildNinjaForHermes(dependsOn: configureNinjaForHermes, type: org.gradle.api.tasks.Exec) {
Expand All @@ -65,3 +75,104 @@ static def windowsAwareCommandLine(String... commands) {
newCommands.addAll(commands)
return newCommands
}

def reactNativeArchitectures() {
def value = project.getProperties().get("reactNativeArchitectures")
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

android {
compileSdkVersion 31

// Used to override the NDK path & version on internal CI
if (System.getenv("ANDROID_NDK") != null && System.getenv("LOCAL_ANDROID_NDK_VERSION") != null) {
ndkPath System.getenv("ANDROID_NDK")
ndkVersion System.getenv("LOCAL_ANDROID_NDK_VERSION")
}

defaultConfig {
minSdkVersion 21

externalNativeBuild {
cmake {
arguments "-DHERMES_IS_ANDROID=True"
arguments "-DANDROID_STL=c++_shared"
arguments "-DANDROID_PIE=True"
arguments "-DIMPORT_HERMESC=${new File(hermesDir, "ninja_build/ImportHermesc.cmake").toString()}"
arguments "-DJSI_DIR=${jsiDir}"
arguments "-DHERMES_SLOW_DEBUG=False"
arguments "-DHERMES_BUILD_SHARED_JSI=True"
// We intentionally build Hermes with Intl support only. This is to simplify
// the build setup and to avoid overcomplicating the build-type matrix.
arguments "-DHERMES_ENABLE_INTL=True"
targets "libhermes"
}
}
ndk {
abiFilters (*reactNativeArchitectures())
}
}

externalNativeBuild {
cmake {
version "3.10.2"
path "$hermesDir/CMakeLists.txt"
}
}

buildTypes {
debug {
externalNativeBuild {
cmake {
arguments "-DHERMES_ENABLE_DEBUGGER=True"
// JS developers aren't VM developers.
// Therefore we're passing as build type Release, to provide a faster build.
// This has the (unlucky) side effect of letting AGP call the build
// tasks `configureCMakeRelease` while is actually building the debug flavor.
arguments "-DCMAKE_BUILD_TYPE=Release"
}
}
}
release {
externalNativeBuild {
cmake {
arguments "-DCMAKE_BUILD_TYPE=MinSizeRel"
}
}
}
}

sourceSets {
main {
manifest.srcFile "$hermesDir/android/hermes/src/main/AndroidManifest.xml"
java.srcDirs = [
"$hermesDir/lib/Platfrom/Intl/java"
]
}
}

buildFeatures {
prefab true
}

dependencies {
implementation("com.facebook.fbjni:fbjni:0.2.2")
implementation("com.facebook.soloader:soloader:0.10.3")
implementation("com.facebook.yoga:proguard-annotations:1.19.0")
implementation("androidx.annotation:annotation:1.3.0")
}

packagingOptions {
exclude "**/libc++_shared.so"
exclude "**/libjsi.so"
exclude "**/libfbjni.so"
}

afterEvaluate {
preBuild.dependsOn(buildNinjaForHermes)
// Needed as some of the native sources needs to be downloaded
// before configureCMakeRelease/configureCMakeMinSizeRel could be executed.
configureCMakeRelease.dependsOn(preBuild)
configureCMakeMinSizeRel.dependsOn(preBuild)
}
}

0 comments on commit a211089

Please sign in to comment.