Appcenter Codepush 集成。无法添加任务 'bundleDebugJsAndAssets',因为同名任务已存在

Appcenter Codepush integration. Cannot add task 'bundleDebugJsAndAssets' as a task with that name already exists

重现步骤

  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();
        }
  1. 将以下内容添加到 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.

环境

外部 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.

我也遇到了同样的问题。

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

最后把你说的那一行删掉,构建成功。 我放在了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"

实际上有两个错误:

  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

遇到了同样的问题,将在这里为任何与我有相同问题的人概述。原来我没有完全按照指示去做。

它也给了我这个错误:

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

我使用 multi-deployment-testing-android.md 来使用暂存密钥和生产密钥。我对将您的密钥添加到 buildTypes 下的 android/app/build.gradle 文件的部分感到困惑。打开这个文件后,只有 release 已经存在,所以我只添加了 releaseStaging 部分。事实证明,即使 debug 不存在,您也必须添加它。帮我解决了。

申请自:"../../node_modules/react-native/react.gradle"之前定义 所以只是重复,尝试评论或删除它,然后你就可以开始了

实际上错误是因为文档不清晰。我们应该只添加 申请自:“../../node_modules/react-native-code-push/android/codepush.gradle”申请自:“../../node_modules/react-native/react.gradle”. 但是,如果我们复制代码示例,它会再次添加 react.gradle 插件,从而导致重复的 gradle 任务错误