Appcenter Codepush 集成。无法添加任务“bundleDebugJsAndAssets”,因为已经存在该名称的任务。

8

复现步骤

  1. yarn add react-native-codepush //运行此命令进行安装
  2. 将以下内容添加到android/app/build.gradle文件中
apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
  1. 将以下内容添加到 MainApplication.java 文件中:
...
import com.microsoft.codepush.react.CodePush;
...
        @Override
        protected String getJSBundleFile() {
            return CodePush.getJSBundleFile();
        }

将以下内容添加到strings.xml中:
    <string name="CodePushDeploymentKey" moduleConfig="true">MYKEY</string>

期望行为

构建应该成功。

实际行为

实际发生了什么?

 $ react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 998 file(s) to forward-jetify. Using 4 workers...
info JS server already running.
info Installing the app...

> Configure project :app
WARNING: BuildType(debug): resValue 'react_native_dev_server_port' value is being replaced: 8081 -> 8081
WARNING: BuildType(debug): resValue 'react_native_inspector_proxy_port' value is being replaced: 8081 -> 8081
WARNING: BuildType(release): resValue 'react_native_dev_server_port' value is being replaced: 8081 -> 8081
WARNING: BuildType(release): resValue 'react_native_inspector_proxy_port' value is being replaced: 8081 -> 8081

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings

FAILURE: Build failed with an exception.

* Where:
Script '/Users/user/Desktop/project/app/native/node_modules/react-native/react.gradle' line: 118

* What went wrong:
A problem occurred configuring project ':app'.
> Cannot add task 'bundleDebugJsAndAssets' as a task with that name already exists.

* 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 4s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* Where:
Script '/Users/user/Desktop/project/app/native/node_modules/react-native/react.gradle' line: 118

* What went wrong:
A problem occurred configuring project ':app'.
> Cannot add task 'bundleDebugJsAndAssets' as a task with that name already exists.

* 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 4s

    at makeError (/Users/user/Desktop/project/app/native/node_modules/execa/index.js:174:9)
    at /Users/user/Desktop/project/app/native/node_modules/execa/index.js:278:16
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async runOnAllDevices (/Users/user/Desktop/project/app/native/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:94:5)
    at async Command.handleAction (/Users/user/Desktop/project/app/native/node_modules/@react-native-community/cli/build/index.js:186:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

环境

  • react-native-code-push 版本:^6.2.1
  • react-native 版本:0.62.2
  • iOS/Android/Windows 版本:未知
  • 这个问题是否能在调试版或发布版上重现?只测试过调试版。
  • 这个问题是否只在模拟器上出现,还是只在实际设备上出现?构建错误。

外部 SDK

我在build.gradle中引入了一个外部 SDK。我不知道这是不是导致这个错误的原因。

buildscript {
    repositories {
        maven { url 'https://plugins.gradle.org/m2/' } // Gradle Plugin Portal
    }
    dependencies {
        classpath 'gradle.plugin.com.onesignal:onesignal-gradle-plugin:[0.12.6, 0.99.99]'
    }
}

apply plugin: 'com.onesignal.androidsdk.onesignal-gradle-plugin'
apply plugin: "com.android.application"
apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"

....
apply plugin: 'com.google.gms.google-services'

我尝试了什么。

我将两个声明移至app/build.gradle的底部。但是错误仍然相同。

// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"


有些文章指出需要删除此行:apply from: "../../node_modules/react-native/react.gradle"。但是会出现新的错误。
yarn run v1.22.4
$ react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 998 file(s) to forward-jetify. Using 4 workers...
info JS server already running.
info Installing the app...

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:mergeDebugAssets'.
> Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'.
   > Could not resolve project :react-native-code-push.
     Required by:
         project :app
      > Unable to find a matching configuration of project :react-native-code-push:
          - None of the consumable configurations have attributes.

* 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 3s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:mergeDebugAssets'.
> Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'.
   > Could not resolve project :react-native-code-push.
     Required by:
         project :app
      > Unable to find a matching configuration of project :react-native-code-push:
          - None of the consumable configurations have attributes.

* 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 3s

    at makeError (/Users/user/Desktop/project/app/native/node_modules/execa/index.js:174:9)
    at /Users/user/Desktop/project/app/native/node_modules/execa/index.js:278:16
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async runOnAllDevices (/Users/user/Desktop/project/app/native/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:94:5)
    at async Command.handleAction (/Users/user/Desktop/project/app/native/node_modules/@react-native-community/cli/build/index.js:186:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我移除了两行代码(实际上它们没有任何意义)错误信息为:

yarn run v1.22.4
$ react-native run-android
info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag.
Jetifier found 998 file(s) to forward-jetify. Using 4 workers...
info JS server already running.
info Installing the app...

Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/6.0.1/userguide/command_line_interface.html#sec:command_line_warnings

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
   > Could not resolve project :react-native-code-push.
     Required by:
         project :app
      > Unable to find a matching configuration of project :react-native-code-push:
          - None of the consumable configurations have attributes.

* 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 4s

error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Run CLI with --verbose flag for more details.
Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081

FAILURE: Build failed with an exception.

* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
   > Could not resolve project :react-native-code-push.
     Required by:
         project :app
      > Unable to find a matching configuration of project :react-native-code-push:
          - None of the consumable configurations have attributes.

* 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 4s

    at makeError (/Users/user/Desktop/project/app/native/node_modules/execa/index.js:174:9)
    at /Users/user/Desktop/project/app/native/node_modules/execa/index.js:278:16
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async runOnAllDevices (/Users/user/Desktop/project/app/native/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:94:5)
    at async Command.handleAction (/Users/user/Desktop/project/app/native/node_modules/@react-native-community/cli/build/index.js:186:9)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.


是的,我尝试了许多次gradle clean。 - Faisal Manzer
很难说实际问题出在哪里。你能否添加可重现的 Git 存储库,以便我可以为您修复错误? - Muhammad Numan
你尝试使用 --verbose 标志运行 cli 以获取更多详细信息了吗? - Saamer
5个回答

17

实际上有两个错误:

  1. android/app/build.gradle文件中实际上有一行代码:apply from: "../../node_modules/react-native/react.gradle"。你不需要添加这一行,而是需要在其下方添加apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"。将它添加到末尾可以解决一些用户的问题,但并非全部。请在适当的位置添加。

  2. 如果出现gradle错误,则是由于multidex。为所有支持启用multidex。在android > defaultConfig中添加multiDexEnable true。参考链接:https://developer.android.com/studio/build/multidex#mdex-gradle


14

我也遇到了同样的问题。

"react-native-code-push": "^6.2.1",
"react-native": "0.61.4",

最后,我删除了你说的那一行,然后它成功地构建了。 我把它放在 build.gradle 的底部。


我把它放在build.gradle文件底部。
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
//apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"

3

实际上,这个错误是由于文档不清晰导致的。我们只需要在apply from: "../../node_modules/react-native/react.gradle"之后添加apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"即可。但如果我们复制代码示例,它会再次添加react.gradle插件,导致重复的gradle任务错误Code Sample on code push site


0

应用自:"../../node_modules/react-native/react.gradle" 在之前已经定义过了,因此这是一个重复的部分。尝试注释或删除它,你就可以继续进行了。


0

我曾经也遇到这个问题,现在我会在这里说明一下,供和我有同样问题的人参考。事实证明我没有完全按照指示来做。

它还给了我这个错误:

cannot find symbol
        new CodePush(getResources().getString(R.string.CodePushDeploymentKey), getApplicationContext(), BuildConfig.DEBUG)

我使用 multi-deployment-testing-android.md 来使用分阶段和生产密钥。我对添加密钥到 android/app/build.gradle 文件的 buildTypes 部分感到困惑。打开这个文件后,只有 release 已经存在,所以我只添加了 releaseStaging 部分。结果发现即使没有出现在文件中,你也必须添加 debug。这为我解决了问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接