记录运行一个react-native遇到的问题

版本信息

遇到的问题 iOS

1. (已解决)iOS执行 pod install 报错 None of your spec sources contain a spec satisfying the dependency: React/Core.
Analyzing dependencies
Fetching podspec for `DoubleConversion` from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`
Fetching podspec for `Folly` from `../node_modules/react-native/third-party-podspecs/Folly.podspec`
Fetching podspec for `glog` from `../node_modules/react-native/third-party-podspecs/glog.podspec`
[!] CocoaPods could not find compatible versions for pod "React/Core":
  In Podfile:
    RNImageCropPicker (from `../node_modules/react-native-image-crop-picker`) was resolved to 0.21.3, which depends on
      React/Core

None of your spec sources contain a spec satisfying the dependency: `React/Core`.

You have either:
 * out-of-date source repos which you can update with `pod repo update` or with `pod install --repo-update`.
 * mistyped the name or version.
 * not added the source repo that hosts the Podspec to your Podfile.
//项目根目录执行以下命令
grep -rl "s.dependency 'React/Core'" node_modules/ | xargs sed -i '' 's=React/Core=React-Core=g'

执行完成没有任何提示,之后继续

cd iOS
pod install
2. (已解决)执行pod install 报错
[!] Error installing Folly
[!] /usr/bin/git clone https://github.com/facebook/folly.git /var/folders/q6/nwts2ggj4j709spst9qb73s40000gp/T/d20221124-11934-1xhyz8b --template= --single-branch --depth 1 --branch v2020.01.13.00

Cloning into '/var/folders/q6/nwts2ggj4j709spst9qb73s40000gp/T/d20221124-11934-1xhyz8b'...
fatal: unable to access 'https://github.com/facebook/folly.git/': HTTP/2 stream 1 was not closed cleanly before end of the underlying stream

多次执行报不同错误

[!] Error installing Folly
[!] /usr/bin/git clone https://github.com/facebook/folly.git /var/folders/q6/nwts2ggj4j709spst9qb73s40000gp/T/d20221124-12508-1r35ys8 --template= --single-branch --depth 1 --branch v2020.01.13.00

Cloning into '/var/folders/q6/nwts2ggj4j709spst9qb73s40000gp/T/d20221124-12508-1r35ys8'...
fatal: unable to access 'https://github.com/facebook/folly.git/': Failed to connect to github.com port 443 after 40648 ms: Operation timed out

感觉是网络问题,怎么解决不知道,可能是需要换源地址,可能是需要添加 hosts github的ip。就是一直重复执行

//一直报错一直执行,一路爆红终于安装完成
pod install
3. (已解决)报错 library not found ljanalytics
ld: library not found for -ljanalytics-ios-2.1.2
clang: error: linker command failed with exit code 1 (use -v to see invocation)

需要把
/node_modules/janalytics-react-native/ios/RCTJAnalyticsModule/janalytics-ios-2.1.2.a
文件名修改为libjanalytics-ios-2.1.2.a

4. (已解决)node报错
node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: EMFILE: too many open files, watch
    at FSWatcher._handle.onchange (node:internal/fs/watchers:204:21)
Emitted 'error' event on NodeWatcher instance at:
    at NodeWatcher.checkedEmitError (/Users/***/project/***/node_modules/sane/src/node_watcher.js:143:12)
    at FSWatcher.emit (node:events:513:28)
    at FSWatcher._handle.onchange (node:internal/fs/watchers:210:12) {
  errno: -24,
  syscall: 'watch',
  code: 'EMFILE',
  filename: null
}

尝试1:网上资料很多都是说端口占用,试过没有效果
1、查看全部端口
sudo lsof -i
2、查看端口监听情况
sudo lsof -i -P | grep -i "listen"
3、查看特定端口
sudo lsof -i:2425
4、查询到之后通过PID进行kill
kill PID
尝试2:

解决办法: 未按官方文档要求安装watchman

brew install watchman
5. (已解决)iOS图片不能加载

应用在IOS 14.0下调试无法显示图片
解决办法:
修改文件RCTUIImageViewAnimated.m 270行左右
路径如:/node_modules/react-native/Libraries/Image 下文件

- (void)displayLayer:(CALayer *)layer
{
  if (_currentFrame) {
    layer.contentsScale = self.animatedImageScale;
    layer.contents = (__bridge id)_currentFrame.CGImage;
  }
}

改成下面:

- (void)displayLayer:(CALayer *)layer
{
  if (_currentFrame) {
    layer.contentsScale = self.animatedImageScale;
    layer.contents = (__bridge id)_currentFrame.CGImage;
  }else{
      [super displayLayer:layer];
  }
}
6. (已解决)iOS选择图片上传报错,Domain=RCTErrorDomain Code=0 "Invalid request token

react-native库报错,使用patch-package这个库可以解决修改本地库造成的团队协作问题

解决办法:
1、修改react-native库

//文件位置
that exists in node_modules/react-native/Libraries/Image/RCTImageLoader.mm

//替换一下方法所有内容
 -(id)sendRequest:(NSURLRequest *)request withDelegate: 

修改成一下内容

- (id)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
{
    @synchronized(self) {
       __block RCTImageLoaderCancellationBlock requestToken = ^{};
       requestToken = [self loadImageWithURLRequest:request callback:^(NSError *error, UIImage *image) {
         @synchronized(self) {

           if (error) {
             [delegate URLRequest:requestToken didCompleteWithError:error];
             return;
           }


        NSString *mimeType = nil;
        NSData *imageData = nil;
        if (RCTImageHasAlpha(image.CGImage)) {
          mimeType = @"image/png";
          imageData = UIImagePNGRepresentation(image);
        } else {
          mimeType = @"image/jpeg";
          imageData = UIImageJPEGRepresentation(image, 1.0);
        }
   NSURLResponse *response = [[NSURLResponse alloc] initWithURL:request.URL
                                                            MIMEType:mimeType
                                               expectedContentLength:imageData.length
                                                    textEncodingName:nil];


             [delegate URLRequest:requestToken didReceiveResponse:response];
                   [delegate URLRequest:requestToken didReceiveData:imageData];
                   [delegate URLRequest:requestToken didCompleteWithError:nil];
                 }
               }];

      return requestToken;
  }
}
  1. 安装patch-package
    安装比较慢,需替换源地址
//查看npm源
npm get registry
//替换淘宝源
npm config set registry http://registry.npm.taobao.org

遇到的问题 Android

1. 环境问题jdk环境变量配置问题,这个问题是环境变量配置和环境不熟悉
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

Exception: Gradle task assembleDebug failed with exit code 1

安装了flutter并且是正常运行Android的,于是忽略了。重新安装就好了

@felixdeMacBook-Pro ~ % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.8, on macOS 12.6 21G115 darwin-x64, locale
    zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 14.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2021.3)
[✓] VS Code (version 1.73.1)
[✓] Connected device (3 available)
[✓] HTTP Host Availability

• No issues found!
@felixdeMacBook-Pro ~ % which java
/usr/bin/java
@felixdeMacBook-Pro ~ % which javac
/usr/bin/javac
@felixdeMacBook-Pro ~ % 
@felixdeMacBook-Pro ~ % java -version
The operation couldn’t be completed. Unable to locate a Java Runtime.
Please visit http://www.java.com for information on installing Java.

下载jdk 完成后直接安装

@felixdeMacBook-Pro ~ % java -version
java version "19.0.1" 2022-10-18
Java(TM) SE Runtime Environment (build 19.0.1+10-21)
Java HotSpot(TM) 64-Bit Server VM (build 19.0.1+10-21, mixed mode, sharing)
2. (已解决)gradle版本过低,改成7.4重新运行解决问题
Configuration on demand is an incubating feature.

FAILURE: Build failed with an exception.

* What went wrong:
Could not open settings generic class cache for settings file '/Users/***/project/***/android/settings.gradle' (/Users/***/.gradle/caches/7.4/scripts/b7pnedn7pgzpudl06hpdmqbc1).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 63

* 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
3. (已解决)jdk版本过高,安装的是19,报错卸载重新安装11运行成功

jdk官网下载太慢,这里用的是华为源
需要进入JavaVirtualMachines目录

//cd根目录
cd /
cd Library
cd Java
cd JavaVirtualMachines
//查看所有版本
ls
//删除
sudo rm -rf 需要删除的版本
Configuration on demand is an incubating feature.

FAILURE: Build failed with an exception.

* What went wrong:
Could not open settings generic class cache for settings file '/Users/***/project/tour-guide-app/android/settings.gradle' (/Users/***/.gradle/caches/7.4/scripts/b7pnedn7pgzpudl06hpdmqbc1).
> BUG! exception in phase 'semantic analysis' in source unit '_BuildScript_' Unsupported class file major version 63

* 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
4. (已解决)of type 'class java.lang.String' as boolean. Expected 'true' or 'false'.

这个问题是gradle.properties文件android.useAndroidX=true后面无输入空格,查看报错地方是否有空格,删除多余空格即可

FAILURE: Build failed with an exception.

* Where:
Build file '/Users/******/project/tour-guide-app/android/app/build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin 'com.android.internal.version-check'.
   > Cannot parse project property android.useAndroidX='true             ' of type 'class java.lang.String' as boolean. Expected 'true' or 'false'.

* 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
5. (未解决)ReactNative libc++_shared.so 冲突导致的uncaught exception of type std::bad_cast:

这个问题场景是百度地图插件,更新到1.0.37作者已经没有维护了,我碰得到问题是部分Android手机打开地图会闪退,具体的原因是jar包网络监听方法报错。

解决方法:
1.尝试失败,首先想到的是更新原生Android百度地图SDK,但是会报动态库编译libc++_shared.so冲突,packagingOptions 排除'lib/x86/libc++_shared.so' 会报如题错误。查资料一般都是重新编译三方库,知识盲区只能放弃了。
2.根据错误修改现在代码,定位报错信息发现是BaiduLBS_Android.jar网络监听报错,看到这大概有了思路,简单粗暴直接把相关的方法干掉。于是就有了如何修改jar包单个class文件,亲历可解决部分问题,步骤上面方法已经有了,这里说下注意的地方:
注意的地方:

//修改后的java打包成class文件,
javac  -classpath android/app/src/e.java

这个有个坑,一般java文件都会引入Android.jar或者其他的文件,直接转会报找不到文件,需要将相关的jar包放到同级,多个jar包可以用: 号连接,这里是mac的方法,Windows好像是;分号,没有试过

javac -classpath android.jar:BaiduLBS_Android.jar e.java

完整步骤:

1.解析jar文件成class文件
jar xf BaiduLBS_Android.jar

2.使用工具或vscode安装插件,将class文件解析成java可读性强的文件,并打包成class
javac -classpath android.jar:BaiduLBS_Android.jar e.java

3.替换修改的class,并将所有class文件打包成jar文件,cd 到class文件目录并执行
jar -cvf BaiduLBS_Android.jar ./

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,776评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,527评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,361评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,430评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,511评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,544评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,561评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,315评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,763评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,070评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,235评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,911评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,554评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,173评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,424评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,106评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,103评论 2 352

推荐阅读更多精彩内容