【问题标题】:React Native : Error: Duplicate resources - AndroidReact Native:错误:重复资源 - Android
【发布时间】:2018-11-10 13:59:47
【问题描述】:

我试图从 Android 创建一个发布 apk 文件,但是当我使用 PNG 图像创建一个发布 apk 时,我收到 Duplicate Resource 错误。最初我认为这是因为我在现有项目中犯了一个错误,但是当我使用单个 Image 组件本身创建一个新项目时,我收到了 Duplicate Resource 错误。这是我遵循的步骤

  1. 创建应用程序 - react-native init demo
  2. 在项目根文件夹中创建 assets 文件夹。
  3. 在资产文件夹中添加PNG 图像。
  4. 现在使用上面的PNG 图像实现Image 组件。
  5. 现在使用 cmd 捆绑它

    react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

  6. 然后使用Generate Signed APKAndroid Studio生成发布apk。

这将引发以下错误:

[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

BUILD FAILED in 22s

注意:当您生成没有任何 PNG 图像的 release apk 时,您不会收到任何错误,它会为您创建 release apk

这是其他文件的代码。

App.js

import React, {Component} from 'react';
import {Platform, StyleSheet, Image, View} from 'react-native';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Image source={require('./assets/mario.png')} />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

package.json

{
  "name": "errorCheck",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.6.0-alpha.8af6728",
    "react-native": "0.57.4"
  },
  "devDependencies": {
    "babel-jest": "23.6.0",
    "jest": "23.6.0",
    "metro-react-native-babel-preset": "0.49.0",
    "react-test-renderer": "16.6.0-alpha.8af6728"
  },
  "jest": {
    "preset": "react-native"
  }
}

有什么解决办法吗?

更新:

这里是其他细节

classpath 'com.android.tools.build:gradle:3.1.4'

ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 16
        compileSdkVersion = 27
        targetSdkVersion = 26
        supportLibVersion = "27.1.1"
    }

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

尝试使用Android Studio 3.0, 3.0.1, 3.1, 3.1.4 &amp; 3.2

【问题讨论】:

  • 你是否使用 react-native run-android 运行应用程序?
  • 是的@Lucefer我没有任何问题直接使用react-native run-android运行应用程序只有当我生成发布apk时我得到了上述错误
  • 你使用的android studio是什么版本?
  • 它的 Android Studio 3.2.1 @FlorinDobre
  • 从这里查看我的答案:stackoverflow.com/a/52767101/1979861 使用 Android studio 3.1.4 Android 3.2 在构建 RN 0.57 项目时遇到问题

标签: react-native android-gradle-plugin react-native-android


【解决方案1】:

(已更新)

这个解决方案适合我

rm -rf ./android/app/src/main/res/drawable-*

rm -rf ./android/app/src/main/res/raw

在我的情况下,构建失败是因为我的 Android 中有重复的资源 项目(在android文件夹内),这两行是删除重复资源所必需的,就是这样。

【讨论】:

  • 解释如何/为什么解决这个问题将使这个问题成为更好的答案......
  • 这看起来只是删除了您的资产。
  • 对我不起作用。在此之后再次尝试 react-native bundle ... 并在尝试构建时遇到相同的错误。
  • 为什么我们必须删除这些目录?这些是 nativ android 项目中所需资产的有效目录。如果我删除它,那么任何使用这些可绘制对象的布局都会被破坏
  • 删除drawable-* 工作并解决了这个问题。对我们来说,删除 /raw 会导致构建失败。我们有两个 ssl 证书,在下一次构建时没有自动复制。
【解决方案2】:

在尝试了很多解决方案后,我发现只有三个解决方案有效。他们来了

解决方案 1:

使用 Gradle 从终端清理 drawable 文件夹。 cd进入android文件夹,然后运行cmd ./gradlew clean

解决方案 2:

捆绑后从Android Studio 中删除drawable 文件夹。你可以在android/app/src/main/res/drawable找到这个

解决方案 3:

请不要使用原作者建议的解决方案#2!node_modules 下的所有包都已生成,并且当重新安装react-native 包时,您所做的任何更改都将丢失/升级。

在此解决方案中,您无需删除任何可绘制文件夹。只需在 react.gradle 文件中添加以下代码,您可以在 node_modules/react-native/react.gradle 路径下找到该文件

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()
}

作为参考,我将在此处添加完整的 react.gradle 文件代码

import org.apache.tools.ant.taskdefs.condition.Os

def config = project.hasProperty("react") ? project.react : [];

def cliPath = config.cliPath ?: "node_modules/react-native/local-cli/cli.js"
def bundleAssetName = config.bundleAssetName ?: "index.android.bundle"
def entryFile = config.entryFile ?: "index.android.js"
def bundleCommand = config.bundleCommand ?: "bundle"
def reactRoot = file(config.root ?: "../../")
def inputExcludes = config.inputExcludes ?: ["android/**", "ios/**"]
def bundleConfig = config.bundleConfig ? "${reactRoot}/${config.bundleConfig}" : null ;


afterEvaluate {
    android.applicationVariants.all { def variant ->
        // Create variant and target names
        def targetName = variant.name.capitalize()
        def targetPath = variant.dirName

        // React js bundle directories
        def jsBundleDir = file("$buildDir/generated/assets/react/${targetPath}")
        def resourcesDir = file("$buildDir/generated/res/react/${targetPath}")

        def jsBundleFile = file("$jsBundleDir/$bundleAssetName")

        // Additional node and packager commandline arguments
        def nodeExecutableAndArgs = config.nodeExecutableAndArgs ?: ["node"]
        def extraPackagerArgs = config.extraPackagerArgs ?: []

        def currentBundleTask = tasks.create(
            name: "bundle${targetName}JsAndAssets",
            type: Exec) {
            group = "react"
            description = "bundle JS and assets for ${targetName}."

            // Create dirs if they are not there (e.g. the "clean" task just ran)
            doFirst {
                jsBundleDir.deleteDir()
                jsBundleDir.mkdirs()
                resourcesDir.deleteDir()
                resourcesDir.mkdirs()
            }

            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()
            }

            // Set up inputs and outputs so gradle can cache the result
            inputs.files fileTree(dir: reactRoot, excludes: inputExcludes)
            outputs.dir jsBundleDir
            outputs.dir resourcesDir

            // Set up the call to the react-native cli
            workingDir reactRoot

            // Set up dev mode
            def devEnabled = !(config."devDisabledIn${targetName}"
                || targetName.toLowerCase().contains("release"))

            def extraArgs = extraPackagerArgs;

            if (bundleConfig) {
                extraArgs = extraArgs.clone()
                extraArgs.add("--config");
                extraArgs.add(bundleConfig);
            }

            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
                commandLine("cmd", "/c", *nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
                    "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
            } else {
                commandLine(*nodeExecutableAndArgs, cliPath, bundleCommand, "--platform", "android", "--dev", "${devEnabled}",
                    "--reset-cache", "--entry-file", entryFile, "--bundle-output", jsBundleFile, "--assets-dest", resourcesDir, *extraArgs)
            }

            enabled config."bundleIn${targetName}" ||
                config."bundleIn${variant.buildType.name.capitalize()}" ?:
                targetName.toLowerCase().contains("release")
        }

        // Expose a minimal interface on the application variant and the task itself:
        variant.ext.bundleJsAndAssets = currentBundleTask
        currentBundleTask.ext.generatedResFolders = files(resourcesDir).builtBy(currentBundleTask)
        currentBundleTask.ext.generatedAssetsFolders = files(jsBundleDir).builtBy(currentBundleTask)

        // registerGeneratedResFolders for Android plugin 3.x
        if (variant.respondsTo("registerGeneratedResFolders")) {
            variant.registerGeneratedResFolders(currentBundleTask.generatedResFolders)
        } else {
            variant.registerResGeneratingTask(currentBundleTask)
        }
        variant.mergeResources.dependsOn(currentBundleTask)

        // packageApplication for Android plugin 3.x
        def packageTask = variant.hasProperty("packageApplication")
            ? variant.packageApplication
            : tasks.findByName("package${targetName}")

        def resourcesDirConfigValue = config."resourcesDir${targetName}"
        if (resourcesDirConfigValue) {
            def currentCopyResTask = tasks.create(
                name: "copy${targetName}BundledResources",
                type: Copy) {
                group = "react"
                description = "copy bundled resources into custom location for ${targetName}."

                from resourcesDir
                into file(resourcesDirConfigValue)

                dependsOn(currentBundleTask)

                enabled currentBundleTask.enabled


            }

            packageTask.dependsOn(currentCopyResTask)
        }

        def currentAssetsCopyTask = tasks.create(
            name: "copy${targetName}BundledJs",
            type: Copy) {
            group = "react"
            description = "copy bundled JS into ${targetName}."

            if (config."jsBundleDir${targetName}") {
                from jsBundleDir
                into file(config."jsBundleDir${targetName}")
            } else {
                into ("$buildDir/intermediates")
                into ("assets/${targetPath}") {
                    from jsBundleDir
                }

                // Workaround for Android Gradle Plugin 3.2+ new asset directory
                into ("merged_assets/${targetPath}/merge${targetName}Assets/out") {
                    from jsBundleDir
                }
            }

            // mergeAssets must run first, as it clears the intermediates directory
            dependsOn(variant.mergeAssets)

            enabled currentBundleTask.enabled
        }

        packageTask.dependsOn(currentAssetsCopyTask)
    }
}

信用: ZeroCool00 mkchx

【讨论】:

  • 很好的解决方案 Jeffery,它也帮助了我 :-) 感谢您的帮助。
  • @Jeffrey-Rajan - 我想给你(连同 ZeroCool00 和 mkchx)作为我最近合并的 react-native 的 PR 的灵感 - github.com/facebook/react-native/commit/… - 我扩展了您在上面提出的处理风味的补丁,它现在在 react-native@master 上,所以希望这不会在将来咬人。
  • 解决方案 #1 对我来说效果很好 - 删除单个 mdpi 文件夹文件,然后重新运行 assembleStaging 工作正常。我在 (#1) 之前尝试过 #2,但在这个特定情况下似乎不起作用。
  • 修改 React 文件对我来说似乎很极端(node_modules 文件夹咳咳...)。在使用流氓解决方案之前,请确保您查看自己的设置。可以很简单:stackoverflow.com/a/49589616/925307
  • 请不要使用解决方案 #2。开发者不要编辑node_modules中的代码,因为安装在这个文件夹中的包是生成的,升级/卸载包时添加的代码会被覆盖/丢失。
【解决方案3】:

用于生成调试 apk

"debug-build": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ && cd android && ./gradlew assembleDebug && cd .."

用于生成发布 apk

"release-build": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/build/intermediates/res/merged/release/ && rm -rf android/app/src/main/res/drawable-* && rm -rf android/app/src/main/res/raw/* && cd android && ./gradlew assembleRelease && cd .."

package.json 文件。

【讨论】:

  • 这种方法应该是公认的答案。不过,您或许可以改用./gradlew clean
  • 你几乎拯救了我的一天。谢谢
  • 对于windows?
  • windows "debug-build": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/ &amp;&amp; cd android &amp;&amp; ./gradlew assembleDebug &amp;&amp; cd app/src/main/res/ &amp;&amp; rmdir /S /Q raw &amp;&amp; rmdir /S /Q drawable-mdpi" "release-build": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/build/intermediates/res/merged/release/ &amp;&amp; cd android &amp;&amp; ./gradlew assembleRelease &amp;&amp; cd .."
【解决方案4】:

接受的答案会起作用,但是它没有考虑到修改节点包意味着如果您更新您的更改将丢失(以及违反最佳实践,您应该以某种方式扩展模块)。

这是来自React-native android release error: duplicate resource

  1. 在项目的“android”文件夹({project-root}/android/fixAndroid)中创建文件夹“fixAndroid”。

  2. 在项目的“fixAndroid”文件夹中创建文件 android-gradle-fix ({project-root}/android/fixAndroid/android-gradle-fix)。

            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()
    }
    
    // Set up inputs and outputs so gradle can cache the result
    
  3. 在您创建的“fixAndroid”文件夹中创建文件 android-release-fix.js:

            const fs = require('fs')
    
        try {
                var curDir = __dirname
                var rootDir = process.cwd()
    
                var file = `${rootDir}/node_modules/react-native/react.gradle`
                var dataFix = fs.readFileSync(`${curDir}/android-gradle-fix`, 'utf8')
                var data = fs.readFileSync(file, 'utf8')
    
                var doLast = "doLast \{"
                if (data.indexOf(doLast) !== -1) {
                    throw "Already fixed."
                }
    
                var result = data.replace(/\/\/ Set up inputs and outputs so gradle can cache the result/g, dataFix);
                fs.writeFileSync(file, result, 'utf8')
                console.log('Android Gradle Fixed!')
            } catch (error) {
                console.error(error)
            }
    
  4. 将脚本添加到 package.json 脚本部分:

    "postinstall": "node ./android/fixAndroid/android-release-fix.js"
    

这将找到“android-gradle-fix”文件的内容并将其插入 node_modules/react-native/react.gradle。

  1. 从项目的根目录运行 npm install。
  2. 从项目的根目录运行 rm -rf android/app/src/main/res/drawable-*。

现在您可以在控制台或 Android Studio 中将版本与 React Native 捆绑在一起:

React Native 命令行

  1. cd {project-root}/android
  2. ./gradlew/bundleRelease

Android Studio

  1. 在 Android Studio 中打开文件夹 android 并构建项目。
  2. 选择构建/生成签名的 APK 以构建版本。

【讨论】:

  • 这个答案应该是正确的,非常感谢。
【解决方案5】:
  1. 删除drawable-xxx文件夹
  2. 删除原始

然后在 src -> main -> res 文件夹中

  1. 在终端中运行此命令:

react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/src/main/assets/index.android.bundle --assets-dest ./android/app/src/main/res_temp

  1. 然后使用终端或 android studio 使用密钥库、别名和密码生成签名的 apk

【讨论】:

  • 你能解释一下第 3 点的脚本是做什么的吗?
【解决方案6】:

谁在 RN 中面临同样的问题! 我认为这个问题已经存在这么长时间了,这绝对是可怕的,但我想在研究不同的解决方案后分享解决它的方法。

Jeffrey Rajan 在https://stackoverflow.com/a/53260522/1611414https://stackoverflow.com/a/53260522/1611414

上的可能解决方案是绝对正确的

我认为在node_modules 中更改react.gradle 文件是非常糟糕的,它会在维护这个RN 项目时导致许多不同的问题。所以我建议选择第一个选项 - 在运行构建之前使用 bash 命令删除该文件夹。

我想分享我在项目中所做的事情,也许你可以重复使用相同的方法:

// ./package.json

...
scripts: {
   "build": "react-native bundle --platform android 
             --dev false
             --entry-file index.js
             --bundle-output android/app/src/main/assets/index.android.bundle
             --assets-dest android/app/src/main/res/
          && rm -rf ./android/app/src/main/res/drawable-mdpi/
          && rm -rf ./android/app/src/main/res/raw/",

   "release": "yarn build && cd ./android && ./gradlew bundleRelease"
}
...

通过yarn release 运行发布。

这几行非常重要:

...
&& rm -rf ./android/app/src/main/res/drawable-mdpi/
&& rm -rf ./android/app/src/main/res/raw/
...

他们在bundleRelease 运行之前从build 步骤中删除重复的资源。该解决方案使用 RN 0.57、0.58、0.59 和 0.60 进行测试

享受吧!

【讨论】:

    【解决方案7】:

    tolotrasmile 的回答对我有用。

    我将它包含在我想要构建和安装 Android 时运行的小 bash 脚本中

            cd $PROJECT_DIRECTORY && react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res
    
            rm -rf $PROJECT_DIRECTORY/android/app/src/main/res/drawable-*
            rm -rf $PROJECT_DIRECTORY/android/app/src/main/res/raw
    
            cd $PROJECT_DIRECTORY/android/
            ./gradlew clean
            ./gradlew assembleRelease
    
            adb install -r $PROJECT_DIRECTORY/android/app/build/outputs/apk/release/app-release.apk
    

    【讨论】:

      【解决方案8】:

      对于最新版本的 React-Native 和 gradle,您不需要捆绑您的资产。完成代码后,只需 cd 进入 android 文件夹并运行:

      ./gradlew assembleRelease
      

      在执行上述命令时,资产会自动捆绑。出现重复资源错误是因为您之前已明确捆绑并再次运行上述命令捆绑,因此出现错误。

      【讨论】:

        【解决方案9】:

        尝试使用另一个目录作为资产目的地(例如 res_temp)。

        react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/src/main/assets/index.android.bundle --assets-dest ./android/app/src/main/res_temp
        

        我还在 gitignore 中添加了 res_temp。 在 Gradle 6.0.1 和 RN 0.62.2 中测试

        【讨论】:

          【解决方案10】:

          在我的情况下,在Jaffrey's 答案中添加几行后它就起作用了

          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()
          
                      File originalDir = file("$buildDir/generated/res/react/release/raw");
                      if (originalDir.exists()) {
                          File destDir = file("$buildDir/../src/main/res/raw");
                          ant.move(file: originalDir, tofile: destDir);
                      }
                  }
          

          【讨论】:

          • 你帮助我意识到我的问题出在 /generated/res/react/release/raw 目录中
          • @RodneySalcedo 真的很高兴它能帮到你!
          • 非常感谢伙计,这对我有用!
          【解决方案11】:

          发布版本出错 /android/app/src/main/res/raw/app.json [原始/应用程序] /android/app/build/generated/res/react/release/raw/app.json:错误:重复资源

          已删除 /android/app/src/main/res/raw/app.json 然后 ./gradlew clean 在 android 文件夹中 然后 ./gradlew bundleRelease

          【讨论】:

          • app.json 被复制了,感谢您的帮助!
          【解决方案12】:

          对我来说这是一个缓存问题。以下命令对我有用

          cd 进入安卓文件夹

          运行 ./gradlew clean

          【讨论】:

            【解决方案13】:

            解决方案
            我只需清理 gradlew 并开始工作无需更改任何内容。
            /node_modules/react-native/react.gradle

            步骤
            1) cd android && ./gradlew clean && cd ..
            2) react-native run-android

            "dependencies": {
                "react": "16.11.0",
                "react-native": "0.62.2",
            }
            

            【讨论】:

              【解决方案14】:

              在这上面花了很多时间!建议的解决方案都不是我的直接解决方案。

              所以我删除了“重复资源”错误消息中提到的所有目录:

              mv ./android/app/src/main/res/drawabl* /tmp
              
              mv ./android/app/src/main/res/raw /tmp
              

              然后我跑了一个

              ./gradlew clean
              

              然后我运行了一个构建:

              /gradlew bundleRelease
              

              构建给了我一些错误,例如“在特定位置找不到特定文件”,因此对于这些文件中的每一个,我都在 tmp/drawable 文件夹中找到它们并将它们复制到确切位置错误消息说它期望它们的路径,例如(您必须根据收到的错误消息自定义它):

              cp /tmp/drawable-mdpi/launch_screen.png app/src/main/res/drawable/
              

              然后:

              ./gradlew clean
              
              /gradlew bundleRelease
              

              终于成功了

              【讨论】:

                【解决方案15】:

                使用 com.android.tools.build:gradle:3.1.4 它应该可以工作。 RN 0.57 在 3.2 中构建存在问题

                此问题可能与以下问题重复:

                React Native Error: Duplicate resources, assets coming in some screens and not coming in others in android release APK

                如果它仍然不起作用,请尝试使用 RN 0.57.2,我正在使用它,并且创建版本非常适合这些部门:

                   "dependencies": {
                    "react": "16.5.0",
                    "react-native": "0.57.2",
                    .......
                  }
                
                  "devDependencies": {
                    "@babel/core": "^7.0.0",
                    "@babel/plugin-proposal-class-properties": "^7.0.0",
                    "@babel/plugin-proposal-decorators": "^7.0.0",
                    "@babel/plugin-proposal-do-expressions": "^7.0.0",
                    "@babel/plugin-proposal-export-default-from": "^7.0.0",
                    "@babel/plugin-proposal-export-namespace-from": "^7.0.0",
                    "@babel/plugin-proposal-function-bind": "^7.0.0",
                    "@babel/plugin-proposal-function-sent": "^7.0.0",
                    "@babel/plugin-proposal-json-strings": "^7.0.0",
                    "@babel/plugin-proposal-logical-assignment-operators": "^7.0.0",
                    "@babel/plugin-proposal-nullish-coalescing-operator": "^7.0.0",
                    "@babel/plugin-proposal-numeric-separator": "^7.0.0",
                    "@babel/plugin-proposal-object-rest-spread": "^7.0.0",
                    "@babel/plugin-proposal-optional-chaining": "^7.0.0",
                    "@babel/plugin-proposal-pipeline-operator": "^7.0.0",
                    "@babel/plugin-proposal-throw-expressions": "^7.0.0",
                    "@babel/plugin-syntax-dynamic-import": "^7.0.0",
                    "@babel/plugin-syntax-import-meta": "^7.0.0",
                    "@babel/plugin-syntax-object-rest-spread": "^7.0.0",
                    "@babel/plugin-transform-runtime": "^7.0.0",
                    "@babel/preset-env": "^7.0.0",
                    "@babel/preset-flow": "^7.0.0",
                    "@babel/register": "^7.0.0",
                    "babel-core": "^7.0.0-bridge.0",
                    "babel-preset-react-native-stage-0": "^1.0.1",
                    .....
                }
                

                Gradle 部门:

                classpath 'com.android.tools.build:gradle:3.1.4'
                classpath "io.realm:realm-gradle-plugin:4.0.0"
                

                应用分级

                compileSdkVersion 27
                    buildToolsVersion "27.0.3"
                
                    defaultConfig {
                        applicationId "de.burda.buntede"
                        minSdkVersion 17
                        targetSdkVersion 27
                

                Gradle 包装器道具:

                distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

                【讨论】:

                • 我尝试使用 Android Studio 3.1 仍然遇到同样的问题。这也不是重复的问题。我也更新了我的问题,提供了更多详细信息。
                【解决方案16】:

                删除您可能拥有的文件:

                android/app/src/main/res/drawable-mdpi/ android/app/src/main/res/drawable-xhdpi/ android/app/src/main/res/drawable-xxhdpi/ 再次运行构建,这解决了我的问题。

                【讨论】:

                  【解决方案17】:

                  所以基本上编辑 /node_modules/react-native/react.gradle 文件 并在 doFirst 块之后手动添加 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()
                  }
                  

                  【讨论】:

                  • 不要编辑node_modules文件夹中的代码 - 此文件夹中的包会生成,并且在升级、重新安装包等时 - 代码更改将丢失。
                  【解决方案18】:

                  我正在使用 react-native 0.63.2。我也遇到了这个问题,并尝试编辑 react.gradle,删除资源/drawable 等。但最后清理 gradle 并运行命令 gradlew assembleRelease 起作用了。

                  我没有单独运行 react-native bundle 命令。 gradlew assembleRelease 正在运行 react-native 包并自行构建 apk。

                  【讨论】:

                    【解决方案19】:

                    修补 react.gradle 文件之类的解决方案可能有效,但它们只是解决方法。真正的解决办法在于找出问题所在。

                    1. 如果它仅在创建发布版本时发生,那么您很可能是单独捆绑。如果您从没有 react.gradle 文件的旧版本 react native 升级,您必须单独捆绑,然后执行 'cd android && ./gradlew assembleRelease' 但引入 react.gradle 捆绑这个过程现在已经被 react.gradle 文件处理了,所以不要单独捆绑,只需运行 'cd android && ./gradlew clean && ./gradlew assembleRelease' 来构建发布 apk。

                    2. 如果在调试中也发生这种情况,那么很可能一些生成的资产或所有生成的资产已被推送并且当前驻留在您的目录中。所以我建议从各自的目录中删除所有这些,如果它们被 git 跟踪,请尝试将它们一一添加。我们有一些以 src_assets_blahblah 开头的资产,我们必须删除它们,因为它们已经在构建过程中生成。

                    【讨论】:

                      【解决方案20】:

                      建议的解决方案都不适合我

                      就我而言,它与.gradle 文件夹有关。在收到此错误之前,我已将文件夹移动到另一个磁盘,但某些文件无法复制到目标文件夹。

                      我只是剪切了旧文件夹并将其与目标文件夹合并。

                      【讨论】:

                        【解决方案21】:

                        就我而言,raw 目录包含一些重复项。因此,我还必须为此添加一个不同的函数,添加函数的名称是 moveFuncRaw 下面对我来说非常适合。

                                doLast {
                        
                                    // Address issue #22234 by moving generated resources into build dir so they are in one spot, not duplicated
                                        def moveFuncRaw = { dirName ->
                                            File originalDir = file("$buildDir/generated/res/react/release/${dirName}");
                                            if (originalDir.exists()) {
                                                File destDir = file("$buildDir/../src/main/res/${dirName}");
                                                ant.move(file: originalDir, tofile: destDir);
                                            }
                                        }
                                        
                                        // Address issue #22234 by moving generated resources into build dir so they are in one spot, not duplicated
                                        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()
                                        moveFuncRaw.curry("raw").call()
                                }
                        

                        还有一件事要意识到 doLast 不会每次都被调用,因为它可能有一些条件,就像我的情况一样,所以你必须单独调用才能执行它。

                        【讨论】:

                          【解决方案22】:

                          我去目录: android/app/src/main/assets/字体

                          检查你期望的 .ttf 文件,删除矢量图标的 .ttf

                          【讨论】:

                            【解决方案23】:

                            在 /node_modules/react-native/react.gradle 中添加这个

                            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()
                            

                            }

                            【讨论】:

                            • 不要编辑node_modules文件夹中的代码 - 此文件夹中的包会生成,当升级、重新安装包等时 - 代码更改将丢失。
                            【解决方案24】:

                            我在使用 0.57.7 后也遇到了同样的问题, 查看node_modules/react-native/react.gradle 文件, 资源输出目录为 $buildDir/generated/res/react/${targetPath} 。 从日志中查看地址为app/build/generated/res/react/release

                            命令 React-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src /main/res/

                            资源输出目录为 android/app/src/main/res/

                            问题就在这里。

                            我解决了这个问题:

                            1. 删除android/app/res中的重复文件 (如果你的资源是从React Native导入的,可以直接删除res下的目录)。

                            2. 删除App/build 文件夹。

                            3. react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/ App/src/main/res/

                            变成 react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle

                            不再指定--assets-dest 运行命令

                            1. 使用Android Studio的Generate Signed APK正确打包。

                            以上

                            【讨论】:

                            • 这里不要使用英语以外的其他语言。
                            猜你喜欢
                            • 2019-03-09
                            • 2019-02-27
                            • 1970-01-01
                            • 1970-01-01
                            • 2015-06-13
                            • 2019-07-14
                            • 2018-11-07
                            • 2017-04-18
                            • 2014-04-06
                            相关资源
                            最近更新 更多