为何要自己编译React Native安卓私有代码

我们在开发中遇到一个HTTP2的问题,React Native安卓客户端在和HTTP2支持的服务器通讯的过程中会有crash,见

  1. React-Native HTTP2 issue
  2. How to build private build

由于时间紧急,发布期限已经拖了好久了,没法等待官方解决方案,只能在本地做修复然后发布。

编译私有React-Native

针对android,React-Native有官方指导说明.

过程中遇到不少坑,记录如下:

boost下载失败

说明文档里面指定的 gradle-download-task版本有问题, 要用最新版,老版的2.0.0会抛403下载失败,导致编译通不过。测试de.undercouch:gradle-download-task:3.1.2可以工作。

classpath 'de.undercouch:gradle-download-task:3.1.2'

NDK版本不对导致编译失败

文档里面指定NDK是android-ndk-r10e, 我用brew搜不到r10e,于是装了一个13b, 结果编译错误,只能乖乖手动下载r10e的包然后解压,然后在.zshrc里面设置ANDROID_NDK路径

export ANDROID_NDK=/Users/your_home/android-ndk/android-ndk-r10e

用android-ndk-r13b编译抛错如下:

	:ReactAndroid:buildReactNdkLib
[armeabi-v7a] Compile++ : reactnativejni <= Dummy.cpp
[armeabi-v7a] Compile++ : reactnativejnifb <= CatalystInstanceImpl.cpp
make: [armeabi-v7a] Compile++ : reactnativejnifb <= CxxModuleWrapper.cpp
/usr/local/Cellar/android-ndk/r13b/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++: No such file or directory
make: /usr/local/Cellar/android-ndk/r13b/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++: No such file or directory
[armeabi-v7a] Compile++ : reactnativejnifb <= JExecutorToken.cpp
make: /usr/local/Cellar/android-ndk/r13b/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++: No such file or directory
make: /usr/local/Cellar/android-ndk/r13b/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++: No such file or directory
make: *** [/Users/andrewy/sourcecode/github/dgandroid3/ReactNative/dgAndroid/node_modules/react-native/ReactAndroid/build/tmp/buildReactNdkLib/local/armeabi-v7a/objs/reactnativejni/Dummy.o] Error 1
make: *** Waiting for unfinished jobs....
make: *** [/Users/andrewy/sourcecode/github/dgandroid3/ReactNative/dgAndroid/node_modules/react-native/ReactAndroid/build/tmp/buildReactNdkLib/local/armeabi-v7a/objs/reactnativejnifb/CxxModuleWrapper.o] Error 1
make: *** [/Users/andrewy/sourcecode/github/dgandroid3/ReactNative/dgAndroid/node_modules/react-native/ReactAndroid/build/tmp/buildReactNdkLib/local/armeabi-v7a/objs/reactnativejnifb/CatalystInstanceImpl.o] Error 1
make: *** [/Users/andrewy/sourcecode/github/dgandroid3/ReactNative/dgAndroid/node_modules/react-native/ReactAndroid/build/tmp/buildReactNdkLib/local/armeabi-v7a/objs/reactnativejnifb/JExecutorToken.o] Error 1
[armeabi-v7a] Compile++ : reactnativejnifb <= JMessageQueueThread.cpp
make: /usr/local/Cellar/android-ndk/r13b/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++: No such file or directory
make: *** [/Users/andrewy/sourcecode/github/dgandroid3/ReactNative/dgAndroid/node_modules/react-native/ReactAndroid/build/tmp/buildReactNdkLib/local/armeabi-v7a/objs/reactnativejnifb/JMessageQueueThread.o] Error 1
:ReactAndroid:buildReactNdkLib FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':ReactAndroid:buildReactNdkLib'.
> Process 'command '/usr/local/Cellar/android-ndk/r13b/ndk-build'' finished with non-zero exit value 2

react-native-fetch-blob编译失败

(实际上把后面的react-native 重复的问题解决后这个问题会自动解决,可以跳过, 还是要手动改依赖)

ndk的问题解决以后,react-native-fetch-blob编译又失败了.提示okhttp3找不到。

解决方法: 把react-native-fetch-blob/android/build.gradle下面的

compile 'com.facebook.react:react-native:+'

替换成

compile project(':ReactAndroid')

Step 2: Create a local.properties file in the android directory of your react-native app with the following contents:

Example:

sdk.dir=/Users/your_unix_name/android-sdk-macosx
ndk.dir=/Users/your_unix_name/android-ndk/android-ndk-r10e
Download links for Android NDK
Mac OS (64-bit) - http://dl.google.com/android/repository/android-ndk-r10e-darwin-x86_64.zip
Linux (64-bit) - http://dl.google.com/android/repository/android-ndk-r10e-linux-x86_64.zip
Windows (64-bit) - http://dl.google.com/android/repository/android-ndk-r10e-windows-x86_64.zip
Windows (32-bit) - http://dl.google.com/android/repository/android-ndk-r10e-windows-x86.zip
You can find further instructions on the official page.

https://facebook.github.io/react-native/docs/android-building-from-source.html

react-native 重复的问题

前面所有问题解决以后还可能会遇到下面的问题

:app:processDebugResources FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugResources'.
> Error: more than one library with package name 'com.facebook.react'

解决办法: 在your_project_root/android/app/build.gradle里面添加如下配置(放在dependency同一层级即可)

configurations.all {
exclude group: 'com.facebook.react', module: 'react-native'
}

Boost 下载失败的问题。

首先,你可以尝试更新 gradle-download-task 到 3.1.2,这可以解决gradle-download-task 2.0.0下载时抛403的问题。

然后,如果你网络比较差,经常下到一半超时断线,你可以在浏览器里面把boost下载下来,然后,命令行里面boost开始下载以后CTRL+C中断,然后用浏览器里面下载的boost覆盖目标boost. 我这边是

node_modules/react-native/ReactAndroid/build/downloads/boost_1_57_0.zip

再次运行react-native run-android

编译成功!

然后你的app跑起来的时候可能会遇到下面的问题, 心累!:

Shutting down VM
FATAL EXCEPTION: main
Process: com.dealglobe, PID: 2162
java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libyoga.so
at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:314)
at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:247)
at com.facebook.csslayout.YogaNode.<clinit>(YogaNode.java:24)
at com.facebook.react.uimanager.ReactShadowNode.<init>(ReactShadowNode.java:82)
at com.facebook.react.uimanager.UIImplementation.createRootShadowNode(UIImplementation.java:84)
at com.facebook.react.uimanager.UIImplementation.registerRootView(UIImplementation.java:120)
at com.facebook.react.uimanager.UIManagerModule.addMeasuredRootView(UIManagerModule.java:198)
at com.facebook.react.XReactInstanceManagerImpl.attachMeasuredRootViewToInstance(XReactInstanceManagerImpl.java:803)
at com.facebook.react.XReactInstanceManagerImpl.setupReactContext(XReactInstanceManagerImpl.java:778)
at com.facebook.react.XReactInstanceManagerImpl.access$700(XReactInstanceManagerImpl.java:110)
at com.facebook.react.XReactInstanceManagerImpl$ReactContextInitAsyncTask.onPostExecute(XReactInstanceManagerImpl.java:224)
at com.facebook.react.XReactInstanceManagerImpl$ReactContextInitAsyncTask.onPostExecute(XReactInstanceManagerImpl.java:193)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

经证实,这个是因为react-native代码不稳定造成的,切换到0.40-stable问题就消失了。

http://blog.ilibrary.me/2016/12/04/%E7%BC%96%E8%AF%91%E7%A7%81%E6%9C%89react-native%E4%BB%A3%E7%A0%81

React Native系列(6) - 编译安卓私有React-Native代码的更多相关文章

  1. C++的性能C#的产能?! - .Net Native 系列《三》:.NET Native部署测试方案及样例

    之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...

  2. C++的性能C#的产能?! - .Net Native 系列《二》:.NET Native开发流程详解

    之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...

  3. C++的性能C#的产能?! - .Net Native 系列《一》:.NET Native安装和配置

    之前一文<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATIVE初窥> 获得很多朋友支持和鼓励,也更让我坚定做这项技术的推广者,希望能让更多的朋友了解这项技术,于是先从官方 ...

  4. React 深入系列1:React 中的元素、组件、实例和节点

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助大家加深对React的理解,以及在项目中 ...

  5. React 深入系列2:组件分类

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列2:组件分类 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助大家加 ...

  6. React 深入系列3:Props 和 State

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...

  7. React 深入系列4:组件的生命周期

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列4:组件的生命周期 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助 ...

  8. React 深入系列5:事件处理

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列5:事件处理 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助大家加 ...

  9. C++的性能C#的产能?! - .Net Native 系列五:.Net Native与反射

    此系列系小九的学堂原创翻译,翻译自微软官方开发向导,一共分为六个主题.本文是第五个主题:.Net Native与反射. 向导文链接:<C++的性能C#的产能?! - .Net Native 系列 ...

随机推荐

  1. Linux配置文件系统及程序的限制 - ulimit

    想象一个状况:我的 Linux 主机里面同时登陆了十个人,这十个人不知怎么搞的, 同时开启了 100 个文件,每个文件的大小约 10MBytes ,请问一下, 我的 Linux 主机的内存要有多大才够 ...

  2. butternife Zelezny自动注入插件

    插件地址:http://plugins.jetbrains.com/plugin/7369 Products: IntelliJ IDEA, RubyMine, WebStorm, PhpStorm, ...

  3. FineReport性能调优的一些办法

    FineReport性能调优的基本思路,就要对应用服务器的内存大小进行合理的设置. 一般服务器默认的内存配置都比较小,在较大型的应用项目中,这点内存是不够的,因此需要加工使其调大. 各应用服务器的内存 ...

  4. iPhone实现自定义多选列表

    好久没更新博客了,今天写了一个自定义的多选列表,可以跟爱学习的各位进行分享,首先我们先来看一下效果图: 一般大家都是用UITableView自己的编辑模式来实现CheckBox的,这里我们用自定义Ce ...

  5. The 13th tip of DB Query Analyzer, powerful processing EXCEL file

    The 13thtip of DB Query Analyzer, powerful processing EXCEL file MA Genfeng (Guangdong UnitollServic ...

  6. python socketserver框架解析

    socketserver框架是一个基本的socket服务器端框架, 使用了threading来处理多个客户端的连接, 使用seletor模块来处理高并发访问, 是值得一看的python 标准库的源码之 ...

  7. Spring Boot 多模块与 Maven 私有仓库

    前言 系统复杂了,抽离单一职责的模块几乎是必须的:若需维护多个项目,抽离公用包上传私有仓库管理也几乎是必须的.其优点无需赘述,以下将记录操作过程. 1. 多模块拆分 在.NET 中由于其统一性,实现上 ...

  8. 移动端 slide拖拽

    <html> <head> <meta charset="UTF-8"> <meta name="viewport" ...

  9. css3属性(1)

    text-transform语法: text-transform : none | capitalize| uppercase| lowercase 参数: none : 无转换发生 capitali ...

  10. partition List(划分链表)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...