问题描述
我们正在尝试将 react-native 集成到我们现有的 android 应用程序中:我们的 build.gradle 的 SDK 要求是:minSdk 版本 16目标SDK版本23
we are trying to integrate react-native into our existing android application:The SDK requirements of our build.gradle are:minSdkVersion 16targetSdkVersion 23
我们使用 Intent 调用初始 React index.js:
We use an Intent to call the initial React index.js:
Intent inte = new Intent(this, ReactNativeStarter.class);
startActivity(inte);
其余代码来自 react 集成教程.
The rest of the code is from the react integrating tutorial.
当我们尝试调用我们的 React 组件时,我们收到以下错误:
When we try to call our react component we get the following error:
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/at.??????/files/instant-run/dex/slice-com.facebook.react-react-native-0.20.1_3762d580ab3ced1fa2f7503493d38e666994b9fa-classes.dex)
at com.facebook.react.modules.netinfo.NetInfoModule.<init>(NetInfoModule.java:55)
你能帮忙吗?
完整的错误日志:
12-19 10:07:22.157 30271-32680/at.???? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #4
Process: at.?????, PID:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
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/at.??????/files/instant-run/dex/slice-com.facebook.react-react-native-0.20.1_3762d580ab3ced1fa2f7503493d38e666994b9fa-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:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
推荐答案
我正在将一个现有的 Android 应用(用 kotlin 编写)与 React Native 集成.
I was integrating an existing Android app (written on kotlin) with React Native.
我编写的用于运行 ReactRootView
的活动 (.kt) 具有以下 onCreate()
方法:
The activity (.kt) I was writing to run the ReactRootView
had the following onCreate()
method:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mReactRootView = ReactRootView(this)
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(application)
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index")
.addPackage(MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState((LifecycleState.RESUMED))
.build()
// The string here (e.g. "MyReactNativeApp") has to match
// the string in AppRegistry.registerComponent() in index.js
mReactRootView!!.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null)
setContentView(mReactRootView)
if (Build.VERSION.SDK_INT >= M) {
if (!canDrawOverlays(this)) {
val intent = Intent(ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:$packageName"))
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE)
}
}
}
问题就在于此
mReactRootView!!.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null)
.
我不得不将模块"名称从MyReactNativeApp"更改为App",这是我在 React 上注册的主要组件的名称.
I had to change the "module" name from 'MyReactNativeApp' to 'App', which is the name of my main component registered on React.
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
AppRegistry.registerComponent(appName, () => App);
Sooo... 正确的 onCreate()
函数是
Sooo... the right onCreate()
function is
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mReactRootView = ReactRootView(this)
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(application)
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index")
.addPackage(MainReactPackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState((LifecycleState.RESUMED))
.build()
// The string here (e.g. "MyReactNativeApp") has to match
// the string in AppRegistry.registerComponent() in index.js
mReactRootView!!.startReactApplication(mReactInstanceManager, "App", null)
setContentView(mReactRootView)
if (Build.VERSION.SDK_INT >= M) {
if (!canDrawOverlays(this)) {
val intent = Intent(ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:$packageName"))
startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE)
}
}
}
这篇关于将 react-native 集成到现有的 Android 应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!