Skip to content

Cannot use yarn android after adding react navigation 6.x

So I’m using https://reactnavigation.org/docs/getting-started to make my navigation. I did the yarn add @react-navigation/native and yarn add react-native-screens react-native-safe-area-context. At this point my app is still fine even after restarting yarn start. The problem started after I add the code needed to MainActivity.java and from there the app won’t launch (it also won’t work even after wrapping it in NavigationContainer in App.js)

This is the code from the MainActivity.java (import android.os.Bundle and @Override proctected void onCreate is the addition inserted into this app from the 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;

This is from yarn android

D:Atur Toko Filesreact-native filesMyDoctor2>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 Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreen.kt: (80, 26): 'constructor GuardedRunnable(ReactContext!)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenStackFragment.kt: (59, 28): 'setter for targetElevation: Float' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenStackFragment.kt: (142, 28): 'setter for targetElevation: Float' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenStackHeaderConfig.kt: (154, 56): 'getter for systemWindowInsetTop: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenStackHeaderConfig.kt: (222, 29): 'setColorFilter(Int, PorterDuff.Mode): Unit' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (56, 22): 'constructor GuardedRunnable(ReactContext!)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (84, 57): 'getter for systemUiVisibility: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (86, 53): 'SYSTEM_UI_FLAG_LIGHT_STATUS_BAR: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (88, 54): 'SYSTEM_UI_FLAG_LIGHT_STATUS_BAR: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (90, 27): 'setter for systemUiVisibility: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (106, 22): 'constructor GuardedRunnable(ReactContext!)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (115, 43): 'replaceSystemWindowInsets(Int, Int, Int, Int): WindowInsets' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (116, 47): 'getter for systemWindowInsetLeft: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (118, 47): 'getter for systemWindowInsetRight: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (119, 47): 'getter for systemWindowInsetBottom: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (138, 69): 'FLAG_FULLSCREEN: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (139, 71): 'FLAG_FORCE_NOT_FULLSCREEN: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (141, 69): 'FLAG_FORCE_NOT_FULLSCREEN: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreensScreenWindowTraits.kt: (142, 71): 'FLAG_FULLSCREEN: Int' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsHeaderBackButtonClickedEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsHeaderBackButtonClickedEvent.kt: (7, 51): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsHeaderBackButtonClickedEvent.kt: (17, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsHeaderBackButtonClickedEvent.kt: (18, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenAppearEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenAppearEvent.kt: (7, 40): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenAppearEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenAppearEvent.kt: (14, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenDisappearEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenDisappearEvent.kt: (7, 43): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenDisappearEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenDisappearEvent.kt: (14, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenDismissedEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenDismissedEvent.kt: (7, 43): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenDismissedEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenDismissedEvent.kt: (17, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenTransitionProgressEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenTransitionProgressEvent.kt: (13, 5): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenTransitionProgressEvent.kt: (22, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenTransitionProgressEvent.kt: (27, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenWillAppearEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenWillAppearEvent.kt: (7, 44): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenWillAppearEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenWillAppearEvent.kt: (14, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenWillDisappearEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenWillDisappearEvent.kt: (7, 47): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenWillDisappearEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsScreenWillDisappearEvent.kt: (14, 25): 'receiveEvent(Int, String!, WritableMap?): Unit' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsStackFinishTransitioningEvent.kt: (5, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsStackFinishTransitioningEvent.kt: (7, 52): 'constructor Event<T : Event<(raw) Event<*>>!>(Int)' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsStackFinishTransitioningEvent.kt: (13, 44): 'RCTEventEmitter' is deprecated. Deprecated in Java
w: D:Atur Toko Filesreact-native filesMyDoctor2node_modulesreact-native-screensandroidsrcmainjavacomswmansionrnscreenseventsStackFinishTransitioningEvent.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 Filesreact-native filesMyDoctor2>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 Filesreact-native filesMyDoctor2androidappsrcmainjavacommydoctor2MainActivity.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 Filesreact-native filesMyDoctor2androidappsrcmainjavacommydoctor2MainActivity.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 Filesreact-native filesMyDoctor2node_modulesexecaindex.js:174:9)
    at D:Atur Toko Filesreact-native filesMyDoctor2node_modulesexecaindex.js:278:16
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async runOnAllDevices (D:Atur Toko Filesreact-native [email protected]droidbuildcommandsrunAndroidrunOnAllDevices.js:106:5)
    at async Command.handleAction (D:Atur Toko Filesreact-native [email protected]s: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.

Answer

You should move the import statement below to your package name.

The import statement is written before the class definition and after the package statement(if there is any)

Simply change your MainActivity.java to,

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);
  }
}