react-native的各種坑

一,Could not resolve all files for configuration ':app:releaseCompileClasspath

react-native版本:0.57.1

這個問題原本不是rn版本的問題,原因是0.57.1將Android SDK的版本更新到27了。

先來看下錯誤日誌: 
> Task :app:preReleaseBuild FAILED
 
FAILURE: Build failed with an exception.
 
* What went wrong:
Could not resolve all files for configuration ':app:releaseCompileClasspath'.
> Could not find support-vector-drawable.aar (com.android.support:support-vector-drawable:27.1.1).
  Searched in the following locations:
      https://jcenter.bintray.com/com/android/support/support-vector-drawable/27.1.1/support-vector-drawable-27.1.1.aar
> Could not find livedata-core.aar (android.arch.lifecycle:livedata-core:1.1.0).
  Searched in the following locations:
      https://jcenter.bintray.com/android/arch/lifecycle/livedata-core/1.1.0/livedata-core-1.1.0.aar
> Could not find viewmodel.aar (android.arch.lifecycle:viewmodel:1.1.0).
  Searched in the following locations:
      https://jcenter.bintray.com/android/arch/lifecycle/viewmodel/1.1.0/viewmodel-1.1.0.aar
> Could not find runtime.aar (android.arch.core:runtime:1.1.0).
  Searched in the following locations:
      https://jcenter.bintray.com/android/arch/core/runtime/1.1.0/runtime-1.1.0.aar
 
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
 
* Get more help at https://help.gradle.org
 
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.1/userguide/command_line_interface.html#sec:command_line_warnings
 
BUILD FAILED in 14s
11 actionable tasks: 1 executed, 10 up-to-date
這個是隻有打包apk時纔會出現的錯誤,需要注意兩個地方來確定你的錯誤和我遇到的是同一類錯誤:

1.":app:releaseCompileClasspath";

2."com.android.support:support-vector-drawable:27.1.1"。

解決方案: 
1.修改android/build.gradle中google()方法的位置,見下方代碼註釋;

2.如果還不行,就升級gradle版本到4.10.1(我使用的版本,不確定別的版本是否可用)。

// Top-level build file where you can add configuration options common to all sub-projects/modules.
 
buildscript {
    ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 16
        compileSdkVersion = 27
        targetSdkVersion = 26
        supportLibVersion = "27.1.1"
    }
    repositories {
        google()
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
 
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
 
allprojects {
    repositories {
        google() // new
        mavenLocal()
        jcenter()
        maven {url "https://jitpack.io"}
        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"}
        // google() // old
    }
}
 
 
task wrapper(type: Wrapper) {
    gradleVersion = '4.10.1'
    distributionUrl = distributionUrl.replace("bin", "all")
}

 


 二,常見錯誤 Could not install the app on the device

 

1.新clone的項目,運行之後報這個錯誤

解決辦法:

在項目根目錄下執行以下命令:

命令:chmod 755 android/gradlew

 

三,Could not resolve all files for configuration ':classpath'.問題解決

今天,使用Android Studio 3.0.1 打開下載的工程項目,build project時結果報錯,如下:

Error:A problem occurred configuring root project 'apptest'.
> Could not resolve all files for configuration ':classpath'.
   > Could not resolve com.android.tools.build:gradle:3.0.0.
     Required by:
         project :
      > Could not resolve com.android.tools.build:gradle:3.0.0.
         > Could not get resource 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.0.0/gradle-3.0.0.pom'.
            > Could not GET 'https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.0.0/gradle-3.0.0.pom'.
               > sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException:unable to find valid certification path to requested target

解決辦法爲將com.android.tools.build:gradle的版本號和Android studio的版本號一致。

注意:如何查看android studio版本:菜單欄 > Help > About  即可查看

四,react-native run-android Build failed報錯(:app:processDebugManifest FAILED)

命令行輸入react-native run-android 的時候build失敗,如下所示:

 :app:processDebugManifest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute application@allowBackup value=(false) from AndroidManifest.xml:12:7-34
        is also present at [AwesomeProject:react-native-splash-screen:unspecified] AndroidManifest.xml:12:9-35 value=(true).
        Suggestion: add 'tools:replace="android:allowBackup"' to <application> element at AndroidManifest.xml:8:5-25:19 to override.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

解決辦法:

一、方法一

仔細看上面報錯日誌可以看到給的建議,“【Suggestion: add 'tools:replace="android:allowBackup"' to <application> element at AndroidManifest.xml:8:5-25:19 to override.】”


打開上圖所示路徑下的AndroidMainfest.xml文件,在manifest的屬性中添加“ xmlns:tools="http://schemas.android.com/tools"”,如下所示:

  • 添加前
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.awesomeproject">
  • 添加後:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.awesomeproject">

然後在<application></application>上添加“tools:replace="android:allowBackup"”,如下所示:

  • 添加前:
<application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme">其他代碼</application>
  • 添加後:
<application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      tools:replace="android:allowBackup"
      android:theme="@style/AppTheme">其他代碼</application>

二、方法二

同樣是在AndroidMainfest.xml中修改,只需要修改<application></application>中的“android:allowBackup="false"”爲“android:allowBackup="true"”就好

  • 修改前:
<application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme">其他代碼</application>
  • 修改後:
<application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="true"
      android:theme="@style/AppTheme">其他代碼</application>

五,使用React-native link的背景

並不是所有的 APP 都需要使用全部的原生功能,包含支持全部特性的代碼會增大應用的體積。但我們仍然希望能讓你簡單地根據自己的需求添加需要的特性。
在這種思想下,我們把許多特性都發布成爲互不相關的靜態庫。
大部分的庫只需要拖進兩個文件就可以使用了,偶爾你還需要幾步額外的工作,但不會再有更多的事情要做了。
我們隨着 React Native 發佈的所有庫都在倉庫中的Libraries文件夾下。其中有一些是純 Javascript 代碼,你只需要去import它們就可以使用了。另外有一些庫基於一些原生代碼實現,你必須把這些文件添加到你的應用,否則應用會在你使用這些庫的時候產生報錯。

2.具體使用React-native link的步驟:
(1).下載某個庫到本地

npm install ******
1
(2).鏈接某個庫到項目中

react-native link *****
1
(3).到此就成功的鏈接到ios和android項目中了


六sdk.dir sdk路徑配置

problem occurred configuring project ':app'.
SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment

在這裏插入圖片描述

七,開發項目中的問題

 

絕對定位元素被鍵盤頂起

打開android工程,在AndroidManifest.xml中配置如下:
 android:windowSoftInputMode=“stateAlwaysHidden|adjustPan”
1
StatusBar 主題污染
componentDidMount() {
    this._navListener = this.props.navigation.addListener('didFocus', () => {
      StatusBar.setBarStyle('dark-content');
    });
  }
  componentWillUnmount() {
    this._navListener.remove();
  }

React Native Text截斷
顯示數字加粗後最後一位無法顯示問題,添加
fontFamily: 'System'
使用Button進行路由跳轉時報錯

這個錯誤一般只在開啓Debug JS Remotely時出現,可以通過使用TouchableOpacity包裹或替換解決

to be continue

八,AndroidStudio SSL peer shut down incorrectly 問題

AndroidStudio 編譯時出現如下問題 SSL peer shut down incorrectly 或者某些jar包下載不下來,一般是因爲牆的原因導致的。這時候我們就需要配置鏡像來解決這個問題。(爲了提高jar包的下載速度也可以配置)配置的方法就是在根build.gradle中添加鏡像倉庫,一般我們選擇阿里的 http://maven.aliyun.com/nexus/content/groups/public/完整的如下所示

buildscript {

    repositories {
        google()
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
    }
}

allprojects {
    repositories {
        google()
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

這裏需要注意要將jcenter放到最後一個,因爲他就是那個下載慢,或者報錯的罪魁禍首


 

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