Cannot resolve symbol ReactApplication/ReactNativeHost
Asked Answered
J

3

15

I have a react native running perfectly on iOS but does not compile in Android studio due to import issues in MainApplication and MainActivity. I followed the React Native FBSDK guidelines.

In MainApplication I get "Cannot resolve symbol" on import statements:

import com.facebook.react.ReactApplication; import com.facebook.react.ReactNativeHost;

And the following errors:

Error:(7, 8) error: MainActivity is not abstract and does not override abstract method getPackages() in ReactActivity Error:Execution failed for task ':app:compileDebugJavaWithJavac'.

Compilation failed; see the compiler error output for details.

My MainApplication looks like:

package com.ExampleApp;

import android.app.Application;

import com.facebook.react.ReactApplication;
import com.tradle.react.UdpSocketsModule;
import com.peel.react.TcpSocketsModule;
import com.peel.react.rnos.RNOSModule;
import com.masteratul.exceptionhandler.ReactNativeExceptionHandlerPackage;
import com.oblador.vectoricons.VectorIconsPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import com.facebook.CallbackManager;
import com.facebook.FacebookSdk;
import com.facebook.reactnative.androidsdk.FBSDKPackage;
import com.facebook.appevents.AppEventsLogger;

import java.util.Arrays;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {

  private static CallbackManager mCallbackManager = CallbackManager.Factory.create();

  protected static CallbackManager getCallbackManager() {
    return mCallbackManager;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    // Use AppEventsLogger to log custom events.
    AppEventsLogger.activateApp(this);
  }

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {

    @Override
    public boolean getUseDeveloperSupport() {
        return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
            new UdpSocketsModule(),
            new TcpSocketsModule(),
            new RNOSModule(),
            new ReactNativeExceptionHandlerPackage(),
            new VectorIconsPackage(),
            new FBSDKPackage(mCallbackManager)

      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

}

And my MainActivity:

import android.content.Intent;
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
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MainApplication.getCallbackManager().onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected String getMainComponentName() {
        return "ExampleApp";
    }
}
Jeanettajeanette answered 5/3, 2018 at 22:19 Comment(0)
C
7

It happened to me too after detach my react-native project from Expo.

In MainApplication I had "error Cannot resolve symbol" for these imports:

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;

in my package.json file I used:

"react-native": "https://github.com/expo/react-native/archive/sdk-29.0.0.tar.gz",

the problem was that the android can't find the directory for node_modules like that.

in order to fix the issue I had to:

  1. change it to :

    "react-native": "^0.57.8",

  2. delete node_modules file

  3. run npm install
  4. delete .gradle folder from android project and sync project.

That's what solved the problem for me. Hope I helped.

Costate answered 13/12, 2018 at 16:52 Comment(0)
C
0

I solved it by upgrading react-native, react-native-cli, gradle plugin to last version

Cernuous answered 20/7, 2018 at 12:36 Comment(0)
H
0

This error happened to me when I tried to install expo modules in a react native app.

https://docs.expo.dev/bare/installing-expo-modules/

To solved the issue I just had to undo manually the changes in MainApplication.java and MainActivity.java (above) that were automatically applied when I installed expo module.

Heaven answered 7/1 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.