Cannot resolve symbol ReactApplication/ReactNativeHost

被刻印的时光 ゝ 提交于 2019-12-18 19:06:25

问题


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

回答1:


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.




回答2:


Upgrade react-native :

react-native upgrade



回答3:


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



来源:https://stackoverflow.com/questions/49120454/cannot-resolve-symbol-reactapplication-reactnativehost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!