参考文章https://www.jianshu.com/p/6111d67f959a
Android原生项目集成React Native
将一个现有工程接入rn开放,中间会有不少坑,今天记录下步骤和遇到的几个坑和解决办法
1. 用AndroidStudio创建一个工程,我的工程名称:AndroidInstallRn
2. 打开命令行终端,进入AndroidInstallRn工程里面
3. 输入命令
npm init
坑:Press ^C at any time to quit. 出现这一行以后开始输入一些配置信息,name不能大写。
npm install --save react react-native
坑:执行的时候报错
npm WARN react-native@0.43.4 requires a peer of react@16.0.0-alpha.6 but none was installed.
解决办法:执行
npm i -S react@16.0.0-alpha.6
找到项目中package.json文件,确认react是刚才安装版本就不用管了
"dependencies": { "react": "^16.0.0-alpha.6", "react-native": "^0.43.4" }
- 在package.json的scripts中加入字段
"start": "node node_modules/react-native/local-cli/cli.js start"
这时候package.json应该张这样
{
"name": "androidinstallrn",
"version": "1.0.0",
"description": "rn test",
"main": "index.android.js",
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start"
},
"keywords": [
"test",
"rn"
],
"author": "",
"license": "ISC",
"dependencies": {
"react": "^16.0.0-alpha.6",
"react-native": "^0.43.4"
}
}
- 继续在命令行执行
curl -o .flowconfig https://raw.githubusercontent.com/facebook/react-native/master/.flowconfig
- 把最简单的rn代码复制到index.android.js中
'use strict';
import React from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
class HelloWorld extends React.Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.hello}>Hello, World</Text>
</View>
)
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
hello: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
AppRegistry.registerComponent('HelloWorld', () => HelloWorld);
4. 修改app工程
- 在你的app中 build.gradle 文件中添加 React Native 依赖:
dependencies {
...
compile "com.facebook.react:react-native:+" // From node_modules.
}
你想要指定构建时的 React Native 版本,请用 npm 已下载的本地 React Native 的版本号替换 +
- 在项目的 build.gradle 文件中为 React Native 添加一个 maven 依赖的入口,必须写在 "allprojects" 代码块中:
allprojects {
repositories {
...
maven {
// All of React Native (JS, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
}
...
}
确保依赖路径的正确!以免在 Android Studio 运行Gradle同步构建时抛出 “Failed to resolve: com.facebook.react:react-native:0.x.x" 异常。
- 接着,在 AndroidManifest.xml 清单文件中声明权限:
<uses-permission android:name="android.permission.INTERNET" />
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
- 修改MainActivity
public class MainActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler {
private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
// 注意这里的HelloWorld必须对应“index.android.js”中的
// “AppRegistry.registerComponent()”的第一个参数
mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", null);
setContentView(mReactRootView);
}
@Override
protected void onPause() {
super.onPause();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostPause(this);
}
}
@Override
protected void onResume() {
super.onResume();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostResume(this,this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostDestroy(this);
}
}
@Override
public void invokeDefaultOnBackPressed() {
super.onBackPressed();
}
@Override
public void onBackPressed() {
if (mReactInstanceManager != null) {
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
mReactInstanceManager.showDevOptionsDialog();
return true;
}
return super.onKeyUp(keyCode, event);
}
}
- 到这基本完成,在命令行运行
npm start
启动rn服务
坑:这个时候 运行npm start可能会报错:
Loading dependency graph... ERROR Packager can't listen on port 8081
Most likely another process is already using this port Run the
following command to find out which process: lsof -i :8081
Then, you can either shut down the other process: kill -9 <PID>
or run packager on different port.
See http://facebook.github.io/react-native/docs/troubleshooting.html
for common problems and solutions.
这是8081端口被占用了,rn默认启动端口是8081解决办法
$ lsof -i :8081 $ kill - 9 <进程id>
- 启动成功。在studio上运用app
坑 app报错
FATAL EXCEPTION: AsyncTask #1
Process: com.example.wuxiaoyong.androidinstallrn, PID: 3121 java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:330)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:776)
Caused by: java.lang.UnsatisfiedLinkError: could find DSO to load: libreactnativejni.so
at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:213)
at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:178)
at com.facebook.react.bridge.JSCJavaScriptExecutor.<clinit>(JSCJavaScriptExecutor.java:25)
at com.facebook.react.bridge.JSCJavaScriptExecutor$Factory.create(JSCJavaScriptExecutor.java:20)
at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:183)
at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:169)
at android.os.AsyncTask$2.call(AsyncTask.java:316)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:776)
问题原因:
Android不能同时加载32和64位本机库。 如果您至少有一个依赖库使用ARM64支持编译的扩展,而另外一些依赖库仅支持ARM32,就会出现问题。 系统将检测ARM64依赖关系,加载它,然后拒绝加载仅ARM32的so库,就可能导致应用程序崩溃。
项目中只有libimagepipeline.so有arm64-v8a支持编译的扩展,Smartisan M1手机发现有64位的本机库,就不会再去加载32位的本机库,所以在Native Libraries installed区就只能看到libimagepipeline.so一个so库。也因此会报我们开篇提到的两个缺省so库的Error,也因此回复了为什么只有一个so库的问题。
解决办法:在你的app中 build.gradle 中加入
defaultConfig { ndk{ abiFilters "armeabi-v7a","x86" }
packagingOptions { exclude "lib/arm64-v8a/libimagepipeline.so" } }
OK 解决这个之后,又一个坑,又一个报错,真是一步一个坑
Caused by: java.lang.IllegalAccessError: Method 'void android.support.v4.net.ConnectivityManagerCompat.<init>()' is inaccessible to class 'com.facebook.react.modules.netinfo.NetInfoModule' (declaration of 'com.facebook.react.modules.netinfo.NetInfoModule' appears in /data/data/com.example.wuxiaoyong.androidinstallrn/files/instant-run/dex/slice-com.facebook.react-react-native-0.20.1_241a9e4c0e7198091bd2bc50912d161a24710fbf-classes.dex)
at com.facebook.react.modules.netinfo.NetInfoModule.<init>(NetInfoModule.java:55)
at com.facebook.react.shell.MainReactPackage.createNativeModules(MainReactPackage.java:67)
at com.facebook.react.ReactInstanceManagerImpl.processPackage(ReactInstanceManagerImpl.java:793)
at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:730)
at com.facebook.react.ReactInstanceManagerImpl.access$600(ReactInstanceManagerImpl.java:91)
at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:184)
at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:169)
at android.os.AsyncTask$2.call(AsyncTask.java:316)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:255)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:776)
问题原因:还记得我们上面再项目build.gradle中的配置吗?
allprojects {
repositories {
...
maven {
// All of React Native (JS, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
}
}
这个是我们按照官方文档写的,但是因为$rootDir/本身已经是跟目录了,在"../"就会找不到。我们把"../"去掉
allprojects {
repositories {
...
maven {
// All of React Native (JS, Android binaries) is installed from npm
url "$rootDir/node_modules/react-native/android"
}
}
}
ok
这个时候,如果是红屏等连不上服务器等,自己去查看解决方案。
如果手机能连上服务器,就会运用成功了
拓展
多个activity引用不同的js文件
这是一个activity绑定的index.android.js文件的布局,如果我们想在多个activity绑定不同的js文件界面怎么做?
- 新建js文件:
'use strict';
import React from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
export default class MyHelloWorld extends React.Component {
render() {
return (
<View style={styles.container}>
<Text style={styles.hello}>Hello, sssssssss</Text>
</View>
)
}
}
var styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
},
hello: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
AppRegistry.registerComponent('MyHelloWorld', () => MyHelloWorld);
- 然后在index.android.js中引入
import MyHelloWorld from './android_a';
- 在activity引用:
mReactRootView.startReactApplication(mReactInstanceManager, "MyHelloWorld", null);
坑:必现在index.android.js中import,然后这个类才会被调用,
AppRegistry.registerComponent('MyHelloWorld', () => MyHelloWorld);
注册才会执行,否则会报错:"MyHelloWorld"找不到
- 同一个界面包含原生文件和RN文件
直接把ReactRootView当Android普通view使用。在绑定的xml中写
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.wuxiaoyong.androidinstallrn.MainActivity">
<TextView
android:background="#ff0000"
android:layout_width="match_parent"
android:layout_height="200dp"
android:text="Hello World!" />
<com.facebook.react.ReactRootView
android:id="@+id/rn_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
- 在activity中找到这个组件并绑定界面
mReactRootView = (ReactRootView) findViewById(R.id.rn_view)
mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", null);
ok 大功告成。