ReactNative ./gradlew assembleRelease打包失败Duplicate file

ReactNative通过gradlew assembleRelease打包的时候会报Duplicate file
/android/app/build/intermediates/res/merged/release/drawable-hdpi/node_modules_reactnative_libraries_customcomponents_navigationexperimental_assets_backicon.png
android/app/build/intermediates/res/merged/release/drawable-hdpi-v4/node_modules_reactnative_libraries_customcomponents_navigationexperimental_assets_backicon.png: error: Duplicate file.


通过 GitHub issues里提到的解决方案

 I found the root cause is that old version's packager collect resources (image etc.) and put under $(rn_project)\android\app\src\main\res, while newer version put them under $(rn_project)\android\app\build\intermediates\res\merged$(buildType). It brings duplication which leads to failure of task 'processReleaseResources'. And it also explains why new created project works well. When you have an old RN project upgrading to newer version, just delete directories like 'drawable-xxxx' (ATTN:no 'v?' postfix) under $(rn_project)\android\app\src\main\res and wolrd will be nice.


简单翻译一下就是,旧版本的打包方式是把资源放在了$(rn_project)\ android \ app \ src \ main \ res下,新版的打包方式是在编译的$(rn_project)\android\app\build\intermediates\res\merged$(buildType)下,所以会造成重复文件的问题,导致导致任务“processReleaseResources”失败


解决方法是把$(rn_project)\android\app\src\main\res文件夹下的带有drawable-xxxx的文件夹删掉就可以了


另一个说法是把:node_modules/react-native/local-cli/bundle/assetPathUtils.js这个文件里面getAndroidAssetSuffix方法改为
function getAndroidAssetSuffix(scale) {
  switch (scale) {
    case 0.75: return 'ldpi-v4';
    case 1: return 'mdpi-v4';
    case 1.5: return 'hdpi-v4';
    case 2: return 'xhdpi-v4';
    case 3: return 'xxhdpi-v4';
    case 4: return 'xxxhdpi-v4';
  }
}
@AvatarQing 把这个统一集成到了react.gradle 里面完美的解决掉了drawable-xxxx重复文件的问题

在react.gradle里的def currentBundleTask = tasks.create方法里面最后面添加:


doLast {
    def moveFunc = { resSuffix ->
        File originalDir = file("${resourcesDir}/drawable-${resSuffix}")
        if (originalDir.exists()) {
            File destDir = file("${resourcesDir}/drawable-${resSuffix}-v4")
            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()

}


第一种方法完美解决各种复杂项目结构,第二种方法也行
共勉

GitHub issues
发布了8 篇原创文章 · 获赞 3 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览