下面是一些rn问题的分类:
1.rn的版本问题,如0.55.6版本就是这样的,安装的时候就会报错,
2.link组件的问题,有些时候会发生自动link不全或者路径错误的情况,
3.安卓版本问题,组件的版本和rn项目版本统一
4.其他问题,
其实我建议还是学一下android原生,包括gradle,不然后面涉及太多原生问题很难解决。
rn版本更新的很快,有些版本说实话根本用不了,比如0.55.6,都启动不了,
我推荐几个我用过的版本,** 0.55.4 , 0.57.0**。 这两个相对来说还算稳定。
link的时候很多时候会出现不全的问题,这也是很多人害怕的问题,尤其一开始做这个项目的时候,项目写的好好的一link就出错,网上半天找不到解决方案。其实主要把link这三个地方注意些全就基本没问题了。以react-native-baidu-map组件为例
rootProject.name = 'apps'
include ':react-native-baidu-map'
project(':react-native-baidu-map').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-baidu-map/android');
include ':app';
dependencies {
implementation project(':react-native-baidu-map')
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new BaiduMapPackage(getApplicationContext()),
);
}
这是个大问题,我现在用的androidsdk28的,rn一开始建议的是23,其实这个已经是很老的版本的,很多组件都是在新版本上构建的
如果组件里面的版本大多不统一,那么最好就全部改成最高的版本,android向下兼容一般改成高版本没什么大问题,
有一个经常遇到的一个语法问题,在link的时候低版本 implementation要改成compile,
dependencies {
implementation project(':react-native-baidu-map')
}
其他问题么就不多说了,各种各样的都有,
接下来就说下其他的报错案例
4.bundlin failed: error:unable to resolve module 'AccessibilityInfo' form '...node_modules/...react-native-implementation.js':Module' AccessibilityInfo does not exist in the Hast..
解决方法: 这个是0.56的版本问题,换成0.55.4版本就行了
import PropTypes from 'prop-types';
把所有react-native-baidu-map里面的js文件都改掉,同时其android/src/main/java/org/lovebing/reactnative/baidumap/BiduMapPackage.java里面注释掉@Override如果还有问题,就找到BaiduMapViewManager.java 文件,注释掉里面含public boolean的@Override
项目的包名必须与key申请的包名一致
注意,只有在 .android 目录下生产的秘钥签名才是百度地图生产key所需要的秘钥
而不是打包的时候在java jdk目录下生产的秘钥
react-native-camera 集成
按照网上配置好集成环境,然后再按照
https://github.com/react-native-community/react-native-camera/issues/1530
文件修改下配置,
最后只能用RNCamera, 不能用Camera组件
<meta-data android:name="android.max_aspect" android:value="2.2"/>
java.nio.file.DirectoryNotEmptyException: D:\work\2018\08-03\water\App\apps\android\app\build\generated\source\buildConfig\release\com
BUILD FAILED in 2s
11 actionable tasks: 1 executed, 10 up-to-date**
要重新清理下文件, ./gradlew clean
Could not find method google() for arguments [] on repository container.**
解决办法: gradle-wrapper.properties 里面的distributionUrl = https://services.gradle.org/distributions/gradle-4.1-all.zip
Android dependency 'com.android.support:support-fragment' has different version for the compile (26.1.0) and runtime (27.1.1) classpath. You should manually set the same version via DependencyResolution**
解决办法: 我所用的没有support-fragment这个依赖,所以应该是build冲突,清除build文件,./gradlew clean后就好了
解决办法删了他吧react-native-fast-image ,换个版本吧,最新版本有问题啊,我之前用的5.1.0
网的问题,下载不到distributionUrl=https://services.gradle.org/distributions/gradle-4.10.2-all.zip文件,可以陪下镜像阿里云
buildscript {
repositories {
google()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
jcenter()
}
}
allprojects {
repositories {
mavenLocal()
google()
maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
jcenter()
maven {
url "$rootDir/../node_modules/react-native/android"
}
}
}
配置了这个也没有,翻墙也下载不下来的话去官网下载吧,
gradle地址
./index
from \node_modules\react-native\scripts/.
: The module ./index
could not be found from \node_modules\react-native\scripts/.
. Indeed, none of these files exist:android {
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 28
multiDexEnabled true
}
...
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
添加这个后项目高版本手机可以运行了,但是低版本的android就会crash,起都起不来,会报错主类加载不到。我用到的是4.4.4版本的手机,文档写的是android5.0之前的手机不行。然后解决办法是, 主类继承MultiDexApplication ,而不是继承Application类,文档里面还写了引用的时候换成android:name="android.support.multidex.MultiDexApplication" ,但是换了后却是无法使用,所有我这里没换,是可以使用的;还有问题可以参照连接,仔细看下。
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
里面添加
<item name="android:windowTranslucentStatus">true</item>
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html**
这个问题是android引用第三方库方法超过了64k大小的限制,
解决办法: 在build.gradle的 defaultConfig里面添加 multiDexEnabled true 设置下就行了,
但是设置后又会出现低版本不兼容的情况,我的是4.4.4版本的android,问题如下
// \android\app\build.gradle
implementation 'com.android.support:support-v4:28.0.0'
然后在 main/java/xxx 目录下的 MainApplication.java 里面修改MainApplication的继承类
import android.support.multidex.MultiDexApplication;
public class MainApplication extends MultiDexApplication implements ReactApplication {
...
}
// 之前是这样的
//public class MainApplication extends Application implements ReactApplication {
//...
// }
27.rn使用StatusBar无效,正常来说不会无效的,我按照其他的的方法设置了
//在 android\app\src\main\res\values\styles.xml 里面
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- <item name="android:windowTranslucentStatus">true</item> -->
</style>
这个的意思其实是设置沉浸栏的状态,是否是半透明的,设置了这个windowTranslucentStatus就会导致StatusBar无效,网上的其他人的方法谨慎参考啊
allprojects {
repositories {
mavenLocal()
google()
jcenter()
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://jitpack.io"
}
maven{
url 'https://maven.google.com/'
name 'Google'
}
}
}
29 skipping devices 'hc70' for 'app"debug' :unkonwn api level
可以先撤销usb调试,在重新连接usb,
还不行需要把允许未知的app应用安装开启,还有usb应用安装功能打开
defaultConfig{
ndk{ "x86","armeabi-v7a"}
}
可以查一下手机的cpu型号,然后根据型号选择ndk库,或者这里取消限制,注释掉,
这里改好后就在as上面起来了,然后又去react-native run-android就没有一开始的错误了。
关于so库的文章可以看这篇
so库详情
32 ** 关于阿里源的配置**
很多时候我们会遇到下载不下来jar包的情况,这时候要看清是哪个jar包下不下来,
比如这个报错 could not get 'https://jcenter.bintray.com/org/apache/...';
这个 jcenter 是国外的地址下载不到,可以配置下阿里源对应的maven 的jcenter仓库
buildscript {
repositories {
maven {
url "https://maven.aliyun.com/repository/jcenter"
}
google()
jcenter()
}
}
allprojects {
repositories {
maven {
url "https://maven.aliyun.com/repository/jcenter"
}
google()
jcenter()
}
}
其他源同理
阿里源maven仓库地址
android.useAndroidX=true
android.enableJetifier=true
在gradle里面添加
implementation 'androidx.annotation:annotation:+'
重新build,如果as没有自动把项目 android.support.annotation.NonNull 替换成 androidx.annotation.NonNull 手动替换下,在build就可以了
其他地市android.support.xxx 都需要换成androidx的。