[TOC]

概述

编译环境

官方的编译环境要求如下,值得注意的是这里的NDK版本最好是r10e,亲测过其他两个版本的NDK并没有成功,因此这里最好是下载r10e版本的NDK。

将Gradle指向你的安卓SDK: 设置$ANDROID_SDK和$ANDORID_NDK为对应的目录,或者按照以下内容在react-native根目录下创建local.properties文件(注意:windows下需要使用反双斜杠)。

1
2
sdk.dir=/Users/frewen/Library/Android/sdk
ndk.dir=/Users/frewen/Library/Android/android-ndk-r19c

获取源码

按照官方给的下面命令获取源码,我试了运行失败。

1
npm install --save github:facebook/react-native#master

其实当我们 react-native init [ProjectName] 的时候,工程node-modules/react-native/ReactAndroid的目录就包含了源码,因此我们可以直接让我们的应用工程引用这一个源码工程,因此这里我们直接运行一下命令初始化一个react native工程testapp。

1
react-native init testapp

添加gradle依赖

(1) 在生成的React Native工程中,将android/build.gradle文件中添加gradle-download-task依赖。

1
2
3
4
5
6
7
8
9
dependencies {
// gradle可以不替换,还是原来的版本
classpath("com.android.tools.build:gradle:3.4.2")
classpath 'de.undercouch:gradle-download-task:3.4.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
// 注意:不要把你的应用的依赖放在这里;
// 它们应该放在各自模块的build.gradle文件中
}

(2) 添加:ReactAndroid项目,在android/settings.gradle中添加:ReactAndroid项目。

1
2
3
4
5
// 添加ReactAndroid的依赖
include ':ReactAndroid'
project(':ReactAndroid').projectDir = new File(
rootProject.projectDir, '../node_modules/react-native/ReactAndroid')

(3) 修改你的android/app/build.gradle文件,使用:ReactAndroid替换预编译库。例如用compile project(‘:ReactAndroid’):替换compile ‘com.facebook.react:react-native:+’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dependencies {
implementation project(':react-native-background-job')
implementation fileTree(dir: "libs", include: ["*.jar"])

implementation project(':ReactAndroid') //添加React-native项目
//implementation "com.facebook.react:react-native:+" // From node_modules //注释掉原来的react-native引用

if (enableHermes) {
def hermesPath = "../../node_modules/hermes-engine/android/";
debugImplementation files(hermesPath + "hermes-debug.aar")
releaseImplementation files(hermesPath + "hermes-release.aar")
} else {
implementation jscFlavor
}
}

(4) 让第三方模块使用你的分支

如果你使用第三方的React Native模块,你需要重写它们的依赖以避免它们仍然打包官方的预编译库。否则当你编译时会报错-Error: more than one library with package name ‘com.facebook.react’.(错误:有几个重名的’com.facebook.react’的包)

修改你的android/app/build.gradle文件,添加如下内容:

提示错误:

1
2
3
4
5
“clang++” cannot be opened because the developer cannot be verified.

macOS cannot verify that this app is free from malware.

Chrome downloaded this file on May 26, 2019.

image

这个是因为使用的NDK的版本版本不对。我们换为:

1
ndk.dir=/Users/frewen/Library/Android/android-ndk-r17c

替换NDK之后,又提示一个错误:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
> Task :app:transformNativeLibsWithMergeJniLibsForDebug FAILED

Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.5/userguide/command_line_interface.html#sec:command_line_warnings
54 actionable tasks: 54 executed
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformNativeLibsWithMergeJniLibsForDebug'.
> More than one file was found with OS independent path 'lib/x86/libc++_shared.so'

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 3m 59s

error Failed to install the app. Make sure you have the Android development environment set up: https://facebook.github.io/react-native/docs/getting-started.html#android-development-environment. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。

FAILURE: Build failed with an exception.

解决方法:
具体参见:https://github.com/react-native-community/jsc-android-buildscripts/issues/80

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
packagingOptions {
pickFirst '**/armeabi-v7a/libc++_shared.so'
pickFirst '**/x86/libc++_shared.so'
pickFirst '**/arm64-v8a/libc++_shared.so'
pickFirst '**/x86_64/libc++_shared.so'
pickFirst '**/x86/libjsc.so'
pickFirst '**/armeabi-v7a/libjsc.so'
pickFirst 'lib/x86_64/libfolly_futures.so'
}


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

pickFirst 'lib/x86/libjsc.so'
pickFirst 'lib/x86_64/libjsc.so'
pickFirst 'lib/armeabi-v7a/libjsc.so'
pickFirst 'lib/arm64-v8a/libjsc.so'

pickFirst 'lib/x86/libhermes-inspector.so'
pickFirst 'lib/x86_64/libhermes-inspector.so'
pickFirst 'lib/armeabi-v7a/libhermes-inspector.so'
pickFirst 'lib/arm64-v8a/libhermes-inspector.so'

pickFirst 'lib/x86/libfbjni.so'
pickFirst 'lib/x86_64/libfbjni.so'
pickFirst 'lib/armeabi-v7a/libfbjni.so'
pickFirst 'lib/arm64-v8a/libfbjni.so'

pickFirst 'lib/x86/libhermes-executor-debug.so'
pickFirst 'lib/x86_64/libhermes-executor-debug.so'
pickFirst 'lib/armeabi-v7a/libhermes-executor-debug.so'
pickFirst 'lib/arm64-v8a/libhermes-executor-debug.so'

pickFirst 'lib/x86/libfolly_futures.so'
pickFirst 'lib/x86_64/libfolly_futures.so'
pickFirst 'lib/armeabi-v7a/libfolly_futures.so'
pickFirst 'lib/arm64-v8a/libfolly_futures.so'

pickFirst 'lib/x86/libglog_init.so'
pickFirst 'lib/x86_64/libglog_init.so'
pickFirst 'lib/armeabi-v7a/libglog_init.so'
pickFirst 'lib/arm64-v8a/libglog_init.so'

pickFirst 'lib/x86/libglog.so'
pickFirst 'lib/x86_64/libglog.so'
pickFirst 'lib/armeabi-v7a/libglog.so'
pickFirst 'lib/arm64-v8a/libglog.so'

pickFirst 'lib/x86/libjsinspector.so'
pickFirst 'lib/x86_64/libjsinspector.so'
pickFirst 'lib/armeabi-v7a/libjsinspector.so'
pickFirst 'lib/arm64-v8a/libjsinspector.so'


pickFirst 'lib/x86/libreactnativeblob.so'
pickFirst 'lib/x86_64/libreactnativeblob.so'
pickFirst 'lib/armeabi-v7a/libreactnativeblob.so'
pickFirst 'lib/arm64-v8a/libreactnativeblob.so'

pickFirst 'lib/x86/libreactnativejni.so'
pickFirst 'lib/x86_64/libreactnativejni.so'
pickFirst 'lib/armeabi-v7a/libreactnativejni.so'
pickFirst 'lib/arm64-v8a/libreactnativejni.so'

pickFirst 'lib/x86/libhermes-executor-release.so'
pickFirst 'lib/x86_64/libhermes-executor-release.so'
pickFirst 'lib/armeabi-v7a/libhermes-executor-release.so'
pickFirst 'lib/arm64-v8a/libhermes-executor-release.so'

pickFirst 'lib/x86/libjscexecutor.so'
pickFirst 'lib/x86_64/libjscexecutor.so'
pickFirst 'lib/armeabi-v7a/libjscexecutor.so'
pickFirst 'lib/arm64-v8a/libjscexecutor.so'

pickFirst 'lib/x86/libfb.so'
pickFirst 'lib/x86_64/libfb.so'
pickFirst 'lib/armeabi-v7a/libfb.so'
pickFirst 'lib/arm64-v8a/libfb.so'

pickFirst 'lib/x86/libyoga.so'
pickFirst 'lib/x86_64/libyoga.so'
pickFirst 'lib/armeabi-v7a/libyoga.so'
pickFirst 'lib/arm64-v8a/libyoga.so'

pickFirst 'lib/x86/libjsijniprofiler.so'
pickFirst 'lib/x86_64/libjsijniprofiler.so'
pickFirst 'lib/armeabi-v7a/libjsijniprofiler.so'
pickFirst 'lib/arm64-v8a/libjsijniprofiler.so'

pickFirst 'lib/x86/libfolly_json.so'
pickFirst 'lib/x86_64/libfolly_json.so'
pickFirst 'lib/armeabi-v7a/libfolly_json.so'
pickFirst 'lib/arm64-v8a/libfolly_json.so'
}

错误:提示:More than one file was found with OS independent path ‘lib/x86_64/libfolly_futures.so’

使用编译运行iOS的工程

编译运行的时候出现错误,解决方法如下:
https://github.com/facebook/react-native/issues/25240

Hello! I faced this issue with React Native version 0.61.2 and this worked for me.

1
2
3
4
5
6
7
8
Hello! I faced this issue with React Native version 0.61.2 and this worked for me.
Navigate to the ios folder of the app and run the command

$ pod install

Once that is done, go back to the root directory and

$run react-native run-ios