记录react-native开发中的一些问题

  1. android studio升级到3.0以上,执行./gradlew assembleRelease进行安卓打包apk时 报错:图片文件重复: Error: Duplicate resources
    解决方法: 在react-native中找到react.gradle文件,然后再文件中doFirst下边添加如下代码(参考issues)
            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()
            }
  1. 集成jpush-react-native时,安卓在debug环境下运行正常,但是打包apk时候发生如下错误
    A5D2AEBB07892832D0AA8C21B7428155.jpg

    解决方法:参考githud上的issues 可以在android目录下的build.gradle中添加如下代码
subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 27     //do this in android/app/build.gradle too
                buildToolsVersion '27.0.3'  //do this in android/app/build.gradle too
            }
        }
    }
}

我的build.gradle文件内容:

buildscript {
    ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 16
        compileSdkVersion = 27
        targetSdkVersion = 26
        supportLibVersion = "27.1.1"
    }
    repositories {
        google()
        jcenter()

    }
    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()
        mavenLocal()
        jcenter()
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}


task wrapper(type: Wrapper) {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 27     //do this in android/app/build.gradle too
                buildToolsVersion '27.0.3'  //do this in android/app/build.gradle too
            }
        }
    }
}
  1. 安卓键盘弹出时会把绝对定位的组件和键盘一块弹上去
    解决方法:在AndroidManifest.xml 文件中的设置
<activity
  ...
  android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
  ....
</activity>
  1. 安卓禁止横屏
    AndroidManifest.xml 文件中添加
<activity
  ...
  android:screenOrientation="portrait"
  ....
</activity>
  1. 加载App中的图片资源
    方法一:使用uri 必须要指定大小
<Image source={{uri: 'app_icon'}} style={{width: 40, height: 40}} />

方法二:使用require('image!x')语法,无需指定尺寸

<Image source={require('image!app_icon')} style={styles.imageStyle} />
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,417评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 14,472评论 2 59
  • 一直非常期待能有这样一种学习小说的课程,上学的时候喜欢看言情小说,曾经把小说里的好比喻句,好句子画出来折角做上记号...
    宁莫之阅读 3,008评论 0 0
  • 置一个空杯 放入几粒盐 滴几滴水 舔一下 很咸 倒入半杯水 再舔一下 有点咸 倒满杯水 小心地尝一口 咸味基本没了...
    且惜且爱阅读 1,306评论 1 6
  • 七绝·期中考后 投影期中分数呈, 谁人金榜上头名。 家班群里几多事, 又把伤心添一层。
    黉门燕阅读 2,994评论 0 0