Skip to content

Commit

Permalink
Merge pull request #391 from Shopify/fix/386-make-gradle-and-cmake-mo…
Browse files Browse the repository at this point in the history
…re-robust

Fix/386 make gradle and cmake more robust
  • Loading branch information
chrfalch authored Apr 18, 2022
2 parents bc34ab6 + 44fc9d2 commit 79725da
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 51 deletions.
46 changes: 30 additions & 16 deletions package/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ set (SKIA_SVG_LIB "svg")
set (SKIA_SKSHAPER_LIB "skshaper")

set(build_DIR ${CMAKE_SOURCE_DIR}/build)
file (GLOB LIBRN_DIR "${build_DIR}/react-native-0*/jni/${ANDROID_ABI}")
file(GLOB LIBRN_DIR "${PREBUILT_DIR}/${ANDROID_ABI}")
file(GLOB libfbjni_link_DIRS "${build_DIR}/fbjni*.aar/jni/${ANDROID_ABI}")
file(GLOB libfbjni_include_DIRS "${build_DIR}/fbjni-*-headers.jar/")

link_directories(../libs/android/${ANDROID_ABI}/)

if(${REACT_NATIVE_VERSION} LESS 66)
file(
TO_CMAKE_PATH
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp"
INCLUDE_JSI_CPP
)
endif()

add_library(
${PACKAGE_NAME}
SHARED
Expand All @@ -39,16 +47,10 @@ target_include_directories(
PRIVATE

# When installed in the development environment
"../node_modules/react-native/ReactCommon/callinvoker"
"../node_modules/react-native/ReactCommon/jsi"
"../node_modules/react-native/ReactCommon/react/nativemodule/core"
"../node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni"

# When installed as a package the paths are a bit different
"../../../react-native/ReactCommon/callinvoker"
"../../../react-native/ReactCommon/jsi"
"../../../react-native/ReactCommon/react/nativemodule/core"
"../../../react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni"
"${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"${NODE_MODULES_DIR}/react-native/ReactCommon/react/nativemodule/core"
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni"

cpp/skia/include/config/
cpp/skia/include/core/
Expand Down Expand Up @@ -87,11 +89,23 @@ find_library(
)

find_library(
JSI_LIB
jsi
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)
JSI_LIB
jsi
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)
if(${REACT_NATIVE_VERSION} LESS 66)
# JSI lib didn't exist on RN 0.65 and before. Simply omit it.
set (JSI_LIB "")
else()
# RN 0.66 distributes libjsi.so, can be used instead of compiling jsi.cpp manually.
find_library(
JSI_LIB
jsi
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)
endif()

find_library(
REACT_LIB
Expand Down
115 changes: 81 additions & 34 deletions package/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.nio.file.Paths

// android/build.gradle

// based on:
Expand Down Expand Up @@ -26,6 +28,60 @@ def safeExtGet(prop, fallback) {

apply plugin: 'com.android.library'

static def findNodeModules(baseDir) {
def basePath = baseDir.toPath().normalize()
// Node's module resolution algorithm searches up to the root directory,
// after which the base path will be null
while (basePath) {
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
return nodeModulesPath.toString()
}
basePath = basePath.getParent()
}
throw new GradleException("React-Native-Skia: Failed to find node_modules/ path!")
}

def nodeModules = findNodeModules(projectDir)
logger.warn("react-native-skia: node_modules/ found at: ${nodeModules}")

def sourceBuild = false
def defaultDir = null
def androidSourcesDir = null
def androidSourcesName = 'React Native sources'

if (rootProject.ext.has('reactNativeAndroidRoot')) {
defaultDir = rootProject.ext.get('reactNativeAndroidRoot')
androidSourcesDir = defaultDir.parentFile.toString()
} else if (findProject(':ReactAndroid') != null) {
sourceBuild = true
defaultDir = project(':ReactAndroid').projectDir
androidSourcesDir = defaultDir.parentFile.toString()
} else {
defaultDir = file("$nodeModules/react-native/android")
androidSourcesDir = defaultDir.parentFile.toString()
}

if (!defaultDir.exists()) {
throw new GradleException(
"${project.name}: React Native android directory (node_modules/react-native/android) does not exist! Resolved node_modules to: ${nodeModules}"
)
}

def prebuiltDir = sourceBuild
? "$nodeModules/react-native/ReactAndroid/src/main/jni/prebuilt/lib"
: "$buildDir/react-native-0*/jni"


def reactProperties = new Properties()
file("$nodeModules/react-native/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }
def REACT_NATIVE_VERSION = reactProperties.getProperty("VERSION_NAME").split("\\.")[1].toInteger()

logger.warn("react-native-skia: React Native version: ${REACT_NATIVE_VERSION}")
logger.warn("react-native-skia: Is Source build: ${sourceBuild}")
logger.warn("react-native-skia: Prebuilt dir: ${prebuiltDir}")

buildscript {
// The Android Gradle plugin is only required when opening the android folder stand-alone.
// This avoids unnecessary downloads and potential conflicts when the library is included as a
Expand Down Expand Up @@ -56,7 +112,10 @@ android {
cmake {
cppFlags "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
arguments '-DANDROID_STL=c++_shared'
arguments '-DANDROID_STL=c++_shared',
"-DREACT_NATIVE_VERSION=${REACT_NATIVE_VERSION}",
"-DNODE_MODULES_DIR=${nodeModules}",
"-DPREBUILT_DIR=${prebuiltDir}"
}
}
}
Expand All @@ -72,7 +131,13 @@ android {
}

packagingOptions {
excludes = ["**/libc++_shared.so"]
excludes = [
"**/libc++_shared.so",
"**/libfbjni.so",
"**/libjsi.so",
"**/libreact_nativemodule_core.so",
"**/libturbomodulejsijni.so"
]
}


Expand Down Expand Up @@ -114,33 +179,6 @@ dependencies {
extractJNI(files(rnAAR))
}

def configureReactNativePom(def pom) {
def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)

pom.project {
name packageJson.title
artifactId packageJson.name
version = packageJson.version
group = "com.shopify.reactnative.skia"
description packageJson.description
url packageJson.repository.baseUrl

licenses {
license {
name packageJson.license
url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
distribution 'repo'
}
}

developers {
developer {
name packageJson.author
}
}
}
}

afterEvaluate { project ->
task androidSourcesJar(type: Jar) {
classifier = 'sources'
Expand Down Expand Up @@ -188,9 +226,18 @@ task extractJNIFiles {
}
}

tasks.whenTaskAdded { task ->
if (task.name.contains('externalNativeBuild')) {
task.dependsOn(extractAARHeaders)
task.dependsOn(extractJNIFiles)
}
extractJNIFiles.mustRunAfter extractAARHeaders

def nativeBuildDependsOn(dependsOnTask, variant) {
def buildTasks = tasks.findAll({ task ->
!task.name.contains("Clean") && (task.name.contains("externalNative") || task.name.contains("CMake")) })
if (variant != null) {
buildTasks = buildTasks.findAll({ task -> task.name.contains(variant) })
}
buildTasks.forEach { task -> task.dependsOn(dependsOnTask) }
}

afterEvaluate {
nativeBuildDependsOn(extractAARHeaders, null)
nativeBuildDependsOn(extractJNIFiles, null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void doFrame(long frameTimeNanos) {
Choreographer.getInstance().postFrameCallback(frameCallback);
}

public void raise(String message) {
public void raise(final String message) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Expand Down

0 comments on commit 79725da

Please sign in to comment.