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
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值