java React Native:错误:升级后找不到符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49542654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React native: error: cannot find symbol after upgrade
提问by Chris Henry
Hi after importing my react native project expo and upgrading react, I've been have the following problems.
嗨,在导入我的 react native 项目 expo 并升级 react 后,我遇到了以下问题。
C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:5: error: cannot find symbol import com.facebook.react.ReactApplication; ^ symbol: class ReactApplication location: package com.facebook.react C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:13: error: cannot find symbol import com.facebook.react.ReactNativeHost; ^ symbol: class ReactNativeHost location: package com.facebook.react C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:21: error: cannot find symbol public class MainApplication extends Application implements ReactApplication { ^ symbol: class ReactApplication C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:23: error: cannot find symbol private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { ^ symbol: class ReactNativeHost location: class MainApplication C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:40: error: cannot find symbol public ReactNativeHost getReactNativeHost() { ^ symbol: class ReactNativeHost location: class MainApplication C:\Users\user\gramic\android\app\src\main\java\com\shop\MainActivity.java:5: error: MainActivity is not abstract and does not override abstract method getPackages() in ReactActivity public class MainActivity extends ReactActivity { ^ C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:23: error: cannot find symbol private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { ^ symbol: class ReactNativeHost location: class MainApplication C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:39: error: method does not override or implement a method from a supertype @Override ^
C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:5: 错误:找不到符号导入 com.facebook.react.ReactApplication; ^ 符号:类 ReactApplication 位置:包 com.facebook.react C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:13: 错误:找不到符号导入 com。 facebook.react.ReactNativeHost; ^ 符号:类 ReactNativeHost 位置:包 com.facebook.react C:\Users\user\gramic\android\app\src\main\java\com\shop\MainApplication.java:21: 错误:找不到符号公共类 MainApplication扩展应用程序实现了 ReactApplication { ^ 符号:类 ReactApplication C:
package com.shop;
import android.app.Application;
import com.facebook.react.ReactApplication;
import com.oblador.vectoricons.VectorIconsPackage;
import com.corbt.keepawake.KCKeepAwakePackage;
import com.oblador.vectoricons.VectorIconsPackage;
import com.github.yamill.orientation.OrientationPackage;
import com.BV.LinearGradient.LinearGradientPackage;
import com.brentvatne.react.ReactVideoPackage;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
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 VectorIconsPackage(),
new VectorIconsPackage(),
new KCKeepAwakePackage(),
new VectorIconsPackage(),
new OrientationPackage(),
new LinearGradientPackage(),
new ReactVideoPackage()
);
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
SoLoader.init(this, /* native exopackage */ false);
}
}
回答by Marcus
First: install all dependencies using yarnor npm;
首先:使用yarn或npm安装所有依赖项;
Second: use the code below to link native dependencies on iOS and Android native code;
第二:使用下面的代码链接iOS原生依赖和Android原生代码;
react-native link
Third: this error can occur because you RN version is different from Android's build.gradle version. When you create a react-native app probably it create android app like:
第三:这个错误可能是因为你的RN版本和Android的build.gradle版本不同。当您创建 react-native 应用程序时,它可能会创建 android 应用程序,例如:
android/app/build.gradle
implementation "com.facebook.react:react-native:+"
So, inspect you node_modules folder, look for react-native folder and look for a folder with a number, that numbers are react-native version. For me it's 0.58.3then update android/app/build.gradle:
因此,检查您的 node_modules 文件夹,查找 react-native 文件夹并查找带有数字的文件夹,该数字是 react-native 版本。对我来说是0.58.3然后更新android/app/build.gradle:
implementation "com.facebook.react:react-native:0.58.3"
It's All.
都是。
回答by mic123456
Happened to me too after detach my react-native project from Expo.
在将我的 react-native 项目与 Expo 分离后,我也发生了这种情况。
As I answered here:
正如我在这里回答的那样:
in order to fix the issue I had to:
为了解决这个问题,我不得不:
in package.js I changed:
"react-native": "https://github.com/expo/react-native/archive/sdk-29.0.0.tar.gz",
to :
"react-native": "^0.57.8",
delete node_modules file
- run npm install
- delete '.gradle' folder from android project and sync project.
在 package.js 我改变了:
"react-native": " https://github.com/expo/react-native/archive/sdk-29.0.0.tar.gz",
到 :
“反应原生”:“^0.57.8”,
删除 node_modules 文件
- 运行 npm 安装
- 从 android 项目和同步项目中删除“.gradle”文件夹。
回答by Lorine
I also had these errors when trying to upgrade React Native.
I have seen people resolve those by cleaning gradlew cache (./gradlew clean
in Android project folder) or deleting the whole .gradle
folder (in Android project folder also).
我在尝试升级 React Native 时也遇到了这些错误。我见过人们通过清理 gradlew 缓存(./gradlew clean
在 Android 项目文件夹中)或删除整个.gradle
文件夹(也在 Android 项目文件夹中)来解决这些问题。
But that did not work for me. My issue was in the android/build.gradle
file. I previously had to add a maven repository for another package and had done it that way:
但这对我不起作用。我的问题出在android/build.gradle
文件中。我以前不得不为另一个包添加一个 maven 存储库,并且已经这样做了:
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
// The following is NOT the right way
url "https://maven.google.com"
}
but it is said in the Android docsthat every maven repository should be in its own maven {}
block.
但是在Android 文档中说每个 Maven 存储库都应该在自己的maven {}
块中。
Thus the solution that worked for me was to do that:
因此,对我有用的解决方案是这样做:
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
maven {
url "https://maven.google.com"
}
Hope it will help, and good luck for your react upgrade! ;)
希望它会有所帮助,并祝您的反应升级好运!;)
回答by SimonEritsch
I had a similar issue and all the advice I found didn't help, so I'm dropping this for anyone else having the same issue.
我有一个类似的问题,我发现的所有建议都没有帮助,所以我放弃了这个给其他有同样问题的人。
For me it turned out to be a 3rd party package (pushwoosh-react-native-plugin) that compiled/implemented a static version of react-native in it's build.gradle file.
对我来说,它原来是一个 3rd 方包(pushwoosh-react-native-plugin),它在 build.gradle 文件中编译/实现了 react-native 的静态版本。
I managed to find it, by
我设法找到了,通过
- going to my app/build.gradle
- commented out all my compiles/implementation on dependencies
- added them one by one until i got the same error
- 转到我的应用程序/build.gradle
- 注释掉我对依赖项的所有编译/实现
- 一一添加,直到出现同样的错误
As said above: It turned out to be the pushwoosh build.gradle file that had the following dependency defined:
如上所述:结果是 pushwoosh build.gradle 文件定义了以下依赖项:
dependencies {
...
compile 'com.facebook.react:react-native:0.20.1'
...
}
So i went ahead and changed it right on the spot to "+"
所以我继续当场把它改成“+”
dependencies {
...
compile 'com.facebook.react:react-native:+'
...
}
The "+" means:the latest available version where each of the leading SEMVER components match the pattern provided
“+”表示:最新的可用版本,其中每个领先的 SEMVER 组件都与所提供的模式相匹配