初学RN,跑公司老项目都跑不起来,QAQ哭,如果你是和我一样,新建项目是可以跑的,运行一切正常,但是想运行老项目出现了Looks like you installed react-native globally, maybe you meant react-native-cli? 底下提示
npm uninstall -g react-native
npm install -g react-native-cli
这种情况的,此篇文章可能可以帮到你,但是会更改原来代码的配置,建议咨询大佬,如果你是喜欢瞎折腾的,可以继续看下去。因为上面的提示,所以我就跑这两个命令跑了十几遍,但是每次react-native run-android还是会出现上面的提示,搜索的话也没搜到。下面这篇文章给了我启发
https://segmentfault.com/a/1190000004278414?utm_source=tag-newest
官方文档要求全局安装react-native-cli,但是局部安装react-native,那么会不会是该项目局部安装的react-native,在电脑上是没有的,我们替换成有的就行了。看了下老项目的配置
"dependencies": {
"antd-mobile": "^2.0.2",
"dva": "^2.0.3",
"native-echarts": "^0.3.0",
"react": "16.0.0-alpha.6",
"react-dom": "^16.0.0",
"react-native": "0.44.3",
"react-native-input-scroll-view": "^1.6.0",
"react-native-linear-gradient": "^2.3.0",
"react-native-vector-icons": "^4.4.2",
"react-navigation": "^1.0.0-beta.13"
},
"devDependencies": {
"babel-jest": "21.2.0",
"babel-plugin-import": "^1.6.2",
"babel-preset-react-native": "4.0.0",
"jest": "21.2.1",
"react-test-renderer": "16.0.0-alpha.6"
}
这个是老项目的配置,对比了我新建的项目,的确有很多看着挺老的了,接下来就是升级,我是用webstorm点开Setting可以直接升级配置
File => Settings=> Languages & Frameworks => Node.js and NPM
最好不要单独升级react-native能升的就升级吧,因为,单独升级了react-native,他的android的依赖,和compileSdkVersion都是会变化的,比如升级后用的v7的包是28.0.0然而其他未升级的,react-native-vector-icons里面用的v7的包是26+的。会冲突。应该也可以直接在package.json直接修改版本号。底下是我修改后的:
"dependencies": {
"antd-mobile": "2.2.11",
"dva": "2.4.1",
"native-echarts": "^0.3.0",
"react": "16.8.6",
"react-dom": "16.8.6",
"react-native": "0.58.6",
"react-native-input-scroll-view": "^1.6.0",
"react-native-linear-gradient": "2.5.4",
"react-native-vector-icons": "6.4.2",
"react-navigation": "3.7.1"
},
"devDependencies": {
"babel-jest": "24.7.1",
"babel-plugin-import": "1.11.0",
"babel-preset-react-native": "4.0.1",
"jest": "24.7.1",
"react-test-renderer": "16.8.6"
},
"jest": {
"preset": "react-native"
}
对于写过android原生的来说,接下来就好改了,升级版本库一般都要修改一些东西,classpath,gradle,v7,compileversion,等等。
项目下android\gradle\wrapper\gradle-wrapper.propertiesapp把gradle改成可用已有的版本,能节省大量时间
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
我的是4.1,你的可以看看新建的项目里是什么
项目下的android\build.gradle里的classpath也要更改成对应的
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
修改项目下android\app\build.gradle里的,compileSdkVersion和buildToolsVersion以及targetSdkVersion,然后是依赖里的v7包版本号改成28.0.0,把compile改成implementation
android {
compileSdkVersion 28
buildToolsVersion "28.0.2"
defaultConfig {
applicationId "com.gngcapp"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
....
}
dependencies {
implementation project(':react-native-linear-gradient')
implementation project(':react-native-vector-icons')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:28.0.0"
implementation "com.facebook.react:react-native:+" // From node_modules
}
诶,还是改原生的舒服,RN学的我有点头疼,哦对在项目下android\build.gradle里的allprojects添加google仓库,会跑安卓原生的,用android studio编译运行的话,就会有添加提示,假如不添加google仓库跑起来就是
Could not resolve all files for configuration ':app:debugCompileClasspath'.
> Could not find com.android.support:appcompat-v7:28.0.0.
Searched in the following locations:
file:/D:/ASSDK/extras/m2repository/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.pom
file:/D:/ASSDK/extras/m2repository/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.jar
file:/D:/ASSDK/extras/google/m2repository/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.pom
file:/D:/ASSDK/extras/google/m2repository/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.jar
file:/D:/ASSDK/extras/android/m2repository/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.pom
file:/D:/ASSDK/extras/android/m2repository/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.jar
file:/C:/Users/ZhuHui/.m2/repository/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.pom
file:/C:/Users/ZhuHui/.m2/repository/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.jar
https://jcenter.bintray.com/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.pom
https://jcenter.bintray.com/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.jar
file:/H:/gngcApp/node_modules/react-native/android/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.pom
file:/H:/gngcApp/node_modules/react-native/android/com/android/support/appcompat-v7/28.0.0/appcompat-v7-28.0.0.jar
会提示无法找到28.0.0的v7包,底下是增加的
allprojects {
repositories {
mavenLocal()
jcenter()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
google()
}
}
这样,我的老项目就在安卓手机里跑起来了,嗯,虽然是红色的,连接不到服务器,需要改下ip地址和端口,这个搜一下网上很多