添加反应导航6.x后无法使用yarn android
Posted
技术标签:
【中文标题】添加反应导航6.x后无法使用yarn android【英文标题】:Cannot use yarn android after adding react navigation 6.x 【发布时间】:2021-11-30 02:25:28 【问题描述】:所以我使用https://reactnavigation.org/docs/getting-started 进行导航。我做了 yarn add @react-navigation/native 和 yarn add react-native-screens react-native-safe-area-context。此时,即使重新启动 yarn start,我的应用程序仍然可以正常运行。在我将所需的代码添加到 MainActivity.java 之后问题就开始了,从那里应用程序将无法启动(即使将其包装在 App.js 中的 NavigationContainer 中也无法运行)
这是来自 MainActivity.java 的代码(import android.os.Bundle 和 @Override proctected void onCreate 是从 reactnavigation 插入到这个应用程序中的添加)
import android.os.Bundle;
package com.mydoctor2;
import com.facebook.react.ReactActivity;
public class MainActivity extends ReactActivity
/**
* Returns the name of the main component registered from javascript. This is used to schedule
* rendering of the component.
*/
@Override
protected String getMainComponentName()
return "MyDoctor2";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(null);
App.js
import React from 'react';
import GetStarted from './pages';
import NavigationContainer from '@react-navigation/native';
function App()
return (
<NavigationContainer>
<GetStarted />
</NavigationContainer>
);
export default App;
这是来自yarn android
D:\Atur Toko Files\react-native files\MyDoctor2>yarn android
yarn run v1.17.3
$ react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 963 file(s) to forward-jetify. Using 12 workers...
info JS server already running.
info Installing the app...
> Task :react-native-safe-area-context:compileDebugJavaWithJavac
> Task :react-native-svg:compileDebugJavaWithJavac
> Task :react-native-screens:compileDebugKotlin
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\Screen.kt: (80, 26): 'constructor GuardedRunnable(ReactContext!)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenStackFragment.kt: (59, 28): 'setter for targetElevation: Float' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenStackFragment.kt: (142, 28): 'setter for targetElevation: Float' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenStackHeaderConfig.kt: (154, 56): 'getter for systemWindowInsetTop: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenStackHeaderConfig.kt: (222, 29): 'setColorFilter(Int, PorterDuff.Mode): Unit' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (56, 22): 'constructor GuardedRunnable(ReactContext!)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (84, 57): 'getter for systemUiVisibility: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (86, 53): 'SYSTEM_UI_FLAG_LIGHT_STATUS_BAR: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (88, 54): 'SYSTEM_UI_FLAG_LIGHT_STATUS_BAR: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (90, 27): 'setter for systemUiVisibility: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (106, 22): 'constructor GuardedRunnable(ReactContext!)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (115, 43): 'replaceSystemWindowInsets(Int, Int, Int, Int): WindowInsets' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (116, 47): 'getter for systemWindowInsetLeft: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (118, 47): 'getter for systemWindowInsetRight: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (119, 47): 'getter for systemWindowInsetBottom: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (138, 69): 'FLAG_FULLSCREEN: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (139, 71): 'FLAG_FORCE_NOT_FULLSCREEN: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (141, 69): 'FLAG_FORCE_NOT_FULLSCREEN: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\ScreenWindowTraits.kt: (142, 71): 'FLAG_FULLSCREEN: Int' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\HeaderBackButtonClickedEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\HeaderBackButtonClickedEvent.kt: (7, 51): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\HeaderBackButtonClickedEvent.kt: (17, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\HeaderBackButtonClickedEvent.kt: (18, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenAppearEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenAppearEvent.kt: (7, 40): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenAppearEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenAppearEvent.kt: (14, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenDisappearEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenDisappearEvent.kt: (7, 43): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenDisappearEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenDisappearEvent.kt: (14, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenDismissedEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenDismissedEvent.kt: (7, 43): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenDismissedEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenDismissedEvent.kt: (17, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenTransitionProgressEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenTransitionProgressEvent.kt: (13, 5): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenTransitionProgressEvent.kt: (22, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenTransitionProgressEvent.kt: (27, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenWillAppearEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenWillAppearEvent.kt: (7, 44): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenWillAppearEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenWillAppearEvent.kt: (14, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenWillDisappearEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenWillDisappearEvent.kt: (7, 47): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenWillDisappearEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\ScreenWillDisappearEvent.kt: (14, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\StackFinishTransitioningEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\StackFinishTransitioningEvent.kt: (7, 52): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\StackFinishTransitioningEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\react-native-screens\android\src\main\java\com\swmansion\rnscreens\events\StackFinishTransitioningEvent.kt: (14, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
> Task :app:compileDebugJavaWithJavac
> Task :app:installDebug
Installing APK 'app-debug.apk' on 'Nexus_6P_API_29(AVD) - 10' for app:debug
Installed on 1 device.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.9/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 27s
84 actionable tasks: 70 executed, 14 up-to-date
info Connecting to the development server...
8081
info Starting the app on "emulator-5554"...
Starting: Intent cmp=com.mydoctor2/.MainActivity
Done in 34.71s.
D:\Atur Toko Files\react-native files\MyDoctor2>yarn android
yarn run v1.17.3
$ react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 995 file(s) to forward-jetify. Using 12 workers...
info JS server already running.
info Installing the app...
> Task :app:compileDebugJavaWithJavac FAILED
Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.9/userguide/command_line_interface.html#sec:command_line_warnings
58 actionable tasks: 2 executed, 56 up-to-date
D:\Atur Toko Files\react-native files\MyDoctor2\android\app\src\main\java\com\mydoctor2\MainActivity.java:2: error: class, interface, or enum expected
package com.mydoctor2;
^
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* 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 7s
error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081
D:\Atur Toko Files\react-native files\MyDoctor2\android\app\src\main\java\com\mydoctor2\MainActivity.java:2: error: class, interface, or enum expected
package com.mydoctor2;
^
1 error
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* 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 7s
at makeError (D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\execa\index.js:174:9)
at D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\execa\index.js:278:16
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async runOnAllDevices (D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:106:5)
at async Command.handleAction (D:\Atur Toko Files\react-native files\MyDoctor2\node_modules\@react-native-community\cli\build\index.js:192:9)
info Run CLI with --verbose flag for more details.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
【问题讨论】:
【参考方案1】:您应该将下面的导入语句移至您的包名称。
import语句写在类定义之前和package语句之后(如果有的话)
只需将您的 MainActivity.java
更改为,
package com.mydoctor2;
import android.os.Bundle; // moved import statement
import com.facebook.react.ReactActivity;
public class MainActivity extends ReactActivity
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
@Override
protected String getMainComponentName()
return "MyDoctor2";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(null);
【讨论】:
现在看到答案感觉自己傻XD谢谢解答,卡了一整天 快乐编码以上是关于添加反应导航6.x后无法使用yarn android的主要内容,如果未能解决你的问题,请参考以下文章
yarn build:styles 错误在添加顺风 css 时做出反应