react-native常見錯誤整理

一、CFBundleIdentifier", Does Not Existjava

sudo rm -rf node_modules && rm -rf ~/.rncache && yarnnode

sudo npm install -g react-native-git-upgradereact

 

 

二、Build input file cannot be found: 'xxx/third-party/double-conversion-1.1.5/'…android

新建的React  Native 項目,凡是版本號大於0.45的,iOS版本在build的時候會卡在build double conversion 這裏。ios

 

三、third-party/glog-0.3.4/src/base/mutex.h:105:10: 'config.h' file not foundgit

cd node_modules/react-native/third-party/glog-0.3.4 github

../../scripts/ios-configure-glog.shweb

 

四、React Native version mismatchnpm

 

 

watchman watch-del-all && react-native start --reset-cachereact-native

This might be related to https://github.com/facebook/react-native/issues/4968

To resolve try the following:

  1. Clear watchman watches: `watchman watch-del-all`.

  2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`.

  3. Reset Metro Bundler cache: `rm -fr $TMPDIR/react-*` or `npm start -- --reset-cache`.

 

 

五、權限問題

chown -R wood:staff ~/.npm 讓該目錄下的全部文件都有wood用戶權限

groups 查看當前用戶所屬組

groups wood 查看指定用戶所屬組

id -a wood 能夠查到指定用戶所屬組更詳細的信息

whoami 查看當前用戶的用戶名

 

六、Execution failed for task ':app:installDebug'.

at com.android.ddmlib.Device.installPackage(Device.java:869)

> com.android.builder.testing.api.DeviceException: com.android.ddmlib.InstallException: INSTALL_FAILED_INSUFFICIENT_STORAGE

at com.android.builder.testing.ConnectedDevice.installPackage

是由於包名和簽名的信息與原先手機上的衝突,最簡單的方式,經過adb uninstall packageName將原來的App卸載乾淨,從新執行就能夠成功了

 

七、Attempted to transition from state RESPONDER_INACTIVE_PRESS_IN to RESPONDER_ACTIVE_LONG_PRESS_IN, which is not supported. This is most likely due to Touchable.longPressDelayTimeout not being cancelled.

https://github.com/facebook/react-native/issues/5823

這是開啓了Debug JS Remotely模式

 

八、React-Native判斷Debug/Release 

//Android Studio項目中
if(BuildConfig.DEBUG){
 
//debug模式
}
else{
 
//release模式
}

 

iOS項目判斷方法

#ifdef DEBUG
  
// debug模式
#else
   
//release 模式
#endif

 

React-Native項目判斷方法

if(__DEV__){
   
// debug模式
}
else{
   
// release模式
}

 

Android打包

build_apk.sh
#!/bin/bash
rm android/app/build/outputs/app-release.apk
cd android
./gradlew assembleRelease

 

 

react-native -> jsbundle,將RN代碼和資源文件打包成jsbundle放在app或apk中,這樣能夠不依賴於開發服務器在真機上運行

react-native bundle 命令

iOS項目: 

1. 工程目錄下執行

react-native bundle --platform ios --entry-file index.js --reset-cache --bundle-output ios/bundle/main.jsbundle --dev false --assets-dest ios/bundle/

2. 在xcode中添加bundle文件夾

3. 在AppDelegate.m中選擇使用main.jsbundle

jsCodeLocation = [[NSBundle mainBundle] URLForResource:@」index.ios」 withExtension:@」jsbundle」];

 

Android項目:

react-native bundle --platform android --entry-file index.js --reset-cache --bundle-output android/app/src/main/assets/index.android.bundle --dev false --assets-dest android/app/src/main/res/ 

 

九、Android生成已簽名的apk時構建時可能出現的錯誤

Duplicate resources 

請刪除drawable-xxx下面的資源文件,由於在打包apk官方文檔中有說明,Gradle 的assembleRelease參數會把全部用到的 JavaScript 代碼都打包到一塊兒,而後內置到 APK 包中。

因此打包apk前不用執行react-native bundle 命令來打出jsbundle,assembleRelease已經包含了這個功能,這回致使資源文件重複而報錯。

該刪的都刪掉吧!

 

十、Cannot set build number with plist path containing $(SRCROOT)

Please remove $(SRCROOT) in your Xcode target build settings

解決方法一: 

https://stackoverflow.com/questions/3730186/how-do-i-force-xcode-to-rebuild-the-info-plist-file-in-my-project-every-time-i-b

解決方法二:

模仿tvOS/Info.plist,去掉$(SRCROOT)改成相對路徑


 

十一、Android RCTWeChat接入注意事項

> Task :react-native-update:verifyReleaseResources FAILED

FAILURE: Build failed with an exception.


----------- 

debug/release運行沒問題,包括用Android Studio生成已簽名的apk也沒問題,可是自動化打包(./gradlew assembleRelease)失敗 


相似這種錯誤須要作如下修改:

保證app/build.gradle中的dependences與依賴工程中的一致,建議都改成implementation,能夠參考國外人寫的工程(這裏須要吐槽下騰訊)

settings.gradle把react-native-wechat 改成RCTWeChat

rootProject.name = 'superbuy'
include ':react-native-fetch-blob'
project(':react-native-fetch-blob').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fetch-blob/android')

include ':react-native-device-info'
project(':react-native-device-info').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-device-info/android')

include ':react-native-i18n'
project(':react-native-i18n').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-i18n/android')

include ':jcore-react-native'
project(':jcore-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jcore-react-native/android')

include ':jpush-react-native'
project(':jpush-react-native').projectDir = new File(rootProject.projectDir, '../node_modules/jpush-react-native/android')

include ':react-native-webview'
project(':react-native-webview').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-webview/android')

include ':react-native-gesture-handler'
project(':react-native-gesture-handler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-gesture-handler/android')

include ':RCTWeChat'
project(':RCTWeChat').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-wechat/android')

include ':react-native-video'
project(':react-native-video').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-video/android-exoplayer')

include ':react-native-vector-icons'
project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android')

include ':react-native-splash-screen'
project(':react-native-splash-screen').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-splash-screen/android')

include ':react-native-linear-gradient'
project(':react-native-linear-gradient').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-linear-gradient/android')

include ':react-native-image-picker'
project(':react-native-image-picker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-image-picker/android')

include ':react-native-update'
project(':react-native-update').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-update/android')

include ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')

include ':app' 

app/build.gradle 

compile是過時的用法要改成implementation,而且相應的依賴工程也須要改成implementation

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
entryFile: "index.js"
]

apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

android {
signingConfigs {
config {
keyAlias 'release'
keyPassword 'xxxxxx'
storeFile file('/Users/wood/Documents/xxx/android/keystores/release.keystore')
storePassword 'xxxxxx'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.xxx.xxx"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode hasProperty("VERSION_CODE") ? Integer.parseInt(VERSION_CODE) + 1 : 1
project.setProperty("VERSION_CODE", versionCode)
versionName hasProperty("VERSION_NAME") ? VERSION_NAME : "1.0.0"
manifestPlaceholders = [
JPUSH_APPKEY: "xxxxxxxxxxxxxxxxxxx",
APP_CHANNEL : "default"
]

}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
}
}
buildTypes {
release {
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
signingConfig signingConfigs.config
buildConfigField "String", "CODEPUSH_KEY", CODEPUSH_KEY_PRODUCTION
buildConfigField "boolean", "IS_CODEPUSH", "true"
}
debug {
applicationIdSuffix ".debug" // 在模擬器上同時安裝debug/release包
buildConfigField "String", "CODEPUSH_KEY", CODEPUSH_KEY_STAGING
buildConfigField "boolean", "IS_CODEPUSH", "true"
}
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
def versionCodes = ["armeabi-v7a":1, "x86":2, "arm64-v8a": 3, "x86_64": 4]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
}
}
}

dependencies {
implementation project(':react-native-update')
implementation project(':react-native-code-push')
implementation project(':react-native-fetch-blob')
implementation project(':react-native-update')
implementation project(':react-native-device-info')
implementation project(':react-native-i18n')
implementation project(':jcore-react-native')
implementation project(':jpush-react-native')
implementation project(':react-native-webview')
implementation project(':react-native-gesture-handler')
implementation project(':react-native-video')
implementation project(':react-native-vector-icons')
implementation project(':react-native-splash-screen')
implementation project(':react-native-linear-gradient')
implementation project(':react-native-image-picker')
// 微信不使用自動連接react-native-wechat
implementation project(':RCTWeChat')

implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
implementation "com.facebook.react:react-native:+" // From node_modules
// From node_modules
// 若是你須要支持GIF動圖
implementation 'com.facebook.fresco:animated-gif:1.10.0'
// 若是你須要支持WebP格式,包括WebP動圖
implementation 'com.facebook.fresco:animated-webp:1.10.0'
implementation 'com.facebook.fresco:webpsupport:1.10.0'
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
from configurations.compile
into 'libs'
}

依賴工程:

RCTWeChat/build.gradle

apply plugin: 'com.android.library'

def DEFAULT_COMPILE_SDK_VERSION = 28
def DEFAULT_BUILD_TOOLS_VERSION = "28.0.0"
def DEFAULT_TARGET_SDK_VERSION = 28

android {
// compileSdkVersion 23
// buildToolsVersion "23.0.1"
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
buildToolsVersion project.hasProperty('buildToolsVersion') ? project.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion 16
// targetSdkVersion 22
targetSdkVersion project.hasProperty('targetSdkVersion') ? project.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
}

allprojects {
repositories {
jcenter()
maven { url "$projectDir/../../react-native/android" }
}
}

dependencies {
implementation 'com.facebook.react:react-native:+' // compile -> implementation
compile files('libs/libammsdk.jar')
}

react-native-fetch-blob/build.gradle

apply plugin: 'com.android.library'

repositories {
mavenCentral()
}

buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
}

def DEFAULT_COMPILE_SDK_VERSION = 28
def DEFAULT_BUILD_TOOLS_VERSION = "28.0.0"
def DEFAULT_TARGET_SDK_VERSION = 28

android {
// compileSdkVersion 23
// buildToolsVersion "23.0.1"
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
buildToolsVersion project.hasProperty('buildToolsVersion') ? project.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion 16
// targetSdkVersion 23
targetSdkVersion project.hasProperty('targetSdkVersion') ? project.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}

dependencies {
implementation 'com.facebook.react:react-native:+' // compile -> implementation
//{RNFetchBlob_PRE_0.28_DEPDENDENCY}
}

react-native-i18n/build.gradle

dependencies {
implementation "com.facebook.react:react-native:+" // compile -> implementation

 

關於這個問題,其餘的三方庫均可以按照這個規格修改,本質都是構建版本不一致問題。 


戳這裏下載這都是修改好的,直接拿去用吧 

-----------

 

十二、iOS  RCTWeChat接入注意事項


 

 

1三、iOS Cannot set build number with plist path containing $(SRCROOT) Please remove $(SRCROOT) in your Xcode target build settings

Please remove $(SRCROOT) in your Xcode target build settings

fastlane 沒法識別出 $(SRCROOT)的路徑,移除 $(SRCROOT)/ xcode仍是會找到Info.plist文件,如何按照提示只移除$(SRCROOT)那確實是找不到的


https://stackoverflow.com/questions/49429944/cannot-find-srcroot-agvtool-increment-build-number-fastlane

The current solution to your problem would be to remove the $(SRCROOT) from your build settings. $(SRCROOT) means "directory where .xcodeproj is" so you will be perfectly fine to remove it (Xcode will still look for it relatively from the .xcodeproj).

We've recently removed agvtool from the get_version_number action (in version 2.87.0 in favor of using the xcodeproj gem where we can more nicely handle which target is found, handle $(SRCROOT), and remove that "avgtool setup process". I will be working on replacing agvtool in increment_build_number and get_build_number soon which should hopefully prevent further issues like this one.

 

 

1四、 Error:The minSdk version should not be declared in the android manifest file

Error:The minSdk version should not be declared in the android manifest file. You can move the version from the manifest to the defaultConfig in the build.gradle file.

解決方案 把manifest.xml 裏的sdk最小版本設置都刪掉 Androidstudio3.0 之後 不能在manifest.xml 設置這些 必須在 gradle裏設置


 

 

1五、 Android mnifest file merging error tool:allowBackup=true

for attribute android:allowBackup, but no new value specified

這種相似問題,都是mainfest文件合併失敗,建議全部mainfest.xml都加上

<application
   
android:allowBackup="true"
   
tools:replace="android:allowBackup">

</application>

 

 

1六、 Android怎麼編譯都不經過 gradle.properties版本變動或

修改或更新了distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-all.zip

~/.gradle 下面會有緩存,而因爲刪除了緩存文件致使找不到,Extenal Libraries會大片的飄紅,解決方法是先移除這些依賴,再從新編譯(這個問題影響了我好多天fuck)

 

 

 

1七、 設置gradle阿里雲鏡像 

新增文件:~/.gradle/init.gradle (全局代理一次性解決全部項目gradle被牆問題)

allprojects{
repositories {
def ALIYUN_REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public'
def ALIYUN_JCENTER_URL = 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
all { ArtifactRepository repo ->
if(repo instanceof MavenArtifactRepository){
def url = repo.url.toString()
if (url.startsWith('https://repo1.maven.org/maven2')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
remove repo
}
if (url.startsWith('https://jcenter.bintray.com/')) {
project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
remove repo
}
}
}
maven {
url ALIYUN_REPOSITORY_URL
url ALIYUN_JCENTER_URL } }}
相關文章
相關標籤/搜索