React Native 与现有 Android 应用程序集成闪回

Posted

技术标签:

【中文标题】React Native 与现有 Android 应用程序集成闪回【英文标题】:React Native Integrating with existing Android app flash back 【发布时间】:2016-09-28 21:35:28 【问题描述】:

我会尽力阐述我的问题并提供足够的信息来展示细节。 首先,粘贴错误日志(FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground()):

FATAL EXCEPTION: AsyncTask #1 java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                          at android.os.AsyncTask$3.done(AsyncTask.java:299)
                                                                                          at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
                                                                                          at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
                                                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:239)
                                                                                          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
                                                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
                                                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
                                                                                          at java.lang.Thread.run(Thread.java:841)
                                                                                       Caused by: java.lang.IllegalAccessError: tried to access method android.support.v4.net.ConnectivityManagerCompat.<init>:(Lcom/facebook/react/bridge/ReactApplicationContext;)V from class com.facebook.react.modules.netinfo.NetInfoModule
                                                                                          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:751)
                                                                                          at com.facebook.react.ReactInstanceManagerImpl.createReactContext(ReactInstanceManagerImpl.java:688)
                                                                                          at com.facebook.react.ReactInstanceManagerImpl.access$600(ReactInstanceManagerImpl.java:84)
                                                                                          at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:177)
                                                                                          at com.facebook.react.ReactInstanceManagerImpl$ReactContextInitAsyncTask.doInBackground(ReactInstanceManagerImpl.java:162)
                                                                                          at android.os.AsyncTask$2.call(AsyncTask.java:287)
                                                                                          at java.util.concurrent.FutureTask.run(FutureTask.java:234)
                                                                                          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
                                                                                          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
                                                                                          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
                                                                                          at java.lang.Thread.run(Thread.java:841)

我的项目结构:

android 依赖:

dependencies 
    ...
    compile 'com.facebook.react:react-native:0.20.0'

AndroidManifest.xml:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>

MainActivity.java:

public class MainActivity extends Activity 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();
        mReactRootView.startReactApplication(mReactInstanceManager, "AndroidToRn03", null);

        setContentView(mReactRootView);
    
    ...

package.json:


  "name": "androidtorn03",
  "version": "1.0.0",
  "description": "",
  "main": "index.android.js",
  "scripts": 
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node node_modules/react-native/local-cli/cli.js start"
  ,
  "author": "",
  "license": "ISC",
  "dependencies": 
    "react-native": "^0.26.2"
  

然后,index.android.js:

import React from 'react';
import 
  AppRegistry,
  StyleSheet,
  Text,
  View
 from 'react-native';

class AndroidToRn03 extends React.Component 
  render() 
    return (
      <View style=styles.container>
        <Text style=styles.hello>React Native....</Text>
      </View>
    )
  

var styles = StyleSheet.create(
  container: 
    flex: 1,
    justifyContent: 'center',
  ,
  hello: 
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  ,
);

AppRegistry.registerComponent('AndroidToRn03', () => AndroidToRn03);

我按照docs on facebook's website试了很多次,都失败了,打开的时候app闪退。 我需要你的帮助!你介意花点时间帮我吗?

【问题讨论】:

我有同样的错误:( @herdotage 你能解决这个问题吗? 对此的任何解决方案。请提出建议。 【参考方案1】:

更改文件app/build.gradle 依赖项:

compile 'com.android.support:appcompat-v7:23.0.1'

【讨论】:

以上是关于React Native 与现有 Android 应用程序集成闪回的主要内容,如果未能解决你的问题,请参考以下文章

现有应用程序中的 React Native 更新版本

用于 React Native Android 的 XMPP 库

React Native 与现有应用程序的集成

如何在 React Native 中创建 Android 即时应用程序?

将 React Native 代码集成到现有的 android 项目中

React-Native系列Android——Native与Javascript通信原理