Skip to content

Commit d98bfc9

Browse files
committed
fix: android build
1 parent f1a3990 commit d98bfc9

File tree

4 files changed

+154
-63
lines changed

4 files changed

+154
-63
lines changed

android/CMakeLists.txt

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
cmake_minimum_required(VERSION 3.4.1)
1+
project(ReactNativeXlog)
2+
cmake_minimum_required(VERSION 3.9.0)
23

3-
set (CMAKE_VERBOSE_MAKEFILE ON)
4-
set (CMAKE_CXX_STANDARD 11)
4+
set (PACKAGE_NAME "react-native-xlog")
55

6-
add_library(cpp
7-
SHARED
8-
../cpp/react-native-xlog-jsi.cpp
9-
cpp-adapter.cpp
10-
)
6+
#add_subdirectory(${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/first-party/fb) # /CMakeLists.txt
117

12-
file (GLOB XLOG_PATH "${BUILD_DIR}/unzipXlog/jni/${ANDROID_ABI}")
13-
find_library(XLOG_LIB marsxlog PATHS ${XLOG_PATH} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
14-
15-
file (GLOB LIBRN_DIR "${BUILD_DIR}/unzipReactNative/jni/${ANDROID_ABI}")
8+
find_package(ReactAndroid REQUIRED CONFIG)
9+
get_target_property(JSI_LIB ReactAndroid::jsi IMPORTED_LOCATION)
10+
get_filename_component(JSI_DIR ${JSI_LIB} DIRECTORY)
11+
file (GLOB LIBRN_DIR "${JSI_DIR}/../../../../../jni/${ANDROID_ABI}")
1612
find_library(
1713
FB_LIB
1814
fb
1915
PATHS ${LIBRN_DIR}
2016
NO_CMAKE_FIND_ROOT_PATH
2117
)
22-
find_library(JSI jsi PATHS ${LIBRN_DIR} NO_CMAKE_FIND_ROOT_PATH)
18+
19+
add_library(reactnativexlog
20+
SHARED
21+
../cpp/react-native-xlog-jsi.cpp
22+
cpp-adapter.cpp
23+
)
24+
25+
file (GLOB XLOG_PATH "${BUILD_DIR}/unzipXlog/jni/${ANDROID_ABI}")
26+
find_library(XLOG_LIB marsxlog PATHS ${XLOG_PATH} NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
2327

2428
# Specifies a path to native header files.
2529
include_directories(
26-
../cpp
30+
../cpp
2731
"../ios/mars.framework/Headers"
2832
"${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/first-party/fb/include/"
2933
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi"
3034
)
3135

32-
target_link_libraries(cpp ${XLOG_LIB} ${FB_LIB} ${JSI})
36+
target_link_libraries(reactnativexlog ${XLOG_LIB} ${FB_LIB} ReactAndroid::jsi)
3337

android/build.gradle

Lines changed: 133 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ buildscript {
1010
}
1111

1212
dependencies {
13-
classpath 'com.android.tools.build:gradle:3.5.3'
13+
classpath "com.android.tools.build:gradle:7.3.1"
1414
}
1515
}
1616
}
@@ -22,30 +22,100 @@ def safeExtGet(prop, fallback) {
2222
}
2323
println("buildDir:" + rootProject.buildDir)
2424
static def findNodeModules(baseDir) {
25-
def basePath = baseDir.toPath().normalize()
26-
// Node's module resolution algorithm searches up to the root directory,
27-
// after which the base path will be null
28-
while (basePath) {
29-
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
30-
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
31-
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
32-
return nodeModulesPath.toString()
33-
}
34-
basePath = basePath.getParent()
35-
}
36-
throw new GradleException("Failed to find node_modules/ path!")
25+
def basePath = baseDir.toPath().normalize()
26+
// Node's module resolution algorithm searches up to the root directory,
27+
// after which the base path will be null
28+
while (basePath) {
29+
def nodeModulesPath = Paths.get(basePath.toString(), "node_modules")
30+
def reactNativePath = Paths.get(nodeModulesPath.toString(), "react-native")
31+
if (nodeModulesPath.toFile().exists() && reactNativePath.toFile().exists()) {
32+
return nodeModulesPath.toString()
33+
}
34+
basePath = basePath.getParent()
35+
}
36+
throw new GradleException("Failed to find node_modules/ path!")
3737
}
3838
def nodeModules = findNodeModules(projectDir)
39-
def prebuiltDir = "$buildDir/react-native-0*/jni"
40-
def reactProperties = new Properties()
41-
file("$nodeModules/react-native/ReactAndroid/gradle.properties").withInputStream { reactProperties.load(it) }
42-
def REACT_NATIVE_VERSION_NAME = reactProperties.getProperty("VERSION_NAME")
39+
40+
static def findNodeModulePath(baseDir, packageName) {
41+
def basePath = baseDir.toPath().normalize()
42+
// Node's module resolution algorithm searches up to the root directory,
43+
// after which the base path will be null
44+
while (basePath) {
45+
def candidatePath = Paths.get(basePath.toString(), "node_modules", packageName)
46+
if (candidatePath.toFile().exists()) {
47+
return candidatePath.toString()
48+
}
49+
basePath = basePath.getParent()
50+
}
51+
return null
52+
}
53+
54+
def safeAppExtGet(prop, fallback) {
55+
def appProject = rootProject.allprojects.find { it.plugins.hasPlugin('com.android.application') }
56+
appProject?.ext?.has(prop) ? appProject.ext.get(prop) : fallback
57+
}
58+
59+
def resolveBuildType() {
60+
Gradle gradle = getGradle()
61+
String tskReqStr = gradle.getStartParameter().getTaskRequests()['args'].toString()
62+
return tskReqStr.contains('Release') ? 'release' : 'debug'
63+
}
64+
65+
def isNewArchitectureEnabled() {
66+
// To opt-in for the New Architecture, you can either:
67+
// - Set `newArchEnabled` to true inside the `gradle.properties` file
68+
// - Invoke gradle with `-newArchEnabled=true`
69+
// - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
70+
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
71+
}
72+
73+
def resolveReactNativeDirectory() {
74+
def reactNativeLocation = safeAppExtGet("REACT_NATIVE_NODE_MODULES_DIR", null)
75+
if (reactNativeLocation != null) {
76+
return file(reactNativeLocation)
77+
}
78+
79+
// monorepo workaround
80+
// react-native can be hoisted or in project's own node_modules
81+
def reactNativeFromProjectNodeModules = file("${rootProject.projectDir}/../node_modules/react-native")
82+
if (reactNativeFromProjectNodeModules.exists()) {
83+
return reactNativeFromProjectNodeModules
84+
}
85+
86+
def reactNativeFromNodeModulesWithReanimated = file("${projectDir}/../../react-native")
87+
if (reactNativeFromNodeModulesWithReanimated.exists()) {
88+
return reactNativeFromNodeModulesWithReanimated
89+
}
90+
91+
throw new GradleException(
92+
"[Reanimated] Unable to resolve react-native location in node_modules. You should project extension property (in `app/build.gradle`) `REACT_NATIVE_NODE_MODULES_DIR` with path to react-native."
93+
)
94+
}
95+
96+
def getReanimatedMajorVersion() {
97+
def (major, minor, patch) = getReanimatedVersion().tokenize('.')
98+
return major.toInteger()
99+
}
100+
101+
def toPlatformFileString(String path) {
102+
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
103+
path = path.replace(File.separatorChar, '/' as char)
104+
}
105+
return path
106+
}
107+
108+
def reactNativeRootDir = resolveReactNativeDirectory()
43109

44110
android {
45-
compileSdkVersion safeExtGet('XlogJsi_compileSdkVersion', 29)
111+
ndkVersion rootProject.ext.ndkVersion
112+
compileSdkVersion safeExtGet('compileSdkVersion', 34)
113+
buildFeatures {
114+
prefab true
115+
}
46116
defaultConfig {
47-
minSdkVersion safeExtGet('XlogJsi_minSdkVersion', 16)
48-
targetSdkVersion safeExtGet('XlogJsi_targetSdkVersion', 29)
117+
minSdkVersion safeExtGet('minSdkVersion', 16)
118+
targetSdkVersion safeExtGet('targetSdkVersion', 34)
49119
versionCode 1
50120
versionName "1.0"
51121

@@ -54,8 +124,8 @@ android {
54124
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
55125
abiFilters 'armeabi-v7a', 'arm64-v8a' // xlog does not support x86 and x86_64
56126
arguments '-DANDROID_STL=c++_shared',
57-
"-DNODE_MODULES_DIR=${nodeModules}",
58-
"-DBUILD_DIR=${buildDir}"
127+
"-DNODE_MODULES_DIR=${nodeModules}",
128+
"-DBUILD_DIR=${buildDir}"
59129
}
60130
}
61131

@@ -86,42 +156,60 @@ android {
86156
}
87157

88158
repositories {
159+
mavenCentral()
89160
mavenLocal()
90161
maven {
91162
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
92-
url("$rootDir/../node_modules/react-native/android")
163+
url "$reactNativeRootDir/android"
164+
}
165+
maven {
166+
// Android JSC is installed from npm
167+
url "$reactNativeRootDir/../jsc-android/dist"
93168
}
94169
google()
95-
mavenCentral()
96170
}
97171

98172
configurations {
99-
cmake_depends
173+
cmake_depends
100174
}
101175

102176
dependencies {
103-
//noinspection GradleDynamicVersion
104-
implementation "com.facebook.react:react-native:+" // From node_modules
105-
api "com.tencent.mars:mars-xlog:1.2.6"
106-
cmake_depends "com.tencent.mars:mars-xlog:1.2.6"
177+
implementation "com.facebook.react:react-android" // version substituted by RNGP
178+
implementation "com.facebook.react:hermes-android" // version substituted by RNGP
179+
api "com.tencent.mars:mars-xlog:1.2.6"
180+
cmake_depends "com.tencent.mars:mars-xlog:1.2.6"
181+
}
182+
183+
tasks.whenTaskAdded { task ->
184+
if (task.name.contains("configureCMakeDebug")) {
185+
rootProject.getTasksByName("packageReactNdkDebugLibs", true).forEach {
186+
task.dependsOn(it)
187+
}
188+
}
189+
// We want to add a dependency for both configureCMakeRelease and configureCMakeRelWithDebInfo
190+
if (task.name.contains("configureCMakeRel")) {
191+
rootProject.getTasksByName("packageReactNdkReleaseLibs", true).forEach {
192+
task.dependsOn(it)
193+
}
194+
}
107195
}
108196

109197
// 作用是build的时候把 mars-xlog 包里面的 so文件拷贝到 src/main/jniLibs 文件夹
110-
task resolveDependencies {
111-
project.configurations.each { configuration ->
112-
if ("cmake_depends".equalsIgnoreCase(configuration.name)) {
113-
def lib = configuration.resolve()[0]
114-
copy {
115-
from zipTree(lib)
116-
into "${project.buildDir}/unzipXlog/"
117-
include "jni/**/*.so"
118-
}
119-
copy {
120-
from zipTree("${nodeModules}/react-native/android/com/facebook/react/react-native/${REACT_NATIVE_VERSION_NAME}/react-native-${REACT_NATIVE_VERSION_NAME}-release.aar")
121-
into "${project.buildDir}/unzipReactNative/"
122-
include "jni/**/*.so"
123-
}
124-
}
125-
}
198+
tasks.register('resolveDependencies') {
199+
project.configurations.each { configuration ->
200+
if ("cmake_depends".equalsIgnoreCase(configuration.name)) {
201+
def lib = configuration.resolve()[0]
202+
copy {
203+
from zipTree(lib)
204+
into "${project.buildDir}/unzipXlog/"
205+
include "jni/**/*.so"
206+
}
207+
// copy {
208+
// from zipTree("${nodeModules}/react-native/android/com/facebook/react/react-native/${REACT_NATIVE_VERSION_NAME}/react-native-${REACT_NATIVE_VERSION_NAME}-release.aar")
209+
// into "${project.buildDir}/unzipReactNative/"
210+
// include "jni/**/*.so"
211+
// }
212+
}
213+
}
126214
}
127215
build.dependsOn resolveDependencies

android/src/main/java/com/reactnativexlogjsi/XlogJsiModule.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ public String getName() {
4747
try {
4848
System.loadLibrary("c++_shared");
4949
System.loadLibrary("marsxlog");
50-
// Used to load the 'native-lib' library on application startup.
51-
System.loadLibrary("cpp");
50+
System.loadLibrary("reactnativexlog");
5251
} catch (Exception ignored) {
5352
}
5453
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-xlog-jsi",
3-
"version": "0.3.3",
3+
"version": "0.4.0",
44
"description": "It allows you to easily use https://github.com/Tencent/mars#mars-xlog inside your React Native applications.",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

0 commit comments

Comments
 (0)