React Native采坑总结

React Native:0.62.2

1.页面上渲染很多个<TextInput></TextInput>刷新很多次时,报错:

Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.new Drawable(android.content.res.Resources)'  on a null object reference

解决方案:在android/app/src/main/res/values/styles.xml 文件中加上下面的配置

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        ...
       <item name="android:editTextBackground">@android:color/transparent</item> //新加的
        ...
    </style>

</resources>

2.react-native 0.62.2版本打包出错:

[drawable-mdpi-v4/assets_mario] /Users/jeffreyrajan/Tutorials/RN/errorCheck/android/app/src/main/res/drawable-mdpi/assets_mario.png	[drawable-mdpi-v4/assets_mario] /Users/jeffreyrajan/Tutorials/RN/errorCheck/android/app/build/generated/res/react/release/drawable-mdpi-v4/assets_mario.png: Error: Duplicate resources
:app:mergeReleaseResources FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeReleaseResources'.
> [drawable-mdpi-v4/assets_mario] /Users/jeffreyrajan/Tutorials/RN/errorCheck/android/app/src/main/res/drawable-mdpi/assets_mario.png	[drawable-mdpi-v4/assets_mario] /Users/jeffreyrajan/Tutorials/RN/errorCheck/android/app/build/generated/res/react/release/drawable-mdpi-v4/assets_mario.png: Error: Duplicate resources

* 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

解决方案:修改 /node_modules/react-native/react.gradle 文件,添加doLast

修改 /node_modules/react-native/react.gradle 文件,添加doLast


doFirst { ... }
doLast {
    def moveFunc = { resSuffix ->
        File originalDir = file("$buildDir/generated/res/react/release/drawable-${resSuffix}");
        if (originalDir.exists()) {
            File destDir = file("$buildDir/../src/main/res/drawable-${resSuffix}");
            ant.move(file: originalDir, tofile: destDir);
        }
    }
    moveFunc.curry("ldpi").call()
    moveFunc.curry("mdpi").call()
    moveFunc.curry("hdpi").call()
    moveFunc.curry("xhdpi").call()
    moveFunc.curry("xxhdpi").call()
    moveFunc.curry("xxxhdpi").call()
}

2.react-native 0.63.4:react-native run-android运行项目报错:Android project not found. Are you sure this is a React Native project? 

解决方案1:找到项目根目录下,android -> app -> build -> intermediates -> signing_config -> debug -> out -> signing-config.json .删除signing-config.json文件,再次运行即可

解决方案2:(并不是我遇到问题的解决方案,网上查到的其他可能原因)可能是react-native版本不支持当前项目的目录结构,在项目根目录执行react-native upgrade,一直输入y。然后项目的目录结构会发生改变,再次运行即可

解决方案3:可能没有解决方案1中signing-config.json文件的权限,debug和release下面都有这个文件,不清楚具体获取哪个的权限,我两个都获取了,然后再次运行也能跑

3. 有时候没有报错误信息时可以在命令窗口输入adb logcat '*:E' 查看android日志

4. 打包报错:

A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade

解决方案:

将这一行添加到gradle.properties中。

org.gradle.jvmargs = -Xmx4608m

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值