React-Native:Looks like you installed react-native globally, maybe you meant react-native-cli?

初學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地址和端口,這個搜一下網上很多

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章