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

fix: build issue with RN 0.71 #1

Merged
merged 3 commits into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 13 additions & 50 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
project(ReactNativeClusterer)
cmake_minimum_required(VERSION 3.9.0)

set (CMAKE_VERBOSE_MAKEFILE ON)
Expand All @@ -8,68 +9,30 @@ set (BUILD_DIR ./build)
file(GLOB RN_CLUSTERER_CPP "../cpp/*.cpp")
file(GLOB RN_CLUSTERER_HPP "../cpp/*.hpp")

file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")

# Add headers
include_directories(
${PACKAGE_NAME}
"${NODE_MODULES_DIR}/react-native/React"
"${NODE_MODULES_DIR}/react-native/React/Base"
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
"."

# rnclusterer headers
"../cpp"
)

# Find JSI on React Native 0.66.x and above
# else manually add JSI on React Native 0.65.x and below
# and add all project source files
if (EXISTS "${LIBRN_DIR}/libjsi.so")
find_library(
JSI_LIB
jsi
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)
add_library(
${PACKAGE_NAME}
SHARED
${RN_CLUSTERER_CPP}
${RN_CLUSTERER_HPP}
./cpp-adapter.cpp
)
else()
set (JSI_LIB "")
add_library(
${PACKAGE_NAME}
SHARED
${RN_CLUSTERER_CPP}
${RN_CLUSTERER_HPP}
${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp
./cpp-adapter.cpp
)
endif()

# Find JNI
find_library(
REACT_NATIVE_JNI_LIB
reactnativejni
PATHS ${LIBRN_DIR}
NO_CMAKE_FIND_ROOT_PATH
)

# Find LOG_LIB
find_library(
LOG_LIB
log
add_library(
${PACKAGE_NAME}
SHARED
${RN_CLUSTERER_CPP}
${RN_CLUSTERER_HPP}
./cpp-adapter.cpp
)

find_package(ReactAndroid REQUIRED CONFIG)
find_library(log-lib log)

# Link JNI, JSI, LOG_LIB
target_link_libraries(
${PACKAGE_NAME}
${REACT_NATIVE_JNI_LIB}
${JSI_LIB}
${LOG_LIB}
android
${log-lib} # <-- Logcat logger
ReactAndroid::jsi # <-- JSI
android # <-- Android JNI core
)
176 changes: 68 additions & 108 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,83 +1,77 @@
import java.nio.file.Paths

buildscript {

repositories {
google()
jcenter()
mavenCentral()

maven {
url "https://plugins.gradle.org/m2/"
}
mavenCentral()
google()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.2.2'
classpath("com.android.tools.build:gradle:7.2.2")
}
}

apply plugin: 'com.android.library'
def resolveBuildType() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString()

def getExtOrDefault(name, defaultValue) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : defaultValue
return tskReqStr.contains('Release') ? 'release' : 'debug'
}

def found = false
def basePath = projectDir.toPath().normalize()
def isNewArchitectureEnabled() {
// To opt-in for the New Architecture, you can either:
// - Set `newArchEnabled` to true inside the `gradle.properties` file
// - Invoke gradle with `-newArchEnabled=true`
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

// Find node_modules inside the example project
def nodeModulesDir = Paths.get(basePath.getParent().toString(), "example/node_modules")
def reactNativeDir = Paths.get(nodeModulesDir.toString(), "react-native/android")
if (nodeModulesDir.toFile().exists() && reactNativeDir.toFile().exists()) {
found = true
if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}
apply plugin: 'com.android.library'

if(!found){
// Node's module resolution algorithm searches up to the root directory,
// after which the base path will be null
while (basePath) {
nodeModulesDir = Paths.get(basePath.toString(), "node_modules")
reactNativeDir = Paths.get(nodeModulesDir.toString(), "react-native/android")
if (nodeModulesDir.toFile().exists() && reactNativeDir.toFile().exists()) {
found = true
break;
}
basePath = basePath.getParent()
}
def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

if(!found) {
throw new GradleException(
"${project.name}: unable to locate React Native android sources. " +
"Ensure you have you installed React Native as a dependency in your project and try again.")
def reactNativeArchitectures() {
def value = project.getProperties().get("reactNativeArchitectures")
return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

def nodeModulesPath = nodeModulesDir.toString().replace("\\", "/")
def reactNativePath = reactNativeDir.toString().replace("\\", "/")
def buildType() {
def buildType = "debug"

tasks.all({ task ->
if (task.name == "buildCMakeRelease") {
buildType = "release"
}
})

return buildType
repositories {
mavenCentral()
}

android {
compileSdkVersion getExtOrDefault('compileSdkVersion', 28)
compileSdkVersion safeExtGet("compileSdkVersion", 28)

defaultConfig {
minSdkVersion getExtOrDefault('minSdkVersion', 16)
targetSdkVersion getExtOrDefault('targetSdkVersion', 28)
// Used to override the NDK path/version on internal CI or by allowing
// users to customize the NDK path/version from their root project (e.g. for M1 support)
if (rootProject.hasProperty("ndkPath")) {
ndkPath rootProject.ext.ndkPath
}
if (rootProject.hasProperty("ndkVersion")) {
ndkVersion rootProject.ext.ndkVersion
}

buildFeatures {
prefab true
}

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 16)
targetSdkVersion safeExtGet('targetSdkVersion', 28)
versionCode 1
versionName "1.0"
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
externalNativeBuild {
cmake {
cppFlags "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID"
abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
arguments '-DANDROID_STL=c++_shared', "-DNODE_MODULES_DIR=${nodeModulesPath}"
cppFlags "-O2 -frtti -fexceptions -Wall -Wno-unused-variable -fstack-protector-all"
arguments "-DANDROID_STL=c++_shared"
abiFilters (*reactNativeArchitectures())
}
}
}
Expand All @@ -87,71 +81,37 @@ android {
targetCompatibility JavaVersion.VERSION_1_8
}

lintOptions{
abortOnError false
disable 'GradleCompatible'
}

externalNativeBuild {
cmake {
path "./CMakeLists.txt"
}
cmake {
path "CMakeLists.txt"
}
}

packagingOptions {
excludes = ["**/libc++_shared.so","**/libjsi.so","**/libreactnativejni.so","META-INF/MANIFEST.MF"]
}

configurations {
extractJNI
}
}

repositories {
mavenCentral()
mavenLocal()
google()

maven {
url reactNativePath
name 'React Native sources'
doNotStrip resolveBuildType() == 'debug' ? "**/**/*.so" : ''
excludes = [
"META-INF",
"META-INF/**",
"**/libjsi.so",
]
}
}

def reactProperties = new Properties()
file("$nodeModulesPath/react-native/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }

def reactNativeVersion = reactProperties.getProperty("VERSION_NAME").split("\\.")[1].toInteger()

dependencies {
//noinspection GradleDynamicVersion
implementation 'com.facebook.react:react-native:+'
def rnMatcher = reactNativeVersion < 69
? "**/**/*.aar"
: "**/react-native/**/*${buildType()}.aar"

def rnAAR = fileTree(reactNativePath).matching({ it.include rnMatcher }).singleFile
extractJNI(files(rnAAR))
implementation 'com.facebook.react:react-android:+'
}

def extracted = false;
task extractJNIFiles {
if (extracted) return
doLast {
configurations.extractJNI.files.each {
def file = it.absoluteFile

copy {
from zipTree(file)
into "$buildDir/$file.name"
include "jni/**/*"
}
extracted = true;
// Resolves "LOCAL_SRC_FILES points to a missing file, Check that libfb.so exists or that its path is correct".
tasks.whenTaskAdded { task ->
if (task.name.contains("configureCMakeDebug")) {
rootProject.getTasksByName("packageReactNdkDebugLibs", true).forEach {
task.dependsOn(it)
}
}
// We want to add a dependency for both configureCMakeRelease and configureCMakeRelWithDebInfo
if (task.name.contains("configureCMakeRel")) {
rootProject.getTasksByName("packageReactNdkReleaseLibs", true).forEach {
task.dependsOn(it)
}
}
}

// Extract JNI files as soon as first task is added
tasks.whenTaskAdded { task ->
task.dependsOn(extractJNIFiles);
}